(* Title: HOL/HOLCF/Tutorial/Fixrec_ex.thy
Author: Brian Huffman
*)
section \<open>Fixrec package examples\<close>
theory Fixrec_ex
imports HOLCF
begin
subsection \<open>Basic \<open>fixrec\<close> examples\<close>
text \<open>
Fixrec patterns can mention any constructor defined by the domain
package, as well as any of the following built-in constructors:
Pair, spair, sinl, sinr, up, ONE, TT, FF.
\<close>
text \<open>Typical usage is with lazy constructors.\<close>
fixrec down :: "'a u \ 'a"
where "down\(up\x) = x"
text \<open>With strict constructors, rewrite rules may require side conditions.\<close>
fixrec from_sinl :: "'a \ 'b \ 'a"
where "x \ \ \ from_sinl\(sinl\x) = x"
text \<open>Lifting can turn a strict constructor into a lazy one.\<close>
fixrec from_sinl_up :: "'a u \ 'b \ 'a"
where "from_sinl_up\(sinl\(up\x)) = x"
text \<open>Fixrec also works with the HOL pair constructor.\<close>
fixrec down2 :: "'a u \ 'b u \ 'a \ 'b"
where "down2\(up\x, up\y) = (x, y)"
subsection \<open>Examples using \<open>fixrec_simp\<close>\<close>
text \<open>A type of lazy lists.\<close>
domain 'a llist = lNil | lCons (lazy 'a) (lazy "'a llist")
text \<open>A zip function for lazy lists.\<close>
text \<open>Notice that the patterns are not exhaustive.\<close>
fixrec
lzip :: "'a llist \ 'b llist \ ('a \ 'b) llist"
where
"lzip\(lCons\x\xs)\(lCons\y\ys) = lCons\(x, y)\(lzip\xs\ys)"
| "lzip\lNil\lNil = lNil"
text \<open>\<open>fixrec_simp\<close> is useful for producing strictness theorems.\<close>
text \<open>Note that pattern matching is done in left-to-right order.\<close>
lemma lzip_stricts [simp]:
"lzip\\\ys = \"
"lzip\lNil\\ = \"
"lzip\(lCons\x\xs)\\ = \"
by fixrec_simp+
text \<open>\<open>fixrec_simp\<close> can also produce rules for missing cases.\<close>
lemma lzip_undefs [simp]:
"lzip\lNil\(lCons\y\ys) = \"
"lzip\(lCons\x\xs)\lNil = \"
by fixrec_simp+
subsection \<open>Pattern matching with bottoms\<close>
text \<open>
As an alternative to using \<open>fixrec_simp\<close>, it is also possible
to use bottom as a constructor pattern. When using a bottom
pattern, the right-hand-side must also be bottom; otherwise, \<open>fixrec\<close> will not be able to prove the equation.
\<close>
fixrec
from_sinr_up :: "'a \ 'b\<^sub>\ \ 'b"
where
"from_sinr_up\\ = \"
| "from_sinr_up\(sinr\(up\x)) = x"
text \<open>
If the function is already strict in that argument, then the bottom
pattern does not change the meaning of the function. For example,
in the definition of \<^term>\<open>from_sinr_up\<close>, the first equation is
actually redundant, and could have been proven separately by
\<open>fixrec_simp\<close>.
\<close>
text \<open>
A bottom pattern can also be used to make a function strict in a
certain argument, similar to a bang-pattern in Haskell.
\<close>
fixrec
seq :: "'a \ 'b \ 'b"
where
"seq\\\y = \"
| "x \ \ \ seq\x\y = y"
subsection \<open>Skipping proofs of rewrite rules\<close>
text \<open>Another zip function for lazy lists.\<close>
text \<open>
Notice that this version has overlapping patterns.
The second equation cannot be proved as a theorem
because it only applies when the first pattern fails.
\<close>
fixrec
lzip2 :: "'a llist \ 'b llist \ ('a \ 'b) llist"
where
"lzip2\(lCons\x\xs)\(lCons\y\ys) = lCons\(x, y)\(lzip2\xs\ys)"
| (unchecked) "lzip2\xs\ys = lNil"
text \<open>
Usually fixrec tries to prove all equations as theorems.
The "unchecked" option overrides this behavior, so fixrec
does not attempt to prove that particular equation.
\<close>
text \<open>Simp rules can be generated later using \<open>fixrec_simp\<close>.\<close>
lemma lzip2_simps [simp]:
"lzip2\(lCons\x\xs)\lNil = lNil"
"lzip2\lNil\(lCons\y\ys) = lNil"
"lzip2\lNil\lNil = lNil"
by fixrec_simp+
lemma lzip2_stricts [simp]:
"lzip2\\\ys = \"
"lzip2\(lCons\x\xs)\\ = \"
by fixrec_simp+
subsection \<open>Mutual recursion with \<open>fixrec\<close>\<close>
text \<open>Tree and forest types.\<close>
domain 'a tree = Leaf (lazy 'a) | Branch (lazy "'a forest")
and 'a forest = Empty | Trees (lazy "'a tree") "'a forest"
text \<open>
To define mutually recursive functions, give multiple type signatures
separated by the keyword \<open>and\<close>.
\<close>
fixrec
map_tree :: "('a \ 'b) \ ('a tree \ 'b tree)"
and
map_forest :: "('a \ 'b) \ ('a forest \ 'b forest)"
where
"map_tree\f\(Leaf\x) = Leaf\(f\x)"
| "map_tree\f\(Branch\ts) = Branch\(map_forest\f\ts)"
| "map_forest\f\Empty = Empty"
| "ts \ \ \
map_forest\<cdot>f\<cdot>(Trees\<cdot>t\<cdot>ts) = Trees\<cdot>(map_tree\<cdot>f\<cdot>t)\<cdot>(map_forest\<cdot>f\<cdot>ts)"
lemma map_tree_strict [simp]: "map_tree\f\\ = \"
by fixrec_simp
lemma map_forest_strict [simp]: "map_forest\f\\ = \"
by fixrec_simp
(*
Theorems generated:
@{text map_tree_def} @{thm map_tree_def}
@{text map_forest_def} @{thm map_forest_def}
@{text map_tree.unfold} @{thm map_tree.unfold}
@{text map_forest.unfold} @{thm map_forest.unfold}
@{text map_tree.simps} @{thm map_tree.simps}
@{text map_forest.simps} @{thm map_forest.simps}
@{text map_tree_map_forest.induct} @{thm map_tree_map_forest.induct}
*)
subsection \<open>Looping simp rules\<close>
text \<open>
The defining equations of a fixrec definition are declared as simp
rules by default. In some cases, especially for constants with no
arguments or functions with variable patterns, the defining
equations may cause the simplifier to loop. In these cases it will
be necessary to use a \<open>[simp del]\<close> declaration.
\<close>
fixrec
repeat :: "'a \ 'a llist"
where
[simp del]: "repeat\x = lCons\x\(repeat\x)"
text \<open>
We can derive other non-looping simp rules for \<^const>\<open>repeat\<close> by
using the \<open>subst\<close> method with the \<open>repeat.simps\<close> rule.
\<close>
lemma repeat_simps [simp]:
"repeat\x \ \"
"repeat\x \ lNil"
"repeat\x = lCons\y\ys \ x = y \ repeat\x = ys"
by (subst repeat.simps, simp)+
lemma llist_case_repeat [simp]:
"llist_case\z\f\(repeat\x) = f\x\(repeat\x)"
by (subst repeat.simps, simp)
text \<open>
For mutually-recursive constants, looping might only occur if all
equations are in the simpset at the same time. In such cases it may
only be necessary to declare \<open>[simp del]\<close> on one equation.
\<close>
fixrec
inf_tree :: "'a tree" and inf_forest :: "'a forest"
where
[simp del]: "inf_tree = Branch\inf_forest"
| "inf_forest = Trees\inf_tree\(Trees\inf_tree\Empty)"
subsection \<open>Using \<open>fixrec\<close> inside locales\<close>
locale test =
fixes foo :: "'a \ 'a"
assumes foo_strict: "foo\\ = \"
begin
fixrec
bar :: "'a u \ 'a"
where
"bar\(up\x) = foo\x"
lemma bar_strict: "bar\\ = \"
by fixrec_simp
end
end
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
|
Haftungshinweis
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.
|