Skip to content
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
wants to merge 17 commits into from
Closed

wip: Implement typesystem #40

wants to merge 17 commits into from

Conversation

itsfarseen
Copy link
Collaborator

No description provided.

This is not super useful at the moment, as it doesn't shrink.
The design is also in flux at the moment.
It makes more sense to remove this for now and implement it once things are more stable.
Removed Internal.ASGType module and colocated the type definitions next to the types where it is relevant.

Implemented typing for `PrimCall`s
@itsfarseen itsfarseen mentioned this pull request Feb 6, 2025
@itsfarseen itsfarseen requested a review from kozross February 6, 2025 18:53
@itsfarseen itsfarseen marked this pull request as ready for review February 6, 2025 19:48
Comment on lines 311 to 314
FstPair -> (TyPair TyPlutusData TyPlutusData, TyPlutusData)
SndPair -> (TyPair TyPlutusData TyPlutusData, TyPlutusData)
HeadList -> (TyList TyPlutusData, TyPlutusData)
TailList -> (TyList TyPlutusData, TyList TyPlutusData)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kozross This PR doesn't include rank-1 polymorphism. I'll add that in a future PR.
For now, I'm using TyPlutusData as a placeholder for polymorphic types.

Copy link
Member

@kozross kozross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good start, modulo the feedback.

@@ -185,12 +199,16 @@ bound Scope = Bound . fromIntegral . natVal $ Proxy @n
-- @since 1.0.0
lam ::
forall (n :: Natural) (m :: Natural).
ASGType ->
Copy link
Member

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.

@@ -209,16 +227,34 @@ lam Scope f = do
-- @since 1.0.0
letBind ::
forall (n :: Natural) (m :: Natural).
ASGType ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Comment on lines 77 to 85
-- | The errors that come up while resolving types.
--
-- @since 1.0.0
data TypeError
= TyErrAppNotALambda ASGType
| TyErrAppArgMismatch ASGType ASGType -- expected, received
| TyErrPrimNotAConstant ASGType
| TyErrPrimArgMismatch (Vector TyConstant) (Vector TyConstant) -- exptected, received
| TyErrNonHomogenousList
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These constructors should have Haddocks of their own explaining their meaning. While in some cases it's fairly clear (TyErrAppNotALambda for example), in others, it's not TyErrAppArgMismatch would require me to read the source code to understand which field was which).

-- | The type of Plutus constant terms.
--
-- @since 1.0.0
data TyConstant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly misnamed: ByteStrings for example aren't necessarily constants, as we can construct them from other operations (such as ConsByteString). From what I can see later-on, this seems to be a general 'type of Covenant expression', which means it's missing a couple of things (such as BLS types for example).

Comment on lines 328 to 333
BLS12_381_G1_neg -> (TyByteString, TyByteString)
BLS12_381_G1_compress -> (TyByteString, TyByteString)
BLS12_381_G1_uncompress -> (TyByteString, TyByteString)
BLS12_381_G2_neg -> (TyByteString, TyByteString)
BLS12_381_G2_compress -> (TyByteString, TyByteString)
BLS12_381_G2_uncompress -> (TyByteString, TyByteString)
Copy link
Member

@kozross kozross Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of these do not consume, or produce, ByteStrings - they produce and consume the corresponding BLS types. The only ones that take or return ByteStrings are compression and uncompression primitives.

Comment on lines 371 to 381
BLS12_381_G1_add -> (TyByteString, TyByteString, TyByteString)
BLS12_381_G1_scalarMul -> (TyInteger, TyByteString, TyByteString)
BLS12_381_G1_equal -> (TyByteString, TyByteString, TyBoolean)
BLS12_381_G1_hashToGroup -> (TyByteString, TyByteString, TyByteString)
BLS12_381_G2_add -> (TyByteString, TyByteString, TyByteString)
BLS12_381_G2_scalarMul -> (TyInteger, TyByteString, TyByteString)
BLS12_381_G2_equal -> (TyByteString, TyByteString, TyBoolean)
BLS12_381_G2_hashToGroup -> (TyByteString, TyByteString, TyByteString)
BLS12_381_millerLoop -> (TyByteString, TyByteString, TyByteString)
BLS12_381_mulMlResult -> (TyByteString, TyByteString, TyByteString)
BLS12_381_finalVerify -> (TyByteString, TyByteString, TyBoolean)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with these.

Comment on lines 414 to 422
CaseData ->
( TyPlutusData,
TyPair TyInteger (TyList TyPlutusData),
TyList (TyPair TyPlutusData TyPlutusData),
TyList TyPlutusData,
TyInteger,
TyByteString,
TyPlutusData
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the stated semantics of CaseData, you will see that it doesn't take arbitrary polymorphic terms, but instead functions with a polymorphic return type. I assume that this is just 'shorthanded' the same way as other polymorphic functions?

Copy link
Collaborator Author

@itsfarseen itsfarseen Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. I meant to update this once I figure out the function types in AConstant.
For some reason I thought PrimCall can only accept constants.
These were not shorthanded, I'll replace these with TyLam.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CaseData is a somewhat unusual situation, as it's the only builtin that does this.

Comment on lines +96 to +99
runASGBuilder :: ASGBuilder a -> ASGBuilderState -> (Either ASGBuilderError a, ASGBuilderState)
runASGBuilder (ASGBuilder m) s =
let stateM = runExceptT m
in runState stateM s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit different to the original, and unfortunately isn't doing quite what we want. The reason why the original builder runner worked the way it did was because, by forcing the monad to produce a result of type Id, we ensure we have a toplevel computation, which this does not do.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean we should restrict the type of runASGBuilder to accept ASGBuilder Id or we should flip the order of State and Either in the stack?
If it's the latter case, it would just make the partial state unavailable in the case of an error, right? It wouldn't ensure we have a top-level computation?

@itsfarseen itsfarseen mentioned this pull request Feb 12, 2025
@itsfarseen
Copy link
Collaborator Author

Closing in favor of #45

@itsfarseen itsfarseen closed this Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants