fun lnth :: "nat \ 'a llist \ 'a" where "lnth 0 (x \<^bold># xs) = x"
| "lnth (Suc n) (x \<^bold># xs) = lnth n xs"
value [code] "llist" value [code] "let x = lnth 2 llist in (x, llist)" value [code] "llist"
fun lfilter :: "('a \ bool) \ 'a llist \ 'a llist" where "lfilter P \<^bold>[\<^bold>] = \<^bold>[\<^bold>]"
| "lfilter P (x \<^bold># xs) = (if P x then x \<^bold># lfilter P xs else lfilter P xs)"
value [code] "lhd (lfilter odd llist)"
definition lfilter_test :: "nat llist \ _" where "lfilter_test xs = lhd (lfilter even xs)" \<comment> \<open>Filtering \<^term>\<open>llist\<close> for \<^term>\<open>even\<close> fails because only the datatype is lazy, not the
filter function itself.\<close>
ML_val \<open> (@{code lfilter_test} @{code llist}; raise Fail "Failure expected") handle Match => () \<close>
codatatype tree = L | Node tree tree (infix\<open>\<triangle>\<close> 900)
code_lazy_type tree
fun mk_tree :: "nat \ tree" where
mk_tree_0: "mk_tree 0 = L"
| "mk_tree (Suc n) = (let t = mk_tree n in t \ t)"
function subtree :: "bool list \ tree \ tree" where "subtree [] t = t"
| "subtree (True # p) (l \ r) = subtree p l"
| "subtree (False # p) (l \ r) = subtree p r"
| "subtree _ L = L" by pat_completeness auto terminationby lexicographic_order
value [code] "mk_tree 10" value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t"
value [code] "let t = mk_tree 10; _ = subtree [True, True, False, False] t in t" value [code] "let t = mk_tree 4; _ = subtree [True, True, False, False] t in t"
subsection \<open>Corecursion\<close>
corec (friend) plus :: "'a :: plus stream \ 'a stream \ 'a stream" where "plus xs ys = (shd xs + shd ys) ## plus (stl xs) (stl ys)"
corec (friend) times :: "'a :: {plus, times} stream \ 'a stream \ 'a stream" where "times xs ys = (shd xs * shd ys) ## plus (times (stl xs) ys) (times xs (stl ys))"
lemma f1_code1 [code]: "f1 True c d \<^bold>[\<^bold>] = ()" "f1 b True d \<^bold>[n\<^bold>] = Code.abort (STR ''2'') (\_. ())" "f1 b c True \<^bold>[n, m\<^bold>] = Code.abort (STR ''3'') (\_. ())" "f1 b c d ns = Code.abort (STR ''4'') (\_. ())" by (simp_all add: f1_def)
value [code] "f1 True False False \<^bold>[\<^bold>]"
deactivate_lazy_type llist value [code] "f1 True False False \<^bold>[\<^bold>]" declare f1_code1(4) [code del] value [code] "f1 True False False \<^bold>[\<^bold>]"
activate_lazy_type llist value [code] "f1 True False False \<^bold>[\<^bold>]"
declare [[code drop: f1]] lemma f1_code2 [code]: "f1 True c d \<^bold>[\<^bold>] = Code.abort (STR ''1'') (\_. ())" "f1 b True d \<^bold>[n\<^bold>] = ()" "f1 b c True \<^bold>[n, m\<^bold>] = Code.abort (STR ''3'') (\_. ())" "f1 b c d ns = Code.abort (STR ''4'') (\_. ())" by (simp_all add: f1_def)
value [code] "f1 True True True \<^bold>[0\<^bold>]" declare f1_code2(4)[code del] value [code] "f1 True True True \<^bold>[0\<^bold>]"
declare [[code drop: f1]] lemma f1_code3 [code]: "f1 True c d \<^bold>[\<^bold>] = Code.abort (STR ''1'') (\_. ())" "f1 b True d \<^bold>[n\<^bold>] = Code.abort (STR ''2'') (\_. ())" "f1 b c True \<^bold>[n, m\<^bold>] = ()" "f1 b c d ns = Code.abort (STR ''4'') (\_. ())" by (simp_all add: f1_def)
value [code] "f1 True True True \<^bold>[0, 1\<^bold>]" declare f1_code3(4)[code del] value [code] "f1 True True True \<^bold>[0, 1\<^bold>]"
declare [[code drop: f1]] lemma f1_code4 [code]: "f1 True c d \<^bold>[\<^bold>] = Code.abort (STR ''1'') (\_. ())" "f1 b True d \<^bold>[n\<^bold>] = Code.abort (STR ''2'') (\_. ())" "f1 b c True \<^bold>[n, m\<^bold>] = Code.abort (STR ''3'') (\_. ())" "f1 b c d ns = ()" by (simp_all add: f1_def)
value [code] "f1 True True True \<^bold>[0, 1, 2\<^bold>]" value [code] "f1 True True False \<^bold>[0, 1\<^bold>]" value [code] "f1 True False True \<^bold>[0\<^bold>]" value [code] "f1 False True True \<^bold>[\<^bold>]"
definition f2 :: "nat llist llist list \ unit" where "f2 _ = ()"
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.