-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubterms.v
More file actions
544 lines (468 loc) · 12.7 KB
/
Subterms.v
File metadata and controls
544 lines (468 loc) · 12.7 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
(* Time-stamp: <Thu 5/25/23 12:47 Dan Dougherty Subterms.v> *)
From Coq Require Import FunInd Nat Bool DecBool String List.
Import ListNotations.
From RC Require Export
Utilities
Sorts
Term
ListUtil
CpdtTactics
.
(* =========================================== *)
(** * Subterm Predicates: [subtermP] and friends *)
(** ** Immediate Subterm *)
Inductive imm_subtermP : Term -> Term -> Prop :=
imm_subtermP_pr1 : forall t1 t2 : Term,
imm_subtermP t1 (Pr t1 t2)
| imm_subtermP_pr2 : forall t1 t2 : Term,
imm_subtermP t2 (Pr t1 t2)
| imm_subtermP_en1 : forall t1 t2 : Term,
imm_subtermP t1 (En t1 t2)
| imm_subtermP_en2 : forall t1 t2 : Term,
imm_subtermP t2 (En t1 t2)
| imm_subtermP_hs : forall t1 : Term, imm_subtermP t1 (Hs t1).
#[export] Hint Constructors imm_subtermP : core.
(** TODO: [well_founded imm_subterm] *)
(* -- *)
(** ** STRICT subterm here *)
Inductive ssubtermP (x: Term) : Term -> Prop :=
| ssubtermP_dir (y:Term) : imm_subtermP x y -> ssubtermP x y
| ssubtermP_trans (y z:Term) : ssubtermP x y -> ssubtermP y z -> ssubtermP x z.
#[export] Hint Constructors ssubtermP : core.
(** TODO: [well_founded ssubterm] *)
(* -- *)
(** ** Strict-or-Equal. *)
Inductive subtermP (x: Term) : Term -> Prop :=
| subtermP_refl : subtermP x x
| subtermP_dir (y:Term) : imm_subtermP x y -> subtermP x y
| subtermP_trans (y z:Term) : subtermP x y -> subtermP y z -> subtermP x z.
#[export] Hint Constructors subtermP : core.
(* ------------------------------------------- *)
(** * Subterms as a List: [subterms] *)
Fixpoint subterms (t: Term) : set Term :=
(match t with
| Pr t1 t2 => t :: (subterms t1) ++ (subterms t2)
| En t1 t2 => t :: (subterms t1) ++ (subterms t2)
| Hs t1 => t :: (subterms t1)
| _ => [t]
end).
Lemma in_subterms t :
In t (subterms t).
Proof.
destruct t eqn:et;
simpl; auto.
Qed.
#[export] Hint Resolve in_subterms : core.
(** [subterm_closed] means closed under taking subtertms *)
Definition subterm_closed (u: set Term) :=
forall (x: Term), In x u -> subterms x <<= u.
(* @@ crush slow here *)
Lemma subterms_subterm_closed (t: Term) :
subterm_closed (subterms t).
Proof.
intros x H.
induction t; simpl; auto.
9:{ destruct H;
subst; auto;
apply in_app_or in H; destruct H; auto. }
9:{ destruct H;
subst; auto;
apply in_app_or in H; destruct H; auto. }
all: simpl in H; inv H; simpl; now auto.
Qed.
Corollary subterms_trans x y z :
In x (subterms y) ->
In y (subterms z) ->
In x (subterms z).
Proof.
intros H1 H2.
assert (a:= subterms_subterm_closed).
specialize (a z y H2); auto.
Qed.
Lemma subterms_dir_subterm x t :
imm_subtermP x t ->
In x (subterms t) .
Proof.
intros H. inv H; simpl.
- right. apply in_or_app. left. apply in_subterms.
- right. apply in_or_app. right. apply in_subterms.
- right. apply in_or_app. left. apply in_subterms.
- right. apply in_or_app. right. apply in_subterms.
- right. apply in_subterms.
Qed.
#[export] Hint Resolve subterms_dir_subterm : core.
(* ------------------------------------------- *)
(** * Lift [subterms] to subterms of a set : [Subterms] *)
Fixpoint Subterms (T: set Term) : set Term :=
match T with
[] => empty_set
| t :: rest => (subterms t) ++ (Subterms rest)
end.
(** Useful characterization of [Subterms] *)
Lemma Subterms_char (T: list Term) (x: Term) :
In x (Subterms T) <->
exists t, In t T /\ In x (subterms t).
Proof.
split.
- intros H.
induction T as [ |t0 rest IH].
+ inv H.
+ simpl in H. apply in_app_or in H.
destruct H as [H1 | H2].
-- exists t0. auto.
-- specialize (IH H2).
destruct IH as [t [H1 H3]].
exists t; auto.
- induction T as [ |t0 rest IH].
intros H.
destruct H as [t [H1 H2]]; inv H1.
intros H.
destruct H as [t [H1 H2]].
(* simpl in H1. *)
destruct H1; subst; simpl; auto.
apply in_or_app. right. apply IH.
now exists t.
Qed.
(* -------------------------------------------- *)
(** [Subterms] is inflationary *)
Lemma Subterms_incl (T: set Term) :
T <<= Subterms T.
Proof.
intros t H.
apply Subterms_char. exists t.
split. easy. apply in_subterms.
Qed.
#[export] Hint Resolve Subterms_incl : core.
(** [Subterms] is monotone *)
Lemma Subterms_monotone (xs ys : list Term) :
xs <<= ys ->
Subterms xs <<= Subterms ys.
Proof.
intros Hss t Hin.
rewrite Subterms_char in Hin.
destruct Hin as [t0 [H1 H2]].
assert (a: In t0 ys).
{ specialize (Hss t0 H1). easy. }
apply Subterms_char. now exists t0.
Qed.
#[export] Hint Resolve Subterms_monotone : core.
(* ----------------------------------- *)
(** ** Relate [Subterms] and [subterm_closed] *)
Lemma subterm_closed_Subterms_if (u : set Term) :
subterm_closed u -> Subterms u <<= u.
Proof.
intros H x Hin.
apply Subterms_char in Hin.
destruct Hin as [t [H1 H2]].
unfold subterm_closed in H.
specialize (H t H1). auto.
Qed.
#[export] Hint Resolve subterm_closed_Subterms_if : core.
Lemma subterm_closed_Subterms_then (T : set Term) :
Subterms T <<= T -> subterm_closed T .
Proof.
unfold subterm_closed.
intros H t H1.
intros x xInT.
enough (In x (Subterms T)).
- auto.
- apply Subterms_char.
now exists t.
Qed.
#[export] Hint Resolve subterm_closed_Subterms_then : core.
Lemma subterm_closed_Subterms_iff (T : set Term) :
Subterms T <<= T <-> subterm_closed T .
Proof.
split.
apply subterm_closed_Subterms_then.
apply subterm_closed_Subterms_if.
Qed.
#[export] Hint Rewrite subterm_closed_Subterms_iff : core.
(* ----------------------------------- *)
(** Important property : [Subterms] of a set is [subterm_closed] *)
Lemma Subterms_subterm_closed (T: set Term) :
subterm_closed (Subterms T).
Proof.
unfold subterm_closed. intros x H.
induction T as [ |t T' IH].
- inv H.
-
simpl in *.
apply in_app_or in H. destruct H.
+ apply incl_appl.
now apply subterms_subterm_closed.
+ apply incl_appr.
auto.
Qed.
#[export] Hint Resolve Subterms_subterm_closed : core.
(** [Subterms] is a toplogical closure operator *)
(** Note that this is [equi] not [eq] *)
Corollary Subterms_closure (T: list Term) :
Subterms (Subterms T) === Subterms T.
Proof.
assert (a: Subterms T <<= Subterms (Subterms T)).
{ apply Subterms_incl. }
assert (b: Subterms (Subterms T) <<= Subterms T).
{ apply subterm_closed_Subterms_if.
apply Subterms_subterm_closed. }
auto.
Qed.
#[export] Hint Resolve Subterms_closure : core.
Theorem subset_subterm_closed (w u : set Term):
subterm_closed u ->
w <<= u ->
Subterms w <<= u.
Proof.
intros H1 H2.
assert (a: Subterms w <<= Subterms u).
{ now apply Subterms_monotone. }
assert (b: Subterms u <<= u).
{ now apply subterm_closed_Subterms_iff. }
apply (incl_tran a b ).
Qed.
#[export] Hint Resolve subset_subterm_closed : core.
(** ** Relate [Subterms] and [subterms] *)
Lemma subterms_if_subtermP x t :
subtermP x t ->
In x (subterms t) .
Proof.
intros H. induction H.
- crush.
- inv H; crush.
- now apply subterms_trans with y.
Qed.
#[export] Hint Resolve subterms_if_subtermP : core.
Lemma if_subterms_subtermP x t :
In x (subterms t) ->
subtermP x t .
Proof.
intros H.
induction t; crush.
- apply in_app_or in H0; destruct H0.
+ crush.
eapply subtermP_trans with t1.
easy.
apply subtermP_dir. apply imm_subtermP_pr1.
+ crush.
eapply subtermP_trans with t2.
easy.
apply subtermP_dir. apply imm_subtermP_pr2.
- apply in_app_or in H0; destruct H0.
+ crush.
eapply subtermP_trans with t1.
easy.
apply subtermP_dir. apply imm_subtermP_en1.
+ crush.
eapply subtermP_trans with t2.
easy.
apply subtermP_dir. apply imm_subtermP_en2.
- crush.
eapply subtermP_trans with t.
easy.
apply subtermP_dir. apply imm_subtermP_hs.
Qed.
#[export] Hint Resolve if_subterms_subtermP : core.
Theorem subterms_subtermP x t :
subtermP x t <->
In x (subterms t) .
Proof.
split.
- intros H; now apply subterms_if_subtermP.
- intros H; now apply if_subterms_subtermP.
Qed.
#[export] Hint Rewrite subterms_subtermP : core.
(** If xs is a set of subterms of ys then the subterms of xs are all subterms of ys *)
Lemma subterms_subterms (xs ys : set Term) :
xs <<= Subterms ys ->
Subterms xs <<= Subterms ys.
Proof.
intros H.
apply Subterms_monotone in H.
assert (a:= (Subterms_closure ys) ).
eapply incl_tran with (Subterms (Subterms ys)).
easy. now unfold equi in a.
Qed.
#[export] Hint Rewrite subterms_subterms : core.
(* ============================== *)
(** * General facts about [subterms] *)
Lemma subterm_refl (t: Term) :
subtermP t t.
Proof.
apply subtermP_refl.
Qed.
#[export] Hint Resolve subterm_refl : core.
(* ============================== *)
(** * Domain-specific boilerplate about [subterms] *)
Lemma in_Subterms (ts: list Term) (t: Term) :
In t ts ->
In t (Subterms ts).
Proof.
intros H.
apply Subterms_char.
now exists t.
Qed.
#[export] Hint Resolve in_Subterms : core.
Lemma subterms_Subterms (ts: list Term) (t tsub: Term) :
In t ts ->
(subterms t) <<= (Subterms ts).
Proof.
intros H1 t0 H2 .
apply Subterms_char.
now exists t.
Qed.
#[export] Hint Resolve subterms_Subterms : core.
(* --------------------------- *)
Lemma subterms_pr1 t1 t2 :
In t1 (subterms (Pr t1 t2)).
Proof.
simpl. right.
apply in_or_app; auto.
Qed.
#[export] Hint Resolve subterms_pr1 : core.
Lemma pr1_subterms (t t1 t2 : Term):
In (Pr t1 t2) (subterms t) ->
In t1 (subterms t).
Proof.
intros H.
apply (subterms_trans) with (Pr t1 t2);
auto.
Qed.
#[export] Hint Resolve pr1_subterms : core.
Lemma pr1_Subterms (ts: list Term) (t1 t2 : Term) :
In (Pr t1 t2) (Subterms ts) ->
In t1 (Subterms ts).
Proof.
intros H.
apply Subterms_char in H.
apply Subterms_char.
destruct H as [t [H1 H2]].
apply pr1_subterms in H2.
now exists t.
Qed.
#[export] Hint Resolve pr1_Subterms : core.
(* ---------------------------- *)
Lemma subterms_pr2 t1 t2 :
In t2 (subterms (Pr t1 t2)).
Proof.
simpl. right.
apply in_or_app; auto.
Qed.
#[export] Hint Resolve subterms_pr2 : core.
Lemma pr2_subterms (t t1 t2 : Term):
In (Pr t1 t2) (subterms t) ->
In t2 (subterms t).
Proof.
intros H.
apply (subterms_trans) with (Pr t1 t2);
auto.
Qed.
#[export] Hint Resolve pr2_subterms : core.
Lemma pr2_Subterms (ts: list Term) (t1 t2 : Term):
In (Pr t1 t2) (Subterms ts) ->
In t2 (Subterms ts).
Proof.
intros H.
apply Subterms_char in H.
apply Subterms_char.
destruct H as [t [H1 H2]].
apply pr2_subterms in H2.
now exists t.
Qed.
#[export] Hint Resolve pr2_Subterms : core.
(* ---------------------------- *)
Lemma subterms_en1 t1 t2 :
In t1 (subterms (En t1 t2)).
Proof.
simpl. right.
apply in_or_app; auto.
Qed.
#[export] Hint Resolve subterms_en1 : core.
Lemma en1_subterms (t t1 t2 : Term):
In (En t1 t2) (subterms t) ->
In t1 (subterms t).
Proof.
intros H.
apply (subterms_trans) with (En t1 t2);
auto.
Qed.
#[export] Hint Resolve en1_subterms : core.
Lemma en1_Subterms (ts: list Term) (t1 t2 : Term):
In (En t1 t2) (Subterms ts) ->
In t1 (Subterms ts).
Proof.
intros H.
apply Subterms_char in H.
apply Subterms_char.
destruct H as [t [H1 H2]].
apply en1_subterms in H2.
now exists t.
Qed.
#[export] Hint Resolve en1_Subterms : core.
(* --------------------------- *)
Lemma subterms_en2 t1 t2 :
In t2 (subterms (En t1 t2)).
Proof.
simpl. right.
apply in_or_app; auto.
Qed.
#[export] Hint Resolve subterms_en2 : core.
Lemma en2_subterms (t t1 t2 : Term):
In (En t1 t2) (subterms t) ->
In t2 (subterms t).
Proof.
intros H.
apply (subterms_trans) with (En t1 t2);
auto.
Qed.
#[export] Hint Resolve en2_subterms : core.
Lemma en2_Subterms (ts: list Term) (t1 t2 : Term):
In (En t1 t2) (Subterms ts) ->
In t2 (Subterms ts).
Proof.
intros H.
apply Subterms_char in H.
apply Subterms_char.
destruct H as [t [H1 H2]].
apply en2_subterms in H2.
now exists t.
Qed.
#[export] Hint Resolve en2_Subterms : core.
(* --------------------------- *)
Lemma subterms_hs t1 :
In t1 (subterms (Hs t1)).
Proof.
simpl. right; auto.
Qed.
#[export] Hint Resolve subterms_hs : core.
Lemma hs_subterms (t t1 : Term):
In (Hs t1) (subterms t) ->
In t1 (subterms t).
Proof.
intros H.
apply (subterms_trans) with (Hs t1 );
auto.
Qed.
#[export] Hint Resolve hs_subterms : core.
Lemma hs_Subterms (ts: list Term) (t1 t2 : Term) :
In (Hs t1) (Subterms ts) ->
In t1 (Subterms ts).
Proof.
intros H.
apply Subterms_char in H.
apply Subterms_char.
destruct H as [t [H1 H2]].
apply hs_subterms in H2.
now exists t.
Qed.
#[export] Hint Resolve hs_Subterms : core.
(*
#[global]
Hint Resolve
subt_list_in
subt_list_pair1
subt_list_pair2
subt_list_en1
subt_list_en2
subt_list_hs
: core.
*)