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