Oak.v 8.46 KB
Newer Older
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
1
From Coq Require Import ZArith.
2
From SmartContracts Require Import Monads.
3
From SmartContracts Require Import Containers.
4
From Coq Require Import List.
5
6

Import ListNotations.
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
7
8
9
10
11
12
13

Inductive OakType :=
  | oak_empty : OakType
  | oak_unit : OakType
  | oak_int : OakType
  | oak_bool : OakType
  | oak_pair : OakType -> OakType -> OakType
14
  | oak_sum : OakType -> OakType -> OakType
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
15
16
17
18
  | oak_list : OakType -> OakType
  | oak_set : OakType -> OakType
  | oak_map : OakType -> OakType -> OakType.

19
20
Definition eq_oak_type_dec (t1 t2 : OakType) : {t1 = t2} + {t1 <> t2}.
Proof. decide equality. Defined.
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
21

22
23
24
25
26
27
28
Proposition eq_oak_type_dec_refl (x : OakType) :
  eq_oak_type_dec x x = left eq_refl.
Proof.
  induction x;
    try simpl; try rewrite IHx; try rewrite IHx1; try rewrite IHx2; reflexivity.
Qed.

29
Program Instance empty_set_strict_order
30
  : StrictOrder (fun (_ _ : Empty_set) => False) (@eq Empty_set).
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
31
Solve Obligations with contradiction.
32
Program Instance empty_set_ordered_type : UsualOrderedType Empty_set.
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
33
Solve Obligations with contradiction.
34

35
36
37
38
39
40
41
42
Set Primitive Projections.
Local Record OakInterpretation :=
  build_interpretation {
    oi_ty : Type;
    oi_order : OrderedType oi_ty;
  }.

Local Fixpoint interp_type_with_ordering (t : OakType) : OakInterpretation :=
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
43
  match t with
44
45
46
47
  | oak_empty => build_interpretation Empty_set _
  | oak_unit => build_interpretation unit _
  | oak_int => build_interpretation Z _
  | oak_bool => build_interpretation bool _
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
48
  | oak_sum a b =>
49
50
51
    let (aT, _) := interp_type_with_ordering a in
    let (bT, _) := interp_type_with_ordering b in
    build_interpretation (aT + bT)%type _
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
52
  | oak_pair a b =>
53
54
55
    let (aT, _) := interp_type_with_ordering a in
    let (bT, _) := interp_type_with_ordering b in
    build_interpretation (aT * bT)%type _
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
56
  | oak_list a =>
57
58
    let (aT, _) := interp_type_with_ordering a in
    build_interpretation (list aT) _
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
59
  | oak_set a =>
60
    let (aT, _) := interp_type_with_ordering a in
61
    build_interpretation (FSet aT) _
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
62
  | oak_map a b =>
63
64
    let (aT, _) := interp_type_with_ordering a in
    let (bT, _) := interp_type_with_ordering b in
65
    build_interpretation (FMap aT bT) _
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
66
67
  end.

68
Definition interp_type (t : OakType) : Type :=
69
  oi_ty (interp_type_with_ordering t).
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
70
71
72
73

Record OakValue :=
  build_oak_value {
    oak_value_type : OakType;
74
    oak_value : interp_type oak_value_type;
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
75
76
  }.

77
Definition extract_oak_value (t : OakType) (value : OakValue) : option (interp_type t).
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
78
79
Proof.
  destruct value as [ty val].
80
81
82
  destruct (eq_oak_type_dec t ty).
  - subst. exact (Some val).
  - exact None.
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
83
84
Defined.

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(* Defines that a type can be serialized into OakValue and deserialized from it,
   and that these are inverses *)
Class OakTypeEquivalence (ty : Type) :=
  {
    serialize : ty -> OakValue;
    deserialize : OakValue -> option ty;
    ote_equivalence : forall (x : ty), deserialize (serialize x) = Some x;
  }.

Global Opaque serialize deserialize ote_equivalence.

Definition make_trivial_equiv (ot : OakType) : OakTypeEquivalence (interp_type ot).
Proof.
  refine {| serialize := build_oak_value ot;
            deserialize := extract_oak_value ot;
            ote_equivalence := _ |}.
  intros x.
  unfold extract_oak_value.
  rewrite eq_oak_type_dec_refl.
  reflexivity.
Defined.

Instance oak_empty_equivalence : OakTypeEquivalence Empty_set :=
  make_trivial_equiv oak_empty.

Instance oak_unit_equivalence : OakTypeEquivalence unit :=
  make_trivial_equiv oak_unit.

Instance oak_int_equivalence : OakTypeEquivalence Z :=
  make_trivial_equiv oak_int.

Instance oak_bool_equivalence : OakTypeEquivalence bool :=
  make_trivial_equiv oak_bool.

119
Instance oak_nat_equivalence : OakTypeEquivalence nat :=
120
121
  {| serialize n := serialize (Z.of_nat n);
     deserialize z := do z' <- deserialize z; Some (Z.to_nat z'); |}.
122
123
Proof.
  intros x.
124
125
126
127
  rewrite ote_equivalence.
  simpl.
  rewrite Nat2Z.id.
  reflexivity.
128
Defined.
129

130
Instance oak_value_equivalence : OakTypeEquivalence OakValue :=
131
  {| serialize v := v;
132
133
     deserialize v := Some v;
     ote_equivalence x := eq_refl (Some x); |}.
134
135

Generalizable Variables A B.
136
Instance oak_sum_equivalence
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
        `{e_a : OakTypeEquivalence A}
        `{e_b : OakTypeEquivalence B}
  : OakTypeEquivalence (A + B)%type :=
  {| serialize s :=
       let (is_left, ov) :=
           match s with
           | inl l => (true, serialize l)
           | inr r => (false, serialize r)
           end in
       build_oak_value (oak_pair oak_bool ov.(oak_value_type)) (is_left, ov.(oak_value));
     deserialize os :=
       match os with
       | build_oak_value (oak_pair oak_bool v) (b, val) =>
         if b
         then do a <- @deserialize _ e_a (build_oak_value v val);
              Some (inl a)
         else do b <- @deserialize _ e_b (build_oak_value v val);
              Some (inr b)
       | _ => None
156
157
158
159
       end; |}.
Proof.
  intros [a | b]; simpl; rewrite ote_equivalence; reflexivity.
Defined.
160

161
Instance oak_pair_equivalence
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        `{e_a : OakTypeEquivalence A}
        `{e_b : OakTypeEquivalence B}
  : OakTypeEquivalence (A * B)%type :=
  {| serialize '(a, b) :=
       let 'build_oak_value a_oty a_val := serialize a in
       let 'build_oak_value b_oty b_val := serialize b in
       build_oak_value (oak_pair a_oty b_oty) (a_val, b_val);
     deserialize op :=
       match op with
       | build_oak_value (oak_pair a_ty b_ty) (a_val, b_val) =>
         do a <- @deserialize _ e_a (build_oak_value a_ty a_val);
         do b <- @deserialize _ e_b (build_oak_value b_ty b_val);
         Some (a, b)
       | _ => None
       end;
  |}.
178
179
180
Proof.
  intros [a b].
  simpl.
181
182
  repeat rewrite ote_equivalence.
  reflexivity.
183
Defined.
184

185
Instance oak_list_equivalence
186
187
188
        `{OakTypeEquivalence A}
  : OakTypeEquivalence (list A) :=
  {| serialize l :=
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
      let go a acc :=
          let 'build_oak_value a_oty a_val := serialize a in
          let 'build_oak_value acc_oty acc_val := acc in
          build_oak_value (oak_pair a_oty acc_oty) (a_val, acc_val) in
      fold_right go (build_oak_value oak_unit tt) l;
    deserialize ol :=
      let fix aux (ty : OakType) (val : interp_type ty) : option (list A) :=
          match ty, val with
          | oak_pair hd_ty tl_ty, (hd_val, tl_val) =>
            do hd <- deserialize (build_oak_value hd_ty hd_val);
            do tl <- aux tl_ty tl_val;
            Some (hd :: tl)
          | oak_unit, _ => Some []
          | _, _ => None
          end in
      let 'build_oak_value ol_ty ol_val := ol in
      aux ol_ty ol_val;
206
  |}.
207
Proof.
208
209
  induction x as [| hd tl IHl].
  - reflexivity.
210
211
  - simpl in *.
    rewrite IHl; clear IHl.
212
213
    rewrite ote_equivalence.
    reflexivity.
214
Defined.
215

216
Instance oak_map_equivalence
217
218
219
        `{OakTypeEquivalence A}
        `{OrderedType A}
        `{OakTypeEquivalence B}
220
221
  : OakTypeEquivalence (FMap A B) :=
  {| serialize m := serialize (FMap.elements m);
222
223
     deserialize om :=
       do elems <- deserialize om;
224
       Some (FMap.of_list elems);
225
  |}.
226
227
228
229
230
231
232
Proof.
  intros m.
  rewrite ote_equivalence.
  simpl.
  rewrite FMap.of_elements_eq.
  reflexivity.
Defined.
233

234
Instance oak_set_equivalence
235
236
        `{OakTypeEquivalence A}
        `{OrderedType A}
237
238
  : OakTypeEquivalence (FSet A) :=
  {| serialize s := serialize (FSet.elements s);
239
240
     deserialize os :=
       do elems <- deserialize os;
241
       Some (FSet.of_list elems);
242
  |}.
243
244
245
246
247
248
249
Proof.
  intros s.
  rewrite ote_equivalence.
  simpl.
  rewrite FSet.of_elements_eq.
  reflexivity.
Defined.
250

Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
251
252
253
254
(*
Examples:
Definition test_bool : OakValue := build_oak_value oak_bool true.
Definition test_int : OakValue := build_oak_value oak_int 5%Z.
255
256
257
Definition test_set : OakValue :=
  build_oak_value
    (oak_set oak_int)
258
    (FSet.of_list [5; 6]%Z).
259
Definition test_fmap : FMap Z Z :=
260
  (FMap.of_list [(5, 10); (6, 10); (5, 15)])%Z.
261

262
263
264
Definition test_map : OakValue :=
  build_oak_value
    (oak_map oak_int oak_int)
265
    test_fmap.
266
267
268
269

Definition test_map2 : OakValue :=
  build_oak_value
    (oak_map (oak_map oak_int oak_int) oak_int)
270
    (FMap.of_list [(test_fmap, 15)])%Z.
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
271

272
273
274
275
Compute (extract_oak_value oak_bool test_bool) : option bool.
Compute (extract_oak_value oak_int test_bool) : option Z.
Compute (extract_oak_value oak_bool test_int) : option bool.
Compute (extract_oak_value oak_int test_int) : option Z.
276
Compute (extract_oak_value (oak_set oak_int) test_set) : option (FSet Z).
277
Compute
278
  (extract_oak_value
279
280
281
282
     (oak_map
        (oak_map oak_int oak_int)
        oak_int)
     test_map2)
283
284
285
  : option (FMap (FMap Z Z) Z).
Compute (option_map FSet.elements (extract_oak_value (oak_set oak_int) test_set)).
Compute (option_map FMap.elements (extract_oak_value (oak_map oak_int oak_int) test_map)).
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
286
*)