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