-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wip: Implement typesystem #40
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
19a6d34
Remove the Arbitrary instancc for `ASGBuilder Id`
itsfarseen 1dd195b
Add ASGType module
itsfarseen 4d880ad
Add error handling using ExceptT to the ASGBuilder monad
itsfarseen 2a96944
Add typing for lam, let, app
itsfarseen f10ccaa
Simplify TyPlutusData
itsfarseen 8cb8f40
wip: typesystem - reorganize, impl prim typing
itsfarseen 44793e5
Add typing for `lit`
itsfarseen 745530a
fix formatting
itsfarseen eac31ce
fix cabal file formatting
itsfarseen c32bd63
fix hlint suggestions
itsfarseen d37a9e0
fix hlint suggestions
itsfarseen 2bd7848
Add haddock for the ASGType parameter for `letBind` and `lam`
itsfarseen 28f55c8
Re-export ASGBuilderError and TypeError in Covenant.ASG
itsfarseen 88428e4
Add docs to the variants of TypeError
itsfarseen f193a0f
Rename TyConstant to TyExpr and update the docs
itsfarseen 0c91038
Add BLS12_381 types in TyExpr, use them in BLS_12_* fns in Prim
itsfarseen 2474088
Modified primitive functions to take and return ASGType instead of Ty…
itsfarseen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ module Covenant.ASG | |
ASGBuilder, | ||
Scope, | ||
ASG, | ||
ASGBuilderError (..), | ||
TypeError (..), | ||
|
||
-- * Functions | ||
emptyScope, | ||
|
@@ -51,27 +53,38 @@ import Algebra.Graph.Acyclic.AdjacencyMap | |
vertex, | ||
) | ||
import Algebra.Graph.AdjacencyMap qualified as Cyclic | ||
import Control.Monad.State.Strict (runState) | ||
import Covenant.Internal.ASGBuilder | ||
( ASGBuilder (ASGBuilder), | ||
( ASGBuilder, | ||
ASGBuilderError (ATypeError), | ||
ASGBuilderState (ASGBuilderState), | ||
TypeError | ||
( TyErrAppArgMismatch, | ||
TyErrAppNotALambda, | ||
TyErrNonHomogenousList, | ||
TyErrPrimArgMismatch | ||
), | ||
app, | ||
idOf, | ||
lit, | ||
prim, | ||
runASGBuilder, | ||
) | ||
import Covenant.Internal.ASGNode | ||
( ASGNode (App, Lam, Let, Lit, Prim), | ||
ASGType (TyLam), | ||
Arg (Arg), | ||
Bound (Bound), | ||
Id, | ||
PrimCall (PrimCallOne, PrimCallSix, PrimCallThree, PrimCallTwo), | ||
Ref (ABound, AnArg, AnId), | ||
typeOfRef, | ||
) | ||
import Data.Bimap (Bimap) | ||
import Data.Bimap qualified as Bimap | ||
import Data.Maybe (mapMaybe) | ||
import Data.Maybe (fromJust, mapMaybe) | ||
import Data.Proxy (Proxy (Proxy)) | ||
import Data.Vector (Vector) | ||
import Data.Vector qualified as Vector | ||
import GHC.TypeNats (CmpNat, KnownNat, natVal, type (+)) | ||
import Numeric.Natural (Natural) | ||
|
||
|
@@ -90,20 +103,21 @@ data ASG = ASG (Id, ASGNode) (AdjacencyMap (Id, ASGNode)) | |
-- refers to into a call graph. This is guaranteed to be acyclic. | ||
-- | ||
-- @since 1.0.0 | ||
toASG :: ASGBuilder Id -> Maybe ASG | ||
toASG (ASGBuilder comp) = do | ||
let (start, ASGBuilderState binds) = runState comp (ASGBuilderState Bimap.empty) | ||
toASG :: ASGBuilder Id -> Either ASGBuilderError ASG | ||
toASG comp = do | ||
let (startOrError, ASGBuilderState (binds :: Bimap Id ASGNode)) = runASGBuilder comp (ASGBuilderState Bimap.empty) | ||
start :: Id <- startOrError | ||
if Bimap.size binds == 1 | ||
then do | ||
-- This cannot fail, but the type system can't show it | ||
initial <- (start,) <$> Bimap.lookup start binds | ||
let -- This cannot fail, but the type system can't show it | ||
initial = (start, (Bimap.!) binds start) | ||
pure . ASG initial . vertex $ initial | ||
else do | ||
let asGraph = Cyclic.edges . go binds $ start | ||
-- This cannot fail, but the type system can't show it | ||
acyclic <- toAcyclic asGraph | ||
-- Same as above | ||
initial <- (start,) <$> Bimap.lookup start binds | ||
-- This cannot fail, but the type system can't show it | ||
acyclic = fromJust $ toAcyclic asGraph | ||
-- Same as above | ||
initial = (start, (Bimap.!) binds start) | ||
pure . ASG initial $ acyclic | ||
where | ||
go :: | ||
|
@@ -125,7 +139,7 @@ toASG (ASGBuilder comp) = do | |
-- bindings (each with a bound variable we can refer to. | ||
-- | ||
-- @since 1.0.0 | ||
data Scope (args :: Natural) (lets :: Natural) = Scope | ||
data Scope (args :: Natural) (lets :: Natural) = Scope (Vector ASGType) (Vector ASGType) | ||
deriving stock | ||
( -- | @since 1.0.0 | ||
Eq, | ||
|
@@ -137,7 +151,7 @@ data Scope (args :: Natural) (lets :: Natural) = Scope | |
-- | ||
-- @since 1.0.0 | ||
emptyScope :: Scope 0 0 | ||
emptyScope = Scope | ||
emptyScope = Scope Vector.empty Vector.empty | ||
|
||
-- | Given a proof of scope, construct one of the arguments in that scope. This | ||
-- requires use of @TypeApplications@ to select which argument you are | ||
|
@@ -153,7 +167,11 @@ arg :: | |
(KnownNat n, CmpNat n m ~ LT) => | ||
Scope m lets -> | ||
Arg | ||
arg Scope = Arg . fromIntegral . natVal $ Proxy @n | ||
arg (Scope args _) = | ||
let n = natVal $ Proxy @n | ||
-- This cannot fail, but the type system can't show it | ||
argTy = (Vector.!) args (fromIntegral n) | ||
in Arg (fromIntegral n) argTy | ||
|
||
-- | Given a proof of scope, construct one of the @let@-bound variables in that | ||
-- scope. This requires use of @TypeApplications@ to select which bound variable | ||
|
@@ -169,28 +187,40 @@ bound :: | |
(KnownNat n, CmpNat n m ~ LT) => | ||
Scope args m -> | ||
Bound | ||
bound Scope = Bound . fromIntegral . natVal $ Proxy @n | ||
bound (Scope _ lets) = | ||
let n = natVal $ Proxy @n | ||
-- This cannot fail, but the type system can't show it | ||
letTy = (Vector.!) lets (fromIntegral n) | ||
in Bound (fromIntegral n) letTy | ||
|
||
-- | Given a proof of scope, and a function to construct a lambda body using a | ||
-- \'larger\' proof of scope, construct a lambda with that body. | ||
-- | Given the type of the lambda argument and a proof of scope, and a function | ||
-- to construct a lambda body using a \'larger\' proof of scope, construct a | ||
-- lambda with that body. | ||
-- | ||
-- Note (Farseen, 2025\/02\/11): Update this example once parametric polymorphism is implemented. | ||
-- For example, this is how you define @id@: | ||
-- | ||
-- > lam emptyScope $ \s10 -> pure . AnArg $ arg @0 s10 | ||
-- | ||
-- Note (Farseen, 2025\/02\/11): Update this example once parametric polymorphism is implemented. | ||
-- This is @const@: | ||
-- | ||
-- > lam emptyScope $ \s10 -> lam s10 $ \s20 -> pure . AnArg $ arg @1 s20 | ||
-- | ||
-- @since 1.0.0 | ||
lam :: | ||
forall (n :: Natural) (m :: Natural). | ||
-- | The type of the lambda argument | ||
ASGType -> | ||
Scope n m -> | ||
(Scope (n + 1) m -> ASGBuilder Ref) -> | ||
ASGBuilder Id | ||
lam Scope f = do | ||
res <- f Scope | ||
idOf . Lam $ res | ||
lam argTy scope f = do | ||
let scope' = pushArgToScope argTy scope | ||
res <- f scope' | ||
let resTy = typeOfRef res | ||
lamTy = TyLam argTy resTy | ||
idOf lamTy . Lam $ res | ||
|
||
-- | Given a proof of scope, a 'Ref' to an expression to bind to, and a function | ||
-- to construct a @let@-binding body using a \'larger\' proof of scope, construct | ||
|
@@ -203,22 +233,41 @@ lam Scope f = do | |
-- > five <- lit 5 | ||
-- > four <- lit 4 | ||
-- > prod <- mul five four | ||
-- > letBind emptyScope prod $ \s01 -> | ||
-- > letBind TyInteger emptyScope prod $ \s01 -> | ||
-- > mul (ABound . bound @0 $ s01) (ABound . bound @0 $ s01) | ||
-- | ||
-- @since 1.0.0 | ||
letBind :: | ||
forall (n :: Natural) (m :: Natural). | ||
-- | Type of the binding | ||
ASGType -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
Scope n m -> | ||
Ref -> | ||
(Scope n (m + 1) -> ASGBuilder Ref) -> | ||
ASGBuilder Id | ||
letBind Scope r f = do | ||
res <- f Scope | ||
idOf . Let r $ res | ||
letBind letTy scope r f = do | ||
let scope' = pushLetToScope letTy scope | ||
res <- f scope' | ||
idOf letTy . Let r $ res | ||
|
||
-- Helpers | ||
|
||
pushArgToScope :: | ||
forall (n :: Natural) (m :: Natural). | ||
ASGType -> | ||
Scope n m -> | ||
Scope (n + 1) m | ||
pushArgToScope ty (Scope args lets) = | ||
Scope (Vector.cons ty args) lets | ||
|
||
pushLetToScope :: | ||
forall (n :: Natural) (m :: Natural). | ||
ASGType -> | ||
Scope n m -> | ||
Scope n (m + 1) | ||
pushLetToScope ty (Scope args lets) = | ||
Scope args (Vector.cons ty lets) | ||
|
||
toIdList :: ASGNode -> [Id] | ||
toIdList = \case | ||
Lit _ -> [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to update the documentation to specify that this is the intended type of the argument, not the type of the lambda as a whole.