Debian Codegraph @

src/Bedrock/Field/Translation/Proofs/ValidComputable/Cmd.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Bool.
3 From Coq Require Import List.
4 From Coq Require Import Lia.
5 Require Import (*hints*) Coq.btauto.Algebra.
6 Require Import bedrock2.Syntax.
7 Require Import coqutil.Word.Interface coqutil.Word.Properties.
8 Require Import coqutil.Map.Interface.
9 Require Import Crypto.Bedrock.Field.Common.Types.
10 Require Import Crypto.Bedrock.Field.Common.Tactics.
11 Require Import Crypto.Bedrock.Field.Translation.Proofs.Expr.
12 Require Import Crypto.Bedrock.Field.Translation.Proofs.Cmd.
13 Require Import Crypto.Bedrock.Field.Translation.Proofs.ValidComputable.Expr.
14 Require Import Crypto.Language.API.
15 Require Import Crypto.Util.Decidable.
16 Require Import Crypto.Util.ZRange.
17 Require Import Crypto.Util.Tactics.BreakMatch.
18 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
19 Require Import Crypto.Util.Bool.Reflect.
20 Local Open Scope Z_scope.
21 Local Open Scope bool_scope.
22
23 Import API.Compilers.
24 Import Types.Notations.
25
26 Section Cmd.
27 Context
28 {width BW word mem locals ext_spec varname_gen error}
29 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
30 Context {ok : ok}.
31
32 Local Existing Instance Types.rep.Z.
33 Local Existing Instance Types.rep.listZ_local.
34
35 Definition is_nil_ident {t} (i : ident.ident t) : bool :=
36 match i with
37 | ident.nil base_Z => true
38 | _ => false
39 end.
40
41 Definition is_cons_ident {t} (i : ident.ident t) : bool :=
42 match i with
43 | ident.cons base_Z => true
44 | _ => false
45 end.
46 Definition valid_cons_App2_bool {t} (f : @API.expr (fun _ => unit) t) : bool :=
47 match f with
48 | expr.Ident
49 (type.arrow type_Z (type.arrow type_listZ type_listZ)) i =>
50 is_cons_ident i
51 | _ => false
52 end.
53 Definition valid_cons_App1_bool {t} (f : @API.expr (fun _ => unit) t) : bool :=
54 match f with
55 | expr.App type_Z (type.arrow type_listZ type_listZ) f x =>
56 valid_cons_App2_bool f && valid_expr_bool true x
57 | _ => false
58 end.
59
60 Definition valid_expr_bool_if_base
61 {t} : @API.expr (fun _ => unit) t -> bool :=
62 match t as t0 return API.expr t0 -> bool with
63 | type.base _ => valid_expr_bool true
64 | _ => fun _ => false
65 end.
66
67 Fixpoint valid_cmd_bool
68 {t} (e : @API.expr (fun _ => unit) t) : bool :=
69 if valid_expr_bool_if_base e
70 then true
71 else
72 match e return bool with
73 | expr.LetIn
74 (type.base (base.type.prod
75 (base.type.type_base a)
76 (base.type.type_base b)))
77 (type.base c) x f =>
78 valid_cmd_bool (f tt) && valid_expr_bool true x
79 | expr.LetIn
80 (type.base (base.type.type_base a))
81 (type.base b) x f =>
82 valid_cmd_bool (f tt) && valid_expr_bool true x
83 | expr.App (type.base s) _ f x =>
84 (valid_cons_App1_bool f && valid_cmd_bool x)
85 | expr.Ident _ i => is_nil_ident i
86 | _ => false
87 end.
88
89 Lemma valid_cmd_valid_expr {t} e :
90 valid_expr_bool_if_base e = true ->
91 valid_cmd (t:=t) e.
92 Proof.
93 cbv [valid_expr_bool_if_base].
94 break_match; try congruence; [ ].
95 intros;
96 match goal with
97 H : valid_expr_bool _ _ = true |- _ =>
98 apply valid_expr_bool_iff in H
99 end.
100 constructor; eauto.
101 Qed.
102
103 Lemma is_nil_ident_valid {t} i :
104 @is_nil_ident t i = true ->
105 valid_cmd (expr.Ident i).
106 Proof.
107 destruct i; cbn [is_nil_ident]; try congruence; [ ].
108 break_match; try congruence; [ ].
109 intros; constructor.
110 Qed.
111
112 Lemma is_cons_ident_eq {t} i :
113 @is_cons_ident t i = true ->
114 (match t as t0 return ident.ident t0 -> Prop with
115 | type.arrow type_Z (type.arrow type_listZ type_listZ) =>
116 fun i => i = ident.cons (t:=base_Z)
117 | _ => fun _ => False
118 end) i.
119 Proof.
120 cbv [is_cons_ident]; break_match; congruence.
121 Qed.
122
123 Lemma valid_cons_App2_bool_impl1 {t} (f : API.expr t) :
124 valid_cons_App2_bool f = true ->
125 (match t as t0 return API.expr t0 -> Prop with
126 | type.arrow _ (type.arrow _ _) =>
127 fun f =>
128 forall x l,
129 valid_expr true x ->
130 valid_cmd l ->
131 valid_cmd (expr.App (expr.App f x) l)
132 | _ => fun _ => False
133 end) f.
134 Proof.
135 remember t.
136 cbv [valid_cons_App2_bool];
137 break_match; try congruence; [ ].
138 intros;
139 repeat match goal with
140 | H : is_cons_ident _ = true |- _ =>
141 apply is_cons_ident_eq in H; subst
142 end.
143 constructor; eauto.
144 Qed.
145
146 Lemma valid_cons_App1_bool_impl1 {t} (f : API.expr t) :
147 valid_cons_App1_bool f = true ->
148 (match t as t0 return API.expr t0 -> Prop with
149 | type.arrow _ _ =>
150 fun f =>
151 forall l,
152 valid_cmd l ->
153 valid_cmd (expr.App f l)
154 | _ => fun _ => False
155 end) f.
156 Proof.
157 remember t.
158 cbv [valid_cons_App1_bool];
159 break_match; try congruence; [ ].
160 intros;
161 repeat match goal with
162 | H : _ && _ = true |- _ =>
163 apply Bool.andb_true_iff in H; destruct H
164 | H : valid_expr_bool _ _ = true |- _ =>
165 apply valid_expr_bool_iff in H
166 | H : valid_cons_App2_bool _ = true |- _ =>
167 apply valid_cons_App2_bool_impl1 in H; apply H;
168 solve [eauto]
169 end.
170 Qed.
171
172 Lemma valid_cmd_bool_impl1 {t} e :
173 @valid_cmd_bool t e = true -> valid_cmd e.
174 Proof.
175 induction e; cbn [valid_cmd_bool]; break_match; intros;
176 repeat match goal with
177 | H : valid_expr_bool_if_base _ = true |- _ =>
178 apply valid_cmd_valid_expr in H; apply H
179 | H : valid_expr_bool _ _ = true |- _ =>
180 apply valid_expr_bool_iff in H
181 | H : is_nil_ident _ = true |- _ =>
182 apply is_nil_ident_valid in H; apply H
183 | H : _ && _ = true |- _ =>
184 apply Bool.andb_true_iff in H; destruct H
185 | H : false = true |- _ =>
186 congruence
187 | H : valid_cons_App1_bool _ = true |- _ =>
188 apply valid_cons_App1_bool_impl1 in H; apply H;
189 solve [eauto]
190 | _ => constructor; solve [eauto]
191 end.
192 Qed.
193
194 Lemma valid_cmd_bool_valid_expr {t} e :
195 valid_expr_bool_if_base (t:=t) e = true ->
196 valid_cmd_bool e = true.
197 Proof.
198 destruct e; cbn [valid_cmd_bool]; cbv [valid_expr_bool_if_base];
199 break_match; congruence.
200 Qed.
201
202 Lemma valid_cmd_bool_impl2 {t} e :
203 valid_cmd e -> @valid_cmd_bool t e = true.
204 Proof.
205 induction 1; intros; subst; cbn;
206 repeat match goal with
207 | H : valid_expr true _ |- _ =>
208 apply valid_expr_bool_iff in H
209 end;
210 auto using Bool.andb_true_iff; [ ].
211 { apply valid_cmd_bool_valid_expr.
212 assumption. }
213 Qed.
214
215 Lemma valid_cmd_bool_iff {t} e :
216 @valid_cmd_bool t e = true <-> valid_cmd e.
217 Proof.
218 split;
219 auto using valid_cmd_bool_impl1, valid_cmd_bool_impl2.
220 Qed.
221 End Cmd.