Debian Codegraph @

src/Util/PartiallyReifiedProp.v

raw
1 (** * Propositions with a distinguished representation of [True], [False], [and], [or], and [impl] *)
2 (** This allows for something between [bool] and [Prop], where we can
3 computationally reduce things like [True /\ True], but can still
4 express equality of types. *)
5 From Coq Require Import Setoid.
6 From Coq.Program Require Import Tactics.
7 Require Import Crypto.Util.Notations.
8 Require Import Crypto.Util.Tactics.DestructHead.
9 Require Import Crypto.Util.Tactics.DestructHyps.
10 Require Import Crypto.Util.Tactics.BreakMatch.
11
12 Declare Scope reified_prop_scope.
13 Delimit Scope reified_prop_scope with reified_prop.
14 Inductive reified_Prop := rTrue | rFalse | rAnd (x y : reified_Prop) | rOr (x y : reified_Prop) | rImpl (x y : reified_Prop) | rForall {T} (f : T -> reified_Prop) | rEq {T} (x y : T) | inject (_ : Prop).
15 Bind Scope reified_prop_scope with reified_Prop.
16
17 Fixpoint to_prop (x : reified_Prop) : Prop
18 := match x with
19 | rTrue => True
20 | rFalse => False
21 | rAnd x y => to_prop x /\ to_prop y
22 | rOr x y => to_prop x \/ to_prop y
23 | rImpl x y => to_prop x -> to_prop y
24 | @rForall _ f => forall x, to_prop (f x)
25 | @rEq _ x y => x = y
26 | inject x => x
27 end.
28
29 Coercion reified_Prop_of_bool (x : bool) : reified_Prop
30 := if x then rTrue else rFalse.
31
32 Definition and_reified_Prop (x y : reified_Prop) : reified_Prop
33 := match x, y with
34 | rTrue, x
35 | x, rTrue
36 => x
37 | rFalse, _
38 | _, rFalse
39 => rFalse
40 | rEq T a b, rEq T' a' b' => rEq (a, a') (b, b')
41 | x', y' => rAnd x' y'
42 end.
43 Definition or_reified_Prop (x y : reified_Prop) : reified_Prop
44 := match x, y with
45 | rTrue, _
46 | _, rTrue
47 => rTrue
48 | rFalse, x
49 | x, rFalse
50 => x
51 | x', y' => rOr x' y'
52 end.
53 Definition impl_reified_Prop (x y : reified_Prop) : reified_Prop
54 := match x, y with
55 | rTrue, y => y
56 | _, rTrue => rTrue
57 | rFalse, _ => rTrue
58 | rImpl x rFalse, rFalse => x
59 | x', y' => rImpl x' y'
60 end.
61
62 Infix "/\" := and_reified_Prop : reified_prop_scope.
63 Infix "\/" := or_reified_Prop : reified_prop_scope.
64 Infix "->" := impl_reified_Prop : reified_prop_scope.
65 Infix "=" := rEq : reified_prop_scope.
66 Notation "~ P" := (P -> rFalse)%reified_prop : reified_prop_scope.
67 Notation "∀ x .. y , P" := (rForall (fun x => .. (rForall (fun y => P%reified_prop)) .. ))
68 (at level 200, x binder, y binder, right associativity) : reified_prop_scope.
69
70 Definition reified_Prop_eq (x y : reified_Prop)
71 := match x, y with
72 | rTrue, _ => y = rTrue
73 | rFalse, _ => y = rFalse
74 | rAnd x0 x1, rAnd y0 y1
75 => x0 = y0 /\ x1 = y1
76 | rAnd _ _, _ => False
77 | rOr x0 x1, rOr y0 y1
78 => x0 = y0 /\ x1 = y1
79 | rOr _ _, _ => False
80 | rImpl x0 x1, rImpl y0 y1
81 => x0 = y0 /\ x1 = y1
82 | rImpl _ _, _ => False
83 | @rForall Tx fx, @rForall Ty fy
84 => exists pf : Tx = Ty,
85 forall x, fx x = fy (eq_rect _ (fun t => t) x _ pf)
86 | rForall _ _, _ => False
87 | @rEq Tx x0 x1, @rEq Ty y0 y1
88 => exists pf : Tx = Ty,
89 eq_rect _ (fun t => t) x0 _ pf = y0
90 /\ eq_rect _ (fun t => t) x1 _ pf = y1
91 | rEq _ _ _, _ => False
92 | inject x, inject y => x = y
93 | inject _, _ => False
94 end.
95
96 Section rel.
97 Local Ltac t :=
98 cbv;
99 repeat (match goal with |- forall x, _ => intro end (* work around broken Ltac [match] in 8.4 that diverges on things under binders *)
100 || break_match
101 || break_match_hyps
102 || intro
103 || (simpl in * )
104 || intuition try congruence
105 || (exists eq_refl)
106 || eauto
107 || (subst * )
108 || apply conj
109 || destruct_head' ex
110 || solve [ apply reflexivity
111 | apply symmetry; eassumption
112 | eapply transitivity; eassumption ] ).
113
114 Global Instance Reflexive_reified_Prop_eq : Reflexive reified_Prop_eq.
115 Proof. t. Qed.
116 Global Instance Symmetric_reified_Prop_eq : Symmetric reified_Prop_eq.
117 Proof. t. Qed.
118 Global Instance Transitive_reified_Prop_eq : Transitive reified_Prop_eq.
119 Proof. t. Qed.
120 Global Instance Equivalence_reified_Prop_eq : Equivalence reified_Prop_eq.
121 Proof. split; exact _. Qed.
122 End rel.
123
124 Definition reified_Prop_leq_to_eq (x y : reified_Prop) : x = y -> reified_Prop_eq x y.
125 Proof. intro; subst; simpl; reflexivity. Qed.
126
127 Ltac inversion_reified_Prop_step :=
128 let do_on H := apply reified_Prop_leq_to_eq in H; unfold reified_Prop_eq in H in
129 match goal with
130 | [ H : False |- _ ] => solve [ destruct H ]
131 | [ H : (_ = _ :> reified_Prop) /\ (_ = _ :> reified_Prop) |- _ ] => destruct H
132 | [ H : ?x = ?x :> reified_Prop |- _ ] => clear H
133 | [ H : exists pf : _ = _ :> Type, forall x, _ = _ :> reified_Prop |- _ ]
134 => destruct H as [? H]; subst; simpl @eq_rect in H
135 | [ H : ?x = _ :> reified_Prop |- _ ] => is_var x; subst x
136 | [ H : _ = ?y :> reified_Prop |- _ ] => is_var y; subst y
137 | [ H : rTrue = rFalse |- _ ] => solve [ inversion H ]
138 | [ H : rFalse = rTrue |- _ ] => solve [ inversion H ]
139 | [ H : rTrue = _ |- _ ] => do_on H; progress subst
140 | [ H : rFalse = _ |- _ ] => do_on H; progress subst
141 | [ H : rAnd _ _ = _ |- _ ] => do_on H
142 | [ H : rOr _ _ = _ |- _ ] => do_on H
143 | [ H : rImpl _ _ = _ |- _ ] => do_on H
144 | [ H : rForall _ = _ |- _ ] => do_on H
145 | [ H : rEq _ _ = _ |- _ ] => do_on H
146 | [ H : inject _ = _ |- _ ] => do_on H
147 end.
148 Ltac inversion_reified_Prop := repeat inversion_reified_Prop_step.
149
150 Lemma to_prop_and_reified_Prop x y : to_prop (x /\ y) <-> (to_prop x /\ to_prop y).
151 Proof.
152 destruct x, y; simpl; try tauto.
153 { split; intro H; inversion H; subst; repeat split. }
154 Qed.
155
156 (** Remove all possibly false terms in a reified prop *)
157 Fixpoint trueify (p : reified_Prop) : reified_Prop
158 := match p with
159 | rTrue => rTrue
160 | rFalse => rTrue
161 | rAnd x y => rAnd (trueify x) (trueify y)
162 | rOr x y => rOr (trueify x) (trueify y)
163 | rImpl x y => rImpl x (trueify y)
164 | rForall T f => rForall (fun x => trueify (f x))
165 | rEq T x y => rEq x x
166 | inject x => inject True
167 end.
168
169 Lemma trueify_true : forall p, to_prop (trueify p).
170 Proof.
171 induction p; simpl; auto.
172 Qed.