src/HOLCF/Fixrec.thy
author wenzelm
Sun Oct 21 14:21:48 2007 +0200 (2007-10-21)
changeset 25131 2c8caac48ade
parent 23152 9497234a2743
child 25158 271e499f2d03
permissions -rw-r--r--
modernized specifications ('definition', 'abbreviation', 'notation');
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
wenzelm@23152
    10
uses ("Tools/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@19092
    17
pcpodef (open) 'a maybe = "UNIV::(one ++ 'a u) set"
huffman@19092
    18
by simp
huffman@16221
    19
huffman@16221
    20
constdefs
huffman@16221
    21
  fail :: "'a maybe"
huffman@19092
    22
  "fail \<equiv> Abs_maybe (sinl\<cdot>ONE)"
huffman@16221
    23
wenzelm@25131
    24
constdefs
wenzelm@25131
    25
  return :: "'a \<rightarrow> 'a maybe" where
huffman@19092
    26
  "return \<equiv> \<Lambda> x. Abs_maybe (sinr\<cdot>(up\<cdot>x))"
huffman@19092
    27
wenzelm@25131
    28
definition
wenzelm@25131
    29
  maybe_when :: "'b \<rightarrow> ('a \<rightarrow> 'b) \<rightarrow> 'a maybe \<rightarrow> 'b::pcpo" where
wenzelm@25131
    30
  "maybe_when = (\<Lambda> f r m. sscase\<cdot>(\<Lambda> x. f)\<cdot>(fup\<cdot>r)\<cdot>(Rep_maybe m))"
huffman@16221
    31
huffman@16221
    32
lemma maybeE:
huffman@16221
    33
  "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = return\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
huffman@16221
    34
apply (unfold fail_def return_def)
huffman@19092
    35
apply (cases p, rename_tac r)
huffman@19092
    36
apply (rule_tac p=r in ssumE, simp add: Abs_maybe_strict)
huffman@16221
    37
apply (rule_tac p=x in oneE, simp, simp)
huffman@19092
    38
apply (rule_tac p=y in upE, simp, simp add: cont_Abs_maybe)
huffman@16221
    39
done
huffman@16221
    40
huffman@18293
    41
lemma return_defined [simp]: "return\<cdot>x \<noteq> \<bottom>"
huffman@19092
    42
by (simp add: return_def cont_Abs_maybe Abs_maybe_defined)
huffman@18293
    43
huffman@18293
    44
lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
huffman@19092
    45
by (simp add: fail_def Abs_maybe_defined)
huffman@18293
    46
huffman@18293
    47
lemma return_eq [simp]: "(return\<cdot>x = return\<cdot>y) = (x = y)"
huffman@19092
    48
by (simp add: return_def cont_Abs_maybe Abs_maybe_inject)
huffman@18293
    49
huffman@18293
    50
lemma return_neq_fail [simp]:
huffman@18293
    51
  "return\<cdot>x \<noteq> fail" "fail \<noteq> return\<cdot>x"
huffman@19092
    52
by (simp_all add: return_def fail_def cont_Abs_maybe Abs_maybe_inject)
huffman@19092
    53
huffman@19092
    54
lemma maybe_when_rews [simp]:
huffman@19092
    55
  "maybe_when\<cdot>f\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@19092
    56
  "maybe_when\<cdot>f\<cdot>r\<cdot>fail = f"
huffman@19092
    57
  "maybe_when\<cdot>f\<cdot>r\<cdot>(return\<cdot>x) = r\<cdot>x"
huffman@19092
    58
by (simp_all add: return_def fail_def maybe_when_def cont_Rep_maybe
huffman@19092
    59
                  cont_Abs_maybe Abs_maybe_inverse Rep_maybe_strict)
huffman@19092
    60
huffman@19092
    61
translations
wenzelm@25131
    62
  "case m of fail \<Rightarrow> t1 | return\<cdot>x \<Rightarrow> t2" == "CONST maybe_when\<cdot>t1\<cdot>(\<Lambda> x. t2)\<cdot>m"
huffman@18293
    63
huffman@18293
    64
huffman@18097
    65
subsubsection {* Monadic bind operator *}
huffman@16221
    66
wenzelm@25131
    67
definition
wenzelm@25131
    68
  bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe" where
wenzelm@25131
    69
  "bind = (\<Lambda> m f. case m of fail \<Rightarrow> fail | return\<cdot>x \<Rightarrow> f\<cdot>x)"
huffman@16221
    70
huffman@16221
    71
text {* monad laws *}
huffman@16221
    72
huffman@19092
    73
lemma bind_strict [simp]: "bind\<cdot>\<bottom>\<cdot>f = \<bottom>"
huffman@19092
    74
by (simp add: bind_def)
huffman@19092
    75
huffman@19092
    76
lemma bind_fail [simp]: "bind\<cdot>fail\<cdot>f = fail"
huffman@16221
    77
by (simp add: bind_def)
huffman@16221
    78
huffman@19092
    79
lemma left_unit [simp]: "bind\<cdot>(return\<cdot>a)\<cdot>k = k\<cdot>a"
huffman@19092
    80
by (simp add: bind_def)
huffman@16221
    81
huffman@19092
    82
lemma right_unit [simp]: "bind\<cdot>m\<cdot>return = m"
huffman@16221
    83
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    84
huffman@19092
    85
lemma bind_assoc:
huffman@19092
    86
 "bind\<cdot>(bind\<cdot>m\<cdot>k)\<cdot>h = bind\<cdot>m\<cdot>(\<Lambda> a. bind\<cdot>(k\<cdot>a)\<cdot>h)"
huffman@16221
    87
by (rule_tac p=m in maybeE, simp_all)
huffman@16221
    88
huffman@18097
    89
subsubsection {* Run operator *}
huffman@16221
    90
wenzelm@25131
    91
definition
wenzelm@25131
    92
  run:: "'a maybe \<rightarrow> 'a::pcpo" where
wenzelm@25131
    93
  "run = maybe_when\<cdot>\<bottom>\<cdot>ID"
huffman@16221
    94
huffman@16221
    95
text {* rewrite rules for run *}
huffman@16221
    96
huffman@16221
    97
lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
huffman@16221
    98
by (simp add: run_def)
huffman@16221
    99
huffman@16221
   100
lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
huffman@19092
   101
by (simp add: run_def)
huffman@16221
   102
huffman@16221
   103
lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
huffman@19092
   104
by (simp add: run_def)
huffman@16221
   105
huffman@18097
   106
subsubsection {* Monad plus operator *}
huffman@16221
   107
wenzelm@25131
   108
definition
wenzelm@25131
   109
  mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe" where
wenzelm@25131
   110
  "mplus = (\<Lambda> m1 m2. case m1 of fail \<Rightarrow> m2 | return\<cdot>x \<Rightarrow> m1)"
huffman@18097
   111
wenzelm@25131
   112
abbreviation
wenzelm@25131
   113
  mplus_syn :: "['a maybe, 'a maybe] \<Rightarrow> 'a maybe"  (infixr "+++" 65)  where
wenzelm@25131
   114
  "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
huffman@16221
   115
huffman@16221
   116
text {* rewrite rules for mplus *}
huffman@16221
   117
huffman@16221
   118
lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
huffman@16221
   119
by (simp add: mplus_def)
huffman@16221
   120
huffman@16221
   121
lemma mplus_fail [simp]: "fail +++ m = m"
huffman@19092
   122
by (simp add: mplus_def)
huffman@16221
   123
huffman@16221
   124
lemma mplus_return [simp]: "return\<cdot>x +++ m = return\<cdot>x"
huffman@19092
   125
by (simp add: mplus_def)
huffman@16221
   126
huffman@16460
   127
lemma mplus_fail2 [simp]: "m +++ fail = m"
huffman@16460
   128
by (rule_tac p=m in maybeE, simp_all)
huffman@16460
   129
huffman@16221
   130
lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
huffman@16221
   131
by (rule_tac p=x in maybeE, simp_all)
huffman@16221
   132
huffman@18097
   133
subsubsection {* Fatbar combinator *}
huffman@18097
   134
wenzelm@25131
   135
definition
wenzelm@25131
   136
  fatbar :: "('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe)" where
wenzelm@25131
   137
  "fatbar = (\<Lambda> a b x. a\<cdot>x +++ b\<cdot>x)"
huffman@18097
   138
wenzelm@25131
   139
abbreviation
wenzelm@25131
   140
  fatbar_syn :: "['a \<rightarrow> 'b maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'a \<rightarrow> 'b maybe" (infixr "\<parallel>" 60)  where
wenzelm@25131
   141
  "m1 \<parallel> m2 == fatbar\<cdot>m1\<cdot>m2"
huffman@18097
   142
huffman@18097
   143
lemma fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> (m \<parallel> ms)\<cdot>x = \<bottom>"
huffman@18097
   144
by (simp add: fatbar_def)
huffman@18097
   145
huffman@18097
   146
lemma fatbar2: "m\<cdot>x = fail \<Longrightarrow> (m \<parallel> ms)\<cdot>x = ms\<cdot>x"
huffman@18097
   147
by (simp add: fatbar_def)
huffman@18097
   148
huffman@18097
   149
lemma fatbar3: "m\<cdot>x = return\<cdot>y \<Longrightarrow> (m \<parallel> ms)\<cdot>x = return\<cdot>y"
huffman@18097
   150
by (simp add: fatbar_def)
huffman@18097
   151
huffman@18097
   152
lemmas fatbar_simps = fatbar1 fatbar2 fatbar3
huffman@18097
   153
huffman@18112
   154
lemma run_fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = \<bottom>"
huffman@18112
   155
by (simp add: fatbar_def)
huffman@18112
   156
huffman@18112
   157
lemma run_fatbar2: "m\<cdot>x = fail \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = run\<cdot>(ms\<cdot>x)"
huffman@18112
   158
by (simp add: fatbar_def)
huffman@18112
   159
huffman@18112
   160
lemma run_fatbar3: "m\<cdot>x = return\<cdot>y \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = y"
huffman@18112
   161
by (simp add: fatbar_def)
huffman@18112
   162
huffman@18112
   163
lemmas run_fatbar_simps [simp] = run_fatbar1 run_fatbar2 run_fatbar3
huffman@18112
   164
huffman@18293
   165
subsection {* Case branch combinator *}
huffman@18097
   166
huffman@18097
   167
constdefs
huffman@18293
   168
  branch :: "('a \<rightarrow> 'b maybe) \<Rightarrow> ('b \<rightarrow> 'c) \<rightarrow> ('a \<rightarrow> 'c maybe)"
huffman@18293
   169
  "branch p \<equiv> \<Lambda> r x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> y. return\<cdot>(r\<cdot>y))"
huffman@18097
   170
huffman@18293
   171
lemma branch_rews:
huffman@18293
   172
  "p\<cdot>x = \<bottom> \<Longrightarrow> branch p\<cdot>r\<cdot>x = \<bottom>"
huffman@18293
   173
  "p\<cdot>x = fail \<Longrightarrow> branch p\<cdot>r\<cdot>x = fail"
huffman@18293
   174
  "p\<cdot>x = return\<cdot>y \<Longrightarrow> branch p\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>y)"
huffman@18293
   175
by (simp_all add: branch_def)
huffman@18097
   176
huffman@18293
   177
lemma branch_return [simp]: "branch return\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>x)"
huffman@18293
   178
by (simp add: branch_def)
huffman@18293
   179
huffman@18097
   180
huffman@18097
   181
subsection {* Case syntax *}
huffman@18097
   182
huffman@18097
   183
nonterminals
huffman@18097
   184
  Case_syn  Cases_syn
huffman@18097
   185
huffman@18097
   186
syntax
huffman@18097
   187
  "_Case_syntax":: "['a, Cases_syn] => 'b"               ("(Case _ of/ _)" 10)
huffman@18097
   188
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ =>/ _)" 10)
huffman@18097
   189
  ""            :: "Case_syn => Cases_syn"               ("_")
huffman@18097
   190
  "_Case2"      :: "[Case_syn, Cases_syn] => Cases_syn"  ("_/ | _")
huffman@18097
   191
huffman@18097
   192
syntax (xsymbols)
huffman@18097
   193
  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
huffman@18097
   194
huffman@18293
   195
translations
huffman@19439
   196
  "_Case_syntax x ms" == "Fixrec.run\<cdot>(ms\<cdot>x)"
huffman@18293
   197
  "_Case2 m ms" == "m \<parallel> ms"
huffman@18112
   198
huffman@18112
   199
text {* Parsing Case expressions *}
huffman@18097
   200
huffman@18293
   201
syntax
huffman@18293
   202
  "_pat" :: "'a"
huffman@18293
   203
  "_var" :: "'a"
huffman@18293
   204
huffman@18097
   205
translations
wenzelm@25131
   206
  "_Case1 p r" => "CONST branch (_pat p)\<cdot>(_var p r)"
wenzelm@25131
   207
  "_var (_args x y) r" => "CONST csplit\<cdot>(_var x (_var y r))"
wenzelm@25131
   208
  "_var () r" => "CONST unit_when\<cdot>r"
huffman@18112
   209
huffman@18112
   210
parse_translation {*
huffman@18293
   211
(* rewrites (_pat x) => (return) *)
huffman@18293
   212
(* rewrites (_var x t) => (Abs_CFun (%x. t)) *)
huffman@19104
   213
  [("_pat", K (Syntax.const "Fixrec.return")),
wenzelm@25131
   214
   mk_binder_tr ("_var", @{const_syntax Abs_CFun})];
huffman@18112
   215
*}
huffman@18112
   216
huffman@18112
   217
text {* Printing Case expressions *}
huffman@18112
   218
huffman@18293
   219
syntax
huffman@18293
   220
  "_match" :: "'a"
huffman@18097
   221
huffman@18112
   222
print_translation {*
huffman@18112
   223
  let
wenzelm@25131
   224
    fun dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax unit_when},_) $ t) =
wenzelm@25131
   225
          (Syntax.const @{const_syntax Unity}, t)
wenzelm@25131
   226
    |   dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax csplit},_) $ t) =
huffman@18293
   227
          let
huffman@18293
   228
            val (v1, t1) = dest_LAM t;
huffman@18293
   229
            val (v2, t2) = dest_LAM t1;
huffman@18293
   230
          in (Syntax.const "_args" $ v1 $ v2, t2) end 
wenzelm@25131
   231
    |   dest_LAM (Const (@{const_syntax Abs_CFun},_) $ t) =
huffman@18112
   232
          let
huffman@18293
   233
            val abs = case t of Abs abs => abs
huffman@18293
   234
                | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
huffman@18293
   235
            val (x, t') = atomic_abs_tr' abs;
huffman@18293
   236
          in (Syntax.const "_var" $ x, t') end
huffman@18293
   237
    |   dest_LAM _ = raise Match; (* too few vars: abort translation *)
huffman@18097
   238
wenzelm@25131
   239
    fun Case1_tr' [Const(@{const_syntax branch},_) $ p, r] =
huffman@18293
   240
          let val (v, t) = dest_LAM r;
huffman@18293
   241
          in Syntax.const "_Case1" $ (Syntax.const "_match" $ p $ v) $ t end;
huffman@18112
   242
wenzelm@25131
   243
  in [(@{const_syntax Rep_CFun}, Case1_tr')] end;
huffman@18112
   244
*}
huffman@18097
   245
huffman@18293
   246
translations
huffman@19104
   247
  "x" <= "_match Fixrec.return (_var x)"
huffman@18293
   248
huffman@18293
   249
huffman@18293
   250
subsection {* Pattern combinators for data constructors *}
huffman@18293
   251
huffman@18293
   252
types ('a, 'b) pat = "'a \<rightarrow> 'b maybe"
huffman@18097
   253
wenzelm@25131
   254
definition
wenzelm@25131
   255
  cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat" where
wenzelm@25131
   256
  "cpair_pat p1 p2 = (\<Lambda>\<langle>x, y\<rangle>.
wenzelm@25131
   257
    bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. return\<cdot>\<langle>a, b\<rangle>)))"
huffman@18097
   258
wenzelm@25131
   259
definition
huffman@18293
   260
  spair_pat ::
wenzelm@25131
   261
  "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat" where
wenzelm@25131
   262
  "spair_pat p1 p2 = (\<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>\<langle>x, y\<rangle>)"
huffman@18097
   263
wenzelm@25131
   264
definition
wenzelm@25131
   265
  sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
wenzelm@25131
   266
  "sinl_pat p = sscase\<cdot>p\<cdot>(\<Lambda> x. fail)"
huffman@18097
   267
wenzelm@25131
   268
definition
wenzelm@25131
   269
  sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
wenzelm@25131
   270
  "sinr_pat p = sscase\<cdot>(\<Lambda> x. fail)\<cdot>p"
huffman@18293
   271
wenzelm@25131
   272
definition
wenzelm@25131
   273
  up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat" where
wenzelm@25131
   274
  "up_pat p = fup\<cdot>p"
huffman@18097
   275
wenzelm@25131
   276
definition
wenzelm@25131
   277
  TT_pat :: "(tr, unit) pat" where
wenzelm@25131
   278
  "TT_pat = (\<Lambda> b. If b then return\<cdot>() else fail fi)"
huffman@18097
   279
wenzelm@25131
   280
definition
wenzelm@25131
   281
  FF_pat :: "(tr, unit) pat" where
wenzelm@25131
   282
  "FF_pat = (\<Lambda> b. If b then fail else return\<cdot>() fi)"
huffman@18097
   283
wenzelm@25131
   284
definition
wenzelm@25131
   285
  ONE_pat :: "(one, unit) pat" where
wenzelm@25131
   286
  "ONE_pat = (\<Lambda> ONE. return\<cdot>())"
huffman@18097
   287
huffman@18293
   288
text {* Parse translations (patterns) *}
huffman@18097
   289
translations
wenzelm@25131
   290
  "_pat (CONST cpair\<cdot>x\<cdot>y)" => "CONST cpair_pat (_pat x) (_pat y)"
wenzelm@25131
   291
  "_pat (CONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
wenzelm@25131
   292
  "_pat (CONST sinl\<cdot>x)" => "CONST sinl_pat (_pat x)"
wenzelm@25131
   293
  "_pat (CONST sinr\<cdot>x)" => "CONST sinr_pat (_pat x)"
wenzelm@25131
   294
  "_pat (CONST up\<cdot>x)" => "CONST up_pat (_pat x)"
wenzelm@25131
   295
  "_pat (CONST TT)" => "CONST TT_pat"
wenzelm@25131
   296
  "_pat (CONST FF)" => "CONST FF_pat"
wenzelm@25131
   297
  "_pat (CONST ONE)" => "CONST ONE_pat"
huffman@18293
   298
huffman@18293
   299
text {* Parse translations (variables) *}
huffman@18293
   300
translations
wenzelm@25131
   301
  "_var (CONST cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
wenzelm@25131
   302
  "_var (CONST spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
wenzelm@25131
   303
  "_var (CONST sinl\<cdot>x) r" => "_var x r"
wenzelm@25131
   304
  "_var (CONST sinr\<cdot>x) r" => "_var x r"
wenzelm@25131
   305
  "_var (CONST up\<cdot>x) r" => "_var x r"
wenzelm@25131
   306
  "_var (CONST TT) r" => "_var () r"
wenzelm@25131
   307
  "_var (CONST FF) r" => "_var () r"
wenzelm@25131
   308
  "_var (CONST ONE) r" => "_var () r"
huffman@18097
   309
huffman@18112
   310
text {* Print translations *}
huffman@18097
   311
translations
wenzelm@25131
   312
  "CONST cpair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
wenzelm@25131
   313
      <= "_match (CONST cpair_pat p1 p2) (_args v1 v2)"
wenzelm@25131
   314
  "CONST spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
wenzelm@25131
   315
      <= "_match (CONST spair_pat p1 p2) (_args v1 v2)"
wenzelm@25131
   316
  "CONST sinl\<cdot>(_match p1 v1)" <= "_match (CONST sinl_pat p1) v1"
wenzelm@25131
   317
  "CONST sinr\<cdot>(_match p1 v1)" <= "_match (CONST sinr_pat p1) v1"
wenzelm@25131
   318
  "CONST up\<cdot>(_match p1 v1)" <= "_match (CONST up_pat p1) v1"
wenzelm@25131
   319
  "CONST TT" <= "_match (CONST TT_pat) ()"
wenzelm@25131
   320
  "CONST FF" <= "_match (CONST FF_pat) ()"
wenzelm@25131
   321
  "CONST ONE" <= "_match (CONST ONE_pat) ()"
huffman@18293
   322
huffman@18293
   323
lemma cpair_pat1:
huffman@18293
   324
  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = \<bottom>"
huffman@18293
   325
apply (simp add: branch_def cpair_pat_def)
huffman@18293
   326
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   327
done
huffman@18097
   328
huffman@18293
   329
lemma cpair_pat2:
huffman@18293
   330
  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = fail"
huffman@18293
   331
apply (simp add: branch_def cpair_pat_def)
huffman@18293
   332
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   333
done
huffman@18097
   334
huffman@18293
   335
lemma cpair_pat3:
huffman@18293
   336
  "branch p\<cdot>r\<cdot>x = return\<cdot>s \<Longrightarrow>
huffman@18293
   337
   branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = branch q\<cdot>s\<cdot>y"
huffman@18293
   338
apply (simp add: branch_def cpair_pat_def)
huffman@18293
   339
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   340
apply (rule_tac p="q\<cdot>y" in maybeE, simp_all)
huffman@18293
   341
done
huffman@18097
   342
huffman@18293
   343
lemmas cpair_pat [simp] =
huffman@18293
   344
  cpair_pat1 cpair_pat2 cpair_pat3
huffman@18097
   345
huffman@18293
   346
lemma spair_pat [simp]:
huffman@18293
   347
  "branch (spair_pat p1 p2)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   348
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk>
huffman@18293
   349
     \<Longrightarrow> branch (spair_pat p1 p2)\<cdot>r\<cdot>(:x, y:) =
huffman@18293
   350
         branch (cpair_pat p1 p2)\<cdot>r\<cdot>\<langle>x, y\<rangle>"
huffman@18293
   351
by (simp_all add: branch_def spair_pat_def)
huffman@18097
   352
huffman@18293
   353
lemma sinl_pat [simp]:
huffman@18293
   354
  "branch (sinl_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   355
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = branch p\<cdot>r\<cdot>x"
huffman@18293
   356
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = fail"
huffman@18293
   357
by (simp_all add: branch_def sinl_pat_def)
huffman@18097
   358
huffman@18293
   359
lemma sinr_pat [simp]:
huffman@18293
   360
  "branch (sinr_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   361
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = fail"
huffman@18293
   362
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = branch p\<cdot>r\<cdot>y"
huffman@18293
   363
by (simp_all add: branch_def sinr_pat_def)
huffman@18097
   364
huffman@18293
   365
lemma up_pat [simp]:
huffman@18293
   366
  "branch (up_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   367
  "branch (up_pat p)\<cdot>r\<cdot>(up\<cdot>x) = branch p\<cdot>r\<cdot>x"
huffman@18293
   368
by (simp_all add: branch_def up_pat_def)
huffman@18293
   369
huffman@18293
   370
lemma TT_pat [simp]:
huffman@18293
   371
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@18293
   372
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = return\<cdot>r"
huffman@18293
   373
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = fail"
huffman@18293
   374
by (simp_all add: branch_def TT_pat_def)
huffman@18097
   375
huffman@18293
   376
lemma FF_pat [simp]:
huffman@18293
   377
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@18293
   378
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = fail"
huffman@18293
   379
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = return\<cdot>r"
huffman@18293
   380
by (simp_all add: branch_def FF_pat_def)
huffman@18097
   381
huffman@18293
   382
lemma ONE_pat [simp]:
huffman@18293
   383
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@18293
   384
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>ONE = return\<cdot>r"
huffman@18293
   385
by (simp_all add: branch_def ONE_pat_def)
huffman@18097
   386
huffman@18293
   387
huffman@18293
   388
subsection {* Wildcards, as-patterns, and lazy patterns *}
huffman@18112
   389
huffman@18112
   390
syntax
huffman@18293
   391
  "_as_pat" :: "[idt, 'a] \<Rightarrow> 'a" (infixr "\<as>" 10)
huffman@18293
   392
  "_lazy_pat" :: "'a \<Rightarrow> 'a" ("\<lazy> _" [1000] 1000)
huffman@18112
   393
wenzelm@25131
   394
definition
wenzelm@25131
   395
  wild_pat :: "'a \<rightarrow> unit maybe" where
wenzelm@25131
   396
  "wild_pat = (\<Lambda> x. return\<cdot>())"
huffman@18293
   397
wenzelm@25131
   398
definition
wenzelm@25131
   399
  as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe" where
wenzelm@25131
   400
  "as_pat p = (\<Lambda> x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. return\<cdot>\<langle>x, a\<rangle>))"
huffman@18112
   401
wenzelm@25131
   402
definition
wenzelm@25131
   403
  lazy_pat :: "('a \<rightarrow> 'b::pcpo maybe) \<Rightarrow> ('a \<rightarrow> 'b maybe)" where
wenzelm@25131
   404
  "lazy_pat p = (\<Lambda> x. return\<cdot>(run\<cdot>(p\<cdot>x)))"
huffman@18293
   405
huffman@18293
   406
text {* Parse translations (patterns) *}
huffman@18293
   407
translations
wenzelm@25131
   408
  "_pat _" => "CONST wild_pat"
wenzelm@25131
   409
  "_pat (_as_pat x y)" => "CONST as_pat (_pat y)"
wenzelm@25131
   410
  "_pat (_lazy_pat x)" => "CONST lazy_pat (_pat x)"
huffman@18293
   411
huffman@18293
   412
text {* Parse translations (variables) *}
huffman@18112
   413
translations
huffman@18293
   414
  "_var _ r" => "_var () r"
huffman@18293
   415
  "_var (_as_pat x y) r" => "_var (_args x y) r"
huffman@18293
   416
  "_var (_lazy_pat x) r" => "_var x r"
huffman@18293
   417
huffman@18293
   418
text {* Print translations *}
huffman@18293
   419
translations
wenzelm@25131
   420
  "_" <= "_match (CONST wild_pat) ()"
wenzelm@25131
   421
  "_as_pat x (_match p v)" <= "_match (CONST as_pat p) (_args (_var x) v)"
wenzelm@25131
   422
  "_lazy_pat (_match p v)" <= "_match (CONST lazy_pat p) v"
huffman@18293
   423
huffman@19327
   424
text {* Lazy patterns in lambda abstractions *}
huffman@19327
   425
translations
wenzelm@25131
   426
  "_cabs (_lazy_pat p) r" == "CONST run oo (_Case1 (_lazy_pat p) r)"
huffman@19327
   427
huffman@18293
   428
lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = return\<cdot>r"
huffman@18293
   429
by (simp add: branch_def wild_pat_def)
huffman@18112
   430
huffman@18293
   431
lemma as_pat [simp]:
huffman@18293
   432
  "branch (as_pat p)\<cdot>(csplit\<cdot>r)\<cdot>x = branch p\<cdot>(r\<cdot>x)\<cdot>x"
huffman@18293
   433
apply (simp add: branch_def as_pat_def)
huffman@18293
   434
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   435
done
huffman@18293
   436
huffman@18293
   437
lemma lazy_pat [simp]:
huffman@18293
   438
  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>\<bottom>)"
huffman@18293
   439
  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>\<bottom>)"
huffman@18293
   440
  "branch p\<cdot>r\<cdot>x = return\<cdot>s \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>s"
huffman@18293
   441
apply (simp_all add: branch_def lazy_pat_def)
huffman@18293
   442
apply (rule_tac [!] p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   443
done
huffman@18293
   444
huffman@18112
   445
huffman@16221
   446
subsection {* Match functions for built-in types *}
huffman@16221
   447
huffman@16776
   448
defaultsort pcpo
huffman@16776
   449
wenzelm@25131
   450
definition
wenzelm@25131
   451
  match_UU :: "'a \<rightarrow> unit maybe" where
wenzelm@25131
   452
  "match_UU = (\<Lambda> x. fail)"
wenzelm@25131
   453
wenzelm@25131
   454
definition
wenzelm@25131
   455
  match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe" where
wenzelm@25131
   456
  "match_cpair = csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16776
   457
wenzelm@25131
   458
definition
wenzelm@25131
   459
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe" where
wenzelm@25131
   460
  "match_spair = ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16221
   461
wenzelm@25131
   462
definition
wenzelm@25131
   463
  match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe" where
wenzelm@25131
   464
  "match_sinl = sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
huffman@16551
   465
wenzelm@25131
   466
definition
wenzelm@25131
   467
  match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe" where
wenzelm@25131
   468
  "match_sinr = sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
huffman@16551
   469
wenzelm@25131
   470
definition
wenzelm@25131
   471
  match_up :: "'a::cpo u \<rightarrow> 'a maybe" where
wenzelm@25131
   472
  "match_up = fup\<cdot>return"
huffman@16221
   473
wenzelm@25131
   474
definition
wenzelm@25131
   475
  match_ONE :: "one \<rightarrow> unit maybe" where
wenzelm@25131
   476
  "match_ONE = (\<Lambda> ONE. return\<cdot>())"
huffman@18094
   477
 
wenzelm@25131
   478
definition
wenzelm@25131
   479
  match_TT :: "tr \<rightarrow> unit maybe" where
wenzelm@25131
   480
  "match_TT = (\<Lambda> b. If b then return\<cdot>() else fail fi)"
huffman@18094
   481
 
wenzelm@25131
   482
definition
wenzelm@25131
   483
  match_FF :: "tr \<rightarrow> unit maybe" where
wenzelm@25131
   484
  "match_FF = (\<Lambda> b. If b then fail else return\<cdot>() fi)"
huffman@16460
   485
huffman@16776
   486
lemma match_UU_simps [simp]:
huffman@16776
   487
  "match_UU\<cdot>x = fail"
huffman@16776
   488
by (simp add: match_UU_def)
huffman@16776
   489
huffman@16221
   490
lemma match_cpair_simps [simp]:
huffman@16221
   491
  "match_cpair\<cdot><x,y> = return\<cdot><x,y>"
huffman@16221
   492
by (simp add: match_cpair_def)
huffman@16221
   493
huffman@16551
   494
lemma match_spair_simps [simp]:
huffman@16551
   495
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x,y:) = return\<cdot><x,y>"
huffman@16551
   496
  "match_spair\<cdot>\<bottom> = \<bottom>"
huffman@16551
   497
by (simp_all add: match_spair_def)
huffman@16551
   498
huffman@16551
   499
lemma match_sinl_simps [simp]:
huffman@16551
   500
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x) = return\<cdot>x"
huffman@16551
   501
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>x) = fail"
huffman@16551
   502
  "match_sinl\<cdot>\<bottom> = \<bottom>"
huffman@16551
   503
by (simp_all add: match_sinl_def)
huffman@16551
   504
huffman@16551
   505
lemma match_sinr_simps [simp]:
huffman@16551
   506
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>x) = return\<cdot>x"
huffman@16551
   507
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x) = fail"
huffman@16551
   508
  "match_sinr\<cdot>\<bottom> = \<bottom>"
huffman@16551
   509
by (simp_all add: match_sinr_def)
huffman@16551
   510
huffman@16221
   511
lemma match_up_simps [simp]:
huffman@16221
   512
  "match_up\<cdot>(up\<cdot>x) = return\<cdot>x"
huffman@16221
   513
  "match_up\<cdot>\<bottom> = \<bottom>"
huffman@16221
   514
by (simp_all add: match_up_def)
huffman@16221
   515
huffman@16460
   516
lemma match_ONE_simps [simp]:
huffman@16460
   517
  "match_ONE\<cdot>ONE = return\<cdot>()"
huffman@16460
   518
  "match_ONE\<cdot>\<bottom> = \<bottom>"
huffman@18094
   519
by (simp_all add: match_ONE_def)
huffman@16460
   520
huffman@16460
   521
lemma match_TT_simps [simp]:
huffman@16460
   522
  "match_TT\<cdot>TT = return\<cdot>()"
huffman@16460
   523
  "match_TT\<cdot>FF = fail"
huffman@16460
   524
  "match_TT\<cdot>\<bottom> = \<bottom>"
huffman@18094
   525
by (simp_all add: match_TT_def)
huffman@16460
   526
huffman@16460
   527
lemma match_FF_simps [simp]:
huffman@16460
   528
  "match_FF\<cdot>FF = return\<cdot>()"
huffman@16460
   529
  "match_FF\<cdot>TT = fail"
huffman@16460
   530
  "match_FF\<cdot>\<bottom> = \<bottom>"
huffman@18094
   531
by (simp_all add: match_FF_def)
huffman@16460
   532
huffman@16401
   533
subsection {* Mutual recursion *}
huffman@16401
   534
huffman@16401
   535
text {*
huffman@16401
   536
  The following rules are used to prove unfolding theorems from
huffman@16401
   537
  fixed-point definitions of mutually recursive functions.
huffman@16401
   538
*}
huffman@16401
   539
huffman@16401
   540
lemma cpair_equalI: "\<lbrakk>x \<equiv> cfst\<cdot>p; y \<equiv> csnd\<cdot>p\<rbrakk> \<Longrightarrow> <x,y> \<equiv> p"
huffman@16401
   541
by (simp add: surjective_pairing_Cprod2)
huffman@16401
   542
huffman@16401
   543
lemma cpair_eqD1: "<x,y> = <x',y'> \<Longrightarrow> x = x'"
huffman@16401
   544
by simp
huffman@16401
   545
huffman@16401
   546
lemma cpair_eqD2: "<x,y> = <x',y'> \<Longrightarrow> y = y'"
huffman@16401
   547
by simp
huffman@16401
   548
huffman@16463
   549
text {* lemma for proving rewrite rules *}
huffman@16463
   550
huffman@16463
   551
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   552
by simp
huffman@16463
   553
huffman@16221
   554
huffman@16758
   555
subsection {* Initializing the fixrec package *}
huffman@16221
   556
wenzelm@23152
   557
use "Tools/fixrec_package.ML"
huffman@16221
   558
huffman@19439
   559
hide (open) const return bind fail run
huffman@19104
   560
huffman@16221
   561
end