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