std:EVM/Operations
lib/EVM/Operations.omg
lib/EVM/Operations.omg166 lines
1-- State-independent, typed operation signatures. An interface packages the
2-- shared record family and structural evidence; importing it does not
3-- specialize or normalize an entire contract builder.
4let E = std:EVM/Effect
5let Solidity = std:EVM/Solidity
6let L = std:List
7-- Only typed structural witnesses enter a declaration. An entry cannot
8-- supply executable decoders that smuggle writes into a view handler.
9let ValueBoundary =
10 \(A : Type) ->
11 forall (F : Type -> Type)
12 -> (forall (P : Solidity.Families -> Type)
13 -> Bool
14 -> Reflect Solidity.types P
15 -> F (P Solidity.values))
16 -> F A
17let nativeCodec =
18 \{A : Solidity.Families -> Type} (d : Reflect Solidity.types A) ->
19 ((\F consume -> consume A False d) : ValueBoundary (A Solidity.values))
20let solidityCodec =
21 \{A : Solidity.Families -> Type} (d : Reflect Solidity.types A) ->
22 ((\F consume -> consume A True d) : ValueBoundary (A Solidity.values))
23-- Why a text is not a public name. A public name is an ASCII letter, `_` or
24-- `$`, then letters, digits, `_` or `$`, as Solidity requires. Native names
25-- follow the same rule, so no name contains the `.` of a qualified name.
26let nameProblems
27 : Text -> List Text
28 = \text ->
29 let initial = Text/chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$"
30 let among = \(set : List Text) (char : Text) -> L.any (Text/equal char) set
31 let chars = Text/chars text
32 let valid =
33 Optional/elim (\(_ : Optional Text) -> Bool) False (among initial) (L.head chars)
34 && L.all (among (initial # Text/chars "0123456789")) chars
35 in if valid then ([] : List Text) else [ "`" ++ text ++ "` is not an identifier" ]
36-- A method's public name on one protocol: its field label, or another name
37-- with the evidence that it is one, such as `Rename ("count", refl)`.
38let Name = < Field | Rename : std:Validation.Checked nameProblems >
39-- The protocols that expose a method, with its name on each. A method has at
40-- least one route by construction: there is no empty alternative.
41let Routes = < Native : Name | Solidity : Name | Both : { native : Name, solidity : Name } >
42let Protocol = < Native | Solidity >
43-- The public name of the method labelled `label` on the protocol; None
44-- exactly when its routes do not include the protocol.
45let nameOn
46 : Text -> Routes -> Protocol -> Optional Text
47 = \label routes protocol ->
48 let named =
49 \(name : Name) ->
50 Some
51 (merge
52 { Field = label
53 , Rename = \(checked : std:Validation.Checked nameProblems) -> checked.1
54 }
55 name)
56 let hidden = None Text
57 in merge
58 { Native = \name -> merge { Native = named name, Solidity = hidden } protocol
59 , Solidity = \name -> merge { Native = hidden, Solidity = named name } protocol
60 , Both =
61 \names ->
62 merge { Native = named names.native, Solidity = named names.solidity } protocol
63 }
64 routes
65-- Whether a method with these routes has a name on the protocol.
66let exposed
67 : Routes -> Protocol -> Bool
68 = \routes protocol ->
69 merge
70 { Native = \_ -> merge { Native = True, Solidity = False } protocol
71 , Solidity = \_ -> merge { Native = False, Solidity = True } protocol
72 , Both = \_ -> True
73 }
74 routes
75-- Whether a route names the method by its field label, which then has to be
76-- a public name too.
77let usesField
78 : Routes -> Bool
79 = \routes ->
80 let field = \(name : Name) -> merge { Field = True, Rename = \_ -> False } name
81 in merge
82 { Native = field
83 , Solidity = field
84 , Both = \names -> field names.native || field names.solidity
85 }
86 routes
87-- A method's public names, resolved.
88let Names = { native : Optional Text, solidity : Optional Text }
89let namesOf
90 : Text -> Routes -> Names
91 = \label routes ->
92 { native = nameOn label routes Protocol.Native
93 , solidity = nameOn label routes Protocol.Solidity
94 }
95let both = Routes.Both { native = Name.Field, solidity = Name.Field }
96-- The interface owns its labels and routes before there is a state or
97-- an implementation. Reflection changes only the leaf interpretation.
98let Key = { arguments : Type, result : Type, mode : E.Mode, routes : Routes }
99let Table = std:Reflection/Table.make Key
100let Family = Table.Families -> Type
101let Operation =
102 \(k : Key) ->
103 { arguments : ValueBoundary k.arguments
104 , result : ValueBoundary k.result
105 , mode : k.mode === k.mode
106 , routes : k.routes === k.routes
107 }
108let boundary =
109 \(mode : E.Mode)
110 {A B : Type}
111 (wire : { arguments : ValueBoundary A, result : ValueBoundary B }) ->
112 ({ arguments = wire.arguments, result = wire.result, mode = refl, routes = refl } :
113 Operation { arguments = A, result = B, mode, routes = both })
114let operation
115 : forall (mode : E.Mode) {A B : Solidity.Families -> Type}
116 -> { Args : Reflect Solidity.types A, Result : Reflect Solidity.types B }
117 -> Operation
118 { arguments = A Solidity.values, result = B Solidity.values, mode, routes = both }
119 = \mode {A} {B} wire ->
120 boundary
121 mode
122 @(A Solidity.values)
123 @(B Solidity.values)
124 { arguments = solidityCodec @A wire.Args, result = solidityCodec @B wire.Result }
125let on
126 : forall (routes : Routes) {A B : Type} {mode : E.Mode} {old : Routes}
127 -> Operation { arguments = A, result = B, mode, routes = old }
128 -> Operation { arguments = A, result = B, mode, routes }
129 = \routes {A} {B} {mode} {old} op ->
130 { arguments = op.arguments, result = op.result, mode = refl, routes = refl }
131let operations = { member = Operation }
132let Selection = \(H : Family) -> { structure : Reflect @1 operations H, value : H operations }
133let Valid = \(H : Family) (s : Selection H) -> Table.Valid @Operation @H s.structure
134-- Carry shape and validity, not a user-selectable pairing of implementations.
135let Schema = exists (H : Family) (s : Selection H) * Valid H s
136let interface =
137 \{H : Family}
138 (selected : { structure : Reflect @1 operations H, value : H operations })
139 {valid : Valid H selected} ->
140 ((H, (selected, valid)) : Schema)
141in { Name
142 , Routes
143 , Protocol
144 , nameProblems
145 , namesOf
146 , nameOn
147 , exposed
148 , usesField
149 , Names
150 , ValueBoundary
151 , nativeCodec
152 , solidityCodec
153 , Key
154 , Table
155 , Family
156 , Operation
157 , operations
158 , Schema
159 , boundary
160 , computing = operation E.Mode.Pure
161 , reading = operation E.Mode.View
162 , writing = operation E.Mode.Write
163 , receiving = operation E.Mode.Payable
164 , on
165 , interface
166 }