src/HOLCF/Fixrec.thy
author huffman
Thu Feb 07 03:30:32 2008 +0100 (2008-02-07)
changeset 26046 1624b3304bb9
parent 25158 271e499f2d03
child 28891 f199def7a6a5
permissions -rw-r--r--
fix broken syntax translations
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
wenzelm@25158
   196
  "_Case_syntax x ms" == "CONST 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@26046
   204
  "_noargs" :: "'a"
huffman@18293
   205
huffman@18097
   206
translations
huffman@26046
   207
  "_Case1 p r" => "CONST branch (_pat p)\<cdot>(_var p r)"
huffman@26046
   208
  "_var (_args x y) r" => "CONST csplit\<cdot>(_var x (_var y r))"
huffman@26046
   209
  "_var _noargs r" => "CONST unit_when\<cdot>r"
huffman@18112
   210
huffman@18112
   211
parse_translation {*
huffman@18293
   212
(* rewrites (_pat x) => (return) *)
huffman@18293
   213
(* rewrites (_var x t) => (Abs_CFun (%x. t)) *)
huffman@19104
   214
  [("_pat", K (Syntax.const "Fixrec.return")),
wenzelm@25158
   215
   mk_binder_tr ("_var", "Abs_CFun")];
huffman@18112
   216
*}
huffman@18112
   217
huffman@18112
   218
text {* Printing Case expressions *}
huffman@18112
   219
huffman@18293
   220
syntax
huffman@18293
   221
  "_match" :: "'a"
huffman@18097
   222
huffman@18112
   223
print_translation {*
huffman@18112
   224
  let
wenzelm@25131
   225
    fun dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax unit_when},_) $ t) =
huffman@26046
   226
          (Syntax.const "_noargs", t)
wenzelm@25131
   227
    |   dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax csplit},_) $ t) =
huffman@18293
   228
          let
huffman@18293
   229
            val (v1, t1) = dest_LAM t;
huffman@18293
   230
            val (v2, t2) = dest_LAM t1;
huffman@18293
   231
          in (Syntax.const "_args" $ v1 $ v2, t2) end 
wenzelm@25131
   232
    |   dest_LAM (Const (@{const_syntax Abs_CFun},_) $ t) =
huffman@18112
   233
          let
huffman@18293
   234
            val abs = case t of Abs abs => abs
huffman@18293
   235
                | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
huffman@18293
   236
            val (x, t') = atomic_abs_tr' abs;
huffman@18293
   237
          in (Syntax.const "_var" $ x, t') end
huffman@18293
   238
    |   dest_LAM _ = raise Match; (* too few vars: abort translation *)
huffman@18097
   239
wenzelm@25131
   240
    fun Case1_tr' [Const(@{const_syntax branch},_) $ p, r] =
huffman@18293
   241
          let val (v, t) = dest_LAM r;
huffman@18293
   242
          in Syntax.const "_Case1" $ (Syntax.const "_match" $ p $ v) $ t end;
huffman@18112
   243
wenzelm@25131
   244
  in [(@{const_syntax Rep_CFun}, Case1_tr')] end;
huffman@18112
   245
*}
huffman@18097
   246
huffman@18293
   247
translations
huffman@19104
   248
  "x" <= "_match Fixrec.return (_var x)"
huffman@18293
   249
huffman@18293
   250
huffman@18293
   251
subsection {* Pattern combinators for data constructors *}
huffman@18293
   252
huffman@18293
   253
types ('a, 'b) pat = "'a \<rightarrow> 'b maybe"
huffman@18097
   254
wenzelm@25131
   255
definition
wenzelm@25131
   256
  cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat" where
wenzelm@25131
   257
  "cpair_pat p1 p2 = (\<Lambda>\<langle>x, y\<rangle>.
wenzelm@25131
   258
    bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. return\<cdot>\<langle>a, b\<rangle>)))"
huffman@18097
   259
wenzelm@25131
   260
definition
huffman@18293
   261
  spair_pat ::
wenzelm@25131
   262
  "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat" where
wenzelm@25131
   263
  "spair_pat p1 p2 = (\<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>\<langle>x, y\<rangle>)"
huffman@18097
   264
wenzelm@25131
   265
definition
wenzelm@25131
   266
  sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
wenzelm@25131
   267
  "sinl_pat p = sscase\<cdot>p\<cdot>(\<Lambda> x. fail)"
huffman@18097
   268
wenzelm@25131
   269
definition
wenzelm@25131
   270
  sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
wenzelm@25131
   271
  "sinr_pat p = sscase\<cdot>(\<Lambda> x. fail)\<cdot>p"
huffman@18293
   272
wenzelm@25131
   273
definition
wenzelm@25131
   274
  up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat" where
wenzelm@25131
   275
  "up_pat p = fup\<cdot>p"
huffman@18097
   276
wenzelm@25131
   277
definition
wenzelm@25131
   278
  TT_pat :: "(tr, unit) pat" where
wenzelm@25131
   279
  "TT_pat = (\<Lambda> b. If b then return\<cdot>() else fail fi)"
huffman@18097
   280
wenzelm@25131
   281
definition
wenzelm@25131
   282
  FF_pat :: "(tr, unit) pat" where
wenzelm@25131
   283
  "FF_pat = (\<Lambda> b. If b then fail else return\<cdot>() fi)"
huffman@18097
   284
wenzelm@25131
   285
definition
wenzelm@25131
   286
  ONE_pat :: "(one, unit) pat" where
wenzelm@25131
   287
  "ONE_pat = (\<Lambda> ONE. return\<cdot>())"
huffman@18097
   288
huffman@18293
   289
text {* Parse translations (patterns) *}
huffman@18097
   290
translations
huffman@26046
   291
  "_pat (XCONST cpair\<cdot>x\<cdot>y)" => "CONST cpair_pat (_pat x) (_pat y)"
huffman@26046
   292
  "_pat (XCONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
huffman@26046
   293
  "_pat (XCONST sinl\<cdot>x)" => "CONST sinl_pat (_pat x)"
huffman@26046
   294
  "_pat (XCONST sinr\<cdot>x)" => "CONST sinr_pat (_pat x)"
huffman@26046
   295
  "_pat (XCONST up\<cdot>x)" => "CONST up_pat (_pat x)"
huffman@26046
   296
  "_pat (XCONST TT)" => "CONST TT_pat"
huffman@26046
   297
  "_pat (XCONST FF)" => "CONST FF_pat"
huffman@26046
   298
  "_pat (XCONST ONE)" => "CONST ONE_pat"
huffman@26046
   299
huffman@26046
   300
text {* CONST version is also needed for constructors with special syntax *}
huffman@26046
   301
translations
huffman@26046
   302
  "_pat (CONST cpair\<cdot>x\<cdot>y)" => "CONST cpair_pat (_pat x) (_pat y)"
huffman@26046
   303
  "_pat (CONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
huffman@18293
   304
huffman@18293
   305
text {* Parse translations (variables) *}
huffman@18293
   306
translations
wenzelm@25158
   307
  "_var (XCONST cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
wenzelm@25158
   308
  "_var (XCONST spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
wenzelm@25158
   309
  "_var (XCONST sinl\<cdot>x) r" => "_var x r"
wenzelm@25158
   310
  "_var (XCONST sinr\<cdot>x) r" => "_var x r"
wenzelm@25158
   311
  "_var (XCONST up\<cdot>x) r" => "_var x r"
huffman@26046
   312
  "_var (XCONST TT) r" => "_var _noargs r"
huffman@26046
   313
  "_var (XCONST FF) r" => "_var _noargs r"
huffman@26046
   314
  "_var (XCONST ONE) r" => "_var _noargs r"
huffman@26046
   315
huffman@26046
   316
translations
huffman@26046
   317
  "_var (CONST cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
huffman@26046
   318
  "_var (CONST spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
huffman@18097
   319
huffman@18112
   320
text {* Print translations *}
huffman@18097
   321
translations
wenzelm@25131
   322
  "CONST cpair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
wenzelm@25131
   323
      <= "_match (CONST cpair_pat p1 p2) (_args v1 v2)"
wenzelm@25131
   324
  "CONST spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
wenzelm@25131
   325
      <= "_match (CONST spair_pat p1 p2) (_args v1 v2)"
wenzelm@25131
   326
  "CONST sinl\<cdot>(_match p1 v1)" <= "_match (CONST sinl_pat p1) v1"
wenzelm@25131
   327
  "CONST sinr\<cdot>(_match p1 v1)" <= "_match (CONST sinr_pat p1) v1"
wenzelm@25131
   328
  "CONST up\<cdot>(_match p1 v1)" <= "_match (CONST up_pat p1) v1"
huffman@26046
   329
  "CONST TT" <= "_match (CONST TT_pat) _noargs"
huffman@26046
   330
  "CONST FF" <= "_match (CONST FF_pat) _noargs"
huffman@26046
   331
  "CONST ONE" <= "_match (CONST ONE_pat) _noargs"
huffman@18293
   332
huffman@18293
   333
lemma cpair_pat1:
huffman@18293
   334
  "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
   335
apply (simp add: branch_def cpair_pat_def)
huffman@18293
   336
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   337
done
huffman@18097
   338
huffman@18293
   339
lemma cpair_pat2:
huffman@18293
   340
  "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
   341
apply (simp add: branch_def cpair_pat_def)
huffman@18293
   342
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   343
done
huffman@18097
   344
huffman@18293
   345
lemma cpair_pat3:
huffman@18293
   346
  "branch p\<cdot>r\<cdot>x = return\<cdot>s \<Longrightarrow>
huffman@18293
   347
   branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = branch q\<cdot>s\<cdot>y"
huffman@18293
   348
apply (simp add: branch_def cpair_pat_def)
huffman@18293
   349
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   350
apply (rule_tac p="q\<cdot>y" in maybeE, simp_all)
huffman@18293
   351
done
huffman@18097
   352
huffman@18293
   353
lemmas cpair_pat [simp] =
huffman@18293
   354
  cpair_pat1 cpair_pat2 cpair_pat3
huffman@18097
   355
huffman@18293
   356
lemma spair_pat [simp]:
huffman@18293
   357
  "branch (spair_pat p1 p2)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   358
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk>
huffman@18293
   359
     \<Longrightarrow> branch (spair_pat p1 p2)\<cdot>r\<cdot>(:x, y:) =
huffman@18293
   360
         branch (cpair_pat p1 p2)\<cdot>r\<cdot>\<langle>x, y\<rangle>"
huffman@18293
   361
by (simp_all add: branch_def spair_pat_def)
huffman@18097
   362
huffman@18293
   363
lemma sinl_pat [simp]:
huffman@18293
   364
  "branch (sinl_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   365
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = branch p\<cdot>r\<cdot>x"
huffman@18293
   366
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = fail"
huffman@18293
   367
by (simp_all add: branch_def sinl_pat_def)
huffman@18097
   368
huffman@18293
   369
lemma sinr_pat [simp]:
huffman@18293
   370
  "branch (sinr_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   371
  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = fail"
huffman@18293
   372
  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = branch p\<cdot>r\<cdot>y"
huffman@18293
   373
by (simp_all add: branch_def sinr_pat_def)
huffman@18097
   374
huffman@18293
   375
lemma up_pat [simp]:
huffman@18293
   376
  "branch (up_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
huffman@18293
   377
  "branch (up_pat p)\<cdot>r\<cdot>(up\<cdot>x) = branch p\<cdot>r\<cdot>x"
huffman@18293
   378
by (simp_all add: branch_def up_pat_def)
huffman@18293
   379
huffman@18293
   380
lemma TT_pat [simp]:
huffman@18293
   381
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@18293
   382
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = return\<cdot>r"
huffman@18293
   383
  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = fail"
huffman@18293
   384
by (simp_all add: branch_def TT_pat_def)
huffman@18097
   385
huffman@18293
   386
lemma FF_pat [simp]:
huffman@18293
   387
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@18293
   388
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = fail"
huffman@18293
   389
  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = return\<cdot>r"
huffman@18293
   390
by (simp_all add: branch_def FF_pat_def)
huffman@18097
   391
huffman@18293
   392
lemma ONE_pat [simp]:
huffman@18293
   393
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
huffman@18293
   394
  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>ONE = return\<cdot>r"
huffman@18293
   395
by (simp_all add: branch_def ONE_pat_def)
huffman@18097
   396
huffman@18293
   397
huffman@18293
   398
subsection {* Wildcards, as-patterns, and lazy patterns *}
huffman@18112
   399
huffman@18112
   400
syntax
huffman@18293
   401
  "_as_pat" :: "[idt, 'a] \<Rightarrow> 'a" (infixr "\<as>" 10)
huffman@18293
   402
  "_lazy_pat" :: "'a \<Rightarrow> 'a" ("\<lazy> _" [1000] 1000)
huffman@18112
   403
wenzelm@25131
   404
definition
wenzelm@25131
   405
  wild_pat :: "'a \<rightarrow> unit maybe" where
wenzelm@25131
   406
  "wild_pat = (\<Lambda> x. return\<cdot>())"
huffman@18293
   407
wenzelm@25131
   408
definition
wenzelm@25131
   409
  as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe" where
wenzelm@25131
   410
  "as_pat p = (\<Lambda> x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. return\<cdot>\<langle>x, a\<rangle>))"
huffman@18112
   411
wenzelm@25131
   412
definition
wenzelm@25131
   413
  lazy_pat :: "('a \<rightarrow> 'b::pcpo maybe) \<Rightarrow> ('a \<rightarrow> 'b maybe)" where
wenzelm@25131
   414
  "lazy_pat p = (\<Lambda> x. return\<cdot>(run\<cdot>(p\<cdot>x)))"
huffman@18293
   415
huffman@18293
   416
text {* Parse translations (patterns) *}
huffman@18293
   417
translations
huffman@26046
   418
  "_pat _" => "CONST wild_pat"
huffman@26046
   419
  "_pat (_as_pat x y)" => "CONST as_pat (_pat y)"
huffman@26046
   420
  "_pat (_lazy_pat x)" => "CONST lazy_pat (_pat x)"
huffman@18293
   421
huffman@18293
   422
text {* Parse translations (variables) *}
huffman@18112
   423
translations
huffman@26046
   424
  "_var _ r" => "_var _noargs r"
huffman@18293
   425
  "_var (_as_pat x y) r" => "_var (_args x y) r"
huffman@18293
   426
  "_var (_lazy_pat x) r" => "_var x r"
huffman@18293
   427
huffman@18293
   428
text {* Print translations *}
huffman@18293
   429
translations
huffman@26046
   430
  "_" <= "_match (CONST wild_pat) _noargs"
wenzelm@25131
   431
  "_as_pat x (_match p v)" <= "_match (CONST as_pat p) (_args (_var x) v)"
wenzelm@25131
   432
  "_lazy_pat (_match p v)" <= "_match (CONST lazy_pat p) v"
huffman@18293
   433
huffman@19327
   434
text {* Lazy patterns in lambda abstractions *}
huffman@19327
   435
translations
wenzelm@25158
   436
  "_cabs (_lazy_pat p) r" == "CONST Fixrec.run oo (_Case1 (_lazy_pat p) r)"
huffman@19327
   437
huffman@18293
   438
lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = return\<cdot>r"
huffman@18293
   439
by (simp add: branch_def wild_pat_def)
huffman@18112
   440
huffman@18293
   441
lemma as_pat [simp]:
huffman@18293
   442
  "branch (as_pat p)\<cdot>(csplit\<cdot>r)\<cdot>x = branch p\<cdot>(r\<cdot>x)\<cdot>x"
huffman@18293
   443
apply (simp add: branch_def as_pat_def)
huffman@18293
   444
apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   445
done
huffman@18293
   446
huffman@18293
   447
lemma lazy_pat [simp]:
huffman@18293
   448
  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>\<bottom>)"
huffman@18293
   449
  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>(r\<cdot>\<bottom>)"
huffman@18293
   450
  "branch p\<cdot>r\<cdot>x = return\<cdot>s \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = return\<cdot>s"
huffman@18293
   451
apply (simp_all add: branch_def lazy_pat_def)
huffman@18293
   452
apply (rule_tac [!] p="p\<cdot>x" in maybeE, simp_all)
huffman@18293
   453
done
huffman@18293
   454
huffman@18112
   455
huffman@16221
   456
subsection {* Match functions for built-in types *}
huffman@16221
   457
huffman@16776
   458
defaultsort pcpo
huffman@16776
   459
wenzelm@25131
   460
definition
wenzelm@25131
   461
  match_UU :: "'a \<rightarrow> unit maybe" where
wenzelm@25131
   462
  "match_UU = (\<Lambda> x. fail)"
wenzelm@25131
   463
wenzelm@25131
   464
definition
wenzelm@25131
   465
  match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe" where
wenzelm@25131
   466
  "match_cpair = csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16776
   467
wenzelm@25131
   468
definition
wenzelm@25131
   469
  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe" where
wenzelm@25131
   470
  "match_spair = ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
huffman@16221
   471
wenzelm@25131
   472
definition
wenzelm@25131
   473
  match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe" where
wenzelm@25131
   474
  "match_sinl = sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
huffman@16551
   475
wenzelm@25131
   476
definition
wenzelm@25131
   477
  match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe" where
wenzelm@25131
   478
  "match_sinr = sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
huffman@16551
   479
wenzelm@25131
   480
definition
wenzelm@25131
   481
  match_up :: "'a::cpo u \<rightarrow> 'a maybe" where
wenzelm@25131
   482
  "match_up = fup\<cdot>return"
huffman@16221
   483
wenzelm@25131
   484
definition
wenzelm@25131
   485
  match_ONE :: "one \<rightarrow> unit maybe" where
wenzelm@25131
   486
  "match_ONE = (\<Lambda> ONE. return\<cdot>())"
huffman@18094
   487
 
wenzelm@25131
   488
definition
wenzelm@25131
   489
  match_TT :: "tr \<rightarrow> unit maybe" where
wenzelm@25131
   490
  "match_TT = (\<Lambda> b. If b then return\<cdot>() else fail fi)"
huffman@18094
   491
 
wenzelm@25131
   492
definition
wenzelm@25131
   493
  match_FF :: "tr \<rightarrow> unit maybe" where
wenzelm@25131
   494
  "match_FF = (\<Lambda> b. If b then fail else return\<cdot>() fi)"
huffman@16460
   495
huffman@16776
   496
lemma match_UU_simps [simp]:
huffman@16776
   497
  "match_UU\<cdot>x = fail"
huffman@16776
   498
by (simp add: match_UU_def)
huffman@16776
   499
huffman@16221
   500
lemma match_cpair_simps [simp]:
huffman@16221
   501
  "match_cpair\<cdot><x,y> = return\<cdot><x,y>"
huffman@16221
   502
by (simp add: match_cpair_def)
huffman@16221
   503
huffman@16551
   504
lemma match_spair_simps [simp]:
huffman@16551
   505
  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x,y:) = return\<cdot><x,y>"
huffman@16551
   506
  "match_spair\<cdot>\<bottom> = \<bottom>"
huffman@16551
   507
by (simp_all add: match_spair_def)
huffman@16551
   508
huffman@16551
   509
lemma match_sinl_simps [simp]:
huffman@16551
   510
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x) = return\<cdot>x"
huffman@16551
   511
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>x) = fail"
huffman@16551
   512
  "match_sinl\<cdot>\<bottom> = \<bottom>"
huffman@16551
   513
by (simp_all add: match_sinl_def)
huffman@16551
   514
huffman@16551
   515
lemma match_sinr_simps [simp]:
huffman@16551
   516
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>x) = return\<cdot>x"
huffman@16551
   517
  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x) = fail"
huffman@16551
   518
  "match_sinr\<cdot>\<bottom> = \<bottom>"
huffman@16551
   519
by (simp_all add: match_sinr_def)
huffman@16551
   520
huffman@16221
   521
lemma match_up_simps [simp]:
huffman@16221
   522
  "match_up\<cdot>(up\<cdot>x) = return\<cdot>x"
huffman@16221
   523
  "match_up\<cdot>\<bottom> = \<bottom>"
huffman@16221
   524
by (simp_all add: match_up_def)
huffman@16221
   525
huffman@16460
   526
lemma match_ONE_simps [simp]:
huffman@16460
   527
  "match_ONE\<cdot>ONE = return\<cdot>()"
huffman@16460
   528
  "match_ONE\<cdot>\<bottom> = \<bottom>"
huffman@18094
   529
by (simp_all add: match_ONE_def)
huffman@16460
   530
huffman@16460
   531
lemma match_TT_simps [simp]:
huffman@16460
   532
  "match_TT\<cdot>TT = return\<cdot>()"
huffman@16460
   533
  "match_TT\<cdot>FF = fail"
huffman@16460
   534
  "match_TT\<cdot>\<bottom> = \<bottom>"
huffman@18094
   535
by (simp_all add: match_TT_def)
huffman@16460
   536
huffman@16460
   537
lemma match_FF_simps [simp]:
huffman@16460
   538
  "match_FF\<cdot>FF = return\<cdot>()"
huffman@16460
   539
  "match_FF\<cdot>TT = fail"
huffman@16460
   540
  "match_FF\<cdot>\<bottom> = \<bottom>"
huffman@18094
   541
by (simp_all add: match_FF_def)
huffman@16460
   542
huffman@16401
   543
subsection {* Mutual recursion *}
huffman@16401
   544
huffman@16401
   545
text {*
huffman@16401
   546
  The following rules are used to prove unfolding theorems from
huffman@16401
   547
  fixed-point definitions of mutually recursive functions.
huffman@16401
   548
*}
huffman@16401
   549
huffman@16401
   550
lemma cpair_equalI: "\<lbrakk>x \<equiv> cfst\<cdot>p; y \<equiv> csnd\<cdot>p\<rbrakk> \<Longrightarrow> <x,y> \<equiv> p"
huffman@16401
   551
by (simp add: surjective_pairing_Cprod2)
huffman@16401
   552
huffman@16401
   553
lemma cpair_eqD1: "<x,y> = <x',y'> \<Longrightarrow> x = x'"
huffman@16401
   554
by simp
huffman@16401
   555
huffman@16401
   556
lemma cpair_eqD2: "<x,y> = <x',y'> \<Longrightarrow> y = y'"
huffman@16401
   557
by simp
huffman@16401
   558
huffman@16463
   559
text {* lemma for proving rewrite rules *}
huffman@16463
   560
huffman@16463
   561
lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
huffman@16463
   562
by simp
huffman@16463
   563
huffman@16221
   564
huffman@16758
   565
subsection {* Initializing the fixrec package *}
huffman@16221
   566
wenzelm@23152
   567
use "Tools/fixrec_package.ML"
huffman@16221
   568
huffman@19439
   569
hide (open) const return bind fail run
huffman@19104
   570
huffman@16221
   571
end