Oak.v 10 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 SmartContracts Require Import Automation.
5
From Coq Require Import List.
6
7

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

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

20
21
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
22

23
24
25
26
27
28
29
30
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.

Set Primitive Projections.
31
Record OakInterpretation :=
32
33
  build_interpretation {
    oi_ty : Type;
34
35
    oi_eqdec : stdpp.base.EqDecision oi_ty;
    oi_countable : countable.Countable oi_ty;
36
37
  }.

38
39
Arguments build_interpretation _ {_ _}.

40
Local Fixpoint interp_type_with_ordering (t : OakType) : OakInterpretation :=
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
41
  match t with
42
43
44
45
  | 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
46
  | oak_sum a b =>
47
48
49
    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
50
  | oak_pair a b =>
51
52
53
    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
54
  | oak_list a =>
55
56
    let (aT, _, _) := interp_type_with_ordering a in
    build_interpretation (list aT)
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
57
  | oak_set a =>
58
59
    let (aT, _, _) := interp_type_with_ordering a in
    build_interpretation (FMap aT unit)
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
60
  | oak_map a b =>
61
62
63
    let (aT, _, _) := interp_type_with_ordering a in
    let (bT, _, _) := interp_type_with_ordering b in
    build_interpretation (FMap aT bT)
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
64
65
  end.

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

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

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

83
84
85
86
87
88
(* 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;
89
    deserialize_serialize : forall (x : ty), deserialize (serialize x) = Some x;
90
91
92
93
  }.



94
95
96
97
98
99
Global Opaque serialize deserialize deserialize_serialize.

Program Instance oak_empty_equivalence : OakTypeEquivalence Empty_set :=
  {| serialize e := ltac:(contradiction);
     deserialize v := None; |}.
Solve Obligations with contradiction.
100

101
102
103
104
Program Instance oak_unit_equivalence : OakTypeEquivalence unit :=
  {| serialize u := build_oak_value oak_unit u;
     deserialize := extract_oak_value oak_unit; |}.
Solve Obligations with reflexivity.
105

106
107
108
109
Program Instance oak_int_equivalence : OakTypeEquivalence Z :=
  {| serialize i := build_oak_value oak_int i;
     deserialize := extract_oak_value oak_int; |}.
Solve Obligations with reflexivity.
110

111
112
113
114
Program Instance oak_bool_equivalence : OakTypeEquivalence bool :=
  {| serialize b := build_oak_value oak_bool b;
     deserialize := extract_oak_value oak_bool; |}.
Solve Obligations with reflexivity.
115

116
117
118
119
Program Instance oak_nat_equivalence : OakTypeEquivalence nat :=
  {| serialize n := serialize (Z.of_nat n);
     deserialize z := do z' <- deserialize z; Some (Z.to_nat z'); |}.
Next Obligation.
120
  intros x.
121
122
  simpl.
  rewrite deserialize_serialize.
123
124
125
  simpl.
  rewrite Nat2Z.id.
  reflexivity.
126
Qed.
127

128
129
130
131
132
Program Instance oak_positive_equivalence : OakTypeEquivalence positive :=
  {| serialize p := serialize (Zpos p);
     deserialize z := do z' <- deserialize z; Some (Z.to_pos z'); |}.
Next Obligation. prove. Qed.

133
Program Instance oak_value_equivalence : OakTypeEquivalence OakValue :=
134
  {| serialize v := v;
135
136
     deserialize v := Some v; |}.
Solve Obligations with reflexivity.
137

138
139
140
141
(* Program Instance generates an insane amount of obligations for sums,
   so we define it by ourselves. *)
Section Sum.
  Context `{OakTypeEquivalence A} `{OakTypeEquivalence B}.
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  Definition serialize_sum (v : A + B) :=
    let (is_left, ov) :=
        match v 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)).

  Definition deserialize_sum
            `{OakTypeEquivalence A} `{OakTypeEquivalence B}
            (os : OakValue) :=
    match os with
    | build_oak_value (oak_pair oak_bool v) (b, val) =>
      if b then
        do a <- @deserialize A _ (build_oak_value v val);
        Some (inl a)
      else
        do b <- @deserialize B _ (build_oak_value v val);
        Some (inr b)
    | _ => None
    end.

  Lemma deserialize_serialize_sum (s : A + B)
    : deserialize_sum (serialize_sum s) = Some s.
  Proof.
    unfold serialize_sum, deserialize_sum.
    destruct s as [a | b]; simpl; rewrite deserialize_serialize; reflexivity.
  Qed.

  Global Instance oak_sum_equivalence : OakTypeEquivalence (A + B)%type :=
    {| serialize := serialize_sum;
       deserialize := deserialize_sum;
       deserialize_serialize := deserialize_serialize_sum; |}.
End Sum.

Section Product.
  Context `{OakTypeEquivalence A} `{OakTypeEquivalence B}.

  Definition serialize_product '(a, b) :=
    let 'build_oak_value a_oty a_val := @serialize A _ a in
    let 'build_oak_value b_oty b_val := @serialize B _ b in
    build_oak_value (oak_pair a_oty b_oty) (a_val, b_val).

  Definition deserialize_product op :=
    match op with
    | build_oak_value (oak_pair a_ty b_ty) (a_val, b_val) =>
      do a <- @deserialize A _ (build_oak_value a_ty a_val);
      do b <- @deserialize B _ (build_oak_value b_ty b_val);
      Some (a, b)
    | _ => None
    end.

  Lemma deserialize_serialize_product (p : A * B)
        : deserialize_product (serialize_product p) = Some p.
  Proof.
    unfold serialize_product, deserialize_product.
    destruct p as [a b].
    repeat rewrite deserialize_serialize.
201
    reflexivity.
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  Qed.

  Global Instance oak_product_equivalence : OakTypeEquivalence (A * B) :=
    {| serialize := serialize_product;
       deserialize := deserialize_product;
       deserialize_serialize := deserialize_serialize_product; |}.
End Product.

Section List.
  Context `{OakTypeEquivalence A}.

  Definition serialize_list (l : list A) :=
    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.

  Definition deserialize_list (ol : OakValue) :=
    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.
232

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  Lemma deserialize_serialize_list (l : list A)
        : deserialize_list (serialize_list l) = Some l.
  Proof.
    unfold serialize_list, deserialize_list.
    induction l as [| hd tl IHl].
    - reflexivity.
    - simpl in *.
      rewrite IHl; clear IHl.
      rewrite deserialize_serialize.
      reflexivity.
  Qed.

  Global Instance oak_list_equivalence : OakTypeEquivalence (list A) :=
    {| serialize := serialize_list;
       deserialize := deserialize_list;
       deserialize_serialize := deserialize_serialize_list; |}.
End List.

Program Instance oak_map_equivalence
252
        `{OakTypeEquivalence A}
253
        `{countable.Countable A}
254
        `{OakTypeEquivalence B}
255
256
257
258
259
260
261
262
263
  : OakTypeEquivalence (FMap A B) :=
  {| serialize m := serialize (@FMap.elements A B _ _ m);
     deserialize om :=
       do elems <- @deserialize (list (A * B)) _ om;
     Some (FMap.of_list elems); |}.
Next Obligation.
  intros A OTE_A Eq_A C_A B OTE_B m.
  simpl.
  rewrite deserialize_serialize.
264
265
266
  simpl.
  rewrite FMap.of_elements_eq.
  reflexivity.
267
Qed.
268

269
Program Instance oak_set_equivalence
270
        `{OakTypeEquivalence A}
271
272
273
274
275
276
277
278
279
280
        `{countable.Countable A}
  : OakTypeEquivalence (FMap A unit) :=
  {| serialize s := serialize (@FMap.elements A unit _ _ s);
     deserialize os :=
       do elems <- @deserialize (list (A * unit)) _ os;
       Some (FMap.of_list elems); |}.
Next Obligation.
  intros A OTE_A Eq_A C_A m.
  simpl.
  rewrite deserialize_serialize.
281
  simpl.
282
  rewrite FMap.of_elements_eq.
283
  reflexivity.
284
Qed.
285

Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
286
287
288
289
(*
Examples:
Definition test_bool : OakValue := build_oak_value oak_bool true.
Definition test_int : OakValue := build_oak_value oak_int 5%Z.
290
291
292
Definition test_set : OakValue :=
  build_oak_value
    (oak_set oak_int)
293
    (FSet.of_list [5; 6]%Z).
294
Definition test_fmap : FMap Z Z :=
295
  (FMap.of_list [(5, 10); (6, 10); (5, 15)])%Z.
296

297
298
299
Definition test_map : OakValue :=
  build_oak_value
    (oak_map oak_int oak_int)
300
    test_fmap.
301
302
303
304

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

307
308
309
310
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.
311
Compute (extract_oak_value (oak_set oak_int) test_set) : option (FSet Z).
312
Compute
313
  (extract_oak_value
314
315
316
317
     (oak_map
        (oak_map oak_int oak_int)
        oak_int)
     test_map2)
318
319
320
  : 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
321
*)