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