-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTree_Calculus.v
355 lines (244 loc) · 9.98 KB
/
Tree_Calculus.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
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
(**********************************************************************)
(* Copyright 2020 Barry Jay *)
(* *)
(* Permission is hereby granted, free of charge, to any person *)
(* obtaining a copy of this software and associated documentation *)
(* files (the "Software"), to deal in the Software without *)
(* restriction, including without limitation the rights to use, copy, *)
(* modify, merge, publish, distribute, sublicense, and/or sell copies *)
(* of the Software, and to permit persons to whom the Software is *)
(* furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be *)
(* included in all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *)
(* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *)
(* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *)
(* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *)
(* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, *)
(* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *)
(* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(**********************************************************************)
(**********************************************************************)
(* Reflective Programs in Tree Calculus *)
(* Chapter 3: Tree Calculus *)
(* *)
(* Barry Jay *)
(* *)
(**********************************************************************)
Require Import String Lia.
Set Default Proof Using "Type".
Ltac caseEq f := generalize (refl_equal f); pattern f at -1; case f.
Ltac auto_t:= eauto with TreeHintDb.
Open Scope string_scope.
Declare Scope tree_scope.
Open Scope tree_scope.
(* Section 3.3 Tree Calculus *)
(* The type Tree0 supports inductive definitions *)
Inductive Tree0: Set :=
| Ref : string -> Tree0 (* variables are indexed by strings *)
| Node : Tree0
| App : Tree0 -> Tree0 -> Tree0
.
Hint Constructors Tree0 : TreeHintDb.
Notation "△" := Node : tree_scope.
Notation "x @ y" := (App x y) (at level 65, left associativity) : tree_scope.
Definition K := △ @ △.
Definition I := △ @ (△ @ △) @ (△ @ △).
Definition KI := K@I.
Definition D := △ @ (△ @ △) @ (△ @ △ @ △).
Definition d x := △ @ (△@x).
Definition Sop := d (K @ D)@(d K @(K@D)).
Ltac inv1 prop :=
match goal with
| H: prop (Ref _) |- _ => inversion H; clear H; inv1 prop
| H: prop (_ @ _) |- _ => inversion H; clear H; inv1 prop
| H: prop △ |- _ => inversion H; clear H; inv1 prop
| _ => auto
end.
(* Equational Theory *)
(* The type Tree is axiomatic, so no inductive definitions ... *)
Axiom Tree: Set.
Axiom Req : string -> Tree.
Axiom Noq : Tree.
Axiom Apq : Tree -> Tree -> Tree.
(* The two types are related by quotienting *)
Fixpoint quotient M :=
match M with
| Ref x => Req x
| Node => Noq
| M1 @ M2 => Apq (quotient M1) (quotient M2)
end.
Definition eq_q x y := quotient x = quotient y.
Notation "x === y" := (eq_q x y) (at level 85) : tree_scope.
(* ... but it does support axioms for equational reasoning *)
Axiom k_eq : forall y z, Apq (Apq (Apq Noq Noq) y) z = y.
Axiom s_eq : forall x y z, Apq (Apq (Apq Noq (Apq Noq x)) y) z = Apq (Apq y z) (Apq x z).
Axiom f_eq : forall w x y z, Apq (Apq (Apq Noq (Apq (Apq Noq w) x)) y) z = Apq (Apq z w) x.
Hint Resolve Req Noq Apq k_eq s_eq f_eq : TreeHintDb.
Ltac ktac := rewrite ? k_eq.
Ltac stac := rewrite ? k_eq.
Ltac ftac := rewrite ? k_eq.
Ltac tree_eq := intros; cbv; repeat (rewrite ? s_eq; rewrite ? k_eq; rewrite ? f_eq); auto.
Lemma quotient_node: quotient Node = Noq.
Proof. auto_t. Qed.
Lemma quotient_app: forall M1 M2, quotient (M1 @ M2) = Apq (quotient M1) (quotient M2).
Proof. auto_t. Qed.
Lemma eq_q_refl: forall x, x===x.
Proof. unfold eq_q; auto. Qed.
Lemma eq_q_sym: forall x y, x=== y -> y ===x.
Proof. unfold eq_q; auto. Qed.
Lemma eq_q_trans: forall x y z, x===y -> y===z -> x === z.
Proof. unfold eq_q; intros; eapply eq_trans; eauto. Qed.
Ltac f_equal_q := rewrite quotient_app; apply eq_sym; rewrite quotient_app; apply eq_sym; f_equal.
Ltac unquotient_tac := repeat rewrite <- quotient_node; repeat rewrite <- quotient_app.
Ltac qtac e :=
unfold eq_q; repeat ((rewrite e || auto_t) || rewrite quotient_app);
rewrite ? quotient_node;
repeat (rewrite ? s_eq || rewrite ? k_eq || rewrite ? f_eq); unquotient_tac; auto_t.
(* Section 3.4: Programs *)
Inductive program : Tree0 -> Prop :=
| pr_leaf : program △
| pr_stem : forall M, program M -> program (△ @ M)
| pr_fork : forall M N, program M -> program N -> program (△ @ M @ N)
.
Ltac program_tac := cbv; repeat (apply pr_fork || apply pr_stem || apply pr_leaf); auto.
(* Section 3.5: Propositional Logic *)
(* Booleans *)
Definition conjunction := d (K@KI).
Definition disjunction := d (K@K) @I.
Definition implies := d (K@K).
Definition negation := d (K@K) @ (d (K@KI) @ I).
Definition bi_implies := △@ (△@ I @ negation)@△.
Lemma conjunction_true : forall y, conjunction @ K @ y === y.
Proof. tree_eq. Qed.
(* Section 3.6: Pairs *)
Definition Pair := △ .
Definition first p := △@ p@ △@ K.
Definition second p := △ @p@ △@ KI.
Lemma first_pair : forall x y, (first (Pair @x@y)) === x.
Proof. tree_eq. Qed.
Lemma second_pair : forall x y, second (Pair @x@y) === y .
Proof. tree_eq. Qed.
(* 3.7: Natural Numbers *)
Definition zero := △.
Definition successor := K.
Definition isZero := d (K@ (K@ (K@ KI))) @ (d (K@ K) @ △).
Definition predecessor := d (K@ KI) @ (d (K@ △) @ △).
Lemma isZero_zero : isZero @ △ === K.
Proof. tree_eq. Qed.
Lemma isZero_successor : forall n, isZero @ (K@ n) === KI.
Proof. tree_eq. Qed.
(* Section 3.8: The Fundamental Queries *)
Definition query is0 is1 is2 :=
d (K@is1)
@(d (K@KI)
@(d (K@ (K@ (K@(K@(K@ is2)))))
@(d (K@(K@(K@ is0)))@△))).
Definition isLeaf := query K (K @ I) (K @ I).
Definition isStem := query (K @ I) K KI.
Definition isFork := query KI KI K.
Lemma query_eq_0:
forall is0 is1 is2, query is0 is1 is2 @ △ === is0.
Proof. tree_eq. Qed.
Lemma query_eq_1:
forall is0 is1 is2 P, query is0 is1 is2 @ (△ @ P) === is0 @ KI @ is1.
Proof. tree_eq. Qed.
Lemma query_eq_2:
forall is0 is1 is2 P Q, query is0 is1 is2 @ (△@ P@ Q) === is2.
Proof. tree_eq. Qed.
Ltac unfold_op := unfold isLeaf, isStem, isFork, query, Sop, D, KI, I, K, d, first, second.
Ltac eqtac := unfold_op; qtac s_eq; auto_t.
(*
repeat (unfold quotient; fold quotient; rewrite ? s_eq; rewrite ? k_eq; rewrite ? f_eq).
*)
Fixpoint term_size M :=
match M with
| App M1 M2 => term_size M1 + term_size M2
| _ => 1
end.
(* Exercises *)
(* 1 *)
Definition D_variant := Sop @ (K@(Sop @ Sop)) @ K.
Lemma d_variant_eval: forall x y z, D_variant @ x @ y @ z === y@ z @ (x @ z).
Proof. tree_eq. Qed.
(* 2 *)
Lemma conjunction_false: forall y, conjunction @ KI @ y === K@I.
Proof. tree_eq. Qed.
(* 3 *)
Lemma disjunction_true : forall y, disjunction @ K @y === K.
Proof. tree_eq. Qed.
Lemma disjunction_false: forall y, disjunction @ KI @y === y.
Proof. tree_eq. Qed.
Lemma implies_true : forall y, implies @ K @ y === y.
Proof. tree_eq. Qed.
Lemma implies_false: forall y, implies @ KI @ y === K.
Proof. tree_eq. Qed.
Lemma negation_true : negation @ K === KI.
Proof. tree_eq. Qed.
Lemma negation_false : negation @ (K@I) === K.
Proof. tree_eq. Qed.
Lemma iff_true : forall y, bi_implies @K@y === y.
Proof. tree_eq. Qed.
Lemma iff_false_true : bi_implies @ KI @K === K@I.
Proof. tree_eq. Qed.
Lemma iff_false_false : bi_implies @ KI @ KI === K.
Proof. tree_eq. Qed.
(* 4 *)
Definition first0 := d (K@K) @ (d (K@ △) @ △).
Definition second0 := d (K@(K@I)) @ (d (K@ △) @ △).
Lemma first0_first: forall p, first0 @ p === (first p).
Proof. tree_eq. Qed.
Lemma second0_second: forall p, second0 @ p === (second p).
Proof. tree_eq. Qed.
(* 5 *)
Lemma predecessor_zero: predecessor @ △ === △.
Proof. tree_eq. Qed.
Lemma predecessor_successor : forall n, predecessor @ (K@ n) === n.
Proof. tree_eq. Qed.
(* 6 *)
Definition successor_variant := △.
Definition isZero_variant := d (K @ KI) @ (d (K@K) @ △).
Definition predecessor_variant :=
d (K@K)
@ (d (K@ (K@(K@ △)))
@ (d (d (K@ △) @ I) @ (K@△))
).
Lemma predecessor_variant_zero: predecessor_variant @ △ === △.
Proof. tree_eq. Qed.
Lemma predecessor_successor_variant : forall n, predecessor_variant @ (successor_variant @ n) === n.
Proof. tree_eq. Qed.
Lemma isZero_variant_Zero : isZero_variant @ zero === K.
Proof. tree_eq. Qed.
Lemma isZero_variant_successor : forall n, isZero_variant @ (successor_variant @ n) === KI.
Proof. tree_eq. Qed.
(* 7 *)
Lemma isLeaf_node: isLeaf @ △ === K.
Proof. tree_eq. Qed.
Lemma isLeaf_K: isLeaf @ K === KI.
Proof. tree_eq. Qed.
Lemma isLeaf_Knode: isLeaf @ (K@△) === KI.
Proof. tree_eq. Qed.
(* 8 *)
Fixpoint maxdepth n :=
match n with
0 => 0
| S k => (1 + maxdepth k + maxdepth k * maxdepth k) (* count leaves, stems and forks *)
end.
(*
Compute (maxdepth 3 = 13).
Compute (maxdepth 4 = 183).
Compute (maxdepth 5 = 33673).
*)
Definition exactdepth n :=
match n with
| 0 => 0
| S k => (maxdepth (S k) - (maxdepth k))
end.
(*
Compute (exactdepth 3 = 10%Z).
Compute (exactdepth 4 = 170%Z).
Compute (exactdepth 5 = 33490%Z).
*)