Oak.v 9.76 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
29
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.
30
Record OakInterpretation :=
31
32
  build_interpretation {
    oi_ty : Type;
33
34
    oi_eqdec : stdpp.base.EqDecision oi_ty;
    oi_countable : countable.Countable oi_ty;
35
36
  }.

37
38
Arguments build_interpretation _ {_ _}.

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

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

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

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

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



93
94
95
96
97
98
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.
99

100
101
102
103
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.
104

105
106
107
108
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.
109

110
111
112
113
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.
114

115
116
117
118
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.
119
  intros x.
120
121
  simpl.
  rewrite deserialize_serialize.
122
123
124
  simpl.
  rewrite Nat2Z.id.
  reflexivity.
125
Qed.
126

127
Program Instance oak_value_equivalence : OakTypeEquivalence OakValue :=
128
  {| serialize v := v;
129
130
     deserialize v := Some v; |}.
Solve Obligations with reflexivity.
131

132
133
134
135
(* Program Instance generates an insane amount of obligations for sums,
   so we define it by ourselves. *)
Section Sum.
  Context `{OakTypeEquivalence A} `{OakTypeEquivalence B}.
136

137
138
139
140
141
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
  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.
195
    reflexivity.
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
  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.
226

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  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
246
        `{OakTypeEquivalence A}
247
        `{countable.Countable A}
248
        `{OakTypeEquivalence B}
249
250
251
252
253
254
255
256
257
  : 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.
258
259
260
  simpl.
  rewrite FMap.of_elements_eq.
  reflexivity.
261
Qed.
262

263
Program Instance oak_set_equivalence
264
        `{OakTypeEquivalence A}
265
266
267
268
269
270
271
272
273
274
        `{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.
275
  simpl.
276
  rewrite FMap.of_elements_eq.
277
  reflexivity.
278
Qed.
279

Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
280
281
282
283
(*
Examples:
Definition test_bool : OakValue := build_oak_value oak_bool true.
Definition test_int : OakValue := build_oak_value oak_int 5%Z.
284
285
286
Definition test_set : OakValue :=
  build_oak_value
    (oak_set oak_int)
287
    (FSet.of_list [5; 6]%Z).
288
Definition test_fmap : FMap Z Z :=
289
  (FMap.of_list [(5, 10); (6, 10); (5, 15)])%Z.
290

291
292
293
Definition test_map : OakValue :=
  build_oak_value
    (oak_map oak_int oak_int)
294
    test_fmap.
295
296
297
298

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

301
302
303
304
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.
305
Compute (extract_oak_value (oak_set oak_int) test_set) : option (FSet Z).
306
Compute
307
  (extract_oak_value
308
309
310
311
     (oak_map
        (oak_map oak_int oak_int)
        oak_int)
     test_map2)
312
313
314
  : 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
315
*)