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