src/HOLCF/ex/Fixrec_ex.thy
author huffman
Fri, 27 Feb 2009 19:05:46 -0800
changeset 30158 83c50c62cf23
parent 16554 5841e7f9eef5
child 31008 b8f4e351b5bf
permissions -rw-r--r--
fixrec package uses new-style syntax and local-theory interface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     1
(*  Title:      HOLCF/ex/Fixrec_ex.thy
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     2
    Author:     Brian Huffman
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     3
*)
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     4
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     5
header {* Fixrec package examples *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     6
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     7
theory Fixrec_ex
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     8
imports HOLCF
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
     9
begin
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    10
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    11
subsection {* basic fixrec examples *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    12
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    13
text {*
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    14
  Fixrec patterns can mention any constructor defined by the domain
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    15
  package, as well as any of the following built-in constructors:
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    16
  cpair, spair, sinl, sinr, up, ONE, TT, FF.
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    17
*}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    18
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    19
text {* typical usage is with lazy constructors *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    20
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    21
fixrec down :: "'a u \<rightarrow> 'a"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    22
where "down\<cdot>(up\<cdot>x) = x"
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    23
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    24
text {* with strict constructors, rewrite rules may require side conditions *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    25
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    26
fixrec from_sinl :: "'a \<oplus> 'b \<rightarrow> 'a"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    27
where "x \<noteq> \<bottom> \<Longrightarrow> from_sinl\<cdot>(sinl\<cdot>x) = x"
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    28
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    29
text {* lifting can turn a strict constructor into a lazy one *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    30
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    31
fixrec from_sinl_up :: "'a u \<oplus> 'b \<rightarrow> 'a"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    32
where "from_sinl_up\<cdot>(sinl\<cdot>(up\<cdot>x)) = x"
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    33
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    34
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    35
subsection {* fixpat examples *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    36
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    37
text {* a type of lazy lists *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    38
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    39
domain 'a llist = lNil | lCons (lazy 'a) (lazy "'a llist")
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    40
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    41
text {* zip function for lazy lists *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    42
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    43
text {* notice that the patterns are not exhaustive *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    44
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    45
fixrec
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    46
  lzip :: "'a llist \<rightarrow> 'b llist \<rightarrow> ('a \<times> 'b) llist"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    47
where
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    48
  "lzip\<cdot>(lCons\<cdot>x\<cdot>xs)\<cdot>(lCons\<cdot>y\<cdot>ys) = lCons\<cdot><x,y>\<cdot>(lzip\<cdot>xs\<cdot>ys)"
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    49
| "lzip\<cdot>lNil\<cdot>lNil = lNil"
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    50
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    51
text {* fixpat is useful for producing strictness theorems *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    52
text {* note that pattern matching is done in left-to-right order *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    53
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    54
fixpat lzip_stricts [simp]:
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    55
  "lzip\<cdot>\<bottom>\<cdot>ys"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    56
  "lzip\<cdot>lNil\<cdot>\<bottom>"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    57
  "lzip\<cdot>(lCons\<cdot>x\<cdot>xs)\<cdot>\<bottom>"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    58
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    59
text {* fixpat can also produce rules for missing cases *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    60
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    61
fixpat lzip_undefs [simp]:
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    62
  "lzip\<cdot>lNil\<cdot>(lCons\<cdot>y\<cdot>ys)"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    63
  "lzip\<cdot>(lCons\<cdot>x\<cdot>xs)\<cdot>lNil"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    64
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    65
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    66
subsection {* skipping proofs of rewrite rules *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    67
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    68
text {* another zip function for lazy lists *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    69
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    70
text {*
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    71
  Notice that this version has overlapping patterns.
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    72
  The second equation cannot be proved as a theorem
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    73
  because it only applies when the first pattern fails.
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    74
*}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    75
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    76
fixrec (permissive)
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    77
  lzip2 :: "'a llist \<rightarrow> 'b llist \<rightarrow> ('a \<times> 'b) llist"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    78
where
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    79
  "lzip2\<cdot>(lCons\<cdot>x\<cdot>xs)\<cdot>(lCons\<cdot>y\<cdot>ys) = lCons\<cdot><x,y>\<cdot>(lzip\<cdot>xs\<cdot>ys)"
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
    80
| "lzip2\<cdot>xs\<cdot>ys = lNil"
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    81
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    82
text {*
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    83
  Usually fixrec tries to prove all equations as theorems.
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    84
  The "permissive" option overrides this behavior, so fixrec
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    85
  does not produce any simp rules.
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    86
*}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    87
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    88
text {* simp rules can be generated later using fixpat *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    89
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    90
fixpat lzip2_simps [simp]:
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    91
  "lzip2\<cdot>(lCons\<cdot>x\<cdot>xs)\<cdot>(lCons\<cdot>y\<cdot>ys)"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    92
  "lzip2\<cdot>(lCons\<cdot>x\<cdot>xs)\<cdot>lNil"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    93
  "lzip2\<cdot>lNil\<cdot>(lCons\<cdot>y\<cdot>ys)"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    94
  "lzip2\<cdot>lNil\<cdot>lNil"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    95
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    96
fixpat lzip2_stricts [simp]:
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    97
  "lzip2\<cdot>\<bottom>\<cdot>ys"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    98
  "lzip2\<cdot>(lCons\<cdot>x\<cdot>xs)\<cdot>\<bottom>"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
    99
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   100
subsection {* mutual recursion with fixrec *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   101
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   102
text {* tree and forest types *}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   103
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   104
domain 'a tree = Leaf (lazy 'a) | Branch (lazy "'a forest")
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   105
and    'a forest = Empty | Trees (lazy "'a tree") "'a forest"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   106
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   107
text {*
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   108
  To define mutually recursive functions, separate the equations
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   109
  for each function using the keyword "and".
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   110
*}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   111
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   112
fixrec
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
   113
  map_tree :: "('a \<rightarrow> 'b) \<rightarrow> ('a tree \<rightarrow> 'b tree)"
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   114
and
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
   115
  map_forest :: "('a \<rightarrow> 'b) \<rightarrow> ('a forest \<rightarrow> 'b forest)"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
   116
where
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
   117
  "map_tree\<cdot>f\<cdot>(Leaf\<cdot>x) = Leaf\<cdot>(f\<cdot>x)"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
   118
| "map_tree\<cdot>f\<cdot>(Branch\<cdot>ts) = Branch\<cdot>(map_forest\<cdot>f\<cdot>ts)"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
   119
| "map_forest\<cdot>f\<cdot>Empty = Empty"
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 16554
diff changeset
   120
| "ts \<noteq> \<bottom> \<Longrightarrow>
16554
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   121
    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)"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   122
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   123
fixpat map_tree_strict [simp]: "map_tree\<cdot>f\<cdot>\<bottom>"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   124
fixpat map_forest_strict [simp]: "map_forest\<cdot>f\<cdot>\<bottom>"
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   125
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   126
text {*
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   127
  Theorems generated:
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   128
  map_tree_def map_forest_def
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   129
  map_tree_unfold map_forest_unfold
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   130
  map_tree_simps map_forest_simps
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   131
  map_tree_map_forest_induct
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   132
*}
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   133
5841e7f9eef5 add new file to test fixrec package
huffman
parents:
diff changeset
   134
end