plutus-core-1.60.0.0: Language library for Plutus Core
Safe HaskellSafe-Inferred
LanguageHaskell2010

PlutusCore.DeBruijn.Internal

Description

Support for using de Bruijn indices for term and type names.

Synopsis

Documentation

newtype Index #

A relative index used for de Bruijn identifiers.

FIXME: downside of using newtype+Num instead of type-synonym is that `-Woverflowed-literals` does not work, e.g.: `DeBruijn (-1)` has no warning. To trigger the warning you have to bypass the Num and write `DeBruijn (Index -1)`. This can be revisited when we implement PLT-1053. Tracked by: https://github.com/IntersectMBO/plutus-private/issues/1552

Constructors

Index Word64 

Instances

Instances details
Text) :*: S1 ('MetaSel ('Just "ndbnIndex") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Index)))

newtype FakeNamedDeBruijn #

A wrapper around NamedDeBruijn that *must* hold the invariant of name=fakeName.

We do not export the FakeNamedDeBruijn constructor: the projection `FND->ND` is safe but injection `ND->FND` is unsafe, thus they are not isomorphic.

See Note [Why newtype FakeNamedDeBruijn]

Instances

Instances details
NumBits #

(GEq uni, Closed uni, Everywhere uni Eq, Eq ann) => Eq (Type NamedTyDeBruijn uni ann) # 
Instance details

Defined in PlutusCore.Core.Instance.Eq

Methods

(==) :: Type NamedTyDeBruijn uni ann -> Type NamedTyDeBruijn uni ann -> Bool #

(/=) :: Type NamedTyDeBruijn uni ann -> Type NamedTyDeBruijn uni ann -> Bool #

(GEq uni, Closed uni, Everywhere uni Eq, Eq fun, Eq ann) => Eq (Term NamedTyDeBruijn NamedDeBruijn uni fun ann) # 
Instance details

Defined in PlutusCore.Core.Instance.Eq

type Rep NamedTyDeBruijn # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

type Rep NamedTyDeBruijn = D1 ('MetaData "NamedTyDeBruijn" "PlutusCore.DeBruijn.Internal" "plutus-core-1.60.0.0-inplace" 'True) (C1 ('MetaCons "NamedTyDeBruijn" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 NamedDeBruijn)))
type Unwrapped NamedTyDeBruijn # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

type Unwrapped NamedTyDeBruijn = GUnwrapped (Rep NamedTyDeBruijn)

data FreeVariableError #

We cannot do a correct translation to or from de Bruijn indices if the program is not well-scoped. So we throw an error in such a case.

Constructors

FreeUnique !Unique 
FreeIndex !Index 

Instances

Instances details
Exception FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Generic FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Associated Types

type Rep FreeVariableError :: Type -> Type #

Show FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

NFData FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Methods

rnf :: FreeVariableError -> () #

Eq FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Ord FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Pretty FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

type Rep FreeVariableError # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

type Rep FreeVariableError = D1 ('MetaData "FreeVariableError" "PlutusCore.DeBruijn.Internal" "plutus-core-1.60.0.0-inplace" 'False) (C1 ('MetaCons "FreeUnique" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Unique)) :+: C1 ('MetaCons "FreeIndex" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Index)))

newtype Level #

An absolute level in the program.

Constructors

Level Integer 

Instances

Instances details
Enum Level # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Num Level # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Integral Level # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Real Level # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Methods

toRational :: Level -> Rational #

Eq Level # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Methods

(==) :: Level -> Level -> Bool #

(/=) :: Level -> Level -> Bool #

Ord Level # 
Instance details

Defined in PlutusCore.DeBruijn.Internal

Methods

compare :: Level -> Level -> Ordering #

(<) :: Level -> Level -> Bool #

(<=) :: Level -> Level -> Bool #

(>) :: Level -> Level -> Bool #

(>=) :: Level -> Level -> Bool #

max :: Level -> Level -> Level #

min :: Level -> Level -> Level #

data LevelInfo #

During visiting the AST we hold a reader "state" of current level and a current scoping (levelMapping). Invariant-A: the current level is positive and greater than all levels in the levelMapping. Invariant-B: only positive levels are stored in the levelMapping.

Constructors

LevelInfo 

declareUnique :: (MonadReader LevelInfo m, HasUnique name unique) => name -> m a -> m a #

Declare a name with a unique, recording the mapping to a Level.

declareBinder :: (MonadReader LevelInfo m, MonadQuote m) => m a -> m a #

Declares a new binder by assigning a fresh unique to the *current level*. Maintains invariant-B of LevelInfo (that only positive levels are stored), since current level is always positive (invariant-A). See Note [DeBruijn indices of Binders]

withScope :: MonadReader LevelInfo m => m a -> m a #

Enter a scope, incrementing the current Level by one Maintains invariant-A (that the current level is positive).

getIndex :: MonadReader LevelInfo m => Unique -> (Unique -> m Index) -> m Index #

Get the Index corresponding to a given Unique. Uses supplied handler for free names (uniques).

getUnique :: MonadReader LevelInfo m => Index -> (Index -> m Unique) -> m Unique #

Get the Unique corresponding to a given Index. Uses supplied handler for free debruijn indices.

freeIndexThrow :: MonadError FreeVariableError m => Index -> m Unique #

The default handler of throwing an error upon encountering a free debruijn index.

freeIndexAsConsistentLevel :: (MonadReader LevelInfo m, MonadState (Map Level Unique) m, MonadQuote m) => Index -> m Unique #

A different implementation of a handler, where "free" debruijn indices do not throw an error but are instead gracefully converted to fresh uniques. These generated uniques remain free; i.e. if the original term was open, it will remain open after applying this handler. These generated free uniques are consistent across the open term (by using a state cache).

freeUniqueThrow :: MonadError FreeVariableError m => Unique -> m Index #

The default handler of throwing an error upon encountering a free name (unique).

deBruijnInitIndex :: Index #

The LamAbs index (for debruijn indices) and the starting level of DeBruijn monad