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