Theory Fixrec_ex

(*  Title:      HOL/HOLCF/Tutorial/Fixrec_ex.thy
    Author:     Brian Huffman

section ‹Fixrec package examples›

theory Fixrec_ex
imports HOLCF

subsection ‹Basic fixrec› examples›

text ‹
  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.

text ‹Typical usage is with lazy constructors.›

fixrec down :: "'a u  'a"
where "down(upx) = x"

text ‹With strict constructors, rewrite rules may require side conditions.›

fixrec from_sinl :: "'a  'b  'a"
where "x    from_sinl(sinlx) = x"

text ‹Lifting can turn a strict constructor into a lazy one.›

fixrec from_sinl_up :: "'a u  'b  'a"
where "from_sinl_up(sinl(upx)) = x"

text ‹Fixrec also works with the HOL pair constructor.›

fixrec down2 :: "'a u × 'b u  'a × 'b"
where "down2(upx, upy) = (x, y)"

subsection ‹Examples using fixrec_simp›

text ‹A type of lazy lists.›

domain 'a llist = lNil | lCons (lazy 'a) (lazy "'a llist")

text ‹A zip function for lazy lists.›

text ‹Notice that the patterns are not exhaustive.›

  lzip :: "'a llist  'b llist  ('a × 'b) llist"
  "lzip(lConsxxs)(lConsyys) = lCons(x, y)(lzipxsys)"
| "lziplNillNil = lNil"

text fixrec_simp› is useful for producing strictness theorems.›
text ‹Note that pattern matching is done in left-to-right order.›

lemma lzip_stricts [simp]:
  "lzipys = "
  "lziplNil = "
  "lzip(lConsxxs) = "
by fixrec_simp+

text fixrec_simp› can also produce rules for missing cases.›

lemma lzip_undefs [simp]:
  "lziplNil(lConsyys) = "
  "lzip(lConsxxs)lNil = "
by fixrec_simp+

subsection ‹Pattern matching with bottoms›

text ‹
  As an alternative to using fixrec_simp›, 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, fixrec› will not be able to prove the equation.

  from_sinr_up :: "'a  'b  'b"
  "from_sinr_up = "
| "from_sinr_up(sinr(upx)) = x"

text ‹
  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 termfrom_sinr_up, the first equation is
  actually redundant, and could have been proven separately by

text ‹
  A bottom pattern can also be used to make a function strict in a
  certain argument, similar to a bang-pattern in Haskell.

  seq :: "'a  'b  'b"
  "seqy = "
| "x    seqxy = y"

subsection ‹Skipping proofs of rewrite rules›

text ‹Another zip function for lazy lists.›

text ‹
  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.

  lzip2 :: "'a llist  'b llist  ('a × 'b) llist"
  "lzip2(lConsxxs)(lConsyys) = lCons(x, y)(lzip2xsys)"
| (unchecked) "lzip2xsys = lNil"

text ‹
  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.

text ‹Simp rules can be generated later using fixrec_simp›.›

lemma lzip2_simps [simp]:
  "lzip2(lConsxxs)lNil = lNil"
  "lzip2lNil(lConsyys) = lNil"
  "lzip2lNillNil = lNil"
by fixrec_simp+

lemma lzip2_stricts [simp]:
  "lzip2ys = "
  "lzip2(lConsxxs) = "
by fixrec_simp+

subsection ‹Mutual recursion with fixrec›

text ‹Tree and forest types.›

domain 'a tree = Leaf (lazy 'a) | Branch (lazy "'a forest")
and    'a forest = Empty | Trees (lazy "'a tree") "'a forest"

text ‹
  To define mutually recursive functions, give multiple type signatures
  separated by the keyword and›.

  map_tree :: "('a  'b)  ('a tree  'b tree)"
  map_forest :: "('a  'b)  ('a forest  'b forest)"
  "map_treef(Leafx) = Leaf(fx)"
| "map_treef(Branchts) = Branch(map_forestfts)"
| "map_forestfEmpty = Empty"
| "ts   
    map_forestf(Treestts) = Trees(map_treeft)(map_forestfts)"

lemma map_tree_strict [simp]: "map_treef = "
by fixrec_simp

lemma map_forest_strict [simp]: "map_forestf = "
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 ‹Looping simp rules›

text ‹
  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 [simp del]› declaration.

  repeat :: "'a  'a llist"
  [simp del]: "repeatx = lConsx(repeatx)"

text ‹
  We can derive other non-looping simp rules for constrepeat by
  using the subst› method with the repeat.simps› rule.

lemma repeat_simps [simp]:
  "repeatx  "
  "repeatx  lNil"
  "repeatx = lConsyys  x = y  repeatx = ys"
by (subst repeat.simps, simp)+

lemma llist_case_repeat [simp]:
  "llist_casezf(repeatx) = fx(repeatx)"
by (subst repeat.simps, simp)

text ‹
  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 [simp del]› on one equation.

  inf_tree :: "'a tree" and inf_forest :: "'a forest"
  [simp del]: "inf_tree = Branchinf_forest"
| "inf_forest = Treesinf_tree(Treesinf_treeEmpty)"

subsection ‹Using fixrec› inside locales›

locale test =
  fixes foo :: "'a  'a"
  assumes foo_strict: "foo = "

  bar :: "'a u  'a"
  "bar(upx) = foox"

lemma bar_strict: "bar = "
by fixrec_simp