-
Notifications
You must be signed in to change notification settings - Fork 2
/
bf_equivalence.v
188 lines (165 loc) · 4.78 KB
/
bf_equivalence.v
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
(* -*- eval: (set-input-method "TeX"); -*- *)
Require Import bf.
Require Import Lists.Streams.
Lemma step_None : forall c m,
step (c, m) = None <->
c = END.
Proof.
intros c m.
split.
intro H; destruct c; simpl in H; try discriminate H.
reflexivity.
intros; subst; reflexivity.
Qed.
Lemma step_Some : forall c m,
(exists config, step (c, m) = Some config) <->
c <> END.
Proof.
intros c m.
split.
intro H.
destruct H.
apply step_step_rel in H.
inversion H; intro Heq; discriminate Heq.
intro H.
destruct c; simpl; eauto.
destruct H; reflexivity.
Qed.
(* [EqState] is an equivalence relation between machine states. The
key difference from [_ = _] is that the [Stream] components of the two
[state]s are compared with [EqSt] (i.e. extensional equality). *)
Inductive EqState : state -> state -> Prop :=
| eqstate :
forall ls curr rs stdin stdout ls' curr' rs' stdin' stdout',
EqSt ls ls' ->
curr = curr' ->
EqSt rs rs' ->
EqSt stdin stdin' ->
stdout = stdout' ->
EqState state[ls, curr, rs, stdin, stdout]
state[ls', curr', rs', stdin', stdout'].
(* [state_reflexivity] can automatically solve the most trivial goals
of the form [EqState _ _], and will give suitable subgoals
otherwise. *)
Ltac state_reflexivity :=
simpl;
match goal with
| [ |- EqState state[?ls, ?curr, ?rs, ?stdin, ?stdout]
state[?ls', ?curr', ?rs', ?stdin', ?stdout'] ] =>
apply eqstate; auto using EqSt_reflex, eqst
end.
Notation "s ≡ₛ s'" := (EqState s s') (at level 70, no associativity) : stateeq_scope.
Delimit Scope stateeq_scope with stateeq.
Open Scope stateeq_scope.
Lemma EqState_refl : forall s, s ≡ₛ s.
Proof.
intro s; destruct s; state_reflexivity.
Qed.
Lemma EqState_trans : forall s s' s'',
s ≡ₛ s' ->
s' ≡ₛ s'' ->
s ≡ₛ s''.
Proof.
intros s s' s'' H H'.
destruct s as [ls curr rs stdin stdout];
destruct s' as [ls' curr' rs' stdin' stdout'];
destruct s'' as [ls'' curr'' rs'' stdin'' stdout''].
inversion H; inversion H'; subst.
state_reflexivity.
apply (@trans_EqSt _ ls ls' ls''); assumption.
apply (@trans_EqSt _ rs rs' rs''); assumption.
apply (@trans_EqSt _ stdin stdin' stdin''); assumption.
Qed.
Lemma EqState_sym {s s' : state} :
s ≡ₛ s' ->
s' ≡ₛ s.
Proof.
intro H.
inversion H; subst.
state_reflexivity; apply sym_EqSt; assumption.
Qed.
(* [EqBf] is an equivalence relation between configurations. It
requires the programs to be equal and the states to be equivalent
w.r.t. [EqState]. *)
Inductive EqBf :
Instr.instruction * state -> Instr.instruction * state -> Prop :=
| eqbf :
forall c s c' s',
c = c' ->
EqState s s' ->
EqBf (c, s) (c', s').
Ltac bf_reflexivity :=
simpl;
match goal with
| [ |- EqBf (?c, ?s) (?c', ?s') ] =>
apply eqbf; [
try reflexivity |
try state_reflexivity ]
end.
Notation "c ≡ c'" := (EqBf c c') (at level 70, no associativity) : bfeq_scope.
Delimit Scope bfeq_scope with bfeq.
Open Scope bfeq_scope.
Lemma EqBf_refl : forall config, config ≡ config.
Proof.
intro config; destruct config; bf_reflexivity; apply EqState_refl.
Qed.
Lemma EqBf_trans : forall config config' config'',
config ≡ config' ->
config' ≡ config'' ->
config ≡ config''.
Proof.
intros conf conf' conf'' H H'.
destruct conf as [c s];
destruct conf' as [c' s'];
destruct conf'' as [c'' s''].
inversion H; subst; inversion H'; subst.
bf_reflexivity.
apply (EqState_trans s s' s''); assumption.
Qed.
Lemma EqBf_sym {conf conf'} :
conf ≡ conf' ->
conf' ≡ conf.
Proof.
intros H.
inversion H; subst.
bf_reflexivity.
apply EqState_sym; assumption.
Qed.
Fixpoint n_unfolded_zeroes (n : nat) : Stream nat :=
match n with
| 0 => zeroes
| S n => Cons 0 (n_unfolded_zeroes n)
end.
Lemma about_zeroes : forall n,
init (n_unfolded_zeroes n) ≡ₛ init zeroes.
Proof.
induction n.
apply EqState_refl.
unfold init.
state_reflexivity.
apply eqst.
reflexivity.
inversion IHn; assumption.
Qed.
Lemma about_zeroes' : forall n c,
(c, init (n_unfolded_zeroes n)) ≡ (c, init zeroes).
Proof.
intros.
bf_reflexivity.
apply about_zeroes.
Qed.
Lemma very_simple_equivalence : forall c,
(c, init zeroes) ≡ (c, init zeroes).
Proof.
intros.
unfold init.
bf_reflexivity.
Qed.
Lemma EqBf_program :
forall c s c' s',
(c, s) ≡ (c', s') ->
c = c'.
Proof.
intros.
inversion H; assumption.
Qed.