Copyright | (C) MLabs 2025 |
---|---|
License | Apache 2.0 |
Maintainer | koz@mlabs.city, sean@mlabs.city |
Safe Haskell | None |
Language | Haskell2010 |
Covenant.ASG
Description
The Covenant ASG, and ways to programmatically build it.
Note
We use the term 'ASG' to refer to 'abstract syntax graph'. This is because Covenant uses hash consing to ensure duplicate nodes do not exist, thus producing a DAG structure, rather than a tree.
Since: 1.0.0
Synopsis
- data ASG
- topLevelNode :: ASG -> ASGNode
- nodeAt :: Id -> ASG -> ASGNode
- data Id
- data Ref
- data Arg
- data CompNodeInfo where
- pattern Builtin1 :: OneArgFunc -> CompNodeInfo
- pattern Builtin2 :: TwoArgFunc -> CompNodeInfo
- pattern Builtin3 :: ThreeArgFunc -> CompNodeInfo
- pattern Lam :: Id -> CompNodeInfo
- pattern Force :: Ref -> CompNodeInfo
- pattern Return :: Ref -> CompNodeInfo
- data ValNodeInfo where
- pattern Lit :: AConstant -> ValNodeInfo
- pattern App :: Id -> Vector Ref -> ValNodeInfo
- pattern Thunk :: Id -> ValNodeInfo
- data ASGNode
- typeASGNode :: ASGNode -> ASGNodeType
- data CovenantError
- data ScopeInfo
- data ASGBuilder a
- data CovenantTypeError
- = BrokenIdReference Id
- | ForceCompType (CompT AbstractTy)
- | ForceNonThunk (ValT AbstractTy)
- | ForceError
- | ThunkValType (ValT AbstractTy)
- | ThunkError
- | ApplyToValType (ValT AbstractTy)
- | ApplyToError
- | ApplyCompType (CompT AbstractTy)
- | RenameFunctionFailed (CompT AbstractTy) RenameError
- | RenameArgumentFailed (ValT AbstractTy) RenameError
- | UnificationError TypeAppError
- | NoSuchArgument DeBruijn (Index "arg")
- | ReturnCompType (CompT AbstractTy)
- | LambdaResultsInValType (ValT AbstractTy)
- | LambdaResultsInNonReturn (CompT AbstractTy)
- | ReturnWrapsError
- | ReturnWrapsCompType (CompT AbstractTy)
- | WrongReturnType (ValT AbstractTy) (ValT AbstractTy)
- data RenameError
- arg :: (MonadError CovenantTypeError m, MonadReader ScopeInfo m) => DeBruijn -> Index "arg" -> m Arg
- builtin1 :: MonadHashCons Id ASGNode m => OneArgFunc -> m Id
- builtin2 :: MonadHashCons Id ASGNode m => TwoArgFunc -> m Id
- builtin3 :: MonadHashCons Id ASGNode m => ThreeArgFunc -> m Id
- force :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Ref -> m Id
- ret :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Ref -> m Id
- lam :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m, MonadReader ScopeInfo m) => CompT AbstractTy -> m Id -> m Id
- err :: MonadHashCons Id ASGNode m => m Id
- lit :: MonadHashCons Id ASGNode m => AConstant -> m Id
- thunk :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Id -> m Id
- app :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Id -> Vector Ref -> m Id
- runASGBuilder :: ASGBuilder a -> Either CovenantError ASG
The ASG itself
Types
A fully-assembled Covenant ASG.
Since: 1.0.0
Functions
topLevelNode :: ASG -> ASGNode Source #
Retrieves the top-level node of an ASG.
Since: 1.0.0
nodeAt :: Id -> ASG -> ASGNode Source #
Given an Id
and an ASG, produces the node corresponding to that Id
.
Important note
This is only safe to use if the Id
comes from a node in the argument ASG
.
Id
s valid in one ASG are not likely to be valid in another. 'Mixing
and matching' Id
s from different ASGs will at best produce unexpected
results, and at worst will crash. You have been warned.
Since: 1.0.0
ASG components
Types
A unique identifier for a node in a Covenant program.
Since: 1.0.0
Instances
A general reference in a Covenant program.
Since: 1.0.0
An argument passed to a function in a Covenant program.
Since: 1.0.0
data CompNodeInfo where Source #
Computation-term-specific node information.
Since: 1.0.0
Bundled Patterns
pattern Builtin1 :: OneArgFunc -> CompNodeInfo | A Plutus primop with one argument. Since: 1.0.0 |
pattern Builtin2 :: TwoArgFunc -> CompNodeInfo | A Plutus primop with two arguments. Since: 1.0.0 |
pattern Builtin3 :: ThreeArgFunc -> CompNodeInfo | A Plutus primop with three arguments. Since: 1.0.0 |
pattern Lam :: Id -> CompNodeInfo | A lambda. Since: 1.0.0 |
pattern Force :: Ref -> CompNodeInfo | Force a thunk back into the computation it wraps. Since: 1.0.0 |
pattern Return :: Ref -> CompNodeInfo | Produce the result of a computation. Since: 1.0.0 |
Instances
Show CompNodeInfo Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term Methods showsPrec :: Int -> CompNodeInfo -> ShowS # show :: CompNodeInfo -> String # showList :: [CompNodeInfo] -> ShowS # | |
Eq CompNodeInfo Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term | |
Ord CompNodeInfo Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term Methods compare :: CompNodeInfo -> CompNodeInfo -> Ordering # (<) :: CompNodeInfo -> CompNodeInfo -> Bool # (<=) :: CompNodeInfo -> CompNodeInfo -> Bool # (>) :: CompNodeInfo -> CompNodeInfo -> Bool # (>=) :: CompNodeInfo -> CompNodeInfo -> Bool # max :: CompNodeInfo -> CompNodeInfo -> CompNodeInfo # min :: CompNodeInfo -> CompNodeInfo -> CompNodeInfo # |
data ValNodeInfo where Source #
Value-term-specific node information.
Since: 1.0.0
Bundled Patterns
pattern Lit :: AConstant -> ValNodeInfo | A compile-time literal of a flat builtin type. Since: 1.0.0 |
pattern App :: Id -> Vector Ref -> ValNodeInfo | An application of a computation (the Since: 1.0.0 |
pattern Thunk :: Id -> ValNodeInfo | Wrap a computation into a value (essentially delaying it). Since: 1.0.0 |
Instances
Show ValNodeInfo Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term Methods showsPrec :: Int -> ValNodeInfo -> ShowS # show :: ValNodeInfo -> String # showList :: [ValNodeInfo] -> ShowS # | |
Eq ValNodeInfo Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term | |
Ord ValNodeInfo Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term Methods compare :: ValNodeInfo -> ValNodeInfo -> Ordering # (<) :: ValNodeInfo -> ValNodeInfo -> Bool # (<=) :: ValNodeInfo -> ValNodeInfo -> Bool # (>) :: ValNodeInfo -> ValNodeInfo -> Bool # (>=) :: ValNodeInfo -> ValNodeInfo -> Bool # max :: ValNodeInfo -> ValNodeInfo -> ValNodeInfo # min :: ValNodeInfo -> ValNodeInfo -> ValNodeInfo # |
A single node in a Covenant ASG. Where appropriate, these carry their types.
Since: 1.0.0
Constructors
ACompNode (CompT AbstractTy) CompNodeInfo | A computation-typed node. Since: 1.0.0 |
AValNode (ValT AbstractTy) ValNodeInfo | A value-typed node Since: 1.0.0 |
AnError | An error node. Since: 1.0.0 |
Instances
Show ASGNode Source # | Since: 1.0.0 |
Eq ASGNode Source # | Since: 1.0.0 |
Ord ASGNode Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term | |
MonadHashCons Id ASGNode ASGBuilder Source # | Since: 1.0.0 |
Defined in Covenant.ASG |
Functions
typeASGNode :: ASGNode -> ASGNodeType Source #
Produces the type of any ASG node.
Since: 1.0.0
ASG builder
Types
data CovenantError Source #
Any problem that might arise when building an ASG programmatically.
Since: 1.0.0
Constructors
TypeError (Bimap Id ASGNode) CovenantTypeError | There was a type error when assembling the ASG. This provides the hash-consed state up to the point of the error. Since: 1.0.0 |
EmptyASG | We tried to generate an ASG with no nodes in it. Since: 1.0.0 |
TopLevelError | We tried to generate as ASG whose top-level node is an error. Since: 1.0.0 |
TopLevelValue (Bimap Id ASGNode) (ValT AbstractTy) ValNodeInfo | We tried to generate an ASG whose top-level node is a value. Since: 1.0.0 |
Instances
Show CovenantError Source # | Since: 1.0.0 |
Defined in Covenant.ASG Methods showsPrec :: Int -> CovenantError -> ShowS # show :: CovenantError -> String # showList :: [CovenantError] -> ShowS # | |
Eq CovenantError Source # | Since: 1.0.0 |
Defined in Covenant.ASG Methods (==) :: CovenantError -> CovenantError -> Bool # (/=) :: CovenantError -> CovenantError -> Bool # |
A tracker for scope-related information while building an ASG programmatically. Currently only tracks available arguments.
Important note
This is a fairly low-level type, designed specifically for ASG construction. While you can do arbitrary things with it, changing things in it outside of the functionality provided by this module is not recommended, unless you know exactly what you're doing.
Since: 1.0.0
Instances
Show ScopeInfo Source # | Since: 1.0.0 |
Eq ScopeInfo Source # | Since: 1.0.0 |
MonadReader ScopeInfo ASGBuilder Source # | Since: 1.0.0 |
Defined in Covenant.ASG Methods ask :: ASGBuilder ScopeInfo # local :: (ScopeInfo -> ScopeInfo) -> ASGBuilder a -> ASGBuilder a # reader :: (ScopeInfo -> a) -> ASGBuilder a # | |
(k ~ A_Lens, a ~ Vector (Vector (ValT AbstractTy)), b ~ Vector (Vector (ValT AbstractTy))) => LabelOptic "argumentInfo" k ScopeInfo ScopeInfo a b Source # | Gives access to the argument information for the current, and all
enclosing, scopes. The 'outer' Since: 1.0.0 |
Defined in Covenant.ASG |
data ASGBuilder a Source #
A concrete monadic stack, providing the minimum amount of functionality needed to build an ASG using the combinators given in this module.
Since: 1.0.0
Instances
data CovenantTypeError Source #
An error that can arise during the construction of an ASG by programmatic means.
Since: 1.0.0
Constructors
BrokenIdReference Id | An Since: 1.0.0 |
ForceCompType (CompT AbstractTy) | Computation-typed nodes can't be forced, but we tried anyway. Since: 1.0.0 |
ForceNonThunk (ValT AbstractTy) | Value-typed nodes that aren't thunks can't be forced, but we tried anyway. Since: 1.0.0 |
ForceError | Error nodes can't be forced, but we tried anyway. Since: 1.0.0 |
ThunkValType (ValT AbstractTy) | Value-typed nodes can't be thunked, but we tried anyway. Since: 1.0.0 |
ThunkError | Error nodes can't be thunked, but we tried anyway. Since: 1.0.0 |
ApplyToValType (ValT AbstractTy) | Arguments can't be applied to a value-typed node, but we tried anyway. Since: 1.0.0 |
ApplyToError | Arguments can't be applied to error nodes, but we tried anyway. Since: 1.0.0 |
ApplyCompType (CompT AbstractTy) | Computation-typed nodes can't be applied as arguments, but we tried anyway. Since: 1.0.0 |
RenameFunctionFailed (CompT AbstractTy) RenameError | Renaming the function in an application failed. Since: 1.0.0 |
RenameArgumentFailed (ValT AbstractTy) RenameError | Renaming an argument in an application failed. Since: 1.0.0 |
UnificationError TypeAppError | We failed to unify an expected argument type with the type of the argument we were actually given. Since: 1.0.0 |
NoSuchArgument DeBruijn (Index "arg") | An argument was requested that doesn't exist. Since: 1.0.0 |
ReturnCompType (CompT AbstractTy) | Can't return a computation-typed node, but we tried anyway. Since: 1.0.0 |
LambdaResultsInValType (ValT AbstractTy) | The body of a lambda results in a value-typed node, which isn't allowed. Since: 1.0.0 |
LambdaResultsInNonReturn (CompT AbstractTy) | The body of a lambda results in a computation-typed node which isn't a return, which isn't allowed. Since: 1.0.0 |
ReturnWrapsError | A lambda body's return is wrapping an error, instead of being directly an error. This should not happen under normal circumstances and is most certainly a bug. Since: 1.0.0 |
ReturnWrapsCompType (CompT AbstractTy) | We tried to return a computation-typed node, but this isn't allowed. Since: 1.0.0 |
WrongReturnType (ValT AbstractTy) (ValT AbstractTy) | The result of an application is not what the computation being applied expected. First field is the expected type, the second is what we actually got. Since: 1.0.0 |
Instances
Show CovenantTypeError Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term Methods showsPrec :: Int -> CovenantTypeError -> ShowS # show :: CovenantTypeError -> String # showList :: [CovenantTypeError] -> ShowS # | |
Eq CovenantTypeError Source # | Since: 1.0.0 |
Defined in Covenant.Internal.Term Methods (==) :: CovenantTypeError -> CovenantTypeError -> Bool # (/=) :: CovenantTypeError -> CovenantTypeError -> Bool # | |
MonadError CovenantTypeError ASGBuilder Source # | Since: 1.0.0 |
Defined in Covenant.ASG Methods throwError :: CovenantTypeError -> ASGBuilder a # catchError :: ASGBuilder a -> (CovenantTypeError -> ASGBuilder a) -> ASGBuilder a # |
data RenameError Source #
Ways in which the renamer can fail.
Since: 1.0.0
Constructors
InvalidAbstractionReference Int (Index "tyvar") | An attempt to reference an abstraction in a scope where this abstraction doesn't exist. First field is the true level, second is the index that was requested. Since: 1.0.0 |
IrrelevantAbstraction | A value type specifies an abstraction that never gets used
anywhere. For example, the type Since: 1.0.0 |
UndeterminedAbstraction | A computation type specifies an abstraction which is not used
by any argument. For example, the type Since: 1.0.0 |
Instances
Show RenameError Source # | |
Defined in Covenant.Internal.Rename Methods showsPrec :: Int -> RenameError -> ShowS # show :: RenameError -> String # showList :: [RenameError] -> ShowS # | |
Eq RenameError Source # | |
Defined in Covenant.Internal.Rename |
Introducers
arg :: (MonadError CovenantTypeError m, MonadReader ScopeInfo m) => DeBruijn -> Index "arg" -> m Arg Source #
Given a scope and a positional argument index, construct that argument. Will fail if that argument doesn't exist in the specified scope, or if the specified scope doesn't exist.
Since: 1.0.0
builtin1 :: MonadHashCons Id ASGNode m => OneArgFunc -> m Id Source #
Construct a node corresponding to the given Plutus primop.
Since: 1.0.0
builtin2 :: MonadHashCons Id ASGNode m => TwoArgFunc -> m Id Source #
As builtin1
, but for two-argument primops.
Since: 1.0.0
builtin3 :: MonadHashCons Id ASGNode m => ThreeArgFunc -> m Id Source #
As builtin1
, but for three-argument primops.
Since: 1.0.0
force :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Ref -> m Id Source #
Given a reference to a thunk, turn it back into a computation. Will fail if the reference isn't a thunk.
Since: 1.0.0
ret :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Ref -> m Id Source #
Given the result of a function body (either a value or an error), construct the return for it. Will fail if that reference aims at a computation node.
Since: 1.0.0
lam :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m, MonadReader ScopeInfo m) => CompT AbstractTy -> m Id -> m Id Source #
Given a desired type, and a computation which will construct a lambda body when executed (with the scope extended with the arguments the functions can expect), construct a lambda.
Important note
This combinator works slightly differently to the others in this module. This
is required because, due to hash consing, an ASG is typically built
'bottom-up', whereas function arguments (and their scopes) are necessarily
top-down. Thus, we need to 'delay' the construction of a lambda's body to
ensure that proper scoped argument information can be given to it, hence why
the argument being passed is an m Id
.
Since: 1.0.0
lit :: MonadHashCons Id ASGNode m => AConstant -> m Id Source #
Construct a node corresponding to the given constant.
Since: 1.0.0
thunk :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Id -> m Id Source #
app :: (MonadHashCons Id ASGNode m, MonadError CovenantTypeError m) => Id -> Vector Ref -> m Id Source #
Given an Id
referring to a computation, and a Vector
of Ref
s to the
desired arguments, construct the application of the arguments to that
computation. This can fail for a range of reasons:
- Type mismatch between what the computation expects and what it's given
- Too many or too few arguments
- Not a computation type for
Id
argument - Not value types for
Ref
s
Since: 1.0.0
Elimination
runASGBuilder :: ASGBuilder a -> Either CovenantError ASG Source #
Executes an ASGBuilder
to make a 'finished' ASG.
Since: 1.0.0