std:EVM/Immutable
lib/EVM/Immutable.omg
lib/EVM/Immutable.omg192 lines
1-- Code immutables have a fixed number of words. The structural fold derives
2-- both directions; dynamic values and functions cannot obtain the certificate.
3let S = std:Reflection/Structure
4let N = std:Natural
5let L = std:List
6let Word256 = Word 256
7let lit = Word/fromNatural 256
8let Of = \(A : Type) -> Reflect {=} (\(_ : {}) -> A)
9let Plan =
10 \(A : Type) ->
11 { width : Natural
12 , valid : Bool
13 , read :
14 forall (Code : Type) -> (Natural -> Word256) -> Natural -> (A -> Code) -> Code -> Code
15 , write : forall (Code : Type) -> A -> (List Word256 -> Code) -> Code -> Code
16 }
17let unsupported =
18 \(A : Type) ->
19 ({ width = 0
20 , valid = False
21 , read = \Code get start next fail -> fail
22 , write = \Code value next fail -> fail
23 } :
24 Plan A)
25let plan =
26 \{A : Type} (description : Of A) ->
27 S.interpret
28 description
29 {=}
30 Plan
31 { leaf = {=}
32 , function = \A B argument result -> unsupported (A -> B)
33 , natural = unsupported Natural
34 , integer = unsupported Integer
35 , double = unsupported Double
36 , text = unsupported Text
37 , list = \A child -> unsupported (List A)
38 , word =
39 \bits ->
40 let width = N.div (bits + 255) 256
41 in { width
42 , valid = True
43 , read =
44 \Code get start next fail ->
45 next
46 (N.fold
47 width
48 (\s ->
49 { index = s.index + 1
50 , value =
51 Word/or
52 (Word/shl 256 s.value)
53 (Word/fromNatural
54 bits
55 (Word/toNatural (get (start + s.index))))
56 })
57 { index = 0, value = Word/fromNatural bits 0 }).value
58 , write =
59 \Code value next fail ->
60 next
61 (L.map
62 (\index ->
63 lit
64 (Word/toNatural
65 (Word/shr (256 * N.minus width (index + 1)) value)))
66 (L.generate width (\index -> index)))
67 }
68 , bool =
69 { width = 1
70 , valid = True
71 , read = \Code get start next fail -> next (Word/equal (get start) (lit 1))
72 , write = \Code value next fail -> next [ if value then lit 1 else lit 0 ]
73 }
74 , pair =
75 \A B first second ->
76 { width = first.width + second.width
77 , valid = first.valid && second.valid
78 , read =
79 \Code get start next fail ->
80 first.read
81 Code
82 get
83 start
84 (\a -> second.read Code get (start + first.width) (\b -> next (a, b)) fail)
85 fail
86 , write =
87 \Code value next fail ->
88 first.write
89 Code
90 value.1
91 (\a -> second.write Code value.2 (\b -> next (a # b)) fail)
92 fail
93 }
94 , optional =
95 \A child ->
96 { width = 1 + child.width
97 , valid = child.valid
98 , read =
99 \Code get start next fail ->
100 if Word/isZero (get start)
101 then next (None A)
102 else child.read Code get (start + 1) (\a -> next (Some a)) fail
103 , write =
104 \Code value next fail ->
105 std:Optional.fold
106 (\a -> child.write Code a (\words -> next ([ lit 1 ] # words)) fail)
107 (next ([ lit 0 ] # L.replicate child.width (lit 0)))
108 value
109 }
110 , record =
111 \R build fields ->
112 { width =
113 fields Natural (\A name position child project rest -> rest + child.width) 0
114 , valid =
115 fields Bool (\A name position child project rest -> rest && child.valid) True
116 , read =
117 \Code get start next fail ->
118 let Reader = \(T : Type) -> Natural -> (T -> Natural -> Code) -> Code
119 let monad
120 : std:Monad.Monad Reader
121 = { pure = \{T} value at after -> after value at
122 , bind =
123 \{T} {U} action after at finish ->
124 action at (\value end -> after value end finish)
125 }
126 in build
127 Reader
128 monad
129 (\T name position child at after ->
130 child.read
131 Code
132 get
133 at
134 (\value -> after value (at + child.width))
135 fail)
136 start
137 (\value end -> next value)
138 , write =
139 \Code value next fail ->
140 fields
141 (List Word256 -> Code)
142 (\A name position child project before words ->
143 child.write Code (project value) (\more -> before (more # words)) fail)
144 next
145 []
146 }
147 , union =
148 \U cases fold ->
149 let labels =
150 cases (List Text) (\A name position child inject rest -> rest # [ name ]) []
151 let width =
152 cases Natural (\A name position child inject rest -> N.max rest child.width) 0
153 let tag =
154 \name ->
155 L.length (L.takeWhile (\other -> Text/equal name other == False) labels)
156 in { width = 1 + width
157 , valid =
158 cases Bool (\A name position child inject rest -> rest && child.valid) True
159 , read =
160 \Code get start next fail ->
161 cases
162 Code
163 (\A name position child inject rest ->
164 if Word/equal (get start) (lit (tag name))
165 then child.read
166 Code
167 get
168 (start + 1)
169 (\value -> next (inject value))
170 fail
171 else rest)
172 fail
173 , write =
174 \Code value next fail ->
175 fold
176 Code
177 (\A name position child payload ->
178 child.write
179 Code
180 payload
181 (\words ->
182 next
183 ([ lit (tag name) ]
184 # words
185 # L.replicate (N.minus width child.width) (lit 0)))
186 fail)
187 value
188 }
189 }
190let Valid = \{A : Type} (description : Of A) -> (plan description).valid === True
191let derive = \{A : Type} (description : Of A) {valid : Valid description} -> plan description
192in { Of, Plan, Valid, derive }