aboutsummaryrefslogtreecommitdiff
path: root/src/Compilers/Z/Syntax/Util.v
blob: 110e6b8168d9c4a81301e38739b4c6b259f43108 (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
Require Import Coq.ZArith.ZArith.
Require Import Coq.micromega.Lia.
Require Import Crypto.Compilers.Syntax.
Require Import Crypto.Compilers.SmartMap.
Require Import Crypto.Compilers.Wf.
Require Import Crypto.Compilers.TypeUtil.
Require Import Crypto.Compilers.TypeInversion.
Require Import Crypto.Compilers.Z.Syntax.
Require Import Crypto.Util.FixedWordSizesEquality.
Require Import Crypto.Util.HProp.
Require Import Crypto.Util.Tactics.BreakMatch.
Require Import Crypto.Util.Tactics.DestructHead.
Require Import Crypto.Util.Notations.
Require Import Crypto.Util.PointedProp.

Definition make_const t : interp_base_type t -> op Unit (Tbase t)
  := fun v => OpConst (cast_const (t2:=TZ) v).
Definition is_const s d (v : op s d) : bool
  := match v with OpConst _ _ => true | _ => false end.
Arguments is_const [s d] v.
Definition is_opp s d (v : op s d) : bool
  := match v with Opp _ _ => true | _ => false end.
Arguments is_opp [s d] v.
Definition is_const_or_opp s d (v : op s d) : bool
  := (is_const v || is_opp v)%bool.
Arguments is_const_or_opp [s d] v.


Definition interped_op_side_conditions {s d} (opc : op s d)
  : interp_flat_type interp_base_type s -> pointed_Prop
  := match opc in op s d return interp_flat_type _ s -> _ with
     | IdWithAlt TZ TZ TZ
       => fun v1v2 : Z * Z
          => inject (fst v1v2 = snd v1v2)
     | _ => fun _ => trivial
     end.

Definition cast_back_flat_const {var t f V}
           (v : interp_flat_type interp_base_type (@SmartFlatTypeMap base_type var f t V))
  : interp_flat_type interp_base_type t
  := @SmartFlatTypeMapUnInterp
       _ var interp_base_type interp_base_type
       f (fun _ _ => cast_const)
       t V v.

Definition cast_flat_const {var t f V}
           (v : interp_flat_type interp_base_type t)
  : interp_flat_type interp_base_type (@SmartFlatTypeMap base_type var f t V)
  := @SmartFlatTypeMapInterp2
       _ var interp_base_type interp_base_type
       f (fun _ _ => cast_const)
       t V v.

Definition base_type_leb (v1 v2 : base_type) : bool
  := match v1, v2 with
     | _, TZ => true
     | TZ, _ => false
     | TWord logsz1, TWord logsz2 => Compare_dec.leb logsz1 logsz2
     end.

Definition base_type_min := base_type_min base_type_leb.
Definition base_type_max := base_type_max base_type_leb.
Global Arguments base_type_min !_ !_ / .
Global Arguments base_type_max !_ !_ / .
Global Arguments TypeUtil.base_type_min _ _ _ /  _.
Global Arguments TypeUtil.base_type_max _ _ _ /  _.

Definition genericize_op {var' src dst} (opc : op src dst) {f}
  : forall {vs vd}, op (@SmartFlatTypeMap _ var' f src vs) (@SmartFlatTypeMap _ var' f dst vd)
  := match opc with
     | OpConst _ z => fun _ _ => OpConst z
     | Add _ _ _ => fun _ _ => Add _ _ _
     | Sub _ _ _ => fun _ _ => Sub _ _ _
     | Mul _ _ _ => fun _ _ => Mul _ _ _
     | Shl _ _ _ => fun _ _ => Shl _ _ _
     | Shr _ _ _ => fun _ _ => Shr _ _ _
     | Land _ _ _ => fun _ _ => Land _ _ _
     | Lor _ _ _ => fun _ _ => Lor _ _ _
     | Opp _ _ => fun _ _ => Opp _ _
     | IdWithAlt _ _ _ => fun _ _ => IdWithAlt _ _ _
     | Zselect _ _ _ _ => fun _ _ => Zselect _ _ _ _
     | MulSplit bitwidth _ _ _ _ => fun _ _ => MulSplit bitwidth _ _ _ _
     | AddWithCarry _ _ _ _ => fun _ _ => AddWithCarry _ _ _ _
     | AddWithGetCarry bitwidth _ _ _ _ _ => fun _ _ => AddWithGetCarry bitwidth _ _ _ _ _
     | SubWithBorrow _ _ _ _ => fun _ _ => SubWithBorrow _ _ _ _
     | SubWithGetBorrow bitwidth _ _ _ _ _ => fun _ _ => SubWithGetBorrow bitwidth _ _ _ _ _
     end.

Lemma cast_const_id {t} v
  : @cast_const t t v = v.
Proof.
  destruct t; simpl; trivial.
  rewrite ZToWord_wordToZ; reflexivity.
Qed.

Lemma cast_const_idempotent_small {a b c} v
  : match b with
    | TZ => True
    | TWord bsz => 0 <= interpToZ (@cast_const a c v) < 2^Z.of_nat (2^bsz)
    end%Z
    -> @cast_const b c (@cast_const a b v) = @cast_const a c v.
Proof.
  repeat first [ reflexivity
               | congruence
               | progress destruct_head' base_type
               | progress simpl
               | progress break_match
               | progress subst
               | intro
               | match goal with
                 | [ H : ?leb _ _ = true |- _ ] => apply Compare_dec.leb_complete in H
                 | [ H : ?leb _ _ = false |- _ ] => apply Compare_dec.leb_iff_conv in H
                 | [ H : TWord _ = TWord _ |- _ ] => inversion H; clear H
                 end
               | rewrite ZToWord_wordToZ_ZToWord by lia
               | rewrite wordToZ_ZToWord_wordToZ by lia
               | rewrite wordToZ_ZToWord by assumption
               | rewrite ZToWord_wordToZ_ZToWord_small by omega ].
Qed.

Lemma cast_const_split_mod {a b} v
  : @cast_const a b v = ZToInterp (match a, b with
                                   | TZ, _ => interpToZ v
                                   | _, TWord lgsz => (interpToZ v) mod (2^Z.of_nat (2^lgsz))
                                   | _, TZ => interpToZ v
                                   end).
Proof.
  destruct_head base_type; simpl; try reflexivity.
  rewrite <- wordToZ_ZToWord_mod, ZToWord_wordToZ by apply wordToZ_range.
  reflexivity.
Qed.

Lemma interpToZ_cast_const_mod {a b} v
  : interpToZ (@cast_const a b v)
    = match b with
      | TZ => interpToZ v
      | TWord lgsz => Z.max 0 (interpToZ v) mod (2^Z.of_nat (2^lgsz))
      end%Z.
Proof.
  repeat first [ progress destruct_head base_type
               | reflexivity
               | rewrite wordToZ_ZToWord_mod_full ].
Qed.

Lemma cast_const_ZToInterp_mod {a b} v
  : @cast_const a b (ZToInterp v)
    = ZToInterp match a with
                | TZ => v
                | TWord lgsz => Z.max 0 v mod 2^Z.of_nat (2^lgsz)
                end%Z.
Proof.
  repeat first [ progress destruct_head base_type
               | reflexivity
               | rewrite wordToZ_ZToWord_mod_full ].
Qed.

Lemma interpToZ_ZToInterp_mod {a} v
  : @interpToZ a (ZToInterp v)
    = match a with
      | TZ => v
      | TWord lgsz => Z.max 0 v mod 2^Z.of_nat (2^lgsz)
      end%Z.
Proof.
  etransitivity; [ apply (@interpToZ_cast_const_mod TZ) | ].
  reflexivity.
Qed.

Lemma cast_const_idempotent {a b c} v
  : base_type_min b (base_type_min a c) = base_type_min a c
    -> @cast_const b c (@cast_const a b v) = @cast_const a c v.
Proof.
  repeat first [ reflexivity
               | congruence
               | progress destruct_head' base_type
               | progress simpl
               | progress break_match
               | progress subst
               | intro
               | match goal with
                 | [ H : ?leb _ _ = true |- _ ] => apply Compare_dec.leb_complete in H
                 | [ H : ?leb _ _ = false |- _ ] => apply Compare_dec.leb_iff_conv in H
                 | [ H : TWord _ = TWord _ |- _ ] => inversion H; clear H
                 end
               | rewrite ZToWord_wordToZ_ZToWord by lia
               | rewrite wordToZ_ZToWord_wordToZ by lia ].
Qed.

Lemma ZToInterp_eq_inj {a} x y
  : @ZToInterp a x = @ZToInterp a y
    <-> match a with
        | TZ => x
        | TWord lgsz => Z.max 0 x mod 2^Z.of_nat (2^lgsz)
        end%Z
        = match a with
          | TZ => y
          | TWord lgsz => Z.max 0 y mod 2^Z.of_nat (2^lgsz)
          end%Z.
Proof.
  rewrite <- !interpToZ_ZToInterp_mod.
  destruct a; try reflexivity; simpl.
  split; intro H; try congruence.
  rewrite <- (ZToWord_wordToZ (FixedWordSizes.ZToWord x)), <- (ZToWord_wordToZ (FixedWordSizes.ZToWord y)).
  congruence.
Qed.

Lemma interpToZ_range {a} (v : interp_base_type a)
  : match a with
    | TZ => True
    | TWord lgsz => 0 <= interpToZ v < 2^Z.of_nat (2^lgsz)
    end%Z.
Proof.
  destruct a; trivial; simpl.
  apply wordToZ_range.
Qed.

Lemma make_const_correct : forall T v, interp_op Unit (Tbase T) (make_const T v) tt = v.
Proof.
  destruct T; cbv -[FixedWordSizes.ZToWord FixedWordSizes.wordToZ FixedWordSizes.wordT];
    intro; rewrite ?ZToWord_wordToZ; reflexivity.
Qed.

Local Notation iffT A B := ((A -> B) * (B -> A))%type (only parsing).

Section unzify.
  Context {var'} {f : forall t : base_type, var' t -> base_type}.
  Let asZ := fun t => SmartFlatTypeMap
                        (fun _ _ => TZ)
                        (SmartValf (fun _ => base_type) (fun t => t) t).
  Definition unzify_op_helper_step
             (unzify_op_helper
              : forall {t : flat_type base_type}
                       {vs : interp_flat_type var' t},
                 iffT (interp_flat_type
                         interp_base_type
                         (asZ t))
                      (interp_flat_type
                         interp_base_type
                         (asZ (SmartFlatTypeMap f vs))))
             {t : flat_type base_type}
    : forall {vs : interp_flat_type var' t},
      iffT (interp_flat_type
              interp_base_type
              (asZ t))
           (interp_flat_type
              interp_base_type
              (asZ (SmartFlatTypeMap f vs)))
    := match t with
       | Tbase T => fun _ => (fun x => x, fun x => x)
       | Unit => fun _ => (fun x => x, fun x => x)
       | Prod A B
         => fun (vs : interp_flat_type _ A * interp_flat_type _ B)
            => let f1 := @unzify_op_helper A (fst vs) in
               let f2 := @unzify_op_helper B (snd vs) in
               ((fun x : interp_flat_type _ (asZ A) * interp_flat_type _ (asZ B)
                 => (fst f1 (fst x), fst f2 (snd x))),
                (fun x : interp_flat_type _ (asZ (SmartFlatTypeMap f (fst vs)))
                         * interp_flat_type _ (asZ (SmartFlatTypeMap f (snd vs)))
                 => (snd f1 (fst x), snd f2 (snd x))))
       end.
  Fixpoint unzify_op_helper {t vs}
    := @unzify_op_helper_step (@unzify_op_helper) t vs.

  Definition unzify_op
             {src dst : flat_type base_type}
             {vs : interp_flat_type var' src} {vd : interp_flat_type var' dst}
             (F : interp_flat_type interp_base_type (asZ src) -> interp_flat_type interp_base_type (asZ dst))
             (x : interp_flat_type interp_base_type (asZ (SmartFlatTypeMap f vs)))
    : interp_flat_type interp_base_type (asZ (SmartFlatTypeMap f vd))
    := fst unzify_op_helper (F (snd unzify_op_helper x)).
End unzify.

Arguments unzify_op_helper_step _ _ _ !_ _ / .
Arguments unzify_op_helper _ _ !_ _ / .

Lemma Zinterp_op_genericize_op {var' src dst opc f vs vd}
  : Zinterp_op _ _ (@genericize_op var' src dst opc f vs vd)
    = unzify_op (Zinterp_op _ _ opc).
Proof.
  destruct opc; unfold unzify_op; reflexivity.
Qed.

Lemma lift_op_prod_dst {src dstA dstB}
      {f : _ -> interp_flat_type _ (SmartFlatTypeMap _ (SmartValf _ _ _)) * interp_flat_type _ (SmartFlatTypeMap _ (SmartValf _ _ _))}
      {x}
  : @lift_op src (Prod dstA dstB) f x
    = (@lift_op src dstA (fun y => fst (f y)) x, @lift_op src dstB (fun y => snd (f y)) x).
Proof. reflexivity. Qed.

Lemma cast_back_flat_const_prod {var A B f} {V : _ * _}
      (v : interp_flat_type interp_base_type (@SmartFlatTypeMap base_type var f A (fst V))
           * interp_flat_type interp_base_type (@SmartFlatTypeMap base_type var f B (snd V)))
  : @cast_back_flat_const var (Prod A B) f V v
    = (@cast_back_flat_const var A f (fst V) (fst v),
       @cast_back_flat_const var B f (snd V) (snd v)).
Proof. reflexivity. Qed.

Lemma base_type_leb_total
  : forall x y : base_type, base_type_leb x y = true \/ base_type_leb y x = true.
Proof.
  induction x, y; simpl; auto.
  rewrite !Nat.leb_le; omega.
Qed.

Lemma eta_match_base_type_impl P1 P2 PZ T
  : match T as T return P1 T -> P2 with
    | TZ => fun _ => PZ
    | TWord _ => fun _ => PZ
    end = fun _ => PZ.
Proof. destruct T; reflexivity. Qed.
Ltac rewrite_eta_match_base_type_impl_step :=
  match goal with
  | [ H : context[match ?T as T' in base_type return (@?P1 T' -> ?P2) with TZ => fun _ => ?PZ | TWord _ => fun _ => ?PZ end] |- _ ]
    => rewrite (@eta_match_base_type_impl P1 P2 PZ T) in H
  | [ |- context[match ?T as T' in base_type return (@?P1 T' -> ?P2) with TZ => fun _ => ?PZ | TWord _ => fun _ => ?PZ end] ]
    => rewrite (@eta_match_base_type_impl P1 P2 PZ T)
  end.
Ltac rewrite_eta_match_base_type_impl := repeat rewrite_eta_match_base_type_impl_step.