src/HOLCF/Fixrec.thy
author huffman
Sat Jun 18 00:38:18 2005 +0200 (2005-06-18)
changeset 16463 342d74ca8815
parent 16460 72a08d509d62
child 16551 7abf8a713613
permissions -rw-r--r--
fixrec shows unsolved subgoals when proofs of rewrites fail
huffman@16221
     1
(*  Title:      HOLCF/Fixrec.thy
huffman@16221
     2
    ID:         $Id$
huffman@16221
     3
    Author:     Amber Telfer and Brian Huffman
huffman@16221
     4
*)
huffman@16221
     5
huffman@16221
     6
header "Package for defining recursive functions in HOLCF"
huffman@16221
     7
huffman@16221
     8
theory Fixrec
huffman@16460
     9
imports Ssum One Up Fix Tr
haftmann@16417
    10
uses ("fixrec_package.ML")
huffman@16221
    11
begin
huffman@16221
    12
huffman@16221
    13
subsection {* Maybe monad type *}
huffman@16221
    14
huffman@16221
    15
types 'a maybe = "one ++ 'a u"
huffman@16221
    16
huffman@16221
    17
constdefs
huffman@16221
    18
  fail :: "'a maybe"
huffman@16221
    19
  "fail \<equiv> sinl\<cdot>ONE"
huffman@16221
    20
huffman@16221
    21
  return :: "'a \<rightarrow> 'a maybe"
huffman@16221
    22
  "return \<equiv> sinr oo up"
huffman@16221
    23
huffman@16221
    24
lemma maybeE:
huffman@16221
    25
  "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = return\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
huffman@16221
    26
apply (unfold fail_def return_def)
huffman@16221
    27
apply (rule_tac p=p in ssumE, simp)
huffman@16221
    28
apply (rule_tac p=x in oneE, simp, simp)
huffman@16221
    29
apply (rule_tac p=y in upE1, simp, simp)
huffman@16221
    30
done
huffman@16221
    31
huffman@16221
    32
subsection {* Monadic bind operator *}
huffman@16221
    33
huffman@16221
    34
constdefs
huffman@16221
    35
  bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe"
huffman@16221
    36
  "bind \<equiv> \<Lambda> m f. sscase\<cdot>sinl\<cdot>(fup\<cdot>f)\<cdot>m"
huffman@16221
    37
huffman@16221
    38
syntax
huffman@16221
    39
  "_bind" :: "'a maybe \<Rightarrow> ('a \<rightarrow> 'b maybe) \<Rightarrow> 'b maybe"
huffman@16221
    40
    ("(_ >>= _)" [50, 51] 50)
huffman@16221
    41
huffman@16221
    42
translations "m >>= k" == "bind\<cdot>m\<cdot>k"
huffman@16221
    43
huffman@16221
    44
nonterminals
huffman@16221
    45
  maybebind maybebinds
huffman@16221
    46
huffman@16221
    47
syntax 
huffman@16221
    48
  "_MBIND"  :: "pttrn \<Rightarrow> 'a maybe \<Rightarrow> maybebind"         ("(2_ <-/ _)" 10)
huffman@16221
    49
  ""        :: "maybebind \<Rightarrow> maybebinds"                ("_")
huffman@16221
    50
huffman@16221
    51
  "_MBINDS" :: "[maybebind, maybebinds] \<Rightarrow> maybebinds"  ("_;/ _")
huffman@16221
    52
  "_MDO"    :: "[maybebinds, 'a maybe] \<Rightarrow> 'a maybe"     ("(do _;/ (_))" 10)
huffman@16221
    53
huffman@16221
    54
translations
huffman@16221
    55
  "_MDO (_MBINDS b bs) e" == "_MDO b (_MDO bs e)"
huffman@16221
    56
  "do (x,y) <- m; e" == "m >>= (LAM <x,y>. e)" 
huffman@16221
    57
  "do x <- m; e"            == "m >>= (LAM x. e)"
huffman@16221
    58
huffman@16221
    59
text {* monad laws *}
huffman@16221
    60
huffman@16221
    61
lemma bind_strict [simp]: "UU >>= f = UU"
huffman@16221
    62
by (simp add: bind_def)
huffman@16221
    63
huffman@16221
    64
lemma bind_fail [simp]: "fail >>= f = fail"
huffman@16221
    65
by (simp add: bind_def fail_def)
huffman@16221
    66
huffman@16221
    67
lemma left_unit [simp]: "(return\<cdot>a) >>= k = k\<cdot>a"
huffman@16221
    68
by (simp add: bind_def return_def)
huffman@16221
    69
huffman@16221
    70
lemma right_unit [simp]: "m >>= return = m"
huffman@16221
    71
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    72
huffman@16221
    73
lemma bind_assoc [simp]:
huffman@16221
    74
 "(do a <- m; b <- k\<cdot>a; h\<cdot>b) = (do b <- (do a <- m; k\<cdot>a); h\<cdot>b)"
huffman@16221
    75
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    76
huffman@16221
    77
subsection {* Run operator *}
huffman@16221
    78
huffman@16221
    79
constdefs
huffman@16221
    80
  run:: "'a maybe \<rightarrow> 'a"
huffman@16221
    81
  "run \<equiv> sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)"
huffman@16221
    82
huffman@16221
    83
text {* rewrite rules for run *}
huffman@16221
    84
huffman@16221
    85
lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
huffman@16221
    86
by (simp add: run_def)
huffman@16221
    87
huffman@16221
    88
lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
huffman@16221
    89
by (simp add: run_def fail_def)
huffman@16221
    90
huffman@16221
    91
lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
huffman@16221
    92
by (simp add: run_def return_def)
huffman@16221
    93
huffman@16221
    94
subsection {* Monad plus operator *}
huffman@16221
    95
huffman@16221
    96
constdefs
huffman@16221
    97
  mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe"
huffman@16221
    98
  "mplus \<equiv> \<Lambda> m1 m2. sscase\<cdot>(\<Lambda> x. m2)\<cdot>(fup\<cdot>return)\<cdot>m1"
huffman@16221
    99
huffman@16221
   100
syntax "+++" :: "'a maybe \<Rightarrow> 'a maybe \<Rightarrow> 'a maybe" (infixr 65)
huffman@16221
   101
translations "x +++ y" == "mplus\<cdot>x\<cdot>y"
huffman@16221
   102
huffman@16221
   103
text {* rewrite rules for mplus *}
huffman@16221
   104
huffman@16221
   105
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
huffman@16221
   106
by (simp add: mplus_def)
huffman@16221
   107
huffman@16221
   108
lemma mplus_fail [simp]: "fail +++ m = m"
huffman@16221
   109
by (simp add: mplus_def fail_def)
huffman@16221
   110
huffman@16221
   111
lemma mplus_return [simp]: "return\<cdot>x +++ m = return\<cdot>x"
huffman@16221
   112
by (simp add: mplus_def return_def)
huffman@16221
   113
huffman@16460
   114
lemma mplus_fail2 [simp]: "m +++ fail = m"
huffman@16460
   115
by (rule_tac p=m in maybeE, simp_all)
huffman@16460
   116
huffman@16221
   117
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
huffman@16221
   118
by (rule_tac p=x in maybeE, simp_all)
huffman@16221
   119
huffman@16221
   120
subsection {* Match functions for built-in types *}
huffman@16221
   121
huffman@16221
   122
text {* Currently the package only supports lazy constructors *}
huffman@16221
   123
huffman@16221
   124
constdefs
huffman@16221
   125
  match_cpair :: "'a \<times> 'b \<rightarrow> ('a \<times> 'b) maybe"
huffman@16221
   126
  "match_cpair \<equiv> csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16221
   127
huffman@16221
   128
  match_up :: "'a u \<rightarrow> 'a maybe"
huffman@16221
   129
  "match_up \<equiv> fup\<cdot>return"
huffman@16221
   130
huffman@16460
   131
  match_ONE :: "one \<rightarrow> unit maybe"
huffman@16460
   132
  "match_ONE \<equiv> flift1 (\<lambda>u. return\<cdot>())"
huffman@16460
   133
huffman@16460
   134
  match_TT :: "tr \<rightarrow> unit maybe"
huffman@16460
   135
  "match_TT \<equiv> flift1 (\<lambda>b. if b then return\<cdot>() else fail)"
huffman@16460
   136
huffman@16460
   137
  match_FF :: "tr \<rightarrow> unit maybe"
huffman@16460
   138
  "match_FF \<equiv> flift1 (\<lambda>b. if b then fail else return\<cdot>())"
huffman@16460
   139
huffman@16221
   140
lemma match_cpair_simps [simp]:
huffman@16221
   141
  "match_cpair\<cdot><x,y> = return\<cdot><x,y>"
huffman@16221
   142
by (simp add: match_cpair_def)
huffman@16221
   143
huffman@16221
   144
lemma match_up_simps [simp]:
huffman@16221
   145
  "match_up\<cdot>(up\<cdot>x) = return\<cdot>x"
huffman@16221
   146
  "match_up\<cdot>\<bottom> = \<bottom>"
huffman@16221
   147
by (simp_all add: match_up_def)
huffman@16221
   148
huffman@16460
   149
lemma match_ONE_simps [simp]:
huffman@16460
   150
  "match_ONE\<cdot>ONE = return\<cdot>()"
huffman@16460
   151
  "match_ONE\<cdot>\<bottom> = \<bottom>"
huffman@16460
   152
by (simp_all add: ONE_def match_ONE_def)
huffman@16460
   153
huffman@16460
   154
lemma match_TT_simps [simp]:
huffman@16460
   155
  "match_TT\<cdot>TT = return\<cdot>()"
huffman@16460
   156
  "match_TT\<cdot>FF = fail"
huffman@16460
   157
  "match_TT\<cdot>\<bottom> = \<bottom>"
huffman@16460
   158
by (simp_all add: TT_def FF_def match_TT_def)
huffman@16460
   159
huffman@16460
   160
lemma match_FF_simps [simp]:
huffman@16460
   161
  "match_FF\<cdot>FF = return\<cdot>()"
huffman@16460
   162
  "match_FF\<cdot>TT = fail"
huffman@16460
   163
  "match_FF\<cdot>\<bottom> = \<bottom>"
huffman@16460
   164
by (simp_all add: TT_def FF_def match_FF_def)
huffman@16460
   165
huffman@16401
   166
subsection {* Mutual recursion *}
huffman@16401
   167
huffman@16401
   168
text {*
huffman@16401
   169
  The following rules are used to prove unfolding theorems from
huffman@16401
   170
  fixed-point definitions of mutually recursive functions.
huffman@16401
   171
*}
huffman@16401
   172
huffman@16401
   173
lemma cpair_equalI: "\<lbrakk>x \<equiv> cfst\<cdot>p; y \<equiv> csnd\<cdot>p\<rbrakk> \<Longrightarrow> <x,y> \<equiv> p"
huffman@16401
   174
by (simp add: surjective_pairing_Cprod2)
huffman@16401
   175
huffman@16401
   176
lemma cpair_eqD1: "<x,y> = <x',y'> \<Longrightarrow> x = x'"
huffman@16401
   177
by simp
huffman@16401
   178
huffman@16401
   179
lemma cpair_eqD2: "<x,y> = <x',y'> \<Longrightarrow> y = y'"
huffman@16401
   180
by simp
huffman@16401
   181
huffman@16463
   182
text {* lemma for proving rewrite rules *}
huffman@16463
   183
huffman@16463
   184
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   185
by simp
huffman@16463
   186
huffman@16401
   187
ML {*
huffman@16401
   188
val cpair_equalI = thm "cpair_equalI";
huffman@16401
   189
val cpair_eqD1 = thm "cpair_eqD1";
huffman@16401
   190
val cpair_eqD2 = thm "cpair_eqD2";
huffman@16463
   191
val ssubst_lhs = thm "ssubst_lhs";
huffman@16401
   192
*}
huffman@16221
   193
huffman@16221
   194
subsection {* Intitializing the fixrec package *}
huffman@16221
   195
huffman@16229
   196
use "fixrec_package.ML"
huffman@16221
   197
huffman@16221
   198
end