std:EVM/State/Bindings

lib/EVM/State/Bindings.omg

lib/EVM/State/Bindings.omg81 lines
1-- Build an owner's lexical storage bindings from its type, not its layout.
2-- Cell interprets a single location. A mapping binds a function of its
3-- actual key. Keep a nested record's whole value separate from its fields:
4-- even a user field named `whole` or `fields` has no reserved meaning.
5let S = std:EVM/State
6let R = std:Reflection/Row
7let scope =
8      \{T : {} -> Type}
9       (shape : Reflect @1 {=} T)
10       (Cell : Type -> Type)
11       (cell : forall (A : Type) -> S.Path (T {=}) A -> Cell A)
12       (Sequence : Type -> Type -> Type)
13       (sequence :
14         forall (A Element : Type)
15         -> S.Path (T {=}) (List A)
16         -> (Word 256 -> Element)
17         -> Sequence A Element) ->
18        let Owner = T {=}
19        let Plan = \(A : Type) -> exists (B : Type) * S.Path Owner A -> B
20        let node = \(F : {} -> Type) -> Bool -> Plan (F {=})
21        let leaf = \(A : Type) (_ : Bool) -> ((Cell A, cell A) : Plan A)
22        let plan =
23              shape.fold
24                node
25                { leaf = {=}
26                , bool = leaf Bool
27                , natural = leaf Natural
28                , integer = leaf Integer
29                , double = leaf Double
30                , text = leaf Text
31                , word = \bits -> leaf (Word bits)
32                , optional = \A _ -> leaf (Optional (A {=}))
33                , pair = \A B _ _ -> leaf (A {=} * B {=})
34                , union = \I row children _ _ -> leaf < ...row (\i -> children i {=}) >
35                , list =
36                    \A child _ ->
37                      let V = A {=}
38                      let element = child False
39                      in  ((Sequence V element.1,
40                           \path ->
41                             sequence
42                               V
43                               element.1
44                               path
45                               (\i -> element.2 (S.item @Owner @V path i))) :
46                             Plan (List V))
47                , function =
48                    \K V _ child _ ->
49                      let entry = child False
50                      in  ((K {=} -> entry.1,
51                           \path key -> entry.2 (S.key @Owner @(K {=}) @(V {=}) path key)) :
52                             Plan (K {=} -> V {=}))
53                , record =
54                    \I row children ops plans top ->
55                      let F = \(i : I) -> children i {=}
56                      let Record = { ...row F }
57                      let parts = \(i : I) -> plans i False
58                      let G = \(i : I) -> (parts i).1
59                      let Fields = { ...row G }
60                      let build =
61                            \(parent : S.Path Owner Record) ->
62                              ops.build
63                                G
64                                R.Identity
65                                R.identityMonad
66                                (\i ->
67                                   (parts i).2
68                                     (S.field @Owner @Record @(F i) parent (ops.project F i)))
69                      in  if top
70                          then ((Fields, build) : Plan Record)
71                          else (({ whole : Cell Record, fields : Fields },
72                                \parent -> { whole = cell Record parent, fields = build parent }) :
73                                  Plan Record)
74                }
75        let root = plan True
76        in  { Bindings = root.1
77            , bind =
78                \{l : Level} {A : Type l} (implementation : root.1 -> A) ->
79                  implementation (root.2 (S.root Owner))
80            }
81in  { scope }
Your result
Complete file: lib/EVM/State/Bindings.omg · with imports