Skip to content

Commit 14a5dc3

Browse files
committed
address comments
1 parent 02c3767 commit 14a5dc3

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

src/plutus/cek.md

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,56 @@ The production Haskell evaluator is based on the CEK machine, and it also provid
77
The following listing defines some key concepts of the CEK machine.
88

99
```text
10-
Σ ∈ State ::= 𝑠; 𝜌 ⊳ 𝑀 // Computing M under environment 𝜌 with stack 𝑠
11-
| 𝑠 ⊲ 𝑉 // Returning a value 𝑉 to stack 𝑠
12-
| ⬥ // Throwing an error
13-
| ◻ 𝑉 // Final state with result 𝑉
10+
Σ ∈ State ::= 𝑠; 𝜌 ⊳ 𝑀 Computing M under environment 𝜌 with stack 𝑠
11+
| 𝑠 ⊲ 𝑉 Returning a value 𝑉 to stack 𝑠
12+
| ⬥ Throwing an error
13+
| ◻𝑉 Final state with result 𝑉
1414
1515
𝑠 ∈ Stack ::= 𝑓* // A stack has zero or more stack frames
1616
17-
𝑉 ∈ CEK value ::= 〈con T 𝑐〉 // A constant 𝑐 with type T
18-
| 〈delay 𝑀 𝜌〉 // A delayed computation, with an
19-
// associated environment
20-
| 〈lam 𝑥 𝑀 𝜌〉 // A lambda abstraction, with an
21-
// associated environment
22-
| 〈constr 𝑖 𝑉*〉 // A constructor application, where
23-
// all arguments are values
24-
| 〈builtin 𝑏 𝑉* 𝜂〉 // A builtin application with all supplied
25-
// arguments as values, and expecting
26-
// at least one more argument
17+
𝑉 ∈ CEK value ::= 〈con T 𝑐〉 A constant 𝑐 with type T
18+
| 〈delay 𝑀 𝜌〉 A delayed computation, with an
19+
associated environment
20+
| 〈lam 𝑥 𝑀 𝜌〉 A lambda abstraction, with an
21+
associated environment
22+
| 〈constr 𝑖 𝑉*〉 A constructor application, where
23+
all arguments are values
24+
| 〈builtin 𝑏 𝑉* 𝜂〉 A builtin application with all supplied
25+
arguments as values, and expecting
26+
at least one more argument
2727
28-
𝜌 ∈ Environment ::= [] // An empty environment
29-
| 𝜌[𝑥 ↦ 𝑉] // Associate 𝑥 with 𝑉 in the environment
28+
𝜌 ∈ Environment ::= [] An empty environment
29+
| 𝜌[𝑥 ↦ 𝑉] Associate 𝑥 with 𝑉 in the environment
3030
3131
𝜂 ∈ Expected builtin arguments ::= [𝜄] // One argument
3232
| 𝜄⋅𝜂 // Two or more arguments
3333
34-
𝑓 ∈ Frame ::= (force _) // Awaiting a delayed computation to be forced
35-
| [_ (𝑀, 𝜌)] // An application awaiting the function, where the
36-
// argument is a term associated with an environment
37-
| [_ 𝑉] // An application awaiting the function, where the
38-
// argument is a value
39-
| [𝑉 _] // An application awaiting the argument, where the
40-
// function is a value
41-
| (constr 𝑖 𝑉* _ (𝑀*, 𝜌)) // A constructor application awaiting
42-
// an argument. The arguments before
43-
// are values, and the arguments after
44-
// are terms to be evaluated.
45-
| (case _ (𝑀*, 𝜌)) // A case expression awaiting the scrutinee
34+
𝑓 ∈ Frame ::= (force _) Awaiting a delayed computation to be forced
35+
| [_ (𝑀, 𝜌)] An application awaiting the function, where the
36+
argument is a term associated with an environment
37+
| [_ 𝑉] An application awaiting the function, where the
38+
argument is a value
39+
| [𝑉 _] An application awaiting the argument, where the
40+
function is a value
41+
| (constr 𝑖 𝑉* _ (𝑀*, 𝜌)) A constructor application awaiting
42+
an argument. The arguments before the hole
43+
are values, and the arguments after
44+
are terms to be evaluated.
45+
| (case _ (𝑀*, 𝜌)) A case expression awaiting the scrutinee
4646
```
4747

4848
The CEK machine has two main kinds of states:
4949
- `𝑠; 𝜌 ⊳ 𝑀` denotes evaluating term `𝑀` with environment `𝜌` and stack `𝑠`.
5050
- `𝑠 ⊲ 𝑉` denotes returning a value `𝑉` to stack `𝑠`.
5151

52-
A value is a fully evaluated term, and an environment is a map binding variables to values.
52+
A value is a fully evaluated term, plus environments necessary for further computation.
53+
An environment is a map binding variables to values.
5354
A stack frame contains a hole to represent a pending value, and the context needed to continue evaluation once the value is received.
55+
A builtin argument `𝜄` is either a term or a type argument.
5456

5557
To evaluate a Plutus program containing a term `𝑀`, the machine starts from state `[]; [] ⊳ 𝑀`, and based on the following transition table, proceeds as follows:
5658
- If the current CEK machine state matches the From State, and the associated condition (if exists) is met, then the CEK machine transitions into the To State.
57-
- If the machine arrives at state `◻𝑉`, the machine terminates with success, yielding `𝑉` as final result.
59+
- If the machine arrives at state `◻𝑉`, the machine terminates with success, yielding the Plutus term corresponding to `𝑉` (which is essentially `𝑉` but with the environments removed) as final result.
5860
- If the machine gets stuck (i.e., no rule applies) or arrives at state ``, the evaluation terminates with a failure.
5961

6062
|Rule|From State | To State | Condition |
@@ -83,7 +85,7 @@ To evaluate a Plutus program containing a term `𝑀`, the machine starts from s
8385
| 22 | `(force _)⋅𝑠 ⊲ 〈builtin 𝑏 𝑉* [𝜄]〉` | `𝖤𝗏𝖺𝗅𝖢𝖤𝖪 (𝑠, 𝑏, 𝑉*)` | `𝜄` is a type argument |
8486
| 23 | `(constr 𝑖 𝑉* _ (𝑀⋅𝑀*, 𝜌))⋅𝑠 ⊲ 𝑉` | `(constr 𝑖 𝑉*⋅𝑉 _ (𝑀*, 𝜌))⋅𝑠; 𝜌 ⊳ 𝑀` | |
8587
| 24 | `(constr 𝑖 𝑉 _ ([], 𝜌))⋅𝑠 ⊲ 𝑉` | `𝑠 ⊲ 〈constr 𝑖 𝑉*⋅𝑉 〉` | |
86-
| 25 | `(case _ (𝑀0𝑀𝑛 , 𝜌))⋅𝑠 ⊲ 〈constr 𝑖 𝑉1𝑉𝑚` | `[_ 𝑉𝑚]⋅⋯⋅[_ 𝑉1]⋅𝑠; 𝜌 ⊳ 𝑀𝑖` | `0 ≤ 𝑖 ≤ 𝑛` |
88+
| 25 | `(case _ (𝑀₀𝑀ₙ , 𝜌))⋅𝑠 ⊲ 〈constr 𝑖 𝑉₁𝑉ₘ` | `[_ 𝑉ₘ]⋅⋯⋅[_ 𝑉₁]⋅𝑠; 𝜌 ⊳ 𝑀𝑖` | `0 ≤ 𝑖 ≤ 𝑛` |
8789

8890
In this table, `X*` denotes a list of `X`s.
8991
The symbol `` denotes either the cons or snoc operator on lists.
@@ -132,5 +134,5 @@ Explanation of the transition rules:
132134
23. When a value `𝑉` is returned to a stack whose top frame is a constructor application, with the hole in any argument position except the last (in other words, there is at least one more argument to be evaluated), the machine replaces the frame with one where the hole is moved to the next argument, and proceeds to evaluate the next argument `𝑀` in the captured environment.
133135
24. Like Rule 23, except that the hole is in the position of the last argument.
134136
In this case, all arguments have been evaluated, so the machine pops the frame and returns a `constr` value.
135-
25. If the returned value is a constructor application with index `𝑖`, and the top stack frame is a `case` frame, the machine will evaluate the `𝑖`th branch - `𝑀𝑖` - applied to arguments `𝑉𝑚𝑉1` (it is important to note that arguments under `constr` are reversed when passing to a `case` branch - this is done for performance reasons).
136-
To do so, it pops the frame, and pushes `𝑚` frames, each representing an application, with the top frame corresponding to `𝑉𝑚` (the first argument).
137+
25. If the returned value is a constructor application with index `𝑖`, and the top stack frame is a `case` frame, the machine will evaluate the `𝑖`th branch - `𝑀𝑖` - applied to arguments `𝑉ₘ𝑉₁` (it is important to note that arguments under `constr` are reversed when passing to a `case` branch - this is done for performance reasons).
138+
To do so, it pops the frame, and pushes `m` frames, each representing an application, with the top frame corresponding to `𝑉ₘ` (the first argument).

0 commit comments

Comments
 (0)