author | wenzelm |
Wed, 28 Aug 2024 22:54:45 +0200 | |
changeset 80786 | 70076ba563d2 |
parent 80768 | c7723cc15de8 |
child 80914 | d97fdabd9e2b |
permissions | -rw-r--r-- |
68639 | 1 |
(* Author: Andreas Lochbihler, Digital Asset *) |
2 |
||
3 |
theory Code_Lazy_Demo imports |
|
4 |
"HOL-Library.Code_Lazy" |
|
5 |
"HOL-Library.Debug" |
|
6 |
"HOL-Library.RBT_Impl" |
|
7 |
begin |
|
8 |
||
69597 | 9 |
text \<open>This theory demonstrates the use of the \<^theory>\<open>HOL-Library.Code_Lazy\<close> theory.\<close> |
68639 | 10 |
|
11 |
section \<open>Streams\<close> |
|
12 |
||
13 |
text \<open>Lazy evaluation for streams\<close> |
|
14 |
||
15 |
codatatype 'a stream = |
|
16 |
SCons (shd: 'a) (stl: "'a stream") (infixr "##" 65) |
|
17 |
||
18 |
primcorec up :: "nat \<Rightarrow> nat stream" where |
|
19 |
"up n = n ## up (n + 1)" |
|
20 |
||
21 |
primrec stake :: "nat \<Rightarrow> 'a stream \<Rightarrow> 'a list" where |
|
22 |
"stake 0 xs = []" |
|
23 |
| "stake (Suc n) xs = shd xs # stake n (stl xs)" |
|
24 |
||
25 |
code_thms up stake \<comment> \<open>The original code equations\<close> |
|
26 |
||
27 |
code_lazy_type stream |
|
28 |
||
29 |
code_thms up stake \<comment> \<open>The lazified code equations\<close> |
|
30 |
||
31 |
value "stake 5 (up 3)" |
|
32 |
||
33 |
||
34 |
section \<open>Finite lazy lists\<close> |
|
35 |
||
36 |
text \<open>Lazy types need not be infinite. We can also have lazy types that are finite.\<close> |
|
37 |
||
38 |
datatype 'a llist |
|
39 |
= LNil ("\<^bold>\<lbrakk>\<^bold>\<rbrakk>") |
|
40 |
| LCons (lhd: 'a) (ltl: "'a llist") (infixr "###" 65) |
|
41 |
||
80786
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
42 |
nonterminal llist_args |
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
43 |
syntax |
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
44 |
"" :: "'a \<Rightarrow> llist_args" ("_") |
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
45 |
"_llist_args" :: "'a \<Rightarrow> llist_args \<Rightarrow> llist_args" ("_,/ _") |
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
46 |
"_llist" :: "llist_args => 'a list" ("\<^bold>\<lbrakk>(_)\<^bold>\<rbrakk>") |
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
47 |
syntax_consts |
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
48 |
"_llist_args" "_llist" == LCons |
68639 | 49 |
translations |
50 |
"\<^bold>\<lbrakk>x, xs\<^bold>\<rbrakk>" == "x###\<^bold>\<lbrakk>xs\<^bold>\<rbrakk>" |
|
51 |
"\<^bold>\<lbrakk>x\<^bold>\<rbrakk>" == "x###\<^bold>\<lbrakk>\<^bold>\<rbrakk>" |
|
52 |
||
53 |
fun lnth :: "nat \<Rightarrow> 'a llist \<Rightarrow> 'a" where |
|
54 |
"lnth 0 (x ### xs) = x" |
|
55 |
| "lnth (Suc n) (x ### xs) = lnth n xs" |
|
56 |
||
57 |
definition llist :: "nat llist" where |
|
58 |
"llist = \<^bold>\<lbrakk>1, 2, 3, hd [], 4\<^bold>\<rbrakk>" |
|
59 |
||
60 |
code_lazy_type llist |
|
61 |
||
62 |
value [code] "llist" |
|
63 |
value [code] "lnth 2 llist" |
|
64 |
value [code] "let x = lnth 2 llist in (x, llist)" |
|
65 |
||
66 |
fun lfilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
|
67 |
"lfilter P \<^bold>\<lbrakk>\<^bold>\<rbrakk> = \<^bold>\<lbrakk>\<^bold>\<rbrakk>" |
|
68 |
| "lfilter P (x ### xs) = |
|
69 |
(if P x then x ### lfilter P xs else lfilter P xs)" |
|
70 |
||
70009
435fb018e8ee
"export_code ... file_prefix ..." is the preferred way to produce output within the logical file-system within the theory context, as well as session exports;
wenzelm
parents:
69597
diff
changeset
|
71 |
export_code lfilter in SML file_prefix lfilter |
68639 | 72 |
|
73 |
value [code] "lfilter odd llist" |
|
74 |
||
75 |
value [code] "lhd (lfilter odd llist)" |
|
76 |
||
77 |
||
78 |
section \<open>Iterator for red-black trees\<close> |
|
79 |
||
80 |
text \<open>Thanks to laziness, we do not need to program a complicated iterator for a tree. |
|
81 |
A conversion function to lazy lists is enough.\<close> |
|
82 |
||
83 |
primrec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" |
|
84 |
(infixr "@@" 65) where |
|
85 |
"\<^bold>\<lbrakk>\<^bold>\<rbrakk> @@ ys = ys" |
|
86 |
| "(x ### xs) @@ ys = x ### (xs @@ ys)" |
|
87 |
||
88 |
primrec rbt_iterator :: "('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) llist" where |
|
89 |
"rbt_iterator rbt.Empty = \<^bold>\<lbrakk>\<^bold>\<rbrakk>" |
|
90 |
| "rbt_iterator (Branch _ l k v r) = |
|
91 |
(let _ = Debug.flush (STR ''tick'') in |
|
92 |
rbt_iterator l @@ (k, v) ### rbt_iterator r)" |
|
93 |
||
94 |
definition tree :: "(nat, unit) rbt" |
|
95 |
where "tree = fold (\<lambda>k. rbt_insert k ()) [0..<100] rbt.Empty" |
|
96 |
||
97 |
definition find_min :: "('a :: linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) option" where |
|
98 |
"find_min rbt = |
|
99 |
(case rbt_iterator rbt of \<^bold>\<lbrakk>\<^bold>\<rbrakk> \<Rightarrow> None |
|
100 |
| kv ### _ \<Rightarrow> Some kv)" |
|
101 |
||
69597 | 102 |
value "find_min tree" \<comment> \<open>Observe that \<^const>\<open>rbt_iterator\<close> is evaluated only for going down |
68639 | 103 |
to the first leaf, not for the whole tree (as seen by the ticks).\<close> |
104 |
||
105 |
text \<open>With strict lists, the whole tree is converted into a list.\<close> |
|
106 |
||
107 |
deactivate_lazy_type llist |
|
108 |
value "find_min tree" |
|
109 |
activate_lazy_type llist |
|
110 |
||
111 |
||
112 |
||
113 |
section \<open>Branching datatypes\<close> |
|
114 |
||
115 |
datatype tree |
|
116 |
= L ("\<spadesuit>") |
|
117 |
| Node tree tree (infix "\<triangle>" 900) |
|
118 |
||
119 |
notation (output) Node ("\<triangle>(//\<^bold>l: _//\<^bold>r: _)") |
|
120 |
||
121 |
code_lazy_type tree |
|
122 |
||
123 |
fun mk_tree :: "nat \<Rightarrow> tree" where mk_tree_0: |
|
124 |
"mk_tree 0 = \<spadesuit>" |
|
125 |
| "mk_tree (Suc n) = (let t = mk_tree n in t \<triangle> t)" |
|
126 |
||
127 |
declare mk_tree.simps [code] |
|
128 |
||
129 |
code_thms mk_tree |
|
130 |
||
131 |
function subtree :: "bool list \<Rightarrow> tree \<Rightarrow> tree" where |
|
132 |
"subtree [] t = t" |
|
133 |
| "subtree (True # p) (l \<triangle> r) = subtree p l" |
|
134 |
| "subtree (False # p) (l \<triangle> r) = subtree p r" |
|
135 |
| "subtree _ \<spadesuit> = \<spadesuit>" |
|
136 |
by pat_completeness auto |
|
137 |
termination by lexicographic_order |
|
138 |
||
139 |
value [code] "mk_tree 10" |
|
140 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
69597 | 141 |
\<comment> \<open>Since \<^const>\<open>mk_tree\<close> shares the two subtrees of a node thanks to the let binding, |
68639 | 142 |
digging into one subtree spreads to the whole tree.\<close> |
143 |
value [code] "let t = mk_tree 3; _ = subtree [True, True, False, False] t in t" |
|
144 |
||
145 |
lemma mk_tree_Suc_debug [code]: \<comment> \<open>Make the evaluation visible with tracing.\<close> |
|
146 |
"mk_tree (Suc n) = |
|
147 |
(let _ = Debug.flush (STR ''tick''); t = mk_tree n in t \<triangle> t)" |
|
148 |
by simp |
|
149 |
||
150 |
value [code] "mk_tree 10" |
|
69597 | 151 |
\<comment> \<open>The recursive call to \<^const>\<open>mk_tree\<close> is not guarded by a lazy constructor, |
68639 | 152 |
so all the suspensions are built up immediately.\<close> |
153 |
||
154 |
lemma mk_tree_Suc [code]: "mk_tree (Suc n) = mk_tree n \<triangle> mk_tree n" |
|
155 |
\<comment> \<open>In this code equation, there is no sharing and the recursive calls are guarded by a constructor.\<close> |
|
156 |
by(simp add: Let_def) |
|
157 |
||
158 |
value [code] "mk_tree 10" |
|
159 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
160 |
||
161 |
lemma mk_tree_Suc_debug' [code]: |
|
162 |
"mk_tree (Suc n) = (let _ = Debug.flush (STR ''tick'') in mk_tree n \<triangle> mk_tree n)" |
|
163 |
by(simp add: Let_def) |
|
164 |
||
165 |
value [code] "mk_tree 10" \<comment> \<open>Only one tick thanks to the guarding constructor\<close> |
|
166 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
167 |
value [code] "let t = mk_tree 3; _ = subtree [True, True, False, False] t in t" |
|
168 |
||
169 |
||
170 |
section \<open>Pattern matching elimination\<close> |
|
171 |
||
172 |
text \<open>The pattern matching elimination handles deep pattern matches and overlapping equations |
|
173 |
and only eliminates necessary pattern matches.\<close> |
|
174 |
||
175 |
function crazy :: "nat llist llist \<Rightarrow> tree \<Rightarrow> bool \<Rightarrow> unit" where |
|
176 |
"crazy (\<^bold>\<lbrakk>0\<^bold>\<rbrakk> ### xs) _ _ = Debug.flush (1 :: integer)" |
|
177 |
| "crazy xs \<spadesuit> True = Debug.flush (2 :: integer)" |
|
178 |
| "crazy xs t b = Debug.flush (3 :: integer)" |
|
179 |
by pat_completeness auto |
|
180 |
termination by lexicographic_order |
|
181 |
||
182 |
code_thms crazy |
|
183 |
||
184 |
end |