std:EVM/State/Owners

lib/EVM/State/Owners.omg

lib/EVM/State/Owners.omg106 lines
1-- A storage ownership plan contains the actual typed layout selections.
2-- Its separation proof is about their physical reservations, not hashes of
3-- logical names. Inspecting or regrouping a plan never reallocates an owner.
4let C = std:Certificate
5let E = std:Equal
6let L = std:List
7let Layout = std:EVM/State/Layout
8let N = std:Natural
9let R = std:Region
10let RC = std:Region/Certificate
11let V = std:Validation
12let capacity = N.pow2 256
13let Owner =
14      forall (Result : Type)
15      -> (forall (State : Type) -> Text -> Layout.Selection State -> Result)
16      -> Result
17let owner =
18      \{State : Type} (name : Text) (selection : Layout.Selection State) ->
19        ((\Result consume -> consume State name selection) : Owner)
20let name = \(entry : Owner) -> entry Text (\State name selection -> name)
21let space =
22      \(entry : Owner) ->
23        entry R.Region (\State name selection -> (Layout.describe selection).space)
24let spaces = L.map space
25let nameProblems =
26      List/elim
27        (\(_ : List Owner) -> List Text)
28        []
29        (\entry rest problems ->
30           (if L.any (\other -> Text/equal (name entry) (name other)) rest
31            then [ "duplicate storage owner identity: ${name entry}" ]
32            else [])
33           # problems)
34let NamesValid = V.Valid nameProblems
35let Evidence =
36      \(entries : List Owner) ->
37        { separated : R.Separated (spaces entries)
38        , fits : R.All (R.Fits capacity) (spaces entries)
39        , names : NamesValid entries
40        }
41let Plan = exists (entries : List Owner) * Evidence entries
42let explicit = \(entries : List Owner) (evidence : Evidence entries) -> ((entries, evidence) : Plan)
43let certificate =
44      \(entries : List Owner) ->
45        let geometry =
46              C.both
47                (C.label
48                   "storage owner reservations must be disjoint"
49                   (RC.separated (spaces entries)))
50                (C.label
51                   "storage owner reservations must fit the address space"
52                   (RC.all (R.Fits capacity) (RC.fits capacity) (spaces entries)))
53        let identities = C.make (nameProblems entries) ([] : List Text) (\proof -> proof)
54        in  C.map
55              (\(proof :
56                  R.Separated (spaces entries)
57                  * R.All (R.Fits capacity) (spaces entries)
58                  * NamesValid entries) ->
59                 explicit entries { separated = proof.1.1, fits = proof.1.2, names = proof.2 })
60              (C.both geometry identities)
61let select =
62      \(entries : List Owner) {valid : C.Valid (certificate entries)} ->
63        C.prove (certificate entries) @valid
64let describe =
65      \(plan : Plan) ->
66        L.map
67          (\(entry : Owner) ->
68             entry
69               { name : Text, layout : Layout.Description }
70               (\State name selection -> { name, layout = Layout.describe selection }))
71          plan.1
72let regions = \(plan : Plan) -> spaces plan.1
73let regionPlan =
74      \(plan : Plan) ->
75        ((regions plan, { separated = plan.2.separated, fits = plan.2.fits }) : R.Plan capacity)
76let append =
77      \(left right : Plan) {valid : C.Valid (certificate (left.1 # right.1))} ->
78        select (left.1 # right.1) @valid
79-- A structural law for the selected owners themselves. It does not claim
80-- equality of proof terms or equivalence of physical EVM executions.
81let appendAssociative
82    : forall (left middle right : List Owner) -> left # middle # right === left # (middle # right)
83    = \left middle right ->
84        List/elim
85          (\left -> left # middle # right === left # (middle # right))
86          refl
87          (\entry rest proof -> E.cong (\tail -> [ entry ] # tail) proof)
88          left
89let regroup = \(left middle right : Plan) -> appendAssociative left.1 middle.1 right.1
90in  { Owner
91    , owner
92    , name
93    , space
94    , spaces
95    , NamesValid
96    , Evidence
97    , Plan
98    , explicit
99    , certificate
100    , select
101    , describe
102    , regions
103    , regionPlan
104    , append
105    , regroup
106    }
Your result
Complete file: lib/EVM/State/Owners.omg · with imports