Skip to content

Commit 8d5102f

Browse files
committed
Doc: Add environment
1 parent d17e2fd commit 8d5102f

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

doc/environment.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Environment
2+
3+
An environment Ξ maps variable names to Simplicity expressions.
4+
5+
All expressions inside an environment share the same source type A. We say the environment is "from type A".
6+
7+
```
8+
Ξ =
9+
[ foo ↦ unit: (𝟚^32? × 2^32) → 𝟙
10+
, bar ↦ take iden: (𝟚^32? × 𝟚^32) → 𝟚^32?
11+
, baz ↦ drop iden: (𝟚^32? × 𝟚^32) → 𝟚^32
12+
]
13+
```
14+
15+
We use environments to translate variables inside Simphony expressions to Simplicity.
16+
17+
The environment tells us the Simplicity expression that returns the value of each variable.
18+
19+
We translate a Simphony program "top to bottom". Each time a variable is defined, we update the environment to reflect this change.
20+
21+
During the translation, we can ignore the source type of Simplicity expressions (translated Simphony expressions) entirely. We can focus on producing a Simplicity value of the expected target type. Environments ensure that we get input values for each variable that is in scope.
22+
23+
Target types are handled by contexts.
24+
25+
We obtain context Ctx(Ξ) from environment Ξ by mapping each variable `a` from Ξ to the target type of Ξ(`x`):
26+
27+
Ctx(Ξ)(`x`) = B if Ξ(`x`) = a: A → B
28+
29+
## Patterns
30+
31+
32+
Patterns occur in let statements `let p := s`.
33+
34+
Pattern `p` binds the output of Simphony expression `s` to variables.
35+
36+
As we translate `s` to Simplicity, we need an environment that maps the variables from `p` to Simplicity expressions.
37+
38+
If `p` is just a variable `p = a`, then the environment is simply [`a` ↦ iden: A → A].
39+
40+
If `p` is a product of two variables `p = (a, b)`, then the environment is [`a` ↦ take iden: A × B → A, `b` ↦ drop iden: A × B → B.
41+
42+
"take" and "drop" are added as we go deeper in the product hierarchy. The pattern `_` is ignored.
43+
44+
PEnv'(t: A → B, `v`) := [`v` ↦ t]
45+
46+
PEnv'(t: A → B, `_`) := []
47+
48+
If `p1` and `p2` contain disjoint sets of variables
49+
50+
Then PEnv'(t: A → B × C, `(p1, p2)`) := PEnv'(take t: A → B, p1) ⊎ PEnv'(drop t: A → C, p2)
51+
52+
PEnv(A, `p`) := PEnv'(iden: A → A, `p`)
53+
54+
Pattern environments are compatible with pattern contexts:
55+
56+
Ctx(PEnv(A, `p`)) = PCtx(A, `p`)
57+
58+
## Product
59+
60+
We write Product(ΞA, ΞB) to denote the **product** of environment ΞA from A and environment ΞB from B.
61+
62+
The product is an environment from type A × B.
63+
64+
When two Simplicity expressions with environments are joined using the "pair" combinator, then the product of both environments gives us updated bindings for all variables.
65+
66+
If the same variable is bound in both environments, then the binding from the first environment is taken.
67+
68+
If ΞA maps `v` to Simplicity expression a: A → C
69+
70+
Then Product(ΞA, ΞB) maps `v` to take a: A × B → C
71+
72+
If ΞB maps `v` to Simplicity expression b: B → C
73+
74+
If ΞA doesn't map `v`
75+
76+
Then Product(ΞA, ΞB) maps `v` to drop b: A × B → C
77+
78+
Environment products are compatible with context updates:
79+
80+
Ctx(Product(ΞA, ΞB)) = Ctx(ΞB) // Ctx(ΞA)
81+
82+
The order of B and A is reversed: The context of ΞB is updated with the dominant context of ΞA.

0 commit comments

Comments
 (0)