Oak.v 8.71 KB
Newer Older
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
1
2
From Coq Require Import ZArith.
From Containers Require Import Sets Maps.
3
4
5
6
From Containers Require SetProperties.
From SmartContracts Require Import Monads.
From Coq Require Import List.
From Coq Require Import Setoid.
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
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
30
31
  : OrderedType.StrictOrder (fun (_ _ : Empty_set) => False) (@eq Empty_set).
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
61
    let (aT, _) := interp_type_with_ordering a in
    build_interpretation (set aT) _
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
62
  | oak_map a b =>
63
64
65
    let (aT, _) := interp_type_with_ordering a in
    let (bT, _) := interp_type_with_ordering b in
    build_interpretation Map[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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
195
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(* 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.

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.
  rewrite ote_equivalence.
  simpl.
  rewrite Nat2Z.id.
  reflexivity.
Qed.

Program Instance oak_value_equivalence : OakTypeEquivalence OakValue :=
  {| serialize v := v;
     deserialize v := Some v; |}.

Generalizable Variables A B.
Program Instance oak_sum_equivalence
        `{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
       end;
  |}.
Next Obligation.
  destruct x as [a | b]; rewrite ote_equivalence; reflexivity.
  (*
  - change ({| oak_value_type := oak_value_type (serialize a);
               oak_value := oak_value (serialize a) |}) with (serialize a).

    rewrite ote_equivalence.
  *)
Qed.

Program Instance oak_pair_equivalence
        `{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;
  |}.
Next Obligation.
  repeat rewrite ote_equivalence.
  reflexivity.
Qed.

Program Instance oak_list_equivalence
        `{OakTypeEquivalence A}
  : OakTypeEquivalence (list A) :=
  {| 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 with
           | oak_pair hd_ty tl_ty =>
             let pair : interp_type hd_ty * interp_type tl_ty := ltac:(subst; exact val) in
             let (hd_val, tl_val) := pair in
             do hd <- deserialize (build_oak_value hd_ty hd_val);
             do tl <- aux tl_ty tl_val;
             Some (hd :: tl)
           | oak_unit => Some nil
           | _ => None
           end in
       let 'build_oak_value ol_ty ol_val := ol in
       aux ol_ty ol_val;
  |}.
Solve Obligations with split; discriminate.
Next Obligation.
  induction x as [| hd tl IHl].
  - reflexivity.
  - simpl.
    rewrite IHl.
    rewrite ote_equivalence.
    reflexivity.
Qed.

Program Instance oak_map_equivalence
        `{OakTypeEquivalence A}
        `{OrderedType A}
        `{OakTypeEquivalence B}
  : OakTypeEquivalence Map[A, B] :=
  {| serialize m := serialize (MF.to_list m);
     deserialize om :=
       do elems <- deserialize om;
       Some (MF.of_list elems);
  |}.
Next Obligation.
  (* TODO: Need to use setoids here... *)
  Admitted.

Program Instance oak_set_equivalence
        `{OakTypeEquivalence A}
        `{OrderedType A}
  : OakTypeEquivalence (SetInterface.set A) :=
  {| serialize s := serialize (SetProperties.to_list s);
     deserialize os :=
       do elems <- deserialize os;
       Some (SetProperties.of_list elems);
  |}.
Next Obligation.
  (* TODO: Setoids ... *)
  Admitted.

Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
249
250
251
252
(*
Examples:
Definition test_bool : OakValue := build_oak_value oak_bool true.
Definition test_int : OakValue := build_oak_value oak_int 5%Z.
253
254
255
256
257
258
259
260
261
262
263
264
265
Definition test_set : OakValue :=
  build_oak_value
    (oak_set oak_int)
    {5; {6; {}}}%Z.
Definition test_map : OakValue :=
  build_oak_value
    (oak_map oak_int oak_int)
    [][5 <- 10][6 <- 10][5 <- 15]%Z.

Definition test_map2 : OakValue :=
  build_oak_value
    (oak_map (oak_map oak_int oak_int) oak_int)
    [][[][5 <- 10][6 <- 10][5 <- 15] <- 15]%Z.
Jakob Botsch Nielsen's avatar
Jakob Botsch Nielsen committed
266

267
268
269
270
271
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.
Compute (extract_oak_value (oak_set oak_int) test_set) : option (set Z).
272
Compute
273
  (extract_oak_value
274
275
276
277
278
     (oak_map
        (oak_map oak_int oak_int)
        oak_int)
     test_map2)
  : option Map[Map[Z, Z], Z].
279
280
Compute (option_map SetInterface.elements (extract_oak_value (oak_set oak_int) test_set)).
Compute (option_map elements (extract_oak_value (oak_map oak_int oak_int) test_map)).
281
*)