src/HOLCF/Fixrec.thy
author huffman
Mon Nov 07 23:33:01 2005 +0100 (2005-11-07)
changeset 18112 dc1d6f588204
parent 18110 08ec4f1f116d
child 18293 4eaa654c92f2
permissions -rw-r--r--
reimplemented Case syntax using print/parse translations; moved as-patterns to separate section
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@18112
   141
lemma run_fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = \<bottom>"
huffman@18112
   142
by (simp add: fatbar_def)
huffman@18112
   143
huffman@18112
   144
lemma run_fatbar2: "m\<cdot>x = fail \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = run\<cdot>(ms\<cdot>x)"
huffman@18112
   145
by (simp add: fatbar_def)
huffman@18112
   146
huffman@18112
   147
lemma run_fatbar3: "m\<cdot>x = return\<cdot>y \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = y"
huffman@18112
   148
by (simp add: fatbar_def)
huffman@18112
   149
huffman@18112
   150
lemmas run_fatbar_simps [simp] = run_fatbar1 run_fatbar2 run_fatbar3
huffman@18112
   151
huffman@18097
   152
subsection {* Pattern combinators *}
huffman@18097
   153
huffman@18112
   154
types ('a,'b,'c) pat = "'b \<rightarrow> 'a \<rightarrow> 'c maybe"
huffman@18097
   155
huffman@18097
   156
constdefs
huffman@18112
   157
  wild_pat :: "('a, 'b, 'b) pat"
huffman@18097
   158
  "wild_pat \<equiv> \<Lambda> r a. return\<cdot>r"
huffman@18097
   159
huffman@18112
   160
  var_pat :: "('a, 'a \<rightarrow> 'b, 'b) pat"
huffman@18097
   161
  "var_pat \<equiv> \<Lambda> r a. return\<cdot>(r\<cdot>a)"
huffman@18097
   162
huffman@18097
   163
lemma wild_pat [simp]: "wild_pat\<cdot>r\<cdot>a = return\<cdot>r"
huffman@18097
   164
by (simp add: wild_pat_def)
huffman@18097
   165
huffman@18097
   166
lemma var_pat [simp]: "var_pat\<cdot>r\<cdot>a = return\<cdot>(r\<cdot>a)"
huffman@18097
   167
by (simp add: var_pat_def)
huffman@18097
   168
huffman@18097
   169
subsection {* Case syntax *}
huffman@18097
   170
huffman@18097
   171
nonterminals
huffman@18097
   172
  Case_syn  Cases_syn
huffman@18097
   173
huffman@18097
   174
syntax
huffman@18097
   175
  "_Case_syntax":: "['a, Cases_syn] => 'b"               ("(Case _ of/ _)" 10)
huffman@18097
   176
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ =>/ _)" 10)
huffman@18097
   177
  ""            :: "Case_syn => Cases_syn"               ("_")
huffman@18097
   178
  "_Case2"      :: "[Case_syn, Cases_syn] => Cases_syn"  ("_/ | _")
huffman@18097
   179
huffman@18097
   180
syntax (xsymbols)
huffman@18097
   181
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
huffman@18097
   182
huffman@18112
   183
text {* Intermediate tags for parsing/printing *}
huffman@18097
   184
syntax
huffman@18112
   185
  "_pat" :: "'a"
huffman@18112
   186
  "_var" :: "'a"
huffman@18112
   187
  "_match" :: "'a"
huffman@18112
   188
huffman@18112
   189
text {* Parsing Case expressions *}
huffman@18097
   190
huffman@18097
   191
translations
huffman@18112
   192
  "_Case_syntax x cs" => "run\<cdot>(cs\<cdot>x)"
huffman@18112
   193
  "_Case2 c cs" => "fatbar\<cdot>c\<cdot>cs"
huffman@18112
   194
  "_Case1 p r" => "_match (_var p) r"
huffman@18112
   195
  "_var _" => "wild_pat"
huffman@18112
   196
huffman@18112
   197
parse_translation {*
huffman@18112
   198
  let
huffman@18112
   199
    fun capp (t,u) = Syntax.const "Rep_CFun" $ t $ u;
huffman@18112
   200
    fun cabs (x,t) = (snd (mk_binder_tr ("_cabs", "Abs_CFun"))) [x,t];
huffman@18112
   201
huffman@18112
   202
    fun get_vars (Const ("_var",_) $ x) = [x]
huffman@18112
   203
    |   get_vars (t $ u) = get_vars t @ get_vars u
huffman@18112
   204
    |   get_vars t = [];
huffman@18112
   205
huffman@18112
   206
    fun rem_vars (Const ("_var",_) $ x) = Syntax.const "var_pat"
huffman@18112
   207
    |   rem_vars (t $ u) = rem_vars t $ rem_vars u
huffman@18112
   208
    |   rem_vars t = t;
huffman@18112
   209
huffman@18112
   210
    fun match_tr [pat, rhs] =
huffman@18112
   211
          capp (rem_vars pat, foldr cabs rhs (get_vars pat))
huffman@18112
   212
    |   match_tr ts = raise TERM ("match_tr", ts);
huffman@18112
   213
huffman@18112
   214
  in [("_match", match_tr)] end;
huffman@18112
   215
*}
huffman@18112
   216
huffman@18112
   217
text {* Printing Case expressions *}
huffman@18112
   218
huffman@18112
   219
translations
huffman@18112
   220
  "_" <= "_pat wild_pat"
huffman@18112
   221
  "x" <= "_pat (_var x)"
huffman@18097
   222
huffman@18112
   223
print_translation {*
huffman@18112
   224
  let
huffman@18112
   225
    fun dest_cabs (Const ("Abs_CFun",_) $ t) =
huffman@18112
   226
          let val abs = case t of Abs abs => abs
huffman@18112
   227
                  | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
huffman@18112
   228
          in atomic_abs_tr' abs end
huffman@18112
   229
    |   dest_cabs _ = raise Match; (* too few vars: abort translation *)
huffman@18097
   230
huffman@18112
   231
    fun put_vars (Const ("var_pat",_), rhs) =
huffman@18112
   232
          let val (x, rhs') = dest_cabs rhs;
huffman@18112
   233
          in (Syntax.const "_var" $ x, rhs') end
huffman@18112
   234
    |   put_vars (t $ u, rhs) =
huffman@18112
   235
          let
huffman@18112
   236
            val (t', rhs') = put_vars (t,rhs);
huffman@18112
   237
            val (u', rhs'') = put_vars (u,rhs');
huffman@18112
   238
          in (t' $ u', rhs'') end
huffman@18112
   239
    |   put_vars (t, rhs) = (t, rhs);
huffman@18097
   240
huffman@18112
   241
    fun Case1_tr' (_ $ pat $ rhs) = let
huffman@18112
   242
          val (pat', rhs') = put_vars (pat, rhs);
huffman@18112
   243
        in Syntax.const "_Case1" $ (Syntax.const "_pat" $ pat') $ rhs' end;
huffman@18097
   244
huffman@18112
   245
    fun Case2_tr' (_ $ (_ $ Const ("fatbar",_) $ m) $ ms) =
huffman@18112
   246
          Syntax.const "_Case2" $ Case1_tr' m $ Case2_tr' ms
huffman@18112
   247
    |   Case2_tr' t = Case1_tr' t;
huffman@18112
   248
huffman@18112
   249
    fun Case_syntax_tr' [Const ("run",_), _ $ ms $ x] =
huffman@18112
   250
          Syntax.const "_Case_syntax" $ x $ Case2_tr' ms;
huffman@18112
   251
huffman@18112
   252
  in [("Rep_CFun", Case_syntax_tr')] end;
huffman@18112
   253
*}
huffman@18097
   254
huffman@18097
   255
subsection {* Pattern combinators for built-in types *}
huffman@18097
   256
huffman@18097
   257
constdefs
huffman@18112
   258
  cpair_pat :: "('a, _, _) pat \<Rightarrow> ('b, _, _) pat \<Rightarrow> ('a \<times> 'b, _, _) pat"
huffman@18097
   259
  "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
   260
huffman@18112
   261
  spair_pat :: "(_, _, _) pat \<Rightarrow> (_, _, _) pat \<Rightarrow> (_, _, _) pat"
huffman@18097
   262
  "spair_pat p1 p2 \<equiv> \<Lambda> r (:x,y:). cpair_pat p1 p2\<cdot>r\<cdot>\<langle>x,y\<rangle>"
huffman@18097
   263
huffman@18112
   264
  sinl_pat :: "(_, _, _) pat \<Rightarrow> (_, _, _) pat"
huffman@18097
   265
  "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
   266
huffman@18112
   267
  sinr_pat :: "(_, _, _) pat \<Rightarrow> (_, _, _) pat"
huffman@18097
   268
  "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
   269
huffman@18112
   270
  up_pat :: "('a, _, _) pat \<Rightarrow> ('a u, _, _) pat"
huffman@18097
   271
  "up_pat p \<equiv> \<Lambda> r a. case a of up\<cdot>x \<Rightarrow> p\<cdot>r\<cdot>x"
huffman@18097
   272
huffman@18112
   273
  Def_pat :: "'a::type \<Rightarrow> ('a lift, _, _) pat"
huffman@18097
   274
  "Def_pat x \<equiv> \<Lambda> r. FLIFT y. if x = y then return\<cdot>r else fail"
huffman@18097
   275
huffman@18112
   276
  ONE_pat :: "(one, _, _) pat"
huffman@18097
   277
  "ONE_pat \<equiv> \<Lambda> r ONE. return\<cdot>r"
huffman@18097
   278
huffman@18112
   279
  TT_pat :: "(tr, _, _) pat"
huffman@18097
   280
  "TT_pat \<equiv> \<Lambda> r b. If b then return\<cdot>r else fail fi"
huffman@18097
   281
huffman@18112
   282
  FF_pat :: "(tr, _, _) pat"
huffman@18097
   283
  "FF_pat \<equiv> \<Lambda> r b. If b then fail else return\<cdot>r fi"
huffman@18097
   284
huffman@18112
   285
text {* Parse translations *}
huffman@18097
   286
translations
huffman@18112
   287
  "_var (cpair\<cdot>p1\<cdot>p2)" => "cpair_pat (_var p1) (_var p2)"
huffman@18112
   288
  "_var (spair\<cdot>p1\<cdot>p2)" => "spair_pat (_var p1) (_var p2)"
huffman@18112
   289
  "_var (sinl\<cdot>p1)"     => "sinl_pat (_var p1)"
huffman@18112
   290
  "_var (sinr\<cdot>p1)"     => "sinr_pat (_var p1)"
huffman@18112
   291
  "_var (up\<cdot>p1)"       => "up_pat (_var p1)"
huffman@18112
   292
  "_var (Def x)"       => "Def_pat x"
huffman@18112
   293
  "_var ONE"           => "ONE_pat"
huffman@18112
   294
  "_var TT"            => "TT_pat"
huffman@18112
   295
  "_var FF"            => "FF_pat"
huffman@18097
   296
huffman@18112
   297
text {* Print translations *}
huffman@18097
   298
translations
huffman@18112
   299
  "cpair\<cdot>(_pat p1)\<cdot>(_pat p2)" <= "_pat (cpair_pat p1 p2)"
huffman@18112
   300
  "spair\<cdot>(_pat p1)\<cdot>(_pat p2)" <= "_pat (spair_pat p1 p2)"
huffman@18112
   301
  "sinl\<cdot>(_pat p1)"            <= "_pat (sinl_pat p1)"
huffman@18112
   302
  "sinr\<cdot>(_pat p1)"            <= "_pat (sinr_pat p1)"
huffman@18112
   303
  "up\<cdot>(_pat p1)"              <= "_pat (up_pat p1)"
huffman@18112
   304
  "Def x"                     <= "_pat (Def_pat x)"
huffman@18112
   305
  "TT"                        <= "_pat (TT_pat)"
huffman@18112
   306
  "FF"                        <= "_pat (FF_pat)"
huffman@18097
   307
huffman@18097
   308
lemma cpair_pat_simps [simp]:
huffman@18097
   309
  "p1\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> cpair_pat p1 p2\<cdot>r\<cdot><x,y> = \<bottom>"
huffman@18097
   310
  "p1\<cdot>r\<cdot>x = fail \<Longrightarrow> cpair_pat p1 p2\<cdot>r\<cdot><x,y> = fail"
huffman@18097
   311
  "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
   312
by (simp_all add: cpair_pat_def)
huffman@18097
   313
huffman@18097
   314
lemma spair_pat_simps [simp]:
huffman@18097
   315
  "spair_pat p1 p2\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   316
  "\<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
   317
by (simp_all add: spair_pat_def)
huffman@18097
   318
huffman@18097
   319
lemma sinl_pat_simps [simp]:
huffman@18097
   320
  "sinl_pat p\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   321
  "x \<noteq> \<bottom> \<Longrightarrow> sinl_pat p\<cdot>r\<cdot>(sinl\<cdot>x) = p\<cdot>r\<cdot>x"
huffman@18097
   322
  "x \<noteq> \<bottom> \<Longrightarrow> sinl_pat p\<cdot>r\<cdot>(sinr\<cdot>x) = fail"
huffman@18097
   323
by (simp_all add: sinl_pat_def)
huffman@18097
   324
huffman@18097
   325
lemma sinr_pat_simps [simp]:
huffman@18097
   326
  "sinr_pat p\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   327
  "x \<noteq> \<bottom> \<Longrightarrow> sinr_pat p\<cdot>r\<cdot>(sinl\<cdot>x) = fail"
huffman@18097
   328
  "x \<noteq> \<bottom> \<Longrightarrow> sinr_pat p\<cdot>r\<cdot>(sinr\<cdot>x) = p\<cdot>r\<cdot>x"
huffman@18097
   329
by (simp_all add: sinr_pat_def)
huffman@18097
   330
huffman@18097
   331
lemma up_pat_simps [simp]:
huffman@18097
   332
  "up_pat p\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   333
  "up_pat p\<cdot>r\<cdot>(up\<cdot>x) = p\<cdot>r\<cdot>x"
huffman@18097
   334
by (simp_all add: up_pat_def)
huffman@18097
   335
huffman@18097
   336
lemma Def_pat_simps [simp]:
huffman@18097
   337
  "Def_pat x\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   338
  "Def_pat x\<cdot>r\<cdot>(Def x) = return\<cdot>r"
huffman@18097
   339
  "x \<noteq> y \<Longrightarrow> Def_pat x\<cdot>r\<cdot>(Def y) = fail"
huffman@18097
   340
by (simp_all add: Def_pat_def)
huffman@18097
   341
huffman@18097
   342
lemma ONE_pat_simps [simp]:
huffman@18097
   343
  "ONE_pat\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   344
  "ONE_pat\<cdot>r\<cdot>ONE = return\<cdot>r"
huffman@18097
   345
by (simp_all add: ONE_pat_def)
huffman@18097
   346
huffman@18097
   347
lemma TT_pat_simps [simp]:
huffman@18097
   348
  "TT_pat\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   349
  "TT_pat\<cdot>r\<cdot>TT = return\<cdot>r"
huffman@18097
   350
  "TT_pat\<cdot>r\<cdot>FF = fail"
huffman@18097
   351
by (simp_all add: TT_pat_def)
huffman@18097
   352
huffman@18097
   353
lemma FF_pat_simps [simp]:
huffman@18097
   354
  "FF_pat\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18097
   355
  "FF_pat\<cdot>r\<cdot>TT = fail"
huffman@18097
   356
  "FF_pat\<cdot>r\<cdot>FF = return\<cdot>r"
huffman@18097
   357
by (simp_all add: FF_pat_def)
huffman@18097
   358
huffman@18112
   359
subsection {* As-patterns *}
huffman@18112
   360
huffman@18112
   361
syntax
huffman@18112
   362
  "_as_pattern" :: "['a, 'a] \<Rightarrow> 'a" (* infixr "as" 10 *)
huffman@18112
   363
  (* TODO: choose a non-ambiguous syntax for as-patterns *)
huffman@18112
   364
huffman@18112
   365
constdefs
huffman@18112
   366
  as_pat :: "('a,'b,'c) pat \<Rightarrow> ('a,'c,'d) pat \<Rightarrow> ('a,'b,'d) pat"
huffman@18112
   367
  "as_pat p1 p2 \<equiv> \<Lambda> r a. cpair_pat p1 p2\<cdot>r\<cdot>\<langle>a, a\<rangle>"
huffman@18112
   368
huffman@18112
   369
translations
huffman@18112
   370
  "_var (_as_pattern p1 p2)" => "as_pat (_var p1) (_var p2)"
huffman@18112
   371
  "_as_pattern (_pat p1) (_pat p2)" <= "_pat (as_pat p1 p2)"
huffman@18112
   372
huffman@18112
   373
lemma as_pat [simp]: "as_pat p1 p2\<cdot>r\<cdot>a = cpair_pat p1 p2\<cdot>r\<cdot>\<langle>a, a\<rangle>"
huffman@18112
   374
by (simp add: as_pat_def)
huffman@18112
   375
huffman@16221
   376
subsection {* Match functions for built-in types *}
huffman@16221
   377
huffman@16776
   378
defaultsort pcpo
huffman@16776
   379
huffman@16221
   380
constdefs
huffman@16776
   381
  match_UU :: "'a \<rightarrow> unit maybe"
huffman@16776
   382
  "match_UU \<equiv> \<Lambda> x. fail"
huffman@16776
   383
huffman@16776
   384
  match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe"
huffman@16221
   385
  "match_cpair \<equiv> csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16221
   386
huffman@16551
   387
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe"
huffman@16551
   388
  "match_spair \<equiv> ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16551
   389
huffman@16551
   390
  match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe"
huffman@16551
   391
  "match_sinl \<equiv> sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
huffman@16551
   392
huffman@16551
   393
  match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe"
huffman@16551
   394
  "match_sinr \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
huffman@16551
   395
huffman@16776
   396
  match_up :: "'a::cpo u \<rightarrow> 'a maybe"
huffman@16221
   397
  "match_up \<equiv> fup\<cdot>return"
huffman@16221
   398
huffman@16460
   399
  match_ONE :: "one \<rightarrow> unit maybe"
huffman@18094
   400
  "match_ONE \<equiv> \<Lambda> ONE. return\<cdot>()"
huffman@18094
   401
 
huffman@16460
   402
  match_TT :: "tr \<rightarrow> unit maybe"
huffman@18094
   403
  "match_TT \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi"
huffman@18094
   404
 
huffman@16460
   405
  match_FF :: "tr \<rightarrow> unit maybe"
huffman@18094
   406
  "match_FF \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi"
huffman@16460
   407
huffman@16776
   408
lemma match_UU_simps [simp]:
huffman@16776
   409
  "match_UU\<cdot>x = fail"
huffman@16776
   410
by (simp add: match_UU_def)
huffman@16776
   411
huffman@16221
   412
lemma match_cpair_simps [simp]:
huffman@16221
   413
  "match_cpair\<cdot><x,y> = return\<cdot><x,y>"
huffman@16221
   414
by (simp add: match_cpair_def)
huffman@16221
   415
huffman@16551
   416
lemma match_spair_simps [simp]:
huffman@16551
   417
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x,y:) = return\<cdot><x,y>"
huffman@16551
   418
  "match_spair\<cdot>\<bottom> = \<bottom>"
huffman@16551
   419
by (simp_all add: match_spair_def)
huffman@16551
   420
huffman@16551
   421
lemma match_sinl_simps [simp]:
huffman@16551
   422
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x) = return\<cdot>x"
huffman@16551
   423
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>x) = fail"
huffman@16551
   424
  "match_sinl\<cdot>\<bottom> = \<bottom>"
huffman@16551
   425
by (simp_all add: match_sinl_def)
huffman@16551
   426
huffman@16551
   427
lemma match_sinr_simps [simp]:
huffman@16551
   428
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>x) = return\<cdot>x"
huffman@16551
   429
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x) = fail"
huffman@16551
   430
  "match_sinr\<cdot>\<bottom> = \<bottom>"
huffman@16551
   431
by (simp_all add: match_sinr_def)
huffman@16551
   432
huffman@16221
   433
lemma match_up_simps [simp]:
huffman@16221
   434
  "match_up\<cdot>(up\<cdot>x) = return\<cdot>x"
huffman@16221
   435
  "match_up\<cdot>\<bottom> = \<bottom>"
huffman@16221
   436
by (simp_all add: match_up_def)
huffman@16221
   437
huffman@16460
   438
lemma match_ONE_simps [simp]:
huffman@16460
   439
  "match_ONE\<cdot>ONE = return\<cdot>()"
huffman@16460
   440
  "match_ONE\<cdot>\<bottom> = \<bottom>"
huffman@18094
   441
by (simp_all add: match_ONE_def)
huffman@16460
   442
huffman@16460
   443
lemma match_TT_simps [simp]:
huffman@16460
   444
  "match_TT\<cdot>TT = return\<cdot>()"
huffman@16460
   445
  "match_TT\<cdot>FF = fail"
huffman@16460
   446
  "match_TT\<cdot>\<bottom> = \<bottom>"
huffman@18094
   447
by (simp_all add: match_TT_def)
huffman@16460
   448
huffman@16460
   449
lemma match_FF_simps [simp]:
huffman@16460
   450
  "match_FF\<cdot>FF = return\<cdot>()"
huffman@16460
   451
  "match_FF\<cdot>TT = fail"
huffman@16460
   452
  "match_FF\<cdot>\<bottom> = \<bottom>"
huffman@18094
   453
by (simp_all add: match_FF_def)
huffman@16460
   454
huffman@16401
   455
subsection {* Mutual recursion *}
huffman@16401
   456
huffman@16401
   457
text {*
huffman@16401
   458
  The following rules are used to prove unfolding theorems from
huffman@16401
   459
  fixed-point definitions of mutually recursive functions.
huffman@16401
   460
*}
huffman@16401
   461
huffman@16401
   462
lemma cpair_equalI: "\<lbrakk>x \<equiv> cfst\<cdot>p; y \<equiv> csnd\<cdot>p\<rbrakk> \<Longrightarrow> <x,y> \<equiv> p"
huffman@16401
   463
by (simp add: surjective_pairing_Cprod2)
huffman@16401
   464
huffman@16401
   465
lemma cpair_eqD1: "<x,y> = <x',y'> \<Longrightarrow> x = x'"
huffman@16401
   466
by simp
huffman@16401
   467
huffman@16401
   468
lemma cpair_eqD2: "<x,y> = <x',y'> \<Longrightarrow> y = y'"
huffman@16401
   469
by simp
huffman@16401
   470
huffman@16463
   471
text {* lemma for proving rewrite rules *}
huffman@16463
   472
huffman@16463
   473
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   474
by simp
huffman@16463
   475
huffman@16401
   476
ML {*
huffman@16401
   477
val cpair_equalI = thm "cpair_equalI";
huffman@16401
   478
val cpair_eqD1 = thm "cpair_eqD1";
huffman@16401
   479
val cpair_eqD2 = thm "cpair_eqD2";
huffman@16463
   480
val ssubst_lhs = thm "ssubst_lhs";
huffman@16401
   481
*}
huffman@16221
   482
huffman@16758
   483
subsection {* Initializing the fixrec package *}
huffman@16221
   484
huffman@16229
   485
use "fixrec_package.ML"
huffman@16221
   486
huffman@16221
   487
end