std:EVM/Solidity
lib/EVM/Solidity.omg
lib/EVM/Solidity.omg339 lines
1-- Explicit Solidity boundary roles. The source values remain ordinary Words,
2-- Text and Lists; these markers are used only in interface declarations.
3let L = std:List
4let N = std:Natural
5let Structure = std:Reflection/Structure
6let Interfaces = std:Reflection/Interface
7-- Markers carry a source type and its checked structural description. Their
8-- interpretation erases the marker, so handlers still receive ordinary values.
9let Node =
10 < Unsigned : Natural
11 | Signed : Natural
12 | Address
13 | FixedBytes : Natural
14 | Bytes
15 | Boolean
16 | String
17 | Array
18 | Tuple : List Text
19 | Union : List Text
20 | Indexed
21 | Anonymous
22 | Unsupported : Text
23 >
24let Description = List Node
25let Marker = Type * Description
26let WordKind = < Unsigned : Natural | Signed : Natural | Address | Bytes : Natural >
27let types =
28 { word = \(kind : WordKind) -> { solidityWord : kind === kind }
29 , bytes = \(key : {}) -> { solidityBytes : key === key }
30 , indexed = \(key : Marker) -> { solidityIndexed : key.2 === key.2, value : key.1 }
31 , anonymous = \(key : Marker) -> { solidityAnonymous : key.2 === key.2, value : key.1 }
32 }
33let Families =
34 { word : WordKind -> Type
35 , bytes : {} -> Type
36 , indexed : Marker -> Type
37 , anonymous : Marker -> Type
38 }
39-- Function-interface derivation is the same source interpretation used by
40-- native interfaces; only this vocabulary's leaf callbacks are supplied here.
41let interfaceSeed : Reflect types (\(_ : Families) -> {}) = {}
42let InterfaceAdapter =
43 Interfaces.make
44 Families
45 (\(P : (Families -> Type) -> Type) ->
46 { word : forall (kind : WordKind) -> P (\F -> F.word kind)
47 , bytes : forall (key : {}) -> P (\F -> F.bytes key)
48 , indexed : forall (key : Marker) -> P (\F -> F.indexed key)
49 , anonymous : forall (key : Marker) -> P (\F -> F.anonymous key)
50 })
51 interfaceSeed.formers
52let inspectInterface =
53 \{S : Families -> Type} (description : Reflect @1 types S) ->
54 description.fold
55 InterfaceAdapter.Plan
56 (InterfaceAdapter.algebra
57 // { leaf =
58 { word =
59 \kind ->
60 InterfaceAdapter.fromBoundary
61 { formers = interfaceSeed.formers
62 , fold = \P algebra -> algebra.leaf.word kind
63 }
64 , bytes =
65 \key ->
66 InterfaceAdapter.fromBoundary
67 { formers = interfaceSeed.formers
68 , fold = \P algebra -> algebra.leaf.bytes key
69 }
70 , indexed =
71 \key ->
72 InterfaceAdapter.fromBoundary
73 { formers = interfaceSeed.formers
74 , fold = \P algebra -> algebra.leaf.indexed key
75 }
76 , anonymous =
77 \key ->
78 InterfaceAdapter.fromBoundary
79 { formers = interfaceSeed.formers
80 , fold = \P algebra -> algebra.leaf.anonymous key
81 }
82 }
83 })
84let InterfaceValid =
85 \{S : Families -> Type} (description : Reflect @1 types S) ->
86 InterfaceAdapter.Valid @S (inspectInterface @S description).interface
87let interface =
88 \{S : Families -> Type} (description : Reflect @1 types S) ->
89 InterfaceAdapter.select @S (inspectInterface @S description).interface
90let wordBits =
91 \(kind : WordKind) ->
92 merge { Unsigned = \n -> n, Signed = \n -> n, Address = 160, Bytes = \n -> 8 * n } kind
93let values
94 : Families
95 = { word = \kind -> Word (wordBits kind)
96 , bytes = \_ -> List (Word 8)
97 , indexed = \key -> key.1
98 , anonymous = \key -> key.1
99 }
100-- The value-oriented interpreter is derived from the common structural fold.
101let Algebra =
102 \(F : Families) (P : Type -> Type) ->
103 Structure.Algebra
104 P
105 { word : forall (kind : WordKind) -> P (F.word kind)
106 , bytes : forall (key : {}) -> P (F.bytes key)
107 , indexed : forall (key : Marker) -> P (F.indexed key)
108 , anonymous : forall (key : Marker) -> P (F.anonymous key)
109 }
110let interpret =
111 \{A : Families -> Type}
112 (d : Reflect types A)
113 (F : Families)
114 (P : Type -> Type)
115 (algebra : Algebra F P) ->
116 d.fold (\S -> P (S F)) (Structure.algebra F P algebra)
117-- A description determines its ordinary source value type. Boundary roles
118-- disappear here; users never repeat the data shape or write a conversion.
119let Value = \{A : Families -> Type} (_ : Reflect types A) -> A values
120let Uint = \bits -> types.word (WordKind.Unsigned bits)
121let Int = \bits -> types.word (WordKind.Signed bits)
122let Address = types.word WordKind.Address
123let Bytes = \count -> types.word (WordKind.Bytes count)
124let ByteArray = types.bytes {=}
125
126-- Postorder descriptions contain no runtime values or user-provided pointers.
127-- Product labels retain ABI order, independently of source record storage.
128let Field = { name : Text, position : Optional Natural, description : Description }
129let ordered =
130 L.sortBy
131 (\(a b : Field) ->
132 std:Optional.fold
133 (\x -> std:Optional.fold (\y -> N.lessThanEqual x y) True b.position)
134 (std:Optional.fold (\_ -> False) True b.position)
135 a.position)
136let product =
137 \(positional : Bool) (tag : List Text -> Node) (fields : List Field) ->
138 if positional
139 && N.lessThan 1 (L.length fields)
140 && L.any (\(field : Field) -> std:Optional.isNone field.position) fields
141 then [ Node.Unsupported "a tuple with multiple fields requires explicit ABI field order" ]
142 else let fields = ordered fields
143 in L.concatMap (\(field : Field) -> field.description) fields
144 # [ tag (L.map (\(field : Field) -> field.name) fields) ]
145let describe =
146 \{A : Families -> Type} (d : Reflect types A) ->
147 interpret
148 d
149 values
150 (\(_ : Type) -> Description)
151 { leaf =
152 { word =
153 \kind ->
154 [ merge
155 { Unsigned = Node.Unsigned
156 , Signed = Node.Signed
157 , Address = Node.Address
158 , Bytes = Node.FixedBytes
159 }
160 kind
161 ]
162 , bytes = \_ -> [ Node.Bytes ]
163 , indexed = \key -> key.2 # [ Node.Indexed ]
164 , anonymous = \key -> key.2 # [ Node.Anonymous ]
165 }
166 , bool = [ Node.Boolean ]
167 , word = \bits -> [ Node.Unsigned bits ]
168 , text = [ Node.String ]
169 , natural = [ Node.Unsupported "Natural requires an explicit bounded conversion" ]
170 , integer = [ Node.Unsupported "Integer requires an explicit bounded conversion" ]
171 , double = [ Node.Unsupported "Double has no Solidity ABI representation" ]
172 , list = \A child -> child # [ Node.Array ]
173 , optional =
174 \A child -> [ Node.Unsupported "Optional requires an explicit Solidity adapter" ]
175 , pair = \A B first second -> first # second # [ Node.Tuple [ "1", "2" ] ]
176 , function =
177 \A B argument result ->
178 [ Node.Unsupported "functions have no Solidity ABI representation" ]
179 , record =
180 \R build fields ->
181 product
182 True
183 Node.Tuple
184 (fields
185 (List Field)
186 (\A name position description project rest ->
187 rest # [ { name, position, description } ])
188 [])
189 , union =
190 \U cases fold ->
191 product
192 False
193 Node.Union
194 (cases
195 (List Field)
196 (\A name position description inject rest ->
197 rest # [ { name, position, description } ])
198 [])
199 }
200-- The canonical ABI spelling of the type a description ends with, such as
201-- `(address,uint8[])`: each node takes its children from a stack of the
202-- spellings before it. A node that no ABI type of a parameter spells (a
203-- union, an event annotation, an unsupported shape) leaves the stack as it
204-- is; the compiler refuses such a description on its own.
205let canonical
206 : Description -> Text
207 = \description ->
208 let push = \(stack : List Text) (spelled : Text) -> [ spelled ] # stack
209 let step =
210 \(stack : List Text) (node : Node) ->
211 let top = std:Optional.default "" (L.head stack)
212 in merge
213 { Unsigned = \(bits : Natural) -> push stack ("uint" ++ Natural/show bits)
214 , Signed = \(bits : Natural) -> push stack ("int" ++ Natural/show bits)
215 , Address = push stack "address"
216 , FixedBytes =
217 \(count : Natural) -> push stack ("bytes" ++ Natural/show count)
218 , Bytes = push stack "bytes"
219 , Boolean = push stack "bool"
220 , String = push stack "string"
221 , Array = push (L.drop 1 stack) (top ++ "[]")
222 , Tuple =
223 \(names : List Text) ->
224 let count = L.length names
225 let fields = L.reverse (L.take count stack)
226 in push
227 (L.drop count stack)
228 ("(" ++ std:Text.concatSep "," fields ++ ")")
229 , Union = \(_ : List Text) -> stack
230 , Indexed = stack
231 , Anonymous = stack
232 , Unsupported = \(_ : Text) -> stack
233 }
234 node
235 in std:Optional.default "" (L.head (L.foldLeft step ([] : List Text) description))
236-- The canonical signature `name(type1,…)` of a function or an error whose
237-- parameters a description gives: a tuple's fields, or a single type.
238let signature
239 : Text -> Description -> Text
240 = \name description ->
241 let tuple =
242 \(node : Node) ->
243 merge
244 { Unsigned = \(_ : Natural) -> False
245 , Signed = \(_ : Natural) -> False
246 , Address = False
247 , FixedBytes = \(_ : Natural) -> False
248 , Bytes = False
249 , Boolean = False
250 , String = False
251 , Array = False
252 , Tuple = \(_ : List Text) -> True
253 , Union = \(_ : List Text) -> False
254 , Indexed = False
255 , Anonymous = False
256 , Unsupported = \(_ : Text) -> False
257 }
258 node
259 let spelled = canonical description
260 in name
261 ++ (if std:Optional.fold tuple False (L.last description)
262 then spelled
263 else "(" ++ spelled ++ ")")
264let Indexed =
265 \{A : Families -> Type} (d : Reflect types A) ->
266 types.indexed ((A values, describe d) : Marker)
267let Anonymous =
268 \{A : Families -> Type} (d : Reflect types A) ->
269 types.anonymous ((A values, describe d) : Marker)
270-- Ordinary native descriptions have no ABI role markers. Words therefore
271-- retain their unsigned role; unsupported native shapes fail only when this
272-- interpretation is actually selected as a public Solidity boundary.
273let describeNative =
274 \{A : {} -> Type} (d : Reflect {=} A) ->
275 Structure.interpret
276 d
277 {=}
278 (\_ -> Description)
279 { leaf = {=}
280 , bool = [ Node.Boolean ]
281 , word = \bits -> [ Node.Unsigned bits ]
282 , text = [ Node.String ]
283 , natural = [ Node.Unsupported "Natural requires an explicit bounded conversion" ]
284 , integer = [ Node.Unsupported "Integer requires an explicit bounded conversion" ]
285 , double = [ Node.Unsupported "Double has no Solidity ABI representation" ]
286 , list = \A child -> child # [ Node.Array ]
287 , optional =
288 \A child -> [ Node.Unsupported "Optional requires an explicit Solidity adapter" ]
289 , pair = \A B first second -> first # second # [ Node.Tuple [ "1", "2" ] ]
290 , function =
291 \A B argument result ->
292 [ Node.Unsupported "functions have no Solidity ABI representation" ]
293 , record =
294 \R build fields ->
295 product
296 True
297 Node.Tuple
298 (fields
299 (List Field)
300 (\A name position description project rest ->
301 rest # [ { name, position, description } ])
302 [])
303 , union =
304 \U cases fold ->
305 product
306 False
307 Node.Union
308 (cases
309 (List Field)
310 (\A name position description inject rest ->
311 rest # [ { name, position, description } ])
312 [])
313 }
314in { Algebra
315 , Interface = InterfaceAdapter.Package
316 , InterfaceValid
317 , interface
318 , WordKind
319 , Marker
320 , interpret
321 , types
322 , values
323 , wordBits
324 , Value
325 , Families
326 , Description
327 , describe
328 , describeNative
329 , signature
330 , Uint
331 , Int
332 , Address
333 , Bytes
334 , ByteArray
335 , Indexed
336 , Anonymous
337 , Node
338 , emptySignals = [ Node.Union [] ]
339 }