Oak.v 8.77 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
Set Primitive Projections.
36
Record OakInterpretation :=
37
38
39
40
41
42
  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
Proof.
121
122
123
  refine {| serialize n := serialize (Z.of_nat n);
            deserialize z := do z' <- deserialize z; Some (Z.to_nat z');
            ote_equivalence := _; |}.
124
  intros x.
125
126
127
128
  rewrite ote_equivalence.
  simpl.
  rewrite Nat2Z.id.
  reflexivity.
129
Defined.
130

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

Generalizable Variables A B.
137
Instance oak_sum_equivalence
138
139
        `{e_a : OakTypeEquivalence A}
        `{e_b : OakTypeEquivalence B}
140
  : OakTypeEquivalence (A + B)%type.
141
Proof.
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  refine
    {| 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
         end;
       ote_equivalence := _; |}.
161
162
  intros [a | b]; simpl; rewrite ote_equivalence; reflexivity.
Defined.
163

164
Instance oak_pair_equivalence
165
166
        `{e_a : OakTypeEquivalence A}
        `{e_b : OakTypeEquivalence B}
167
168
169
170
171
172
173
174
175
176
177
  : OakTypeEquivalence (A * B)%type.
Proof.
  refine
    {| 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);
178
179
         do b <- @deserialize _ e_b (build_oak_value b_ty b_val);
         Some (a, b)
180
181
182
         | _ => None
         end;
       ote_equivalence := _;
183
  |}.
184
185
  intros [a b].
  simpl.
186
187
  repeat rewrite ote_equivalence.
  reflexivity.
188
Defined.
189

190
Instance oak_list_equivalence
191
        `{OakTypeEquivalence A}
192
  : OakTypeEquivalence (list A).
193
Proof.
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  refine
    {| serialize l :=
         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;
       ote_equivalence := _; |}.
214
215
  induction x as [| hd tl IHl].
  - reflexivity.
216
217
  - simpl in *.
    rewrite IHl; clear IHl.
218
219
    rewrite ote_equivalence.
    reflexivity.
220
Defined.
221

222
Instance oak_map_equivalence
223
224
225
        `{OakTypeEquivalence A}
        `{OrderedType A}
        `{OakTypeEquivalence B}
226
  : OakTypeEquivalence (FMap A B).
227
Proof.
228
229
230
231
232
233
  refine
    {| serialize m := serialize (FMap.elements m);
       deserialize om :=
         do elems <- deserialize om;
       Some (FMap.of_list elems);
       ote_equivalence := _; |}.
234
235
236
237
238
239
  intros m.
  rewrite ote_equivalence.
  simpl.
  rewrite FMap.of_elements_eq.
  reflexivity.
Defined.
240

241
Instance oak_set_equivalence
242
243
        `{OakTypeEquivalence A}
        `{OrderedType A}
244
  : OakTypeEquivalence (FSet A).
245
Proof.
246
247
248
249
250
251
  refine
    {| serialize s := serialize (FSet.elements s);
       deserialize os :=
         do elems <- deserialize os;
       Some (FSet.of_list elems);
       ote_equivalence := _; |}.
252
253
254
255
256
257
  intros s.
  rewrite ote_equivalence.
  simpl.
  rewrite FSet.of_elements_eq.
  reflexivity.
Defined.
258

Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
259
260
261
262
(*
Examples:
Definition test_bool : OakValue := build_oak_value oak_bool true.
Definition test_int : OakValue := build_oak_value oak_int 5%Z.
263
264
265
Definition test_set : OakValue :=
  build_oak_value
    (oak_set oak_int)
266
    (FSet.of_list [5; 6]%Z).
267
Definition test_fmap : FMap Z Z :=
268
  (FMap.of_list [(5, 10); (6, 10); (5, 15)])%Z.
269

270
271
272
Definition test_map : OakValue :=
  build_oak_value
    (oak_map oak_int oak_int)
273
    test_fmap.
274
275
276
277

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

280
281
282
283
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.
284
Compute (extract_oak_value (oak_set oak_int) test_set) : option (FSet Z).
285
Compute
286
  (extract_oak_value
287
288
289
290
     (oak_map
        (oak_map oak_int oak_int)
        oak_int)
     test_map2)
291
292
293
  : 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
294
*)