src/HOLCF/Fixrec.thy
author huffman
Sun Nov 06 00:35:24 2005 +0100 (2005-11-06)
changeset 18096 574aa0487069
parent 18094 404f298220af
child 18097 d196d84c306f
permissions -rw-r--r--
use consts for infix syntax
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@16551
     9
imports Sprod Ssum Up One Tr Fix
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@16776
    15
defaultsort cpo
huffman@16776
    16
huffman@16221
    17
types 'a maybe = "one ++ 'a u"
huffman@16221
    18
huffman@16221
    19
constdefs
huffman@16221
    20
  fail :: "'a maybe"
huffman@16221
    21
  "fail \<equiv> sinl\<cdot>ONE"
huffman@16221
    22
huffman@16221
    23
  return :: "'a \<rightarrow> 'a maybe"
huffman@16221
    24
  "return \<equiv> sinr oo up"
huffman@16221
    25
huffman@16221
    26
lemma maybeE:
huffman@16221
    27
  "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = return\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
huffman@16221
    28
apply (unfold fail_def return_def)
huffman@16221
    29
apply (rule_tac p=p in ssumE, simp)
huffman@16221
    30
apply (rule_tac p=x in oneE, simp, simp)
huffman@16754
    31
apply (rule_tac p=y in upE, simp, simp)
huffman@16221
    32
done
huffman@16221
    33
huffman@16221
    34
subsection {* Monadic bind operator *}
huffman@16221
    35
huffman@18096
    36
consts
huffman@18096
    37
  bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe" (infixl ">>=" 50)
huffman@18096
    38
defs
huffman@18096
    39
  bind_def: "bind \<equiv> \<Lambda> m f. sscase\<cdot>sinl\<cdot>(fup\<cdot>f)\<cdot>m"
huffman@16221
    40
huffman@16221
    41
nonterminals
huffman@16221
    42
  maybebind maybebinds
huffman@16221
    43
huffman@16221
    44
syntax 
huffman@16221
    45
  "_MBIND"  :: "pttrn \<Rightarrow> 'a maybe \<Rightarrow> maybebind"         ("(2_ <-/ _)" 10)
huffman@16221
    46
  ""        :: "maybebind \<Rightarrow> maybebinds"                ("_")
huffman@16221
    47
huffman@16221
    48
  "_MBINDS" :: "[maybebind, maybebinds] \<Rightarrow> maybebinds"  ("_;/ _")
huffman@16221
    49
  "_MDO"    :: "[maybebinds, 'a maybe] \<Rightarrow> 'a maybe"     ("(do _;/ (_))" 10)
huffman@16221
    50
huffman@16221
    51
translations
huffman@16221
    52
  "_MDO (_MBINDS b bs) e" == "_MDO b (_MDO bs e)"
huffman@16221
    53
  "do (x,y) <- m; e" == "m >>= (LAM <x,y>. e)" 
huffman@16221
    54
  "do x <- m; e"            == "m >>= (LAM x. e)"
huffman@16221
    55
huffman@16221
    56
text {* monad laws *}
huffman@16221
    57
huffman@16221
    58
lemma bind_strict [simp]: "UU >>= f = UU"
huffman@16221
    59
by (simp add: bind_def)
huffman@16221
    60
huffman@16221
    61
lemma bind_fail [simp]: "fail >>= f = fail"
huffman@16221
    62
by (simp add: bind_def fail_def)
huffman@16221
    63
huffman@16221
    64
lemma left_unit [simp]: "(return\<cdot>a) >>= k = k\<cdot>a"
huffman@16221
    65
by (simp add: bind_def return_def)
huffman@16221
    66
huffman@16221
    67
lemma right_unit [simp]: "m >>= return = m"
huffman@16221
    68
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    69
huffman@16221
    70
lemma bind_assoc [simp]:
huffman@16779
    71
 "(do b <- (do a <- m; k\<cdot>a); h\<cdot>b) = (do a <- m; b <- k\<cdot>a; h\<cdot>b)"
huffman@16221
    72
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    73
huffman@16221
    74
subsection {* Run operator *}
huffman@16221
    75
huffman@16221
    76
constdefs
huffman@16776
    77
  run:: "'a::pcpo maybe \<rightarrow> 'a"
huffman@16221
    78
  "run \<equiv> sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)"
huffman@16221
    79
huffman@16221
    80
text {* rewrite rules for run *}
huffman@16221
    81
huffman@16221
    82
lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
huffman@16221
    83
by (simp add: run_def)
huffman@16221
    84
huffman@16221
    85
lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
huffman@16221
    86
by (simp add: run_def fail_def)
huffman@16221
    87
huffman@16221
    88
lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
huffman@16221
    89
by (simp add: run_def return_def)
huffman@16221
    90
huffman@16221
    91
subsection {* Monad plus operator *}
huffman@16221
    92
huffman@18096
    93
consts
huffman@18096
    94
  mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe" (infixr "+++" 65)
huffman@18096
    95
defs
huffman@18096
    96
  mplus_def: "mplus \<equiv> \<Lambda> m1 m2. sscase\<cdot>(\<Lambda> x. m2)\<cdot>(fup\<cdot>return)\<cdot>m1"
huffman@16221
    97
huffman@16221
    98
text {* rewrite rules for mplus *}
huffman@16221
    99
huffman@16221
   100
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
huffman@16221
   101
by (simp add: mplus_def)
huffman@16221
   102
huffman@16221
   103
lemma mplus_fail [simp]: "fail +++ m = m"
huffman@16221
   104
by (simp add: mplus_def fail_def)
huffman@16221
   105
huffman@16221
   106
lemma mplus_return [simp]: "return\<cdot>x +++ m = return\<cdot>x"
huffman@16221
   107
by (simp add: mplus_def return_def)
huffman@16221
   108
huffman@16460
   109
lemma mplus_fail2 [simp]: "m +++ fail = m"
huffman@16460
   110
by (rule_tac p=m in maybeE, simp_all)
huffman@16460
   111
huffman@16221
   112
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
huffman@16221
   113
by (rule_tac p=x in maybeE, simp_all)
huffman@16221
   114
huffman@16221
   115
subsection {* Match functions for built-in types *}
huffman@16221
   116
huffman@16776
   117
defaultsort pcpo
huffman@16776
   118
huffman@16221
   119
constdefs
huffman@16776
   120
  match_UU :: "'a \<rightarrow> unit maybe"
huffman@16776
   121
  "match_UU \<equiv> \<Lambda> x. fail"
huffman@16776
   122
huffman@16776
   123
  match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe"
huffman@16221
   124
  "match_cpair \<equiv> csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16221
   125
huffman@16551
   126
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe"
huffman@16551
   127
  "match_spair \<equiv> ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16551
   128
huffman@16551
   129
  match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe"
huffman@16551
   130
  "match_sinl \<equiv> sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
huffman@16551
   131
huffman@16551
   132
  match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe"
huffman@16551
   133
  "match_sinr \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
huffman@16551
   134
huffman@16776
   135
  match_up :: "'a::cpo u \<rightarrow> 'a maybe"
huffman@16221
   136
  "match_up \<equiv> fup\<cdot>return"
huffman@16221
   137
huffman@16460
   138
  match_ONE :: "one \<rightarrow> unit maybe"
huffman@18094
   139
  "match_ONE \<equiv> \<Lambda> ONE. return\<cdot>()"
huffman@18094
   140
 
huffman@16460
   141
  match_TT :: "tr \<rightarrow> unit maybe"
huffman@18094
   142
  "match_TT \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi"
huffman@18094
   143
 
huffman@16460
   144
  match_FF :: "tr \<rightarrow> unit maybe"
huffman@18094
   145
  "match_FF \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi"
huffman@16460
   146
huffman@16776
   147
lemma match_UU_simps [simp]:
huffman@16776
   148
  "match_UU\<cdot>x = fail"
huffman@16776
   149
by (simp add: match_UU_def)
huffman@16776
   150
huffman@16221
   151
lemma match_cpair_simps [simp]:
huffman@16221
   152
  "match_cpair\<cdot><x,y> = return\<cdot><x,y>"
huffman@16221
   153
by (simp add: match_cpair_def)
huffman@16221
   154
huffman@16551
   155
lemma match_spair_simps [simp]:
huffman@16551
   156
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x,y:) = return\<cdot><x,y>"
huffman@16551
   157
  "match_spair\<cdot>\<bottom> = \<bottom>"
huffman@16551
   158
by (simp_all add: match_spair_def)
huffman@16551
   159
huffman@16551
   160
lemma match_sinl_simps [simp]:
huffman@16551
   161
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x) = return\<cdot>x"
huffman@16551
   162
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>x) = fail"
huffman@16551
   163
  "match_sinl\<cdot>\<bottom> = \<bottom>"
huffman@16551
   164
by (simp_all add: match_sinl_def)
huffman@16551
   165
huffman@16551
   166
lemma match_sinr_simps [simp]:
huffman@16551
   167
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>x) = return\<cdot>x"
huffman@16551
   168
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x) = fail"
huffman@16551
   169
  "match_sinr\<cdot>\<bottom> = \<bottom>"
huffman@16551
   170
by (simp_all add: match_sinr_def)
huffman@16551
   171
huffman@16221
   172
lemma match_up_simps [simp]:
huffman@16221
   173
  "match_up\<cdot>(up\<cdot>x) = return\<cdot>x"
huffman@16221
   174
  "match_up\<cdot>\<bottom> = \<bottom>"
huffman@16221
   175
by (simp_all add: match_up_def)
huffman@16221
   176
huffman@16460
   177
lemma match_ONE_simps [simp]:
huffman@16460
   178
  "match_ONE\<cdot>ONE = return\<cdot>()"
huffman@16460
   179
  "match_ONE\<cdot>\<bottom> = \<bottom>"
huffman@18094
   180
by (simp_all add: match_ONE_def)
huffman@16460
   181
huffman@16460
   182
lemma match_TT_simps [simp]:
huffman@16460
   183
  "match_TT\<cdot>TT = return\<cdot>()"
huffman@16460
   184
  "match_TT\<cdot>FF = fail"
huffman@16460
   185
  "match_TT\<cdot>\<bottom> = \<bottom>"
huffman@18094
   186
by (simp_all add: match_TT_def)
huffman@16460
   187
huffman@16460
   188
lemma match_FF_simps [simp]:
huffman@16460
   189
  "match_FF\<cdot>FF = return\<cdot>()"
huffman@16460
   190
  "match_FF\<cdot>TT = fail"
huffman@16460
   191
  "match_FF\<cdot>\<bottom> = \<bottom>"
huffman@18094
   192
by (simp_all add: match_FF_def)
huffman@16460
   193
huffman@16401
   194
subsection {* Mutual recursion *}
huffman@16401
   195
huffman@16401
   196
text {*
huffman@16401
   197
  The following rules are used to prove unfolding theorems from
huffman@16401
   198
  fixed-point definitions of mutually recursive functions.
huffman@16401
   199
*}
huffman@16401
   200
huffman@16401
   201
lemma cpair_equalI: "\<lbrakk>x \<equiv> cfst\<cdot>p; y \<equiv> csnd\<cdot>p\<rbrakk> \<Longrightarrow> <x,y> \<equiv> p"
huffman@16401
   202
by (simp add: surjective_pairing_Cprod2)
huffman@16401
   203
huffman@16401
   204
lemma cpair_eqD1: "<x,y> = <x',y'> \<Longrightarrow> x = x'"
huffman@16401
   205
by simp
huffman@16401
   206
huffman@16401
   207
lemma cpair_eqD2: "<x,y> = <x',y'> \<Longrightarrow> y = y'"
huffman@16401
   208
by simp
huffman@16401
   209
huffman@16463
   210
text {* lemma for proving rewrite rules *}
huffman@16463
   211
huffman@16463
   212
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   213
by simp
huffman@16463
   214
huffman@16401
   215
ML {*
huffman@16401
   216
val cpair_equalI = thm "cpair_equalI";
huffman@16401
   217
val cpair_eqD1 = thm "cpair_eqD1";
huffman@16401
   218
val cpair_eqD2 = thm "cpair_eqD2";
huffman@16463
   219
val ssubst_lhs = thm "ssubst_lhs";
huffman@16401
   220
*}
huffman@16221
   221
huffman@16758
   222
subsection {* Initializing the fixrec package *}
huffman@16221
   223
huffman@16229
   224
use "fixrec_package.ML"
huffman@16221
   225
huffman@16221
   226
end