aboutsummaryrefslogtreecommitdiff
path: root/src/Compilers/Z/ArithmeticSimplifierInterp.v
blob: 6eec2f2a47b8cc7e666983c3c514d9450792085f (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
Require Import Coq.micromega.Psatz.
Require Import Coq.ZArith.ZArith.
Require Import Crypto.Compilers.Syntax.
Require Import Crypto.Compilers.TypeInversion.
Require Import Crypto.Compilers.ExprInversion.
Require Import Crypto.Compilers.RewriterInterp.
Require Import Crypto.Compilers.Z.Syntax.
Require Import Crypto.Compilers.Z.OpInversion.
Require Import Crypto.Compilers.Z.ArithmeticSimplifier.
Require Import Crypto.Compilers.Z.ArithmeticSimplifierUtil.
Require Import Crypto.Compilers.Z.Syntax.Equality.
Require Import Crypto.Util.ZUtil.
Require Import Crypto.Util.Option.
Require Import Crypto.Util.Prod.
Require Import Crypto.Util.Sum.
Require Import Crypto.Util.HProp.
Require Import Crypto.Util.Tactics.BreakMatch.
Require Import Crypto.Util.Tactics.DestructHead.

Local Notation exprf := (@exprf base_type op interp_base_type).
Local Notation expr := (@expr base_type op interp_base_type).
Local Notation Expr := (@Expr base_type op).

Local Ltac fin_t :=
  first [ exact I
        | reflexivity
        | congruence
        | assumption
        | lia
        | exfalso; assumption ].
Local Ltac break_t_step :=
  first [ progress subst
        | progress inversion_option
        | progress inversion_sum
        | progress inversion_expr
        | progress inversion_prod
        | progress inversion_inverted_expr
        | progress inversion_flat_type
        | progress destruct_head'_and
        | progress destruct_head'_prod
        | progress eliminate_hprop_eq
        | progress break_innermost_match_step
        | progress break_match_hyps ].


Lemma interp_as_expr_or_const_correct_base {t} e z
  : @interp_as_expr_or_const interp_base_type (Tbase t) e = Some z
    -> interpf interp_op e = match z with
                             | const_of z => cast_const (t1:=TZ) z
                             | gen_expr e => interpf interp_op e
                             | neg_expr e => interpf interp_op (Op (Opp _ _) e)
                             end.
Proof.
  destruct z.
  { repeat first [ fin_t
                 | progress simpl in *
                 | progress intros
                 | break_t_step
                 | progress invert_expr
                 | progress invert_op ]. }
  { do 2 (invert_expr; break_innermost_match; intros);
      repeat first [ fin_t
                   | progress simpl in *
                   | progress intros
                   | break_t_step
                   | progress invert_op ]. }
  { do 2 (invert_expr; break_innermost_match; intros);
      repeat first [ fin_t
                   | progress simpl in *
                   | progress intros
                   | break_t_step
                   | progress invert_op ]. }
Qed.

Lemma interp_as_expr_or_const_correct_prod_base {A B} e (v : _ * _)
  : @interp_as_expr_or_const interp_base_type (Prod (Tbase A) (Tbase B)) e = Some v
    -> interpf interp_op e = (match fst v with
                              | const_of z => cast_const (t1:=TZ) z
                              | gen_expr e => interpf interp_op e
                              | neg_expr e => interpf interp_op (Op (Opp _ _) e)
                              end,
                              match snd v with
                              | const_of z => cast_const (t1:=TZ) z
                              | gen_expr e => interpf interp_op e
                              | neg_expr e => interpf interp_op (Op (Opp _ _) e)
                              end).
Proof.
  invert_expr;
    repeat first [ fin_t
                 | progress simpl in *
                 | progress intros
                 | break_t_step
                 | erewrite !interp_as_expr_or_const_correct_base by eassumption; cbv beta iota ].
Qed.

Local Arguments Z.mul !_ !_.
Local Arguments Z.add !_ !_.
Local Arguments Z.sub !_ !_.
Local Arguments Z.opp !_.

Lemma InterpSimplifyArith {t} (e : Expr t)
  : forall x, Interp interp_op (SimplifyArith e) x = Interp interp_op e x.
Proof.
  apply InterpRewriteOp; intros; unfold simplify_op_expr.
  break_innermost_match;
    repeat first [ fin_t
                 | progress simpl in *
                 | progress subst
                 | erewrite !interp_as_expr_or_const_correct_prod_base by eassumption; cbv beta iota
                 | erewrite !interp_as_expr_or_const_correct_base by eassumption; cbv beta iota
                 | match goal with
                   | [ |- context[interpf _ ?e] ]
                     => erewrite !(@interp_as_expr_or_const_correct_base _ e) by eassumption; cbv beta iota
                   end
                 | progress unfold interp_op, lift_op
                 | progress Z.ltb_to_lt
                 | progress rewrite ?Z.land_0_l, ?Z.land_0_r, ?Z.lor_0_l, ?Z.lor_0_r ].
Qed.

Hint Rewrite @InterpSimplifyArith : reflective_interp.