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