std:EVM/State
lib/EVM/State.omg
lib/EVM/State.omg57 lines
1-- Typed storage paths. Field selectors are ordinary checked projections;
2-- indexing is explicit because a missing list element reverts at runtime.
3-- The compiler interprets the path against the owner's selected layout.
4let E = std:EVM/Effect
5let Word256 = Word 256
6let Navigation =
7 \(S : Type) (P : Type -> Type) ->
8 { root : P S
9 , field : forall (A B : Type) -> (A -> B) -> P A -> P B
10 , item : forall (A : Type) -> Word256 -> P (List A) -> P A
11 , key : forall (K V : Type) -> K -> P (K -> V) -> P V
12 }
13let Path = \(S A : Type) -> forall (P : Type -> Type) -> Navigation S P -> P A
14let root : forall (S : Type) -> Path S S = \S P navigation -> navigation.root
15let field
16 : forall {S A B : Type} -> Path S A -> (A -> B) -> Path S B
17 = \{S} {A} {B} path select P navigation -> navigation.field A B select (path P navigation)
18let item
19 : forall {S A : Type} -> Path S (List A) -> Word256 -> Path S A
20 = \{S} {A} path index P navigation -> navigation.item A index (path P navigation)
21-- A storage function is a total, non-enumerable mapping: a key never written
22-- reads the default of the value type, and there is no presence test.
23let key
24 : forall {S K V : Type} -> Path S (K -> V) -> K -> Path S V
25 = \{S} {K} {V} path value P navigation -> navigation.key K V value (path P navigation)
26-- Interpret the same path against an ordinary state value. A list index
27-- can be absent; unlike storage placement, field and key access need no slot.
28let peek
29 : forall {S A : Type} -> S -> Path S A -> Optional A
30 = \{S} {A} state path ->
31 path
32 Optional
33 { root = Some state
34 , field = \B C select -> std:Optional.map select
35 , key = \K V k -> std:Optional.map (\(f : K -> V) -> f k)
36 , item = \B i xs -> std:Optional.andThen xs (std:List.index (Word/toNatural i))
37 }
38let Read =
39 \(S : Type) (m : Type -> Type) ->
40 { get : forall (A : Type) -> Path S A -> m A
41 , length : forall (A : Type) -> Path S (List A) -> m Word256
42 }
43let Write =
44 \(S : Type) (m : Type -> Type) ->
45 { set : forall (A : Type) -> Path S A -> A -> m unit
46 , append : forall (A : Type) -> Path S (List A) -> List A -> m unit
47 }
48let Access =
49 \(S : Type) (kind : E.Mode) (m : Type -> Type) ->
50 merge
51 { Pure = {}
52 , View = { read : Read S m }
53 , Write = { read : Read S m, write : Write S m }
54 , Payable = { read : Read S m, write : Write S m }
55 }
56 kind
57in { Path, Navigation, root, field, item, key, peek, Read, Write, Access }