-
Notifications
You must be signed in to change notification settings - Fork 479
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
Add Delay type and use it #5910
Open
michaelpj
wants to merge
3
commits into
master
Choose a base branch
from
mpj/delay-library
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1 +1 @@ | ||
346 | ||
343 |
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 |
---|---|---|
@@ -1,10 +1,40 @@ | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# OPTIONS_GHC -Wno-unused-foralls #-} | ||
module PlutusTx.Utils where | ||
|
||
-- We do not use qualified import because the whole module contains off-chain code | ||
import PlutusTx.Builtins.Internal qualified as BI | ||
import Prelude as Haskell | ||
|
||
mustBeReplaced :: String -> a | ||
mustBeReplaced placeholder = | ||
error $ | ||
"The " <> show placeholder <> " placeholder must have been replaced by the \ | ||
\core-to-plc plugin during compilation." | ||
|
||
-- | Delay evalaution of the expression inside the 'Delay' constructor. | ||
data Delay a = Delay ~(forall b. a) | ||
|
||
-- | Force the evaluation of the expression delayed by the 'Delay'. | ||
force :: Delay a -> a | ||
-- We can use any type here, but the type instantiation will keep the | ||
-- type alive, so best to use a builtin type to avoid keeping other stuff | ||
-- needlessly live. | ||
force (Delay a) = a @BI.BuiltinUnit | ||
|
||
{- Note [The Delay type] | ||
Occasionally we need to explicitly delay evaluation of an expression, since PLC | ||
is strict. Ideally, this would eventually translate into the underlying 'delay' | ||
and 'force' constructors of UPLC, as this is the fastest way to do it. | ||
|
||
Our typed intermediate languages do not have 'delay' and 'force', however. | ||
What they do have is type abstractions and type instantiations, which get | ||
compiled into 'delay' and 'force', respectively. So what we want is something | ||
which will create a (needless) type abstraction/type instantiation. | ||
|
||
This is what 'Delay' does: the constructor accepts a 'forall' type, so the | ||
expression inside the constructor application should be a type abstraction; | ||
and 'force' just instantiates this to an arbitrary type, in this case | ||
'BuiltinUnit'. | ||
-} |
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.
I guess the
BuiltinUnit
is necessary in order for it to be compiled intodelay
andforce
?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.
Mysteriously it compiles without it, but I think it's better to be explicit about doing the type instantiation.