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