aboutsummaryrefslogtreecommitdiff
path: root/src/Assembly/Conversions.v
blob: bb2f1e6faa35a433c554e62f5ae53b00f7dfb29e (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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
Require Import Crypto.Assembly.PhoasCommon.

Require Export Crypto.Assembly.QhasmUtil.
Require Export Crypto.Assembly.QhasmEvalCommon.
Require Export Crypto.Assembly.WordizeUtil.
Require Export Crypto.Assembly.Evaluables.
Require Export Crypto.Assembly.HL.
Require Export Crypto.Assembly.LL.

Require Export FunctionalExtensionality.

Require Import Bedrock.Nomega.

Require Import Coq.ZArith.ZArith_dec.
Require Import Coq.ZArith.Znat.

Require Import Coq.NArith.Nnat Coq.NArith.Ndigits.

Module HLConversions.
  Import HL.

  Fixpoint mapVar {A: Type} {t V0 V1} (f: forall t, V0 t -> V1 t) (g: forall t, V1 t -> V0 t) (a: expr (T := A) (var := V0) t): expr (T := A) (var := V1) t :=
    match a with
    | Const x => Const x
    | Var t x => Var (f _ x)
    | Binop t1 t2 t3 o e1 e2 => Binop o (mapVar f g e1) (mapVar f g e2)
    | Let tx e tC h => Let (mapVar f g e) (fun x => mapVar f g (h (g _ x)))
    | Pair t1 e1 t2 e2 => Pair (mapVar f g e1) (mapVar f g e2)
    | MatchPair t1 t2 e tC h => MatchPair (mapVar f g e) (fun x y => mapVar f g (h (g _ x) (g _ y)))
    end.

  Definition convertVar {A B: Type} {EA: Evaluable A} {EB: Evaluable B} {t} (a: interp_type (T := A) t): interp_type (T := B) t.
  Proof.
    induction t as [| t3 IHt1 t4 IHt2].

    - refine (@toT B EB (@fromT A EA _)); assumption.

    - destruct a as [a1 a2]; constructor;
        [exact (IHt1 a1) | exact (IHt2 a2)].
  Defined.

  Fixpoint convertExpr {A B: Type} {EA: Evaluable A} {EB: Evaluable B} {t v} (a: expr (T := A) (var := v) t): expr (T := B) (var := v) t :=
    match a with
    | Const x => Const (@toT B EB (@fromT A EA x))
    | Var t x => @Var B _ t x
    | Binop t1 t2 t3 o e1 e2 =>
      @Binop B _ t1 t2 t3 o (convertExpr e1) (convertExpr e2)
    | Let tx e tC f =>
      Let (convertExpr e) (fun x => convertExpr (f x))
    | Pair t1 e1 t2 e2 => Pair (convertExpr e1) (convertExpr e2)
    | MatchPair t1 t2 e tC f => MatchPair (convertExpr e) (fun x y =>
        convertExpr (f x y))
    end.

  Fixpoint convertExpr' {A B: Type} {EA: Evaluable A} {EB: Evaluable B} {t} (a: expr (T := A) (var := @interp_type A) t): expr (T := B) (var := @interp_type B) t :=
    match a with
    | Const x => Const (@toT B EB (@fromT A EA x))
    | Var t x => @Var B _ t (convertVar x)
    | Binop t1 t2 t3 o e1 e2 =>
      @Binop B _ t1 t2 t3 o (convertExpr' e1) (convertExpr' e2)
    | Let tx e tC f =>
      Let (convertExpr' e) (fun x => convertExpr' (f (convertVar x)))
    | Pair t1 e1 t2 e2 => Pair (convertExpr' e1) (convertExpr' e2)
    | MatchPair t1 t2 e tC f => MatchPair (convertExpr' e) (fun x y =>
        convertExpr' (f (convertVar x) (convertVar y)))
    end.

  Definition convertZToWord {t} n v a :=
    @convertExpr Z (word n) ZEvaluable (@WordEvaluable n) t v a.

  Definition convertZToWordRangeOpt {t} n v a :=
    @convertExpr Z (@WordRangeOpt n) ZEvaluable (@WordRangeOptEvaluable n) t v a.

  Definition convertZToWord' {t} n a :=
    @convertExpr' Z (word n) ZEvaluable (@WordEvaluable n) t a.

  Definition convertZToWordRangeOpt' {t} n a :=
    @convertExpr' Z (@WordRangeOpt n) ZEvaluable (@WordRangeOptEvaluable n) t a.

  Definition ZToWord {t} n (a: @Expr Z t): @Expr (word n) t :=
    fun v => convertZToWord n v (a v).

  Definition ZToRange {t} n (a: @Expr Z t): @Expr (@WordRangeOpt n) t :=
    fun v => convertZToWordRangeOpt n v (a v).

  Definition typeMap {A B t} (f: A -> B) (x: @interp_type A t): @interp_type B t.
  Proof.
    induction t; [refine (f x)|].
    destruct x as [x1 x2].
    refine (IHt1 x1, IHt2 x2).
  Defined.

  Definition RangeInterp {n t} E: @interp_type (option (Range N)) t :=
    typeMap rangeEval (@Interp (@WordRangeOpt n) (@WordRangeOptEvaluable n) t E).

  Example example_Expr : Expr TT := fun var => (
    Let (Const 7) (fun a =>
      Let (Let (Binop OPadd (Var a) (Var a)) (fun b => Pair (Var b) (Var b))) (fun p =>
        MatchPair (Var p) (fun x y =>
          Binop OPadd (Var x) (Var y)))))%Z.

  Example interp_example_range :
    RangeInterp (ZToRange 32 example_Expr) = Some (range N 28%N 28%N).
  Proof. reflexivity. Qed.
End HLConversions.

Module LLConversions.
  Import LL.

  Definition convertVar {A B: Type} {EA: Evaluable A} {EB: Evaluable B} {t} (a: interp_type (T := A) t): interp_type (T := B) t.
  Proof.
    induction t as [| t3 IHt1 t4 IHt2].

    - refine (@toT B EB (@fromT A EA _)); assumption.

    - destruct a as [a1 a2]; constructor;
        [exact (IHt1 a1) | exact (IHt2 a2)].
  Defined.

  Fixpoint convertArg {A B: Type} {EA: Evaluable A} {EB: Evaluable B} t {struct t}: @arg A A t -> @arg B B t :=
    match t as t' return @arg A A t' -> @arg B B t' with
    | TT => fun x =>
      match x with
      | Const c => Const (convertVar (t := TT) c)
      | Var v => Var (convertVar (t := TT) v)
      end
    | Prod t0 t1 => fun x =>
      match (match_arg_Prod x) with
      | (a, b) => Pair ((convertArg t0) a) ((convertArg t1) b)
      end
    end.

  Arguments convertArg [_ _ _ _ _ _].

  Fixpoint convertExpr {A B: Type} {EA: Evaluable A} {EB: Evaluable B} {t} (a: expr (T := A) t): expr (T := B) t :=
    match a with
    | LetBinop _ _ out op a b _ eC =>
      LetBinop (T := B) op (convertArg a) (convertArg b) (fun x: (arg out) => convertExpr (eC (convertArg x)))
    | Return _ a => Return (convertArg a)
    end.

  Definition convertZToWord {t} n a :=
    @convertExpr Z (word n) ZEvaluable (@WordEvaluable n) t a.

  Definition convertZToWordRangeOpt {t} n a :=
    @convertExpr Z (@WordRangeOpt n) ZEvaluable (@WordRangeOptEvaluable n) t a.

  Definition typeMap {A B t} (f: A -> B) (x: @interp_type A t): @interp_type B t.
  Proof.
    induction t; [refine (f x)|].
    destruct x as [x1 x2].
    refine (IHt1 x1, IHt2 x2).
  Defined.

  Definition zinterp {t} E := @interp Z ZEvaluable t E.

  Definition wordInterp {n t} E := @interp (word n) (@WordEvaluable n) t E.

  Definition rangeInterp {n t} E: @interp_type (@WordRangeOpt n) t :=
    @interp (@WordRangeOpt n) (@WordRangeOptEvaluable n) t E.

  Section Correctness.
    (* Aliases to make the proofs easier to read. *)

    Definition varZToRange {n t} (v: @interp_type Z t): @interp_type (@WordRangeOpt n) t :=
      @convertVar Z (@WordRangeOpt n) ZEvaluable (WordRangeOptEvaluable) t v.

    Definition varRangeToZ {n t} (v: @interp_type (@WordRangeOpt n) t): @interp_type Z t :=
      @convertVar (@WordRangeOpt n) Z (WordRangeOptEvaluable) ZEvaluable t v.

    Definition varZToWord {n t} (v: @interp_type Z t): @interp_type (@word n) t :=
      @convertVar Z (@word n) ZEvaluable (@WordEvaluable n) t v.

    Definition varWordToZ {n t} (v: @interp_type (@word n) t): @interp_type Z t :=
      @convertVar (word n) Z (@WordEvaluable n) ZEvaluable t v.

    Definition zOp {tx ty tz: type} (op: binop tx ty tz)
               (x: @interp_type Z tx) (y: @interp_type Z ty): @interp_type Z tz :=
      @interp_binop Z ZEvaluable _ _ _ op x y.

    Definition wordOp {n} {tx ty tz: type} (op: binop tx ty tz)
               (x: @interp_type _ tx) (y: @interp_type _ ty): @interp_type _ tz :=
      @interp_binop (word n) (@WordEvaluable n) _ _ _ op x y.

    Definition rangeOp {n} {tx ty tz: type} (op: binop tx ty tz)
               (x: @interp_type _ tx) (y: @interp_type _ ty): @interp_type _ tz :=
      @interp_binop (@WordRangeOpt n) (@WordRangeOptEvaluable n) _ _ _ op x y.

    Definition rangeArg {n t} (x: @arg (@WordRangeOpt n) (@WordRangeOpt n) t) := @interp_arg _ _ x.

    Definition wordArg {n t} (x: @arg (word n) (word n) t) := @interp_arg _ _ x.

    (* Bounds-checking fixpoint *)

    Fixpoint checkVar {n t} (x: @interp_type (@WordRangeOpt n) t) :=
      match t as t' return (interp_type t') -> Prop with
      | TT => fun x' =>
        match (rangeEval x') with
        | Some (range low high) => True
        | None => False
        end
      | Prod t0 t1 => fun x' =>
        match x' with
        | (x0, x1) => (checkVar (n := n) x0) /\ (checkVar (n := n) x1)
        end
      end x.

    Fixpoint check {n t} (e : @expr (@WordRangeOpt n) (@WordRangeOpt n) t): Prop :=
      match e with
      | LetBinop ta tb tc op a b _ eC =>
          (@checkVar n tc (rangeOp op (interp_arg a) (interp_arg b)))
        /\ (check (eC (uninterp_arg (rangeOp op (interp_arg a) (interp_arg b)))))
      | Return _ a => checkVar (interp_arg a)
      end.

    (* Bounds-checking fixpoint *)

    Fixpoint checkVar' {n t} (x: @interp_type (@WordRangeOpt n) t) :=
      match t as t' return (interp_type t') -> bool with
      | TT => fun x' =>
        match (rangeEval x') with
        | Some (range low high) => true
        | None => false
        end
      | Prod t0 t1 => fun x' =>
        match x' with
        | (x0, x1) => andb (checkVar' (n := n) x0) (checkVar' (n := n) x1)
        end
      end x.

    Fixpoint check' {n t} (e : @expr (@WordRangeOpt n) (@WordRangeOpt n) t): bool :=
      match e with
      | LetBinop ta tb tc op a b _ eC =>
        andb (@checkVar' n tc (rangeOp op (interp_arg a) (interp_arg b)))
             (check' (eC (uninterp_arg (rangeOp op (interp_arg a) (interp_arg b)))))
      | Return _ a => checkVar' (interp_arg a)
      end.

    Lemma checkVar'_spec: forall {n t} x, @checkVar' n t x = true <-> @checkVar n t x.
    Proof.
      intros; induction t; simpl; split; intro C.

      - induction (rangeEval x) as [|a]; try induction a; auto.
        inversion C.

      - induction (rangeEval x) as [|a]; try induction a; auto.

      - destruct x.
        apply andb_true_iff in C; destruct C.
        split; try apply IHt1; try apply IHt2; assumption.

      - destruct x, C.
        apply andb_true_iff; split;
          try apply IHt1; try apply IHt2; assumption.
    Qed.

    Lemma check'_spec: forall {n t} e, @check' n t e = true <-> @check n t e.
    Proof.
      intros; induction e; simpl; split; intro C;
        try abstract (apply checkVar'_spec; assumption).

      - apply andb_true_iff in C; destruct C as [C0 C1]; split;
          [apply checkVar'_spec | apply H in C1]; assumption.

      - apply andb_true_iff; destruct C as [C0 C1]; split;
          [apply checkVar'_spec | apply H]; assumption.
    Qed.

    (* Utility Lemmas *)

    Lemma convertArg_interp: forall {A B t} {EA: Evaluable A} {EB: Evaluable B} (x: @arg A A t),
        (interp_arg (@convertArg A B EA EB t x)) = @convertVar A B EA EB t (interp_arg x).
    Proof.
      induction x as [| |t0 t1 i0 i1]; [reflexivity|reflexivity|].
      induction EA, EB; simpl; f_equal; assumption.
    Qed.

    Lemma convertArg_var: forall {A B EA EB t} (x: @interp_type A t),
        @convertArg A B EA EB t (uninterp_arg x) = uninterp_arg (@convertVar A B EA EB t x).
    Proof.
      induction t as [|t0 IHt_0 t1 IHt_1]; simpl; intros; [reflexivity|].
      induction x as [a b]; simpl; f_equal;
        induction t0 as [|t0a IHt0_0 t0b IHt0_1],
                  t1 as [|t1a IHt1_0]; simpl in *;
        try rewrite IHt_0;
        try rewrite IHt_1;
        reflexivity.
    Qed.

    Ltac kill_N2Z_id :=
      try match goal with
      | [H: context[Z.of_N (Z.to_N (interp_arg _))] |- _] => rewrite Z2N.id in H
      end; try assumption.

    Ltac kill_ineq :=
      repeat match goal with
      | [H: context[Z_le_dec ?a ?b] |- _]  => destruct (Z_le_dec a b)
      | [H: context[Z_lt_dec ?a ?b] |- _]  => destruct (Z_lt_dec a b)
      end; simpl in *;

      repeat match goal with
      | [H: context[Nge_dec ?a ?b] |- _]   => destruct (Nge_dec a b)
      | [H: context[overflows ?a ?b] |- _] => destruct (overflows a b)
      end; simpl in *; intuition;

      repeat match goal with
      | [H: (_ >= _)%N |- _] => apply N2Z.inj_ge in H; kill_N2Z_id
      | [H: (_ < _)%N |- _] => apply N2Z.inj_lt in H; kill_N2Z_id
      | [H1: (?a < ?b)%Z, H2: (?a >= ?b)%Z |- _] =>
        unfold Z.lt in H1; unfold Z.ge in H2;
        apply H2 in H1; intuition
      end.

    Lemma ZToRange_binop_correct : forall {n tx ty tz} (op: binop tx ty tz) (x: arg tx) (y: arg ty) e,
        check (t := tz) (convertZToWordRangeOpt n (LetBinop op x y e))
      -> zOp op (interp_arg x) (interp_arg y) =
          varRangeToZ (rangeOp (n := n) op (varZToRange (interp_arg x)) (varZToRange (interp_arg y))).
    Proof.
      intros until e; intro H.
      unfold convertZToWordRangeOpt, convertExpr, check, rangeOp in H.
      repeat rewrite interp_arg_convert in H; simpl in H.
      destruct H as [H H0]; clear H0.

      induction op; unfold zOp, varRangeToZ, rangeOp.

      - simpl; unfold getUpperBoundOpt.
        repeat rewrite convertArg_interp in H.
        unfold interp_binop, eadd, WordRangeOptEvaluable in H.
        unfold applyBinOp, makeRange in *; simpl in *; unfold id in *.
        kill_ineq.
        rewrite N2Z.inj_add.
        repeat rewrite Z2N.id; try assumption.
        reflexivity.

      - simpl; unfold getUpperBoundOpt.
        repeat rewrite convertArg_interp in H.
        unfold interp_binop, esub, WordRangeOptEvaluable in H.
        unfold applyBinOp, makeRange in *; simpl in *; unfold id in *.
        kill_ineq.

        admit.

        admit.

      - simpl; unfold getUpperBoundOpt.
        repeat rewrite convertArg_interp in H.
        unfold interp_binop, emul, WordRangeOptEvaluable in H.
        unfold applyBinOp, makeRange in *; simpl in *; unfold id in *.
        kill_ineq.

        rewrite N2Z.inj_mul.
        repeat rewrite Z2N.id; try assumption.
        reflexivity.

      - simpl; unfold getUpperBoundOpt.
        repeat rewrite convertArg_interp in H.
        unfold interp_binop, eand, WordRangeOptEvaluable in H.
        unfold applyBinOp, makeRange in *; simpl in *; unfold id in *.
        kill_ineq.

        admit.

        admit.

      - simpl; unfold getUpperBoundOpt.
        repeat rewrite convertArg_interp in H.
        unfold interp_binop, esub, WordRangeOptEvaluable in H.
        unfold applyBinOp, makeRange in *; simpl in *; unfold id in *.
        kill_ineq.

        rewrite Z.shiftr_div_pow2; try assumption.
        rewrite N.shiftr_div_pow2; try assumption.
        rewrite N2Z.inj_div.
        rewrite Z2N.id; try assumption.
        repeat f_equal.
        rewrite N2Z.inj_pow; simpl; f_equal.
        rewrite Z2N.id; auto.

    Admitted.

    Lemma ZToWord_binop_correct : forall {n tx ty tz} (op: binop tx ty tz) (x: arg tx) (y: arg ty) e,
        check (t := tz) (convertZToWordRangeOpt n (LetBinop op x y e))
      -> zOp op (interp_arg x) (interp_arg y) =
          varWordToZ (wordOp (n := n) op (varZToWord (interp_arg x)) (varZToWord (interp_arg y))).
    Proof.
      intros until e; intro H.
      unfold convertZToWordRangeOpt, convertExpr, check, rangeOp in H.
      repeat rewrite convertArg_interp in H; simpl in H.
      destruct H as [H H0]; clear H0.

      induction op; unfold zOp, varRangeToZ, rangeOp.

      - simpl; unfold getUpperBoundOpt; simpl in H.
        rewrite applyBinOp_constr_spec in H; simpl in H.

        unfold makeRange in H.
        kill_ineq; unfold id in *.

        rewrite <- wordize_plus.

        + repeat rewrite wordToN_NToWord; try assumption;
            try abstract (apply N2Z.inj_lt; rewrite Z2N.id; assumption).

          rewrite N2Z.inj_add.
          repeat rewrite Z2N.id; try assumption.
          reflexivity.

        + repeat rewrite wordToN_NToWord;
            apply N2Z.inj_lt;
            repeat rewrite Z2N.id;
            assumption.

      - simpl; unfold getUpperBoundOpt; simpl in H.
        rewrite applyBinOp_constr_spec in H; simpl in H.

        unfold makeRange in H.
        kill_ineq; unfold id in *.

        admit. (* TODO: wordize_minus *)

      - simpl; unfold getUpperBoundOpt; simpl in H.
        rewrite applyBinOp_constr_spec in H; simpl in H.

        unfold makeRange in H.
        kill_ineq; unfold id in *.

        rewrite <- wordize_mult.

        + repeat rewrite wordToN_NToWord; try assumption;
            try abstract (apply N2Z.inj_lt; rewrite Z2N.id; assumption).

          rewrite N2Z.inj_mul.
          repeat rewrite Z2N.id; try assumption.
          reflexivity.

        + repeat rewrite wordToN_NToWord;
            apply N2Z.inj_lt;
            repeat rewrite Z2N.id;
            assumption.

      - simpl; unfold getUpperBoundOpt; simpl in H.
        rewrite applyBinOp_constr_spec in H; simpl in H.

        unfold makeRange in H.
        kill_ineq; unfold id in *.

        rewrite wordize_and.
        repeat rewrite wordToN_NToWord;
          try (apply N2Z.inj_lt; rewrite Z2N.id; assumption).

        apply Z.bits_inj_iff; unfold Z.eqf; intro k.
        destruct (Z_ge_dec k 0%Z) as [G|G].

        + apply Z.ge_le in G.
          rewrite Z.land_spec.
          rewrite Z2N.inj_testbit; try assumption.
          rewrite N.land_spec.
          repeat rewrite <- Z2N.inj_testbit; try assumption.
          repeat rewrite Z2N.id; try assumption; reflexivity.

        + assert (k < 0)%Z by (
            unfold Z.lt; unfold Z.ge in G;
            induction (Z.compare k 0%Z);
            [| reflexivity |];
            contradict G; intro G; inversion G).

          repeat rewrite Z.testbit_neg_r; [reflexivity| |]; assumption.

      - simpl; unfold getUpperBoundOpt; simpl in H.
        rewrite applyBinOp_constr_spec in H; simpl in H.

        unfold makeRange in H.
        kill_ineq; unfold id in *.

        rewrite <- wordize_shiftr.
        rewrite <- (Nat2N.id (wordToNat _)).
        rewrite Nshiftr_nat_equiv.

    Admitted.

    Lemma check_zero: forall {n}, @check n TT (@convertExpr Z _ ZEvaluable (@WordRangeOptEvaluable n) TT (Return (Const 0%Z))).
    Proof.
      intros; simpl; unfold makeRange.

      repeat match goal with
      | [|- context[Z_le_dec ?A ?B] ] => destruct (Z_le_dec A B)
      | [|- context[Z_lt_dec ?A ?B] ] => destruct (Z_lt_dec A B)
      end; simpl; unfold id in *; intuition.

      - match goal with
        | [A: (0 < Z.of_N (Npow2 n))%Z -> False |- _] =>
          revert A; intro H
        end.

        apply H.
        replace 0%Z with (Z.of_N 0%N) by (cbv; auto).
        apply -> N2Z.inj_lt.
        apply Npow2_gt0.

      - match goal with
        | [A: (0 <= 0)%Z -> False |- _] =>
          apply A; cbv; intro Q; inversion Q
        end.
    Qed.

    (* Main correctness guarantee *)

    Lemma RangeInterp_correct: forall {n t} (E: expr t),
         check (convertZToWordRangeOpt n E)
      -> typeMap (fun x => NToWord n (Z.to_N x)) (zinterp E) = wordInterp (convertZToWord n E).
    Proof.
      intros n t E S.
      unfold rangeInterp, convertZToWordRangeOpt, zinterp,
             convertZToWord, wordInterp.

      induction E as [tx ty tz op x y z|]; simpl; try reflexivity.

      - rewrite H; clear H.

        + rewrite convertArg_var; repeat f_equal.
          repeat rewrite convertArg_interp.
          destruct S as [S0 S1]; unfold convertZToWordRangeOpt in *.

          pose proof (ZToWord_binop_correct (n := n) op x y) as C;
            unfold zOp, wordOp, varWordToZ, varZToWord in C;
            simpl in C.

          induction op; apply (C (fun _ => Return (Const 0%Z))); clear C; split;
            try apply check_zero;
            repeat rewrite convertArg_interp in S0;
            repeat rewrite convertArg_interp;
            assumption.

        + destruct S as [S0 S1]; unfold convertZToWordRangeOpt.

          replace (interp_binop op _ _)
             with (varRangeToZ (rangeOp op
                    (rangeArg (@convertArg _ _ ZEvaluable (@WordRangeOptEvaluable n) _ x))
                    (rangeArg (@convertArg _ _ ZEvaluable (@WordRangeOptEvaluable n) _ y))));
            [unfold varRangeToZ, rangeArg; rewrite <- convertArg_var; assumption | clear S1].

          pose proof (ZToRange_binop_correct (n := n) op x y) as C;
            unfold rangeArg, zOp, wordOp, varZToRange, varRangeToZ, convertZToWordRangeOpt in *;
            simpl in C.

          repeat rewrite convertArg_interp; symmetry.

          induction op; apply (C (fun _ => Return (Const 0%Z))); clear C; split;
            try apply check_zero;
            repeat rewrite convertArg_interp in S0;
            repeat rewrite convertArg_interp;
            assumption.

      - simpl in S.
        induction a as [| |t0 t1 a0 IHa0 a1 IHa1]; simpl in *; try reflexivity.
        destruct S; rewrite IHa0, IHa1; try reflexivity; assumption.
    Qed.
  End Correctness.
End LLConversions.

Section ConversionTest.
  Import HL HLConversions.

  Fixpoint keepAddingOne {var} (x : @expr Z var TT) (n : nat) : @expr Z var TT :=
    match n with
    | O => x
    | S n' => Let (Binop OPadd x (Const 1%Z)) (fun y => keepAddingOne (Var y) n')
    end.

  Definition KeepAddingOne (n : nat) : Expr (T := Z) TT :=
    fun var => keepAddingOne (Const 1%Z) n.

  Definition testCase := Eval vm_compute in KeepAddingOne 4000.

  Eval vm_compute in RangeInterp (ZToRange 0 testCase).
  Eval vm_compute in RangeInterp (ZToRange 1 testCase).
  Eval vm_compute in RangeInterp (ZToRange 10 testCase).
  Eval vm_compute in RangeInterp (ZToRange 32 testCase).
  Eval vm_compute in RangeInterp (ZToRange 64 testCase).
  Eval vm_compute in RangeInterp (ZToRange 128 testCase).

  Definition nefarious : Expr (T := Z) TT :=
    fun var => Let (Binop OPadd (Const 10%Z) (Const 20%Z))
                   (fun y => Binop OPmul (Var y) (Const 0%Z)).

  Eval vm_compute in RangeInterp (ZToRange 0 nefarious).
  Eval vm_compute in RangeInterp (ZToRange 1 nefarious).
  Eval vm_compute in RangeInterp (ZToRange 4 nefarious).
  Eval vm_compute in RangeInterp (ZToRange 5 nefarious).
  Eval vm_compute in RangeInterp (ZToRange 32 nefarious).
  Eval vm_compute in RangeInterp (ZToRange 64 nefarious).
  Eval vm_compute in RangeInterp (ZToRange 128 nefarious).
End ConversionTest.