author | wenzelm |
Mon, 30 Sep 2024 23:32:26 +0200 | |
changeset 81090 | 843dba3d307a |
parent 81019 | dd59daa3c37a |
child 81091 | c007e6d9941d |
permissions | -rw-r--r-- |
68155 | 1 |
(* Author: Andreas Lochbihler, Digital Asset *) |
2 |
||
3 |
section \<open>Laziness tests\<close> |
|
4 |
||
5 |
theory Code_Lazy_Test imports |
|
6 |
"HOL-Library.Code_Lazy" |
|
7 |
"HOL-Library.Stream" |
|
8 |
"HOL-Library.Code_Test" |
|
9 |
"HOL-Library.BNF_Corec" |
|
10 |
begin |
|
11 |
||
12 |
subsection \<open>Linear codatatype\<close> |
|
13 |
||
14 |
code_lazy_type stream |
|
15 |
||
16 |
value [code] "cycle ''ab''" |
|
17 |
value [code] "let x = cycle ''ab''; y = snth x 10 in x" |
|
18 |
||
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
19 |
datatype 'a llist = LNil (\<open>\<^bold>[\<^bold>]\<close>) | LCons (lhd: 'a) (ltl: "'a llist") (infixr \<open>\<^bold>#\<close> 65) |
68155 | 20 |
|
21 |
subsection \<open>Finite lazy lists\<close> |
|
22 |
||
23 |
code_lazy_type llist |
|
24 |
||
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
25 |
no_notation lazy_llist (\<open>_\<close>) |
80786
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
26 |
syntax |
81090 | 27 |
"_llist" :: "args => 'a list" (\<open>(\<open>indent=1 notation=\<open>mixfix lazy list enumeration\<close>\<close>\<^bold>[_\<^bold>])\<close>) |
80786
70076ba563d2
more specific "args" syntax, to support more markup for syntax consts;
wenzelm
parents:
80768
diff
changeset
|
28 |
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
|
29 |
lazy_llist |
68155 | 30 |
translations |
31 |
"\<^bold>[x, xs\<^bold>]" == "x\<^bold>#\<^bold>[xs\<^bold>]" |
|
32 |
"\<^bold>[x\<^bold>]" == "x\<^bold>#\<^bold>[\<^bold>]" |
|
33 |
"\<^bold>[x\<^bold>]" == "CONST lazy_llist x" |
|
34 |
||
35 |
definition llist :: "nat llist" where |
|
36 |
"llist = \<^bold>[1, 2, 3, hd [], 4\<^bold>]" |
|
37 |
||
38 |
fun lnth :: "nat \<Rightarrow> 'a llist \<Rightarrow> 'a" where |
|
39 |
"lnth 0 (x \<^bold># xs) = x" |
|
40 |
| "lnth (Suc n) (x \<^bold># xs) = lnth n xs" |
|
41 |
||
42 |
value [code] "llist" |
|
43 |
value [code] "let x = lnth 2 llist in (x, llist)" |
|
44 |
value [code] "llist" |
|
45 |
||
46 |
fun lfilter :: "('a \<Rightarrow> bool) \<Rightarrow> 'a llist \<Rightarrow> 'a llist" where |
|
47 |
"lfilter P \<^bold>[\<^bold>] = \<^bold>[\<^bold>]" |
|
48 |
| "lfilter P (x \<^bold># xs) = (if P x then x \<^bold># lfilter P xs else lfilter P xs)" |
|
49 |
||
50 |
value [code] "lhd (lfilter odd llist)" |
|
51 |
||
52 |
definition lfilter_test :: "nat llist \<Rightarrow> _" where "lfilter_test xs = lhd (lfilter even xs)" |
|
69597 | 53 |
\<comment> \<open>Filtering \<^term>\<open>llist\<close> for \<^term>\<open>even\<close> fails because only the datatype is lazy, not the |
68155 | 54 |
filter function itself.\<close> |
55 |
ML_val \<open> (@{code lfilter_test} @{code llist}; raise Fail "Failure expected") handle Match => () \<close> |
|
56 |
||
57 |
subsection \<open>Records as free type\<close> |
|
58 |
||
59 |
record ('a, 'b) rec = |
|
60 |
rec1 :: 'a |
|
61 |
rec2 :: 'b |
|
62 |
||
63 |
free_constructors rec_ext for rec.rec_ext |
|
64 |
subgoal by(rule rec.cases_scheme) |
|
65 |
subgoal by(rule rec.ext_inject) |
|
66 |
done |
|
67 |
||
68 |
code_lazy_type rec_ext |
|
69 |
||
70 |
definition rec_test1 where "rec_test1 = rec1 (\<lparr>rec1 = Suc 5, rec2 = True\<rparr>\<lparr>rec1 := 0\<rparr>)" |
|
71 |
definition rec_test2 :: "(bool, bool) rec" where "rec_test2 = \<lparr>rec1 = hd [], rec2 = True\<rparr>" |
|
72 |
test_code "rec_test1 = 0" in PolyML Scala |
|
73 |
value [code] "rec_test2" |
|
74 |
||
75 |
subsection \<open>Branching codatatypes\<close> |
|
76 |
||
80914
d97fdabd9e2b
standardize mixfix annotations via "isabelle update -a -u mixfix_cartouches" --- to simplify systematic editing;
wenzelm
parents:
80786
diff
changeset
|
77 |
codatatype tree = L | Node tree tree (infix \<open>\<triangle>\<close> 900) |
68155 | 78 |
|
79 |
code_lazy_type tree |
|
80 |
||
81 |
fun mk_tree :: "nat \<Rightarrow> tree" where |
|
82 |
mk_tree_0: "mk_tree 0 = L" |
|
83 |
| "mk_tree (Suc n) = (let t = mk_tree n in t \<triangle> t)" |
|
84 |
||
85 |
function subtree :: "bool list \<Rightarrow> tree \<Rightarrow> tree" where |
|
86 |
"subtree [] t = t" |
|
87 |
| "subtree (True # p) (l \<triangle> r) = subtree p l" |
|
88 |
| "subtree (False # p) (l \<triangle> r) = subtree p r" |
|
89 |
| "subtree _ L = L" |
|
90 |
by pat_completeness auto |
|
91 |
termination by lexicographic_order |
|
92 |
||
93 |
value [code] "mk_tree 10" |
|
94 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
95 |
||
96 |
lemma mk_tree_Suc: "mk_tree (Suc n) = mk_tree n \<triangle> mk_tree n" |
|
97 |
by(simp add: Let_def) |
|
98 |
lemmas [code] = mk_tree_0 mk_tree_Suc |
|
99 |
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" |
|
100 |
value [code] "let t = mk_tree 4; _ = subtree [True, True, False, False] t in t" |
|
101 |
||
102 |
subsection \<open>Corecursion\<close> |
|
103 |
||
104 |
corec (friend) plus :: "'a :: plus stream \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where |
|
105 |
"plus xs ys = (shd xs + shd ys) ## plus (stl xs) (stl ys)" |
|
106 |
||
107 |
corec (friend) times :: "'a :: {plus, times} stream \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where |
|
108 |
"times xs ys = (shd xs * shd ys) ## plus (times (stl xs) ys) (times xs (stl ys))" |
|
109 |
||
110 |
subsection \<open>Pattern-matching tests\<close> |
|
111 |
||
112 |
definition f1 :: "bool \<Rightarrow> bool \<Rightarrow> bool \<Rightarrow> nat llist \<Rightarrow> unit" where |
|
113 |
"f1 _ _ _ _ = ()" |
|
114 |
||
115 |
declare [[code drop: f1]] |
|
116 |
lemma f1_code1 [code]: |
|
117 |
"f1 b c d ns = Code.abort (STR ''4'') (\<lambda>_. ())" |
|
118 |
"f1 b c True \<^bold>[n, m\<^bold>] = Code.abort (STR ''3'') (\<lambda>_. ())" |
|
119 |
"f1 b True d \<^bold>[n\<^bold>] = Code.abort (STR ''2'') (\<lambda>_. ())" |
|
120 |
"f1 True c d \<^bold>[\<^bold>] = ()" |
|
121 |
by(simp_all add: f1_def) |
|
122 |
||
123 |
value [code] "f1 True False False \<^bold>[\<^bold>]" |
|
124 |
deactivate_lazy_type llist |
|
125 |
value [code] "f1 True False False \<^bold>[\<^bold>]" |
|
126 |
declare f1_code1(1) [code del] |
|
127 |
value [code] "f1 True False False \<^bold>[\<^bold>]" |
|
128 |
activate_lazy_type llist |
|
129 |
value [code] "f1 True False False \<^bold>[\<^bold>]" |
|
130 |
||
131 |
declare [[code drop: f1]] |
|
132 |
lemma f1_code2 [code]: |
|
133 |
"f1 b c d ns = Code.abort (STR ''4'') (\<lambda>_. ())" |
|
134 |
"f1 b c True \<^bold>[n, m\<^bold>] = Code.abort (STR ''3'') (\<lambda>_. ())" |
|
135 |
"f1 b True d \<^bold>[n\<^bold>] = ()" |
|
136 |
"f1 True c d \<^bold>[\<^bold>] = Code.abort (STR ''1'') (\<lambda>_. ())" |
|
137 |
by(simp_all add: f1_def) |
|
138 |
||
139 |
value [code] "f1 True True True \<^bold>[0\<^bold>]" |
|
140 |
declare f1_code2(1)[code del] |
|
141 |
value [code] "f1 True True True \<^bold>[0\<^bold>]" |
|
142 |
||
143 |
declare [[code drop: f1]] |
|
144 |
lemma f1_code3 [code]: |
|
145 |
"f1 b c d ns = Code.abort (STR ''4'') (\<lambda>_. ())" |
|
146 |
"f1 b c True \<^bold>[n, m\<^bold>] = ()" |
|
147 |
"f1 b True d \<^bold>[n\<^bold>] = Code.abort (STR ''2'') (\<lambda>_. ())" |
|
148 |
"f1 True c d \<^bold>[\<^bold>] = Code.abort (STR ''1'') (\<lambda>_. ())" |
|
149 |
by(simp_all add: f1_def) |
|
150 |
||
151 |
value [code] "f1 True True True \<^bold>[0, 1\<^bold>]" |
|
152 |
declare f1_code3(1)[code del] |
|
153 |
value [code] "f1 True True True \<^bold>[0, 1\<^bold>]" |
|
154 |
||
155 |
declare [[code drop: f1]] |
|
156 |
lemma f1_code4 [code]: |
|
157 |
"f1 b c d ns = ()" |
|
158 |
"f1 b c True \<^bold>[n, m\<^bold>] = Code.abort (STR ''3'') (\<lambda>_. ())" |
|
159 |
"f1 b True d \<^bold>[n\<^bold>] = Code.abort (STR ''2'') (\<lambda>_. ())" |
|
160 |
"f1 True c d \<^bold>[\<^bold>] = Code.abort (STR ''1'') (\<lambda>_. ())" |
|
161 |
by(simp_all add: f1_def) |
|
162 |
||
163 |
value [code] "f1 True True True \<^bold>[0, 1, 2\<^bold>]" |
|
164 |
value [code] "f1 True True False \<^bold>[0, 1\<^bold>]" |
|
165 |
value [code] "f1 True False True \<^bold>[0\<^bold>]" |
|
166 |
value [code] "f1 False True True \<^bold>[\<^bold>]" |
|
167 |
||
168 |
definition f2 :: "nat llist llist list \<Rightarrow> unit" where "f2 _ = ()" |
|
169 |
||
170 |
declare [[code drop: f2]] |
|
171 |
lemma f2_code1 [code]: |
|
172 |
"f2 xs = Code.abort (STR ''a'') (\<lambda>_. ())" |
|
173 |
"f2 [\<^bold>[\<^bold>[\<^bold>]\<^bold>]] = ()" |
|
174 |
"f2 [\<^bold>[\<^bold>[Suc n\<^bold>]\<^bold>]] = ()" |
|
175 |
"f2 [\<^bold>[\<^bold>[0, Suc n\<^bold>]\<^bold>]] = ()" |
|
176 |
by(simp_all add: f2_def) |
|
177 |
||
178 |
value [code] "f2 [\<^bold>[\<^bold>[\<^bold>]\<^bold>]]" |
|
179 |
value [code] "f2 [\<^bold>[\<^bold>[4\<^bold>]\<^bold>]]" |
|
180 |
value [code] "f2 [\<^bold>[\<^bold>[0, 1\<^bold>]\<^bold>]]" |
|
181 |
ML_val \<open> (@{code f2} []; error "Fail expected") handle Fail _ => () \<close> |
|
182 |
||
183 |
definition f3 :: "nat set llist \<Rightarrow> unit" where "f3 _ = ()" |
|
184 |
||
185 |
declare [[code drop: f3]] |
|
186 |
lemma f3_code1 [code]: |
|
187 |
"f3 \<^bold>[\<^bold>] = ()" |
|
188 |
"f3 \<^bold>[A\<^bold>] = ()" |
|
189 |
by(simp_all add: f3_def) |
|
190 |
||
191 |
value [code] "f3 \<^bold>[\<^bold>]" |
|
192 |
value [code] "f3 \<^bold>[{}\<^bold>]" |
|
193 |
value [code] "f3 \<^bold>[UNIV\<^bold>]" |
|
194 |
||
195 |
end |