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