author | wenzelm |
Mon, 30 Sep 2024 23:32:26 +0200 | |
changeset 81090 | 843dba3d307a |
parent 81019 | dd59daa3c37a |
child 81091 | c007e6d9941d |
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 = |
|
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
16 |
SCons (shd: 'a) (stl: "'a stream") (infixr \<open>##\<close> 65) |
68639 | 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 |
|
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
39 |
= LNil (\<open>\<^bold>\<lbrakk>\<^bold>\<rbrakk>\<close>) |
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
40 |
| LCons (lhd: 'a) (ltl: "'a llist") (infixr \<open>###\<close> 65) |
68639 | 41 |
|
80786
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
42 |
syntax |
81090 | 43 |
"_llist" :: "args => 'a list" (\<open>(\<open>indent=1 notation=\<open>mixfix lazy list enumeration\<close>\<close>\<^bold>\<lbrakk>_\<^bold>\<rbrakk>)\<close>) |
80786
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
44 |
syntax_consts |
81019
dd59daa3c37a
clarified inner-syntax markup, notably for enumerations: prefer "notation=mixfix" over "entity" via 'syntax_consts' (see also 70076ba563d2);
wenzelm
parents:
80914
diff
changeset
|
45 |
LCons |
68639 | 46 |
translations |
47 |
"\<^bold>\<lbrakk>x, xs\<^bold>\<rbrakk>" == "x###\<^bold>\<lbrakk>xs\<^bold>\<rbrakk>" |
|
48 |
"\<^bold>\<lbrakk>x\<^bold>\<rbrakk>" == "x###\<^bold>\<lbrakk>\<^bold>\<rbrakk>" |
|
49 |
||
50 |
fun lnth :: "nat \<Rightarrow> 'a llist \<Rightarrow> 'a" where |
|
51 |
"lnth 0 (x ### xs) = x" |
|
52 |
| "lnth (Suc n) (x ### xs) = lnth n xs" |
|
53 |
||
54 |
definition llist :: "nat llist" where |
|
55 |
"llist = \<^bold>\<lbrakk>1, 2, 3, hd [], 4\<^bold>\<rbrakk>" |
|
56 |
||
57 |
code_lazy_type llist |
|
58 |
||
59 |
value [code] "llist" |
|
60 |
value [code] "lnth 2 llist" |
|
61 |
value [code] "let x = lnth 2 llist in (x, llist)" |
|
62 |
||
63 |
fun lfilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
|
64 |
"lfilter P \<^bold>\<lbrakk>\<^bold>\<rbrakk> = \<^bold>\<lbrakk>\<^bold>\<rbrakk>" |
|
65 |
| "lfilter P (x ### xs) = |
|
66 |
(if P x then x ### lfilter P xs else lfilter P xs)" |
|
67 |
||
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
|
68 |
export_code lfilter in SML file_prefix lfilter |
68639 | 69 |
|
70 |
value [code] "lfilter odd llist" |
|
71 |
||
72 |
value [code] "lhd (lfilter odd llist)" |
|
73 |
||
74 |
||
75 |
section \<open>Iterator for red-black trees\<close> |
|
76 |
||
77 |
text \<open>Thanks to laziness, we do not need to program a complicated iterator for a tree. |
|
78 |
A conversion function to lazy lists is enough.\<close> |
|
79 |
||
80 |
primrec lappend :: "'a llist \<Rightarrow> 'a llist \<Rightarrow> 'a llist" |
|
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
81 |
(infixr \<open>@@\<close> 65) where |
68639 | 82 |
"\<^bold>\<lbrakk>\<^bold>\<rbrakk> @@ ys = ys" |
83 |
| "(x ### xs) @@ ys = x ### (xs @@ ys)" |
|
84 |
||
85 |
primrec rbt_iterator :: "('a, 'b) rbt \<Rightarrow> ('a \<times> 'b) llist" where |
|
86 |
"rbt_iterator rbt.Empty = \<^bold>\<lbrakk>\<^bold>\<rbrakk>" |
|
87 |
| "rbt_iterator (Branch _ l k v r) = |
|
88 |
(let _ = Debug.flush (STR ''tick'') in |
|
89 |
rbt_iterator l @@ (k, v) ### rbt_iterator r)" |
|
90 |
||
91 |
definition tree :: "(nat, unit) rbt" |
|
92 |
where "tree = fold (\<lambda>k. rbt_insert k ()) [0..<100] rbt.Empty" |
|
93 |
||
94 |
definition find_min :: "('a :: linorder, 'b) rbt \<Rightarrow> ('a \<times> 'b) option" where |
|
95 |
"find_min rbt = |
|
96 |
(case rbt_iterator rbt of \<^bold>\<lbrakk>\<^bold>\<rbrakk> \<Rightarrow> None |
|
97 |
| kv ### _ \<Rightarrow> Some kv)" |
|
98 |
||
69597 | 99 |
value "find_min tree" \<comment> \<open>Observe that \<^const>\<open>rbt_iterator\<close> is evaluated only for going down |
68639 | 100 |
to the first leaf, not for the whole tree (as seen by the ticks).\<close> |
101 |
||
102 |
text \<open>With strict lists, the whole tree is converted into a list.\<close> |
|
103 |
||
104 |
deactivate_lazy_type llist |
|
105 |
value "find_min tree" |
|
106 |
activate_lazy_type llist |
|
107 |
||
108 |
||
109 |
||
110 |
section \<open>Branching datatypes\<close> |
|
111 |
||
112 |
datatype tree |
|
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
113 |
= L (\<open>\<spadesuit>\<close>) |
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
114 |
| Node tree tree (infix \<open>\<triangle>\<close> 900) |
68639 | 115 |
|
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
116 |
notation (output) Node (\<open>\<triangle>(//\<^bold>l: _//\<^bold>r: _)\<close>) |
68639 | 117 |
|
118 |
code_lazy_type tree |
|
119 |
||
120 |
fun mk_tree :: "nat \<Rightarrow> tree" where mk_tree_0: |
|
121 |
"mk_tree 0 = \<spadesuit>" |
|
122 |
| "mk_tree (Suc n) = (let t = mk_tree n in t \<triangle> t)" |
|
123 |
||
124 |
declare mk_tree.simps [code] |
|
125 |
||
126 |
code_thms mk_tree |
|
127 |
||
128 |
function subtree :: "bool list \<Rightarrow> tree \<Rightarrow> tree" where |
|
129 |
"subtree [] t = t" |
|
130 |
| "subtree (True # p) (l \<triangle> r) = subtree p l" |
|
131 |
| "subtree (False # p) (l \<triangle> r) = subtree p r" |
|
132 |
| "subtree _ \<spadesuit> = \<spadesuit>" |
|
133 |
by pat_completeness auto |
|
134 |
termination by lexicographic_order |
|
135 |
||
136 |
value [code] "mk_tree 10" |
|
137 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
69597 | 138 |
\<comment> \<open>Since \<^const>\<open>mk_tree\<close> shares the two subtrees of a node thanks to the let binding, |
68639 | 139 |
digging into one subtree spreads to the whole tree.\<close> |
140 |
value [code] "let t = mk_tree 3; _ = subtree [True, True, False, False] t in t" |
|
141 |
||
142 |
lemma mk_tree_Suc_debug [code]: \<comment> \<open>Make the evaluation visible with tracing.\<close> |
|
143 |
"mk_tree (Suc n) = |
|
144 |
(let _ = Debug.flush (STR ''tick''); t = mk_tree n in t \<triangle> t)" |
|
145 |
by simp |
|
146 |
||
147 |
value [code] "mk_tree 10" |
|
69597 | 148 |
\<comment> \<open>The recursive call to \<^const>\<open>mk_tree\<close> is not guarded by a lazy constructor, |
68639 | 149 |
so all the suspensions are built up immediately.\<close> |
150 |
||
151 |
lemma mk_tree_Suc [code]: "mk_tree (Suc n) = mk_tree n \<triangle> mk_tree n" |
|
152 |
\<comment> \<open>In this code equation, there is no sharing and the recursive calls are guarded by a constructor.\<close> |
|
153 |
by(simp add: Let_def) |
|
154 |
||
155 |
value [code] "mk_tree 10" |
|
156 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
157 |
||
158 |
lemma mk_tree_Suc_debug' [code]: |
|
159 |
"mk_tree (Suc n) = (let _ = Debug.flush (STR ''tick'') in mk_tree n \<triangle> mk_tree n)" |
|
160 |
by(simp add: Let_def) |
|
161 |
||
162 |
value [code] "mk_tree 10" \<comment> \<open>Only one tick thanks to the guarding constructor\<close> |
|
163 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
164 |
value [code] "let t = mk_tree 3; _ = subtree [True, True, False, False] t in t" |
|
165 |
||
166 |
||
167 |
section \<open>Pattern matching elimination\<close> |
|
168 |
||
169 |
text \<open>The pattern matching elimination handles deep pattern matches and overlapping equations |
|
170 |
and only eliminates necessary pattern matches.\<close> |
|
171 |
||
172 |
function crazy :: "nat llist llist \<Rightarrow> tree \<Rightarrow> bool \<Rightarrow> unit" where |
|
173 |
"crazy (\<^bold>\<lbrakk>0\<^bold>\<rbrakk> ### xs) _ _ = Debug.flush (1 :: integer)" |
|
174 |
| "crazy xs \<spadesuit> True = Debug.flush (2 :: integer)" |
|
175 |
| "crazy xs t b = Debug.flush (3 :: integer)" |
|
176 |
by pat_completeness auto |
|
177 |
termination by lexicographic_order |
|
178 |
||
179 |
code_thms crazy |
|
180 |
||
181 |
end |