You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/getting-started.md
+47-36Lines changed: 47 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@
5
5
An instance of `CEMScript` revolves primarily around the following two type classes.
6
6
7
7
Here, `script` is an uninhabited type that is used to
8
-
tie together the different types associated with an instance of CEMScript.
8
+
tie together the different types associated with an instance of CEMScript.on
9
9
10
10
`CEMScriptTypes` defines those types:
11
11
@@ -37,8 +37,8 @@ class
37
37
--| The crux part - a map that defines constraints for each transition via DSL.
38
38
transitionSpec::CEMScriptSpecFalsescript
39
39
40
-
--| Optional Plutus script to calculate things, whic can be used in the cases
41
-
-- when CEM constrainsts and/or inlining Plutarch functions are not expresisble
40
+
--| Optional Plutus script to calculate things, which can be used in the cases
41
+
-- when CEM constraints and/or inlining Plutarch functions are not expressible
42
42
-- enough.
43
43
transitionComp::
44
44
Maybe
@@ -61,11 +61,11 @@ type CEMScriptSpec resolved script =
61
61
62
62
### Spine
63
63
64
-
Spineof an ADT is a sumtype that is just a list ofit’s constructors.
64
+
The spine of an ADT is a sumtype that is just a list ofits constructors.
65
65
66
-
`HasSpine` type class maps from an ADT to it’sSpinetypeand:
66
+
`HasSpine` type class maps from an ADT to itsSpinetypeand:
67
67
68
-
- associates the type to it’sSpinetype through a type family
68
+
- associates the type to itsSpinetype through a type family
69
69
- defines a function that translates an instance of the type to an instance of the spine type
70
70
71
71
```haskell
@@ -75,12 +75,12 @@ class HasSpine sop where
75
75
getSpine::sop->Spinesop
76
76
```
77
77
78
-
Weprovide a function `deriveSpine` to automatically derive `HasSpine` using template haskell.
78
+
Using the TemplateHaskell, we provide a function `deriveSpine` to automatically derive `HasSpine`.
79
79
80
80
### DSL
81
81
82
82
When writing `CEMScript` transitions, we start by describing constraints
83
-
at a high level using a DSL that consistesof the following types:
83
+
at a high level using a DSL that consistsof the following types:
84
84
85
85
#### TxConstraint
86
86
@@ -92,8 +92,10 @@ data TxConstraint (resolved :: Bool) script where
92
92
`TxConstraint` is a GADT (GeneralizedAlgebraicDataType) parameterized by two things:
93
93
94
94
1.**`resolved`**:ABooleantype flag (usually `True` or `False`) indicating whether the constraint is in a "resolved" form.
95
-
In on-chain code, `resolved` will be `False`, representing the actual computation that’s going to take place.In off-chain code, `resolved` will be `True`, which is used to derive a valid transaction from a list of `TxConstraint`s.See the Off-chain machinery section for more details on how this works.
96
-
2.**`script`**:A phantom type parameter indicating which CEMScript (state machine) the constraints belong to.
95
+
In on-chain code, `resolved` will be `False`, representing the actual computation that’s going to take place.
96
+
In off-chain code, `resolved` will be `True`, which is used to derive a valid transaction from a list of `TxConstraint`s.
97
+
See the Off-chain machinery section for more details on how this works.
98
+
3.**`script`**:A phantom type parameter indicating which CEMScript (state machine) the constraints belong to.
97
99
98
100
See the reference section for a full reference of `TxConstraint`.
99
101
@@ -102,9 +104,10 @@ See the reference section for a full reference of `TxConstraint`.
102
104
`ConstraintDSL script value` is a GADT that represents a symbolic expression in the DSL.
103
105
104
106
-`script` is a phantom type parameter, just like in `TxConstraint`.
105
-
-`value` is the typeofthe what this expression will resolve to during runtime.
107
+
-`value` is the typeof what this expression will resolve during runtime.
106
108
107
-
`ConstraintDSL` allows us to reference parts of the state machine's parameters, the current state, the transition arguments, and so forth.
109
+
`ConstraintDSL` allows us to reference parts of the state machine's parameters,
110
+
the current state, the transition arguments, and so forth.
108
111
109
112
It also lets us perform checks (like equality) and apply transformations (like lifting a Plutarch function).
110
113
@@ -122,7 +125,7 @@ type family DSLPattern (resolved :: Bool) script value where
122
125
DSLPatternTrue_value=Void
123
126
```
124
127
125
-
These are both typelevel wrappers over `ConstraintDSL`.
128
+
These are both type-level wrappers over `ConstraintDSL`.
126
129
127
130
Thetype parameter `resolved` will be False for on-chain code, while it will be True for off-chain code.
128
131
@@ -179,7 +182,7 @@ data ResolvedTx = MkResolvedTx
179
182
180
183
```
181
184
182
-
`resolveTx` is the primary entrypoint for off-chain code.
185
+
`resolveTx` is the primary entry point for off-chain code.
183
186
It accepts a `TxSpec`, which consists of a list of actions and a signer,
184
187
and produces a `ResolvedTx`, which is all the information needed to construct
185
188
a transaction that’s ready to be submitted to the chain.
@@ -188,10 +191,10 @@ Here’s a rough outline of how it works:
188
191
189
192
-For each `Transition` in the list of actions
190
193
-Find the corresponding transition in the `transitionSpec`
191
-
-If the transition spec contains a `TxFanInSameScript`, check the current on-chain state is the same a required by the constraint.
192
-
-Convert the `[TxConstraint (resolved ::False)]` of the `Transition` to `[TxConstraint (resolved ::True)]` by invoking `compileConstraint`.
194
+
-If the transition spec contains a `TxFanInSameScript`, check the current on-chain state is the same as required by the constraint.
195
+
-Convert the `[TxConstraint (resolved ::False)]` of the `Transition` to `[TxConstraint (resolved ::True)]` by invoking `compileConstraint`.
193
196
194
-
This evaluates all the `DSLValue _ value` expressions andresolve it to a value oftype`value`.
197
+
This evaluates all the `DSLValue _ value` expressions andresolves it to a value oftype`value`.
195
198
196
199
The following variants of `TxConstraint`
197
200
@@ -320,12 +323,12 @@ data TxConstraint (resolved :: Bool) script
320
323
Ensure
321
324
322
325
-Thesumof input utxos belonging to `user` is greater than `inValue`
323
-
-Thesumeof output utxos belonging to `user` is greater than `outValue`
326
+
-Thesumof output utxos belonging to `user` is greater than `outValue`
324
327
- `MainSignerNoValue address` Ensure that the given address is part of the transaction signatories.
325
328
- `Error message` Output`message` using `ptrace` as an error message.
326
329
- `If`
327
330
-Evaluate the condition and resolve to the `TxConstraint` in the “then” branch or the “else” branch.
328
-
- `MatchBySpinepatternmap` Evaluatepatternand execute the corresponding action from `map`
331
+
- `MatchBySpinepatternmap` Evaluatethe patternand execute the corresponding action from `map`
329
332
-`map`Map from `Spine` to `TxConstraint`
330
333
331
334
In addition to the above, CEMScript provides the following helper functions to create `TxConstraint`s
@@ -343,9 +346,9 @@ byFlagError flag message = If flag (Error message) Noop
343
346
344
347
```
345
348
346
-
-`cNot`invert a boolean
347
-
-`offchainOnly`execute only on chain. Resolve to `Noop` off chain.
348
-
-`byFlagError`evaluate to `Error message` if `flag` evaluates to true.
349
+
-`cNot`inverts a boolean
350
+
-`offchainOnly`executes only on chain. Resolve to `Noop` off chain.
351
+
-`byFlagError`evaluates to `Error message` if `flag` evaluates to true.
349
352
350
353
### ConstraintDSL
351
354
@@ -371,7 +374,7 @@ data ConstraintDSL script value where
371
374
-- Primitives
372
375
373
376
-- A wildcard pattern that matches any value.
374
-
-- Used in patternmatching contexts where the actual value is irrelevant.
377
+
-- Used in pattern-matching contexts where the actual value is irrelevant.
@@ -415,7 +418,7 @@ cMinLovelace = cMkAdaOnlyValue $ Pure 3_000_000
415
418
(@==)=Eq
416
419
```
417
420
418
-
The following are lifted versions of plutrach operators
421
+
The following are lifted versions of Plutarch operators
419
422
420
423
```haskell
421
424
(@<=)-- #<=
@@ -438,8 +441,8 @@ The following are lifted versions of plutrach operators
438
441
439
442
### How to define a script
440
443
441
-
-Define an empty type for your script: `dataMyScript`, whereMyScript can be any name.
442
-
-Define a `CEMScript` instance for it by providing `Params`, `State`, `Transition`, `transitionSpec`and optionally `transitionComp`.
444
+
-Define an empty type for your script: `dataMyScript`, whereMyScript can be any name.
445
+
-Define a `CEMScript` instance for it by providing `Params`, `State`, `Transition`, `transitionSpec`,and optionally `transitionComp`.
443
446
-DoTemplateHaskell derivations (`deriveCEMAssociatedTypes`) to generate dataand spine instances for pattern matching.
444
447
-Invoke the `compileCEM` function (e.g., `$(compileCEM True ''MyScript)`) to process the DSL specification, compile optional `transitionComp` code, and produce a `CEMScriptCompiled` instance.
445
448
-This generates an instance of `CEMScriptCompiled` for your script type.
@@ -449,13 +452,15 @@ The following are lifted versions of plutrach operators
449
452
450
453
#### Setup: The Types
451
454
452
-
First we define a type to denote our script.It’s an uninhabited type, it can’t be constructed.It’s only used as a tag for connecting different type classes together.
455
+
First, we define a type to denote our script.It’s an uninhabited type, it can’t be constructed.
456
+
It’s only used as a tag for connecting all instances oftype classes together.
453
457
454
458
```haskell
455
459
dataSimpleAuction
456
460
```
457
461
458
-
We define a type for the read-only state of our script.This state can’t be modified once created.This becomes the `Params` associated typeof the `CEMScript` typeclass.
462
+
We define a type for the read-only state of our script.This state can’t be modified once created.
463
+
This becomes the `Params` associated typeof the `CEMScript` type class.
459
464
460
465
```haskell
461
466
dataSimpleAuctionParams=MkAuctionParams
@@ -465,7 +470,7 @@ data SimpleAuctionParams = MkAuctionParams
465
470
derivingstock (Prelude.Eq, Prelude.Show)
466
471
```
467
472
468
-
We define a type for the evolving state of our script.This becomes the `State` associated typeof the `CEMScript` typeclass.
473
+
We define a type for the evolving state of our script.This becomes the `State` associated typeof the `CEMScript` type class.
469
474
470
475
```haskell
471
476
dataBid=MkBid
@@ -487,7 +492,7 @@ data SimpleAuctionState
487
492
derivingstock (Prelude.Eq, Prelude.Show)
488
493
```
489
494
490
-
Lastly, we define a type for the state transitions of our script.This becomes the `Transition` associated typeof the `CEMScript` typeclass.
495
+
Lastly, we define a type for the state transitions of our script.This becomes the `Transition` associated typeof the `CEMScript` type class.
491
496
492
497
```haskell
493
498
dataSimpleAuctionTransition
@@ -501,7 +506,10 @@ data SimpleAuctionTransition
501
506
derivingstock (Prelude.Eq, Prelude.Show)
502
507
```
503
508
504
-
We can now define an instance of the `CEMScriptTypes` for `SimpleAuction` . `CEMScriptTypes` is a super class of `CEMScript`, which just includes the associated types.By defining the associated types separately, we can use the `deriveCEMAssociatedTypes` template haskell function to generate some boilerplate.
509
+
We can now define an instance of the `CEMScriptTypes` for `SimpleAuction`.
510
+
`CEMScriptTypes` is a superclass of `CEMScript`, which just includes the associated types.
511
+
By defining the associated types separately, we can use the `deriveCEMAssociatedTypes`
512
+
TemplateHaskell function to generate some boilerplate.
505
513
506
514
```haskell
507
515
instanceCEMScriptTypesSimpleAuctionwhere
@@ -512,9 +520,11 @@ instance CEMScriptTypes SimpleAuction where
512
520
$(deriveCEMAssociatedTypes False ''SimpleAuction)
513
521
```
514
522
515
-
`deriveCEMAssociatedTypes` just executes `derivePlutusSpine` for all three of the associated types. But it can only do that if all the members of a type have a `HasPlutusSpine` implementation. This is why we need to do `derivePlutusSpine` for the `Bid` type ourselves.
523
+
`deriveCEMAssociatedTypes` just executes `derivePlutusSpine` for all three of the associated types.
524
+
But it can only do that if all the members of a type have a `HasPlutusSpine` implementation.
525
+
This is why we need to do `derivePlutusSpine` for the `Bid` type ourselves.
516
526
517
-
The boolean argument to `deriveCEMAssociatedTypes` is unused for now, and it is recommended to use a value of `False`.
527
+
The boolean argument to `deriveCEMAssociatedTypes` is unused for now, and it is recommended to use a value of `False`.
518
528
519
529
#### Implementation
520
530
@@ -524,13 +534,14 @@ To implement the logic of our script, we define an instance of `CEMScript` for o
524
534
instanceCEMScriptSimpleAuctionwhere
525
535
```
526
536
527
-
We provide a value for `compilationConfig` , which at the moment contains only a prefix for error codes to tell errors from different programs apart.
537
+
We provide a value for `compilationConfig`, which at the moment contains
538
+
only a prefix for error codes to tell errors from different programs apart.
528
539
529
540
```haskell
530
541
compilationConfig =MkCompilationConfig"AUC"
531
542
```
532
543
533
-
Next comes the meat of the script: `transitionSpec`. This is where we define state transitions
544
+
Next comes the meat of the script: `transitionSpec`. This is where we define state transitions
534
545
535
546
We create a Map of `Spine (Transition script)` → `[TxConstraint False script]`
0 commit comments