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