aboutsummaryrefslogtreecommitdiff
path: root/src/Compilers/Relations.v
blob: 27a101e4f7f20989463822c30adc22ed1416a089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
Require Import Coq.Lists.List Coq.Classes.RelationClasses Coq.Classes.Morphisms.
Require Import Crypto.Compilers.Syntax.
Require Import Crypto.Compilers.SmartMap.
Require Import Crypto.Compilers.Wf.
Require Import Crypto.Util.Tactics.RewriteHyp.
Require Import Crypto.Util.Tactics.DestructHead.
Require Import Crypto.Util.Tactics.SplitInContext.
Require Import Crypto.Util.Prod.
Require Import Crypto.Util.Sigma.

Local Coercion is_true : bool >-> Sortclass.

Local Open Scope ctype_scope.
Section language.
  Context {base_type_code : Type}.

  Local Notation flat_type := (flat_type base_type_code).
  Local Notation type := (type base_type_code).

  Local Ltac rel_relb_t :=
    repeat first [ progress simpl in *
                 | reflexivity
                 | intuition congruence
                 | setoid_rewrite Bool.andb_true_iff
                 | intro
                 | rewrite_hyp <- !* ].

  Section flat_type.
    Context {interp_base_type1 interp_base_type2 : base_type_code -> Type}.
    Local Notation interp_flat_type1 := (interp_flat_type interp_base_type1).
    Local Notation interp_flat_type2 := (interp_flat_type interp_base_type2).

    Section gen_Prop.
      Context (P : Type)
              (and : P -> P -> P)
              (True : P)
              (False : P).
      Section pointwise1.
        Context (R : forall t, interp_base_type1 t -> P).
        Fixpoint interp_flat_type_rel_pointwise1_gen_Prop (t : flat_type)
          : interp_flat_type1 t -> P :=
          match t with
          | Tbase t => R t
          | Unit => fun _ => True
          | Prod A B => fun x : interp_flat_type _ A * interp_flat_type _ B
                        => and (interp_flat_type_rel_pointwise1_gen_Prop _ (fst x))
                               (interp_flat_type_rel_pointwise1_gen_Prop _ (snd x))
          end.
      End pointwise1.
      Section pointwise2.
        Context (R : forall t, interp_base_type1 t -> interp_base_type2 t -> P).
        Fixpoint interp_flat_type_rel_pointwise_gen_Prop (t : flat_type)
          : interp_flat_type1 t -> interp_flat_type2 t -> P :=
          match t with
          | Tbase t => R t
          | Unit => fun _ _ => True
          | Prod A B
            => fun (x : interp_flat_type _ A * interp_flat_type _ B)
                   (y : interp_flat_type _ A * interp_flat_type _ B)
               => and (interp_flat_type_rel_pointwise_gen_Prop _ (fst x) (fst y))
                      (interp_flat_type_rel_pointwise_gen_Prop _ (snd x) (snd y))
          end.
      End pointwise2.
      Section pointwise2_hetero.
        Context (R : forall t1 t2, interp_base_type1 t1 -> interp_base_type2 t2 -> P).
        Fixpoint interp_flat_type_rel_pointwise_hetero_gen_Prop (t1 t2 : flat_type)
          : interp_flat_type1 t1 -> interp_flat_type2 t2 -> P
          := match t1, t2 with
             | Tbase t1, Tbase t2 => R t1 t2
             | Unit, Unit => fun _ _ => True
             | Prod x1 y1, Prod x2 y2
               => fun (a b : interp_flat_type _ _ * interp_flat_type _ _)
                  => and (interp_flat_type_rel_pointwise_hetero_gen_Prop x1 x2 (fst a) (fst b))
                         (interp_flat_type_rel_pointwise_hetero_gen_Prop y1 y2 (snd a) (snd b))
             | Tbase _, _
             | Unit, _
             | Prod _ _, _
               => fun _ _ => False
             end.
      End pointwise2_hetero.
    End gen_Prop.

    Definition interp_flat_type_relb_pointwise1
      := @interp_flat_type_rel_pointwise1_gen_Prop bool andb true.
    Global Arguments interp_flat_type_relb_pointwise1 _ !_ _ / .
    Definition interp_flat_type_rel_pointwise1
      := @interp_flat_type_rel_pointwise1_gen_Prop Prop and True.
    Global Arguments interp_flat_type_rel_pointwise1 _ !_ _ / .
    Lemma interp_flat_type_rel_pointwise1_iff_relb {R} t x
      : interp_flat_type_relb_pointwise1 R t x <-> interp_flat_type_rel_pointwise1 R t x.
    Proof using Type. clear; induction t; rel_relb_t. Qed.
    Definition interp_flat_type_rel_pointwise1_gen_Prop_iff_bool
      : forall {R} t x,
        interp_flat_type_rel_pointwise1_gen_Prop bool _ _ R t x
        <-> interp_flat_type_rel_pointwise1_gen_Prop Prop _ _ R t x
      := @interp_flat_type_rel_pointwise1_iff_relb.
    Definition interp_flat_type_relb_pointwise
      := @interp_flat_type_rel_pointwise_gen_Prop bool andb true.
    Global Arguments interp_flat_type_relb_pointwise _ !_ _ _ / .
    Definition interp_flat_type_rel_pointwise
      := @interp_flat_type_rel_pointwise_gen_Prop Prop and True.
    Global Arguments interp_flat_type_rel_pointwise _ !_ _ _ / .
    Lemma interp_flat_type_rel_pointwise_iff_relb {R} t x y
      : interp_flat_type_relb_pointwise R t x y <-> interp_flat_type_rel_pointwise R t x y.
    Proof using Type. clear; induction t; rel_relb_t. Qed.
    Definition interp_flat_type_rel_pointwise_gen_Prop_iff_bool
      : forall {R} t x y,
        interp_flat_type_rel_pointwise_gen_Prop bool _ _ R t x y
        <-> interp_flat_type_rel_pointwise_gen_Prop Prop _ _ R t x y
      := @interp_flat_type_rel_pointwise_iff_relb.
    Definition interp_flat_type_relb_pointwise_hetero
      := @interp_flat_type_rel_pointwise_hetero_gen_Prop bool andb true false.
    Global Arguments interp_flat_type_relb_pointwise_hetero _ !_ !_ _ _ / .
    Definition interp_flat_type_rel_pointwise_hetero
      := @interp_flat_type_rel_pointwise_hetero_gen_Prop Prop and True False.
    Global Arguments interp_flat_type_rel_pointwise_hetero _ !_ !_ _ _ / .
    Lemma interp_flat_type_rel_pointwise_hetero_iff_relb {R} t1 t2 x y
      : interp_flat_type_relb_pointwise_hetero R t1 t2 x y <-> interp_flat_type_rel_pointwise_hetero R t1 t2 x y.
    Proof using Type. clear; revert dependent t2; induction t1, t2; rel_relb_t. Qed.
    Definition interp_flat_type_rel_pointwise_hetero_gen_Prop_iff_bool
      : forall {R} t1 t2 x y,
        interp_flat_type_rel_pointwise_hetero_gen_Prop bool _ _ _ R t1 t2 x y
        <-> interp_flat_type_rel_pointwise_hetero_gen_Prop Prop _ _ _ R t1 t2 x y
      := @interp_flat_type_rel_pointwise_hetero_iff_relb.

    Lemma interp_flat_type_rel_pointwise_hetero_iff {R t} x y
      : interp_flat_type_rel_pointwise (fun t => R t t) t x y
        <-> interp_flat_type_rel_pointwise_hetero R t t x y.
    Proof using Type. induction t; simpl; rewrite_hyp ?*; reflexivity. Qed.

    Lemma interp_flat_type_rel_pointwise_impl {R1 R2 : forall t, _ -> _ -> Prop} t x y
      : interp_flat_type_rel_pointwise (fun t x y => (R1 t x y -> R2 t x y)%type) t x y
        -> (interp_flat_type_rel_pointwise R1 t x y
            -> interp_flat_type_rel_pointwise R2 t x y).
    Proof using Type. induction t; simpl; intuition. Qed.

    Lemma interp_flat_type_rel_pointwise_always {R : forall t, _ -> _ -> Prop}
      : (forall t x y, R t x y)
        -> forall t x y, interp_flat_type_rel_pointwise R t x y.
    Proof using Type. induction t; simpl; intuition. Qed.
  End flat_type.
  Section flat_type_extra.
    Context {interp_base_type1 interp_base_type2 : base_type_code -> Type}.
    Lemma interp_flat_type_rel_pointwise_impl' {R1 R2 : forall t, _ -> _ -> Prop} t x y
      : @interp_flat_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => (R1 t y x -> R2 t x y)%type) t x y
        -> (interp_flat_type_rel_pointwise R1 t y x
            -> interp_flat_type_rel_pointwise R2 t x y).
    Proof using Type. induction t; simpl; intuition. Qed.

    Global Instance interp_flat_type_rel_pointwise_Reflexive {R : forall t, _ -> _ -> Prop} {H : forall t, Reflexive (R t)}
      : forall t, Reflexive (@interp_flat_type_rel_pointwise interp_base_type1 interp_base_type1 R t).
    Proof using Type.
      induction t; intro; simpl; try apply conj; try reflexivity.
    Qed.

    Lemma interp_flat_type_rel_pointwise_SmartVarfMap
          {interp_base_type1' interp_base_type2'}
          {R : forall t, _ -> _ -> Prop}
          t f g x y
      : @interp_flat_type_rel_pointwise interp_base_type1 interp_base_type2 R t (SmartVarfMap f x) (SmartVarfMap g y)
        <-> @interp_flat_type_rel_pointwise interp_base_type1' interp_base_type2' (fun t x y => R t (f _ x) (g _ y)) t x y.
    Proof using Type.
      induction t; simpl; try reflexivity.
      rewrite_hyp <- !*; reflexivity.
    Qed.
  End flat_type_extra.

  Section type.
    Section hetero.
      Context (interp_src1 interp_src2 : flat_type -> Type)
              (interp_dst1 interp_dst2 : flat_type -> Type).
      Section hetero.
        Context (Rsrc : forall t, interp_src1 t -> interp_src2 t -> Prop)
                (Rdst : forall t, interp_dst1 t -> interp_dst2 t -> Prop).

        Definition interp_type_gen_rel_pointwise_hetero (t : type)
          : interp_type_gen_hetero interp_src1 interp_dst1 t
            -> interp_type_gen_hetero interp_src2 interp_dst2 t
            -> Prop
          := @respectful_hetero _ _ _ _ (Rsrc _) (fun _ _ => Rdst _).
        Global Arguments interp_type_gen_rel_pointwise_hetero _ _ _ / .
      End hetero.
      Section hetero_hetero.
        Context (Rsrc : forall t1 t2, interp_src1 t1 -> interp_src2 t2 -> Prop)
                (Rdst : forall t1 t2, interp_dst1 t1 -> interp_dst2 t2 -> Prop).

        Fixpoint interp_type_gen_rel_pointwise_hetero_hetero (t1 t2 : type)
          : interp_type_gen_hetero interp_src1 interp_dst1 t1
            -> interp_type_gen_hetero interp_src2 interp_dst2 t2
            -> Prop
          := @respectful_hetero _ _ _ _ (Rsrc _ _) (fun _ _ => Rdst _ _).
        Global Arguments interp_type_gen_rel_pointwise_hetero_hetero _ _ _ _ / .
      End hetero_hetero.
    End hetero.

    Section partially_hetero.
      Context (interp_flat_type1 interp_flat_type2 : flat_type -> Type)
              (R : forall t, interp_flat_type1 t -> interp_flat_type2 t -> Prop).

      Definition interp_type_gen_rel_pointwise
        : forall t,
          interp_type_gen interp_flat_type1 t
          -> interp_type_gen interp_flat_type2 t
          -> Prop
        := interp_type_gen_rel_pointwise_hetero
             interp_flat_type1 interp_flat_type2
             interp_flat_type1 interp_flat_type2
             R R.
      Global Arguments interp_type_gen_rel_pointwise _ _ _ / .
    End partially_hetero.
  End type.

  Section specialized_type.
    Section hetero.
      Context (interp_base_type1 interp_base_type2 : base_type_code -> Type).
      Definition interp_type_rel_pointwise R
        : forall t, interp_type interp_base_type1 t
                    -> interp_type interp_base_type2 t
                    -> Prop
        := interp_type_gen_rel_pointwise _ _ (interp_flat_type_rel_pointwise R).
      Global Arguments interp_type_rel_pointwise _ !_ _ _ / .

      Definition interp_type_rel_pointwise_hetero R
        : forall t1 t2, interp_type interp_base_type1 t1
                        -> interp_type interp_base_type2 t2
                        -> Prop
        := interp_type_gen_rel_pointwise_hetero_hetero _ _ _ _ (interp_flat_type_rel_pointwise_hetero R) (interp_flat_type_rel_pointwise_hetero R).
      Global Arguments interp_type_rel_pointwise_hetero _ !_ !_ _ _ / .
    End hetero.
  End specialized_type.

  Section lifting.
    Context {interp_base_type1 interp_base_type2 : base_type_code -> Type}.
    Local Notation interp_flat_type1 := (interp_flat_type interp_base_type1).
    Local Notation interp_flat_type2 := (interp_flat_type interp_base_type2).
    Let Tbase := (@Tbase base_type_code).
    Local Coercion Tbase : base_type_code >-> flat_type.

    Section with_rel.
      Context (R : forall t, interp_flat_type1 t -> interp_flat_type2 t -> Prop)
              (RUnit : R Unit tt tt).
      Section RProd.
        Context (RProd : forall A B x y, R A (fst x) (fst y) /\ R B (snd x) (snd y) -> R (Prod A B) x y)
                (RProd' : forall A B x y, R (Prod A B) x y -> R A (fst x) (fst y) /\ R B (snd x) (snd y)).
        Lemma lift_interp_flat_type_rel_pointwise1 t (x : interp_flat_type1 t) (y : interp_flat_type2 t)
          : interp_flat_type_rel_pointwise R t x y -> R t x y.
        Proof using RProd RUnit. clear RProd'; induction t; simpl; destruct_head_hnf' unit; intuition. Qed.
        Lemma lift_interp_flat_type_rel_pointwise2 t (x : interp_flat_type1 t) (y : interp_flat_type2 t)
          : R t x y -> interp_flat_type_rel_pointwise R t x y.
        Proof using RProd'. clear RProd; induction t; simpl; destruct_head_hnf' unit; split_and; intuition. Qed.
      End RProd.
      Section RProd_iff.
        Context (RProd : forall A B x y, R A (fst x) (fst y) /\ R B (snd x) (snd y) <-> R (Prod A B) x y).
        Lemma lift_interp_flat_type_rel_pointwise t (x : interp_flat_type1 t) (y : interp_flat_type2 t)
          : interp_flat_type_rel_pointwise R t x y <-> R t x y.
        Proof using RProd RUnit.
          split_iff; split; auto using lift_interp_flat_type_rel_pointwise1, lift_interp_flat_type_rel_pointwise2.
        Qed.
      End RProd_iff.
    End with_rel.
    Lemma lift_interp_flat_type_rel_pointwise_f_eq {T} (f g : forall t, _ -> T t) t x y
      : @interp_flat_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => f t x = g t y)
          t x y
        <-> SmartVarfMap f x = SmartVarfMap g y.
    Proof using Type.
      induction t; unfold SmartVarfMap in *; simpl in *; destruct_head_hnf unit; try tauto.
      rewrite_hyp !*; intuition congruence.
    Qed.
    Lemma lift_interp_flat_type_rel_pointwise_f_eq_id1 (f : forall t, _ -> _) t x y
      : @interp_flat_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => x = f t y)
          t x y
        <-> x = SmartVarfMap f y.
    Proof using Type. rewrite lift_interp_flat_type_rel_pointwise_f_eq, SmartVarfMap_id; reflexivity. Qed.
    Lemma lift_interp_flat_type_rel_pointwise_f_eq_id2 (f : forall t, _ -> _) t x y
      : @interp_flat_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => f t x = y)
          t x y
        <-> SmartVarfMap f x = y.
    Proof using Type. rewrite lift_interp_flat_type_rel_pointwise_f_eq, SmartVarfMap_id; reflexivity. Qed.
    Lemma lift_interp_flat_type_rel_pointwise_f_eq2 {T} (f g : forall t, _ -> _ -> T t) t x y
      : @interp_flat_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => f t x y = g t x y)
          t x y
        <-> SmartVarfMap2 f x y = SmartVarfMap2 g x y.
    Proof using Type.
      induction t; unfold SmartVarfMap2 in *; simpl in *; destruct_head_hnf unit; try tauto.
      rewrite_hyp !*; intuition congruence.
    Qed.
    Lemma lift_interp_type_rel_pointwise_f_eq {T} (f g : forall t, _ -> T t) t x y
      : interp_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => f t x = g t y)
          t x y
        <-> (forall a b, SmartVarfMap f a = SmartVarfMap g b -> SmartVarfMap f (x a) = SmartVarfMap g (y b)).
    Proof using Type.
      destruct t; simpl; unfold interp_type_rel_pointwise, respectful_hetero.
      setoid_rewrite lift_interp_flat_type_rel_pointwise_f_eq; reflexivity.
    Qed.
    Lemma lift_interp_type_rel_pointwise_f_eq_id1 (f : forall t, _ -> _) t x y
      : interp_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => x = f t y)
          t x y
        <-> (forall a, x (SmartVarfMap f a) = SmartVarfMap f (y a)).
    Proof using Type. rewrite lift_interp_type_rel_pointwise_f_eq; setoid_rewrite SmartVarfMap_id; firstorder (subst; eauto). Qed.
    Lemma lift_interp_type_rel_pointwise_f_eq_id2 (f : forall t, _ -> _) t x y
      : interp_type_rel_pointwise
          interp_base_type1 interp_base_type2
          (fun t x y => f t x = y)
          t x y
        <-> (forall a, SmartVarfMap f (x a) = y (SmartVarfMap f a)).
    Proof using Type. rewrite lift_interp_type_rel_pointwise_f_eq; setoid_rewrite SmartVarfMap_id; firstorder (subst; eauto). Qed.
  End lifting.

  Local Ltac t :=
    repeat match goal with
           | _ => intro
           | [ H : False |- _ ] => exfalso; assumption
           | _ => progress subst
           | _ => assumption
           | _ => progress inversion_sigma
           | _ => progress inversion_prod
           | _ => progress simpl in *
           | _ => progress destruct_head_hnf' and
           | [ H : context[List.In _ (_ ++ _)] |- _ ]
             => rewrite List.in_app_iff in H
           | _ => progress destruct_head' or
           | _ => solve [ eauto ]
           end.

  Lemma interp_flat_type_rel_pointwise_flatten_binding_list
        {interp_base_type1 interp_base_type2 t T} R' e1 e2 v1 v2
        (H : List.In (existT _ t (v1, v2)%core) (flatten_binding_list e1 e2))
        (HR : @interp_flat_type_rel_pointwise interp_base_type1 interp_base_type2 R' T e1 e2)
    : R' t v1 v2.
  Proof using Type. induction T; t. Qed.

  Lemma interp_flat_type_rel_pointwise_hetero_flatten_binding_list2
        {interp_base_type1 interp_base_type2 t1 t2 T1 T2} R' e1 e2 v1 v2
        (H : List.In (existT _ (t1, t2)%core (v1, v2)%core) (flatten_binding_list2 e1 e2))
        (HR : @interp_flat_type_rel_pointwise_hetero interp_base_type1 interp_base_type2 R' T1 T2 e1 e2)
    : R' t1 t2 v1 v2.
  Proof using Type.
    revert dependent T2; induction T1, T2; t.
  Qed.
End language.

Global Arguments interp_type_rel_pointwise {_ _ _} R {t} _ _.
Global Arguments interp_type_rel_pointwise_hetero {_ _ _} R {t1 t2} _ _.
Global Arguments interp_type_gen_rel_pointwise_hetero_hetero {_ _ _ _ _} Rsrc Rdst {t1 t2} _ _.
Global Arguments interp_type_gen_rel_pointwise_hetero {_ _ _ _ _} Rsrc Rdst {t} _ _.
Global Arguments interp_type_gen_rel_pointwise {_ _ _} R {t} _ _.
Global Arguments interp_flat_type_rel_pointwise_gen_Prop {_ _ _ P} and True R {t} _ _.
Global Arguments interp_flat_type_rel_pointwise_hetero_gen_Prop {_ _ _ P} and True False R {t1 t2} _ _.
Global Arguments interp_flat_type_rel_pointwise_hetero {_ _ _} R {t1 t2} _ _.
Global Arguments interp_flat_type_relb_pointwise_hetero {_ _ _} R {t1 t2} _ _.
Global Arguments interp_flat_type_rel_pointwise1 {_ _} R {t} _.
Global Arguments interp_flat_type_relb_pointwise1 {_ _} R {t} _.
Global Arguments interp_flat_type_rel_pointwise {_ _ _} R {t} _ _.
Global Arguments interp_flat_type_relb_pointwise {_ _ _} R {t} _ _.