author | wenzelm |
Sat, 26 Jun 2021 20:55:43 +0200 | |
changeset 73884 | 0a12ca4f3e8d |
parent 71469 | d7ef73df3d15 |
permissions | -rw-r--r-- |
71263 | 1 |
section \<open>Terminated Lazy Lists as Quotients of Lazy Lists\<close> |
2 |
||
3 |
text \<open> |
|
4 |
This file demonstrates the lift_bnf utility for quotients on the example of lazy lists. |
|
5 |
||
6 |
See the Coinductive AFP entry for a much more extensive library |
|
7 |
of (terminated) lazy lists. |
|
8 |
\<close> |
|
9 |
||
10 |
theory TLList |
|
11 |
imports Main |
|
12 |
begin |
|
13 |
||
14 |
codatatype (lset: 'a) llist = LNil | LCons 'a "'a llist" |
|
15 |
for map: lmap rel: lrel |
|
16 |
||
17 |
inductive lfinite where |
|
18 |
LNil: "lfinite LNil" |
|
19 |
| LCons: "\<And>x xs. lfinite xs \<Longrightarrow> lfinite (LCons x xs)" |
|
20 |
||
21 |
lemma lfinite_lmapI: "lfinite xs \<Longrightarrow> lfinite (lmap f xs)" |
|
22 |
by (induct xs rule: lfinite.induct) (auto intro: lfinite.intros) |
|
23 |
||
24 |
lemma lfinite_lmapD: "lfinite (lmap f xs) \<Longrightarrow> lfinite xs" |
|
25 |
proof (induct "lmap f xs" arbitrary: xs rule: lfinite.induct) |
|
26 |
case LNil |
|
27 |
then show ?case |
|
28 |
by (cases xs) (auto intro: lfinite.intros) |
|
29 |
next |
|
30 |
case (LCons y ys) |
|
31 |
then show ?case |
|
32 |
by (cases xs) (auto intro: lfinite.intros) |
|
33 |
qed |
|
34 |
||
35 |
lemma lfinite_lmap[simp]: "lfinite (lmap f xs) = lfinite xs" |
|
36 |
by (metis lfinite_lmapI lfinite_lmapD) |
|
37 |
||
38 |
lemma lfinite_lrel: "lfinite xs \<Longrightarrow> lrel R xs ys \<Longrightarrow> lfinite ys" |
|
39 |
proof (induct xs arbitrary: ys rule: lfinite.induct) |
|
40 |
case LNil |
|
41 |
then show ?case |
|
42 |
by (cases ys) (auto intro: lfinite.intros) |
|
43 |
next |
|
44 |
case (LCons x xs) |
|
45 |
then show ?case |
|
46 |
by (cases ys) (auto intro: lfinite.intros) |
|
47 |
qed |
|
48 |
||
49 |
lemma lfinite_lrel': "lfinite ys \<Longrightarrow> lrel R xs ys \<Longrightarrow> lfinite xs" |
|
50 |
using lfinite_lrel llist.rel_flip by blast |
|
51 |
||
52 |
lemma lfinite_lrel_eq: |
|
53 |
"lrel R xs ys \<Longrightarrow> lfinite xs = lfinite ys" |
|
54 |
using lfinite_lrel lfinite_lrel' by blast+ |
|
55 |
||
56 |
definition eq_tllist where |
|
57 |
"eq_tllist p q = (fst p = fst q \<and> (if lfinite (fst p) then snd p = snd q else True))" |
|
58 |
||
59 |
quotient_type ('a, 'b) tllist = "'a llist \<times> 'b" / eq_tllist |
|
60 |
apply (rule equivpI) |
|
61 |
apply (rule reflpI; auto simp: eq_tllist_def) |
|
62 |
apply (rule sympI; auto simp: eq_tllist_def) |
|
63 |
apply (rule transpI; auto simp: eq_tllist_def) |
|
64 |
done |
|
65 |
||
66 |
primcorec lconst where |
|
67 |
"lconst a = LCons a (lconst a)" |
|
68 |
||
69 |
lemma lfinite_lconst[simp]: "\<not> lfinite (lconst a)" |
|
70 |
proof |
|
71 |
assume "lfinite (lconst a)" |
|
72 |
then show "False" |
|
73 |
apply (induct "lconst a" rule: lfinite.induct) |
|
74 |
subgoal by (subst (asm) lconst.code) auto |
|
75 |
subgoal by (subst (asm) (2) lconst.code) auto |
|
76 |
done |
|
77 |
qed |
|
78 |
||
79 |
lemma lset_lconst: "x \<in> lset (lconst b) \<Longrightarrow> x = b" |
|
80 |
apply (induct x "lconst b" arbitrary: b rule: llist.set_induct) |
|
81 |
subgoal by (subst (asm) lconst.code) auto |
|
82 |
subgoal by (subst (asm) (2) lconst.code) auto |
|
83 |
done |
|
84 |
||
85 |
lift_bnf (tlset1: 'a, tlset2: 'b) tllist |
|
71469
d7ef73df3d15
lift BNF witnesses for quotients (unless better ones are specified by the user)
traytel
parents:
71354
diff
changeset
|
86 |
[wits: "\<lambda>a. (lconst a, undefined)" ] |
71263 | 87 |
for map: tlmap rel: tlrel |
88 |
subgoal for P Q P' Q' |
|
89 |
by (force simp: eq_tllist_def relcompp_apply llist.rel_compp lfinite_lrel_eq split: if_splits) |
|
90 |
subgoal for Ss |
|
91 |
by (auto simp: eq_tllist_def) |
|
92 |
subgoal for Ss |
|
93 |
apply (auto 0 0 simp: eq_tllist_def) |
|
94 |
by metis |
|
95 |
subgoal for x b |
|
96 |
by (auto simp: eq_tllist_def llist.set_map dest: lset_lconst split: if_splits) |
|
97 |
subgoal for x b |
|
98 |
by (auto simp: eq_tllist_def sum_set_defs split: if_splits sum.splits) |
|
99 |
done |
|
100 |
||
101 |
lift_definition TLNil :: "'b \<Rightarrow> ('a, 'b) tllist" is "\<lambda>b. (LNil, b)" . |
|
102 |
lift_definition TLCons :: "'a \<Rightarrow> ('a, 'b) tllist \<Rightarrow> ('a, 'b) tllist" is "\<lambda>x (xs, b). (LCons x xs, b)" |
|
103 |
by (auto simp: eq_tllist_def split: if_splits elim: lfinite.cases) |
|
104 |
||
105 |
lemma lfinite_LCons: "lfinite (LCons x xs) = lfinite xs" |
|
106 |
using lfinite.simps by auto |
|
107 |
||
108 |
lemmas lfinite_simps[simp] = lfinite.LNil lfinite_LCons |
|
109 |
||
110 |
lemma tlset_TLNil: "tlset1 (TLNil b) = {}" "tlset2 (TLNil b) = {b}" |
|
111 |
by (transfer; auto simp: eq_tllist_def split: if_splits)+ |
|
112 |
||
113 |
lemma tlset_TLCons: "tlset1 (TLCons x xs) = {x} \<union> tlset1 xs" "tlset2 (TLCons x xs) = tlset2 xs" |
|
114 |
by (transfer; auto simp: eq_tllist_def split: if_splits)+ |
|
115 |
||
116 |
lift_definition tlfinite :: "('a, 'b) tllist \<Rightarrow> bool" is "\<lambda>(xs, _). lfinite xs" |
|
117 |
by (auto simp: eq_tllist_def) |
|
118 |
||
119 |
lemma tlfinite_tlset2: "tlfinite xs \<Longrightarrow> tlset2 xs \<noteq> {}" |
|
120 |
apply (transfer, safe) |
|
121 |
subgoal for xs b |
|
122 |
by (induct xs rule: lfinite.induct) (auto simp: eq_tllist_def setr.simps) |
|
123 |
done |
|
124 |
||
125 |
lemma tlfinite_tlset2': "b \<in> tlset2 xs \<Longrightarrow> tlfinite xs" |
|
126 |
by (transfer fixing: b, auto simp: eq_tllist_def setr.simps split: if_splits) |
|
127 |
||
128 |
lemma "\<not> tlfinite xs \<Longrightarrow> tlset2 xs = {}" |
|
129 |
by (meson equals0I tlfinite_tlset2') |
|
130 |
||
131 |
end |