src/HOLCF/Fixrec.thy
author huffman
Sun Nov 06 01:21:37 2005 +0100 (2005-11-06)
changeset 18097 d196d84c306f
parent 18096 574aa0487069
child 18110 08ec4f1f116d
permissions -rw-r--r--
add case syntax stuff
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@18097
    34
subsubsection {* Monadic bind operator *}
huffman@16221
    35
huffman@18097
    36
constdefs
huffman@18097
    37
  bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe"
huffman@18097
    38
  "bind \<equiv> \<Lambda> m f. sscase\<cdot>sinl\<cdot>(fup\<cdot>f)\<cdot>m"
huffman@18097
    39
huffman@18097
    40
syntax ">>=" :: "['a maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'b maybe" (infixl ">>=" 50)
huffman@18097
    41
translations "m >>= f" == "bind\<cdot>m\<cdot>f"
huffman@16221
    42
huffman@16221
    43
nonterminals
huffman@16221
    44
  maybebind maybebinds
huffman@16221
    45
huffman@16221
    46
syntax 
huffman@16221
    47
  "_MBIND"  :: "pttrn \<Rightarrow> 'a maybe \<Rightarrow> maybebind"         ("(2_ <-/ _)" 10)
huffman@16221
    48
  ""        :: "maybebind \<Rightarrow> maybebinds"                ("_")
huffman@16221
    49
huffman@16221
    50
  "_MBINDS" :: "[maybebind, maybebinds] \<Rightarrow> maybebinds"  ("_;/ _")
huffman@16221
    51
  "_MDO"    :: "[maybebinds, 'a maybe] \<Rightarrow> 'a maybe"     ("(do _;/ (_))" 10)
huffman@16221
    52
huffman@16221
    53
translations
huffman@16221
    54
  "_MDO (_MBINDS b bs) e" == "_MDO b (_MDO bs e)"
huffman@16221
    55
  "do (x,y) <- m; e" == "m >>= (LAM <x,y>. e)" 
huffman@16221
    56
  "do x <- m; e"            == "m >>= (LAM x. e)"
huffman@16221
    57
huffman@16221
    58
text {* monad laws *}
huffman@16221
    59
huffman@16221
    60
lemma bind_strict [simp]: "UU >>= f = UU"
huffman@16221
    61
by (simp add: bind_def)
huffman@16221
    62
huffman@16221
    63
lemma bind_fail [simp]: "fail >>= f = fail"
huffman@16221
    64
by (simp add: bind_def fail_def)
huffman@16221
    65
huffman@16221
    66
lemma left_unit [simp]: "(return\<cdot>a) >>= k = k\<cdot>a"
huffman@16221
    67
by (simp add: bind_def return_def)
huffman@16221
    68
huffman@16221
    69
lemma right_unit [simp]: "m >>= return = m"
huffman@16221
    70
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    71
huffman@16221
    72
lemma bind_assoc [simp]:
huffman@16779
    73
 "(do b <- (do a <- m; k\<cdot>a); h\<cdot>b) = (do a <- m; b <- k\<cdot>a; h\<cdot>b)"
huffman@16221
    74
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    75
huffman@18097
    76
subsubsection {* Run operator *}
huffman@16221
    77
huffman@16221
    78
constdefs
huffman@16776
    79
  run:: "'a::pcpo maybe \<rightarrow> 'a"
huffman@16221
    80
  "run \<equiv> sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)"
huffman@16221
    81
huffman@16221
    82
text {* rewrite rules for run *}
huffman@16221
    83
huffman@16221
    84
lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
huffman@16221
    85
by (simp add: run_def)
huffman@16221
    86
huffman@16221
    87
lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
huffman@16221
    88
by (simp add: run_def fail_def)
huffman@16221
    89
huffman@16221
    90
lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
huffman@16221
    91
by (simp add: run_def return_def)
huffman@16221
    92
huffman@18097
    93
subsubsection {* Monad plus operator *}
huffman@16221
    94
huffman@18097
    95
constdefs
huffman@18097
    96
  mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe"
huffman@18097
    97
  "mplus \<equiv> \<Lambda> m1 m2. sscase\<cdot>(\<Lambda> x. m2)\<cdot>(fup\<cdot>return)\<cdot>m1"
huffman@18097
    98
huffman@18097
    99
syntax "+++" :: "['a maybe, 'a maybe] \<Rightarrow> 'a maybe" (infixr "+++" 65)
huffman@18097
   100
translations "m1 +++ m2" == "mplus\<cdot>m1\<cdot>m2"
huffman@16221
   101
huffman@16221
   102
text {* rewrite rules for mplus *}
huffman@16221
   103
huffman@16221
   104
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
huffman@16221
   105
by (simp add: mplus_def)
huffman@16221
   106
huffman@16221
   107
lemma mplus_fail [simp]: "fail +++ m = m"
huffman@16221
   108
by (simp add: mplus_def fail_def)
huffman@16221
   109
huffman@16221
   110
lemma mplus_return [simp]: "return\<cdot>x +++ m = return\<cdot>x"
huffman@16221
   111
by (simp add: mplus_def return_def)
huffman@16221
   112
huffman@16460
   113
lemma mplus_fail2 [simp]: "m +++ fail = m"
huffman@16460
   114
by (rule_tac p=m in maybeE, simp_all)
huffman@16460
   115
huffman@16221
   116
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
huffman@16221
   117
by (rule_tac p=x in maybeE, simp_all)
huffman@16221
   118
huffman@18097
   119
subsubsection {* Fatbar combinator *}
huffman@18097
   120
huffman@18097
   121
constdefs
huffman@18097
   122
  fatbar :: "('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe)"
huffman@18097
   123
  "fatbar \<equiv> \<Lambda> a b x. a\<cdot>x +++ b\<cdot>x"
huffman@18097
   124
huffman@18097
   125
syntax
huffman@18097
   126
  "\<parallel>" :: "['a \<rightarrow> 'b maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'a \<rightarrow> 'b maybe" (infixr "\<parallel>" 60)
huffman@18097
   127
translations
huffman@18097
   128
  "m1 \<parallel> m2" == "fatbar\<cdot>m1\<cdot>m2"
huffman@18097
   129
huffman@18097
   130
lemma fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> (m \<parallel> ms)\<cdot>x = \<bottom>"
huffman@18097
   131
by (simp add: fatbar_def)
huffman@18097
   132
huffman@18097
   133
lemma fatbar2: "m\<cdot>x = fail \<Longrightarrow> (m \<parallel> ms)\<cdot>x = ms\<cdot>x"
huffman@18097
   134
by (simp add: fatbar_def)
huffman@18097
   135
huffman@18097
   136
lemma fatbar3: "m\<cdot>x = return\<cdot>y \<Longrightarrow> (m \<parallel> ms)\<cdot>x = return\<cdot>y"
huffman@18097
   137
by (simp add: fatbar_def)
huffman@18097
   138
huffman@18097
   139
lemmas fatbar_simps = fatbar1 fatbar2 fatbar3
huffman@18097
   140
huffman@18097
   141
subsection {* Pattern combinators *}
huffman@18097
   142
huffman@18097
   143
types ('a,'b,'c) pattern = "'b \<rightarrow> 'a \<rightarrow> 'c maybe"
huffman@18097
   144
huffman@18097
   145
constdefs
huffman@18097
   146
  wild_pat :: "('a, 'b, 'b) pattern"
huffman@18097
   147
  "wild_pat \<equiv> \<Lambda> r a. return\<cdot>r"
huffman@18097
   148
huffman@18097
   149
  var_pat :: "('a, 'a \<rightarrow> 'b, 'b) pattern"
huffman@18097
   150
  "var_pat \<equiv> \<Lambda> r a. return\<cdot>(r\<cdot>a)"
huffman@18097
   151
huffman@18097
   152
  as_pat :: "('a, 'b, 'c) pattern \<Rightarrow> ('a, 'a \<rightarrow> 'b, 'c) pattern"
huffman@18097
   153
  "as_pat p \<equiv> \<Lambda> r a. p\<cdot>(r\<cdot>a)\<cdot>a"
huffman@18097
   154
huffman@18097
   155
lemma wild_pat [simp]: "wild_pat\<cdot>r\<cdot>a = return\<cdot>r"
huffman@18097
   156
by (simp add: wild_pat_def)
huffman@18097
   157
huffman@18097
   158
lemma var_pat [simp]: "var_pat\<cdot>r\<cdot>a = return\<cdot>(r\<cdot>a)"
huffman@18097
   159
by (simp add: var_pat_def)
huffman@18097
   160
huffman@18097
   161
lemma as_pat [simp]: "as_pat p\<cdot>r\<cdot>a = p\<cdot>(r\<cdot>a)\<cdot>a"
huffman@18097
   162
by (simp add: as_pat_def)
huffman@18097
   163
huffman@18097
   164
subsection {* Case syntax *}
huffman@18097
   165
huffman@18097
   166
nonterminals
huffman@18097
   167
  Case_syn  Cases_syn
huffman@18097
   168
huffman@18097
   169
syntax
huffman@18097
   170
  "_Case_syntax":: "['a, Cases_syn] => 'b"               ("(Case _ of/ _)" 10)
huffman@18097
   171
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ =>/ _)" 10)
huffman@18097
   172
  ""            :: "Case_syn => Cases_syn"               ("_")
huffman@18097
   173
  "_Case2"      :: "[Case_syn, Cases_syn] => Cases_syn"  ("_/ | _")
huffman@18097
   174
  "_as_pattern" :: "[idt, 'a] \<Rightarrow> 'a"                     (infixr "as" 10)
huffman@18097
   175
huffman@18097
   176
syntax (xsymbols)
huffman@18097
   177
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
huffman@18097
   178
huffman@18097
   179
syntax
huffman@18097
   180
  "_match"   :: "'a \<Rightarrow> Case_syn" (* or Cases_syn *)
huffman@18097
   181
  "_as"      :: "[pttrn, Case_syn] \<Rightarrow> Case_syn"
huffman@18097
   182
  "_matches" :: "['a, Case_syn, 'a list] \<Rightarrow> Case_syn"
huffman@18097
   183
  "_cons"    :: "['a, 'a list] \<Rightarrow> 'a list"
huffman@18097
   184
  "_nil"     :: "'a list"
huffman@18097
   185
huffman@18097
   186
translations
huffman@18097
   187
  "_Case_syntax x (_match m)"     == "run\<cdot>(m\<cdot>x)"
huffman@18097
   188
  "_Case2 (_match c) (_match cs)" == "_match (c \<parallel> cs)"
huffman@18097
   189
  "_Case1 dummy_pattern r"        == "_match (wild_pat\<cdot>r)"
huffman@18097
   190
  "_as x (_match (p\<cdot>t))"          == "_match ((as_pat p)\<cdot>(\<Lambda> x. t))"
huffman@18097
   191
  "_Case1 (_as_pattern x e) r"    == "_as x (_Case1 e r)"
huffman@18097
   192
  "_Case1 x t"                    == "_match (var_pat\<cdot>(\<Lambda> x. t))"
huffman@18097
   193
  "_Case1 (f\<cdot>e) r" == "_matches f (_Case1 e r) _nil"
huffman@18097
   194
  "_matches (f\<cdot>e) (_match (p\<cdot>r)) ps" == "_matches f (_Case1 e r) (_cons p ps)"
huffman@18097
   195
huffman@18097
   196
lemma run_fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = \<bottom>"
huffman@18097
   197
by (simp add: fatbar_def)
huffman@18097
   198
huffman@18097
   199
lemma run_fatbar2: "m\<cdot>x = fail \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = run\<cdot>(ms\<cdot>x)"
huffman@18097
   200
by (simp add: fatbar_def)
huffman@18097
   201
huffman@18097
   202
lemma run_fatbar3: "m\<cdot>x = return\<cdot>y \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = y"
huffman@18097
   203
by (simp add: fatbar_def)
huffman@18097
   204
huffman@18097
   205
lemmas run_fatbar_simps [simp] = run_fatbar1 run_fatbar2 run_fatbar3
huffman@18097
   206
huffman@18097
   207
subsection {* Pattern combinators for built-in types *}
huffman@18097
   208
huffman@18097
   209
constdefs
huffman@18097
   210
  cpair_pat :: "_"
huffman@18097
   211
  "cpair_pat p1 p2 \<equiv> \<Lambda> r1 \<langle>x1,x2\<rangle>. bind\<cdot>(p1\<cdot>r1\<cdot>x1)\<cdot>(\<Lambda> r2. p2\<cdot>r2\<cdot>x2)"
huffman@18097
   212
huffman@18097
   213
  spair_pat :: "_"
huffman@18097
   214
  "spair_pat p1 p2 \<equiv> \<Lambda> r (:x,y:). cpair_pat p1 p2\<cdot>r\<cdot>\<langle>x,y\<rangle>"
huffman@18097
   215
huffman@18097
   216
  sinl_pat :: "_"
huffman@18097
   217
  "sinl_pat p \<equiv> \<Lambda> r a. case a of sinl\<cdot>x \<Rightarrow> p\<cdot>r\<cdot>x | sinr\<cdot>y \<Rightarrow> fail"
huffman@18097
   218
huffman@18097
   219
  sinr_pat :: "_"
huffman@18097
   220
  "sinr_pat p \<equiv> \<Lambda> r a. case a of sinl\<cdot>x \<Rightarrow> fail | sinr\<cdot>y \<Rightarrow> p\<cdot>r\<cdot>y"
huffman@18097
   221
huffman@18097
   222
  up_pat :: "_"
huffman@18097
   223
  "up_pat p \<equiv> \<Lambda> r a. case a of up\<cdot>x \<Rightarrow> p\<cdot>r\<cdot>x"
huffman@18097
   224
huffman@18097
   225
  Def_pat :: "'a::type \<Rightarrow> ('a lift, 'b, 'b) pattern"
huffman@18097
   226
  "Def_pat x \<equiv> \<Lambda> r. FLIFT y. if x = y then return\<cdot>r else fail"
huffman@18097
   227
huffman@18097
   228
  ONE_pat :: "_"
huffman@18097
   229
  "ONE_pat \<equiv> \<Lambda> r ONE. return\<cdot>r"
huffman@18097
   230
huffman@18097
   231
  TT_pat :: "(tr, _, _) pattern"
huffman@18097
   232
  "TT_pat \<equiv> \<Lambda> r b. If b then return\<cdot>r else fail fi"
huffman@18097
   233
huffman@18097
   234
  FF_pat :: "(tr, _, _) pattern"
huffman@18097
   235
  "FF_pat \<equiv> \<Lambda> r b. If b then fail else return\<cdot>r fi"
huffman@18097
   236
huffman@18097
   237
translations
huffman@18097
   238
  "_matches cpair (_match (p1\<cdot>r)) (_cons p2 _nil)"
huffman@18097
   239
    == "_match ((cpair_pat p1 p2)\<cdot>r)"
huffman@18097
   240
huffman@18097
   241
  "_matches spair (_match (p1\<cdot>r)) (_cons p2 _nil)"
huffman@18097
   242
    == "_match ((spair_pat p1 p2)\<cdot>r)"
huffman@18097
   243
huffman@18097
   244
translations
huffman@18097
   245
  "_matches sinl (_match (p1\<cdot>r)) _nil" == "_match ((sinl_pat p1)\<cdot>r)"
huffman@18097
   246
  "_matches sinr (_match (p1\<cdot>r)) _nil" == "_match ((sinr_pat p1)\<cdot>r)"
huffman@18097
   247
  "_matches up (_match (p1\<cdot>r)) _nil" == "_match ((up_pat p1)\<cdot>r)"
huffman@18097
   248
huffman@18097
   249
translations
huffman@18097
   250
  "_Case1 (Def x) r" == "_match (Def_pat x\<cdot>r)"
huffman@18097
   251
  "_Case1 ONE r" == "_match (ONE_pat\<cdot>r)"
huffman@18097
   252
  "_Case1 TT r" == "_match (TT_pat\<cdot>r)"
huffman@18097
   253
  "_Case1 FF r" == "_match (FF_pat\<cdot>r)"
huffman@18097
   254
huffman@18097
   255
lemma cpair_pat_simps [simp]:
huffman@18097
   256
  "p1\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> cpair_pat p1 p2\<cdot>r\<cdot><x,y> = \<bottom>"
huffman@18097
   257
  "p1\<cdot>r\<cdot>x = fail \<Longrightarrow> cpair_pat p1 p2\<cdot>r\<cdot><x,y> = fail"
huffman@18097
   258
  "p1\<cdot>r\<cdot>x = return\<cdot>r' \<Longrightarrow> cpair_pat p1 p2\<cdot>r\<cdot><x,y> = p2\<cdot>r'\<cdot>y"
huffman@18097
   259
by (simp_all add: cpair_pat_def)
huffman@18097
   260
huffman@18097
   261
lemma spair_pat_simps [simp]:
huffman@18097
   262
  "spair_pat p1 p2\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   263
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> spair_pat p1 p2\<cdot>r\<cdot>(:x, y:) = cpair_pat p1 p2\<cdot>r\<cdot>\<langle>x, y\<rangle>"
huffman@18097
   264
by (simp_all add: spair_pat_def)
huffman@18097
   265
huffman@18097
   266
lemma sinl_pat_simps [simp]:
huffman@18097
   267
  "sinl_pat p\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   268
  "x \<noteq> \<bottom> \<Longrightarrow> sinl_pat p\<cdot>r\<cdot>(sinl\<cdot>x) = p\<cdot>r\<cdot>x"
huffman@18097
   269
  "x \<noteq> \<bottom> \<Longrightarrow> sinl_pat p\<cdot>r\<cdot>(sinr\<cdot>x) = fail"
huffman@18097
   270
by (simp_all add: sinl_pat_def)
huffman@18097
   271
huffman@18097
   272
lemma sinr_pat_simps [simp]:
huffman@18097
   273
  "sinr_pat p\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   274
  "x \<noteq> \<bottom> \<Longrightarrow> sinr_pat p\<cdot>r\<cdot>(sinl\<cdot>x) = fail"
huffman@18097
   275
  "x \<noteq> \<bottom> \<Longrightarrow> sinr_pat p\<cdot>r\<cdot>(sinr\<cdot>x) = p\<cdot>r\<cdot>x"
huffman@18097
   276
by (simp_all add: sinr_pat_def)
huffman@18097
   277
huffman@18097
   278
lemma up_pat_simps [simp]:
huffman@18097
   279
  "up_pat p\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   280
  "up_pat p\<cdot>r\<cdot>(up\<cdot>x) = p\<cdot>r\<cdot>x"
huffman@18097
   281
by (simp_all add: up_pat_def)
huffman@18097
   282
huffman@18097
   283
lemma Def_pat_simps [simp]:
huffman@18097
   284
  "Def_pat x\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   285
  "Def_pat x\<cdot>r\<cdot>(Def x) = return\<cdot>r"
huffman@18097
   286
  "x \<noteq> y \<Longrightarrow> Def_pat x\<cdot>r\<cdot>(Def y) = fail"
huffman@18097
   287
by (simp_all add: Def_pat_def)
huffman@18097
   288
huffman@18097
   289
lemma ONE_pat_simps [simp]:
huffman@18097
   290
  "ONE_pat\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   291
  "ONE_pat\<cdot>r\<cdot>ONE = return\<cdot>r"
huffman@18097
   292
by (simp_all add: ONE_pat_def)
huffman@18097
   293
huffman@18097
   294
lemma TT_pat_simps [simp]:
huffman@18097
   295
  "TT_pat\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   296
  "TT_pat\<cdot>r\<cdot>TT = return\<cdot>r"
huffman@18097
   297
  "TT_pat\<cdot>r\<cdot>FF = fail"
huffman@18097
   298
by (simp_all add: TT_pat_def)
huffman@18097
   299
huffman@18097
   300
lemma FF_pat_simps [simp]:
huffman@18097
   301
  "FF_pat\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   302
  "FF_pat\<cdot>r\<cdot>TT = fail"
huffman@18097
   303
  "FF_pat\<cdot>r\<cdot>FF = return\<cdot>r"
huffman@18097
   304
by (simp_all add: FF_pat_def)
huffman@18097
   305
huffman@16221
   306
subsection {* Match functions for built-in types *}
huffman@16221
   307
huffman@16776
   308
defaultsort pcpo
huffman@16776
   309
huffman@16221
   310
constdefs
huffman@16776
   311
  match_UU :: "'a \<rightarrow> unit maybe"
huffman@16776
   312
  "match_UU \<equiv> \<Lambda> x. fail"
huffman@16776
   313
huffman@16776
   314
  match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe"
huffman@16221
   315
  "match_cpair \<equiv> csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16221
   316
huffman@16551
   317
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe"
huffman@16551
   318
  "match_spair \<equiv> ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16551
   319
huffman@16551
   320
  match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe"
huffman@16551
   321
  "match_sinl \<equiv> sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
huffman@16551
   322
huffman@16551
   323
  match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe"
huffman@16551
   324
  "match_sinr \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
huffman@16551
   325
huffman@16776
   326
  match_up :: "'a::cpo u \<rightarrow> 'a maybe"
huffman@16221
   327
  "match_up \<equiv> fup\<cdot>return"
huffman@16221
   328
huffman@16460
   329
  match_ONE :: "one \<rightarrow> unit maybe"
huffman@18094
   330
  "match_ONE \<equiv> \<Lambda> ONE. return\<cdot>()"
huffman@18094
   331
 
huffman@16460
   332
  match_TT :: "tr \<rightarrow> unit maybe"
huffman@18094
   333
  "match_TT \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi"
huffman@18094
   334
 
huffman@16460
   335
  match_FF :: "tr \<rightarrow> unit maybe"
huffman@18094
   336
  "match_FF \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi"
huffman@16460
   337
huffman@16776
   338
lemma match_UU_simps [simp]:
huffman@16776
   339
  "match_UU\<cdot>x = fail"
huffman@16776
   340
by (simp add: match_UU_def)
huffman@16776
   341
huffman@16221
   342
lemma match_cpair_simps [simp]:
huffman@16221
   343
  "match_cpair\<cdot><x,y> = return\<cdot><x,y>"
huffman@16221
   344
by (simp add: match_cpair_def)
huffman@16221
   345
huffman@16551
   346
lemma match_spair_simps [simp]:
huffman@16551
   347
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x,y:) = return\<cdot><x,y>"
huffman@16551
   348
  "match_spair\<cdot>\<bottom> = \<bottom>"
huffman@16551
   349
by (simp_all add: match_spair_def)
huffman@16551
   350
huffman@16551
   351
lemma match_sinl_simps [simp]:
huffman@16551
   352
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x) = return\<cdot>x"
huffman@16551
   353
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>x) = fail"
huffman@16551
   354
  "match_sinl\<cdot>\<bottom> = \<bottom>"
huffman@16551
   355
by (simp_all add: match_sinl_def)
huffman@16551
   356
huffman@16551
   357
lemma match_sinr_simps [simp]:
huffman@16551
   358
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>x) = return\<cdot>x"
huffman@16551
   359
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x) = fail"
huffman@16551
   360
  "match_sinr\<cdot>\<bottom> = \<bottom>"
huffman@16551
   361
by (simp_all add: match_sinr_def)
huffman@16551
   362
huffman@16221
   363
lemma match_up_simps [simp]:
huffman@16221
   364
  "match_up\<cdot>(up\<cdot>x) = return\<cdot>x"
huffman@16221
   365
  "match_up\<cdot>\<bottom> = \<bottom>"
huffman@16221
   366
by (simp_all add: match_up_def)
huffman@16221
   367
huffman@16460
   368
lemma match_ONE_simps [simp]:
huffman@16460
   369
  "match_ONE\<cdot>ONE = return\<cdot>()"
huffman@16460
   370
  "match_ONE\<cdot>\<bottom> = \<bottom>"
huffman@18094
   371
by (simp_all add: match_ONE_def)
huffman@16460
   372
huffman@16460
   373
lemma match_TT_simps [simp]:
huffman@16460
   374
  "match_TT\<cdot>TT = return\<cdot>()"
huffman@16460
   375
  "match_TT\<cdot>FF = fail"
huffman@16460
   376
  "match_TT\<cdot>\<bottom> = \<bottom>"
huffman@18094
   377
by (simp_all add: match_TT_def)
huffman@16460
   378
huffman@16460
   379
lemma match_FF_simps [simp]:
huffman@16460
   380
  "match_FF\<cdot>FF = return\<cdot>()"
huffman@16460
   381
  "match_FF\<cdot>TT = fail"
huffman@16460
   382
  "match_FF\<cdot>\<bottom> = \<bottom>"
huffman@18094
   383
by (simp_all add: match_FF_def)
huffman@16460
   384
huffman@16401
   385
subsection {* Mutual recursion *}
huffman@16401
   386
huffman@16401
   387
text {*
huffman@16401
   388
  The following rules are used to prove unfolding theorems from
huffman@16401
   389
  fixed-point definitions of mutually recursive functions.
huffman@16401
   390
*}
huffman@16401
   391
huffman@16401
   392
lemma cpair_equalI: "\<lbrakk>x \<equiv> cfst\<cdot>p; y \<equiv> csnd\<cdot>p\<rbrakk> \<Longrightarrow> <x,y> \<equiv> p"
huffman@16401
   393
by (simp add: surjective_pairing_Cprod2)
huffman@16401
   394
huffman@16401
   395
lemma cpair_eqD1: "<x,y> = <x',y'> \<Longrightarrow> x = x'"
huffman@16401
   396
by simp
huffman@16401
   397
huffman@16401
   398
lemma cpair_eqD2: "<x,y> = <x',y'> \<Longrightarrow> y = y'"
huffman@16401
   399
by simp
huffman@16401
   400
huffman@16463
   401
text {* lemma for proving rewrite rules *}
huffman@16463
   402
huffman@16463
   403
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   404
by simp
huffman@16463
   405
huffman@16401
   406
ML {*
huffman@16401
   407
val cpair_equalI = thm "cpair_equalI";
huffman@16401
   408
val cpair_eqD1 = thm "cpair_eqD1";
huffman@16401
   409
val cpair_eqD2 = thm "cpair_eqD2";
huffman@16463
   410
val ssubst_lhs = thm "ssubst_lhs";
huffman@16401
   411
*}
huffman@16221
   412
huffman@16758
   413
subsection {* Initializing the fixrec package *}
huffman@16221
   414
huffman@16229
   415
use "fixrec_package.ML"
huffman@16221
   416
huffman@16221
   417
end