src/HOLCF/Fixrec.thy
changeset 25131 2c8caac48ade
parent 23152 9497234a2743
child 25158 271e499f2d03
equal deleted inserted replaced
25130:d91391a8705b 25131:2c8caac48ade
    19 
    19 
    20 constdefs
    20 constdefs
    21   fail :: "'a maybe"
    21   fail :: "'a maybe"
    22   "fail \<equiv> Abs_maybe (sinl\<cdot>ONE)"
    22   "fail \<equiv> Abs_maybe (sinl\<cdot>ONE)"
    23 
    23 
    24   return :: "'a \<rightarrow> 'a maybe"
    24 constdefs
       
    25   return :: "'a \<rightarrow> 'a maybe" where
    25   "return \<equiv> \<Lambda> x. Abs_maybe (sinr\<cdot>(up\<cdot>x))"
    26   "return \<equiv> \<Lambda> x. Abs_maybe (sinr\<cdot>(up\<cdot>x))"
    26 
    27 
    27   maybe_when :: "'b \<rightarrow> ('a \<rightarrow> 'b) \<rightarrow> 'a maybe \<rightarrow> 'b::pcpo"
    28 definition
    28   "maybe_when \<equiv> \<Lambda> f r m. sscase\<cdot>(\<Lambda> x. f)\<cdot>(fup\<cdot>r)\<cdot>(Rep_maybe m)"
    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))"
    29 
    31 
    30 lemma maybeE:
    32 lemma maybeE:
    31   "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = return\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
    33   "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = return\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
    32 apply (unfold fail_def return_def)
    34 apply (unfold fail_def return_def)
    33 apply (cases p, rename_tac r)
    35 apply (cases p, rename_tac r)
    55   "maybe_when\<cdot>f\<cdot>r\<cdot>(return\<cdot>x) = r\<cdot>x"
    57   "maybe_when\<cdot>f\<cdot>r\<cdot>(return\<cdot>x) = r\<cdot>x"
    56 by (simp_all add: return_def fail_def maybe_when_def cont_Rep_maybe
    58 by (simp_all add: return_def fail_def maybe_when_def cont_Rep_maybe
    57                   cont_Abs_maybe Abs_maybe_inverse Rep_maybe_strict)
    59                   cont_Abs_maybe Abs_maybe_inverse Rep_maybe_strict)
    58 
    60 
    59 translations
    61 translations
    60   "case m of fail \<Rightarrow> t1 | return\<cdot>x \<Rightarrow> t2" == "maybe_when\<cdot>t1\<cdot>(\<Lambda> x. t2)\<cdot>m"
    62   "case m of fail \<Rightarrow> t1 | return\<cdot>x \<Rightarrow> t2" == "CONST maybe_when\<cdot>t1\<cdot>(\<Lambda> x. t2)\<cdot>m"
    61 
    63 
    62 
    64 
    63 subsubsection {* Monadic bind operator *}
    65 subsubsection {* Monadic bind operator *}
    64 
    66 
    65 constdefs
    67 definition
    66   bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe"
    68   bind :: "'a maybe \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> 'b maybe" where
    67   "bind \<equiv> \<Lambda> m f. case m of fail \<Rightarrow> fail | return\<cdot>x \<Rightarrow> f\<cdot>x"
    69   "bind = (\<Lambda> m f. case m of fail \<Rightarrow> fail | return\<cdot>x \<Rightarrow> f\<cdot>x)"
    68 
    70 
    69 text {* monad laws *}
    71 text {* monad laws *}
    70 
    72 
    71 lemma bind_strict [simp]: "bind\<cdot>\<bottom>\<cdot>f = \<bottom>"
    73 lemma bind_strict [simp]: "bind\<cdot>\<bottom>\<cdot>f = \<bottom>"
    72 by (simp add: bind_def)
    74 by (simp add: bind_def)
    84  "bind\<cdot>(bind\<cdot>m\<cdot>k)\<cdot>h = bind\<cdot>m\<cdot>(\<Lambda> a. bind\<cdot>(k\<cdot>a)\<cdot>h)"
    86  "bind\<cdot>(bind\<cdot>m\<cdot>k)\<cdot>h = bind\<cdot>m\<cdot>(\<Lambda> a. bind\<cdot>(k\<cdot>a)\<cdot>h)"
    85 by (rule_tac p=m in maybeE, simp_all)
    87 by (rule_tac p=m in maybeE, simp_all)
    86 
    88 
    87 subsubsection {* Run operator *}
    89 subsubsection {* Run operator *}
    88 
    90 
    89 constdefs
    91 definition
    90   run:: "'a maybe \<rightarrow> 'a::pcpo"
    92   run:: "'a maybe \<rightarrow> 'a::pcpo" where
    91   "run \<equiv> maybe_when\<cdot>\<bottom>\<cdot>ID"
    93   "run = maybe_when\<cdot>\<bottom>\<cdot>ID"
    92 
    94 
    93 text {* rewrite rules for run *}
    95 text {* rewrite rules for run *}
    94 
    96 
    95 lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
    97 lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
    96 by (simp add: run_def)
    98 by (simp add: run_def)
   101 lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
   103 lemma run_return [simp]: "run\<cdot>(return\<cdot>x) = x"
   102 by (simp add: run_def)
   104 by (simp add: run_def)
   103 
   105 
   104 subsubsection {* Monad plus operator *}
   106 subsubsection {* Monad plus operator *}
   105 
   107 
   106 constdefs
   108 definition
   107   mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe"
   109   mplus :: "'a maybe \<rightarrow> 'a maybe \<rightarrow> 'a maybe" where
   108   "mplus \<equiv> \<Lambda> m1 m2. case m1 of fail \<Rightarrow> m2 | return\<cdot>x \<Rightarrow> m1"
   110   "mplus = (\<Lambda> m1 m2. case m1 of fail \<Rightarrow> m2 | return\<cdot>x \<Rightarrow> m1)"
   109 
   111 
   110 syntax "+++" :: "['a maybe, 'a maybe] \<Rightarrow> 'a maybe" (infixr "+++" 65)
   112 abbreviation
   111 translations "m1 +++ m2" == "mplus\<cdot>m1\<cdot>m2"
   113   mplus_syn :: "['a maybe, 'a maybe] \<Rightarrow> 'a maybe"  (infixr "+++" 65)  where
       
   114   "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
   112 
   115 
   113 text {* rewrite rules for mplus *}
   116 text {* rewrite rules for mplus *}
   114 
   117 
   115 lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
   118 lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
   116 by (simp add: mplus_def)
   119 by (simp add: mplus_def)
   127 lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
   130 lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
   128 by (rule_tac p=x in maybeE, simp_all)
   131 by (rule_tac p=x in maybeE, simp_all)
   129 
   132 
   130 subsubsection {* Fatbar combinator *}
   133 subsubsection {* Fatbar combinator *}
   131 
   134 
   132 constdefs
   135 definition
   133   fatbar :: "('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe)"
   136   fatbar :: "('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe) \<rightarrow> ('a \<rightarrow> 'b maybe)" where
   134   "fatbar \<equiv> \<Lambda> a b x. a\<cdot>x +++ b\<cdot>x"
   137   "fatbar = (\<Lambda> a b x. a\<cdot>x +++ b\<cdot>x)"
   135 
   138 
   136 syntax
   139 abbreviation
   137   "\<parallel>" :: "['a \<rightarrow> 'b maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'a \<rightarrow> 'b maybe" (infixr "\<parallel>" 60)
   140   fatbar_syn :: "['a \<rightarrow> 'b maybe, 'a \<rightarrow> 'b maybe] \<Rightarrow> 'a \<rightarrow> 'b maybe" (infixr "\<parallel>" 60)  where
   138 translations
   141   "m1 \<parallel> m2 == fatbar\<cdot>m1\<cdot>m2"
   139   "m1 \<parallel> m2" == "fatbar\<cdot>m1\<cdot>m2"
       
   140 
   142 
   141 lemma fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> (m \<parallel> ms)\<cdot>x = \<bottom>"
   143 lemma fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> (m \<parallel> ms)\<cdot>x = \<bottom>"
   142 by (simp add: fatbar_def)
   144 by (simp add: fatbar_def)
   143 
   145 
   144 lemma fatbar2: "m\<cdot>x = fail \<Longrightarrow> (m \<parallel> ms)\<cdot>x = ms\<cdot>x"
   146 lemma fatbar2: "m\<cdot>x = fail \<Longrightarrow> (m \<parallel> ms)\<cdot>x = ms\<cdot>x"
   199 syntax
   201 syntax
   200   "_pat" :: "'a"
   202   "_pat" :: "'a"
   201   "_var" :: "'a"
   203   "_var" :: "'a"
   202 
   204 
   203 translations
   205 translations
   204   "_Case1 p r" => "branch (_pat p)\<cdot>(_var p r)"
   206   "_Case1 p r" => "CONST branch (_pat p)\<cdot>(_var p r)"
   205   "_var (_args x y) r" => "csplit\<cdot>(_var x (_var y r))"
   207   "_var (_args x y) r" => "CONST csplit\<cdot>(_var x (_var y r))"
   206   "_var () r" => "unit_when\<cdot>r"
   208   "_var () r" => "CONST unit_when\<cdot>r"
   207 
   209 
   208 parse_translation {*
   210 parse_translation {*
   209 (* rewrites (_pat x) => (return) *)
   211 (* rewrites (_pat x) => (return) *)
   210 (* rewrites (_var x t) => (Abs_CFun (%x. t)) *)
   212 (* rewrites (_var x t) => (Abs_CFun (%x. t)) *)
   211   [("_pat", K (Syntax.const "Fixrec.return")),
   213   [("_pat", K (Syntax.const "Fixrec.return")),
   212    mk_binder_tr ("_var", "Abs_CFun")];
   214    mk_binder_tr ("_var", @{const_syntax Abs_CFun})];
   213 *}
   215 *}
   214 
   216 
   215 text {* Printing Case expressions *}
   217 text {* Printing Case expressions *}
   216 
   218 
   217 syntax
   219 syntax
   218   "_match" :: "'a"
   220   "_match" :: "'a"
   219 
   221 
   220 print_translation {*
   222 print_translation {*
   221   let
   223   let
   222     fun dest_LAM (Const ("Rep_CFun",_) $ Const ("unit_when",_) $ t) =
   224     fun dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax unit_when},_) $ t) =
   223           (Syntax.const "Unity", t)
   225           (Syntax.const @{const_syntax Unity}, t)
   224     |   dest_LAM (Const ("Rep_CFun",_) $ Const ("csplit",_) $ t) =
   226     |   dest_LAM (Const (@{const_syntax Rep_CFun},_) $ Const (@{const_syntax csplit},_) $ t) =
   225           let
   227           let
   226             val (v1, t1) = dest_LAM t;
   228             val (v1, t1) = dest_LAM t;
   227             val (v2, t2) = dest_LAM t1;
   229             val (v2, t2) = dest_LAM t1;
   228           in (Syntax.const "_args" $ v1 $ v2, t2) end 
   230           in (Syntax.const "_args" $ v1 $ v2, t2) end 
   229     |   dest_LAM (Const ("Abs_CFun",_) $ t) =
   231     |   dest_LAM (Const (@{const_syntax Abs_CFun},_) $ t) =
   230           let
   232           let
   231             val abs = case t of Abs abs => abs
   233             val abs = case t of Abs abs => abs
   232                 | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
   234                 | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
   233             val (x, t') = atomic_abs_tr' abs;
   235             val (x, t') = atomic_abs_tr' abs;
   234           in (Syntax.const "_var" $ x, t') end
   236           in (Syntax.const "_var" $ x, t') end
   235     |   dest_LAM _ = raise Match; (* too few vars: abort translation *)
   237     |   dest_LAM _ = raise Match; (* too few vars: abort translation *)
   236 
   238 
   237     fun Case1_tr' [Const("branch",_) $ p, r] =
   239     fun Case1_tr' [Const(@{const_syntax branch},_) $ p, r] =
   238           let val (v, t) = dest_LAM r;
   240           let val (v, t) = dest_LAM r;
   239           in Syntax.const "_Case1" $ (Syntax.const "_match" $ p $ v) $ t end;
   241           in Syntax.const "_Case1" $ (Syntax.const "_match" $ p $ v) $ t end;
   240 
   242 
   241   in [("Rep_CFun", Case1_tr')] end;
   243   in [(@{const_syntax Rep_CFun}, Case1_tr')] end;
   242 *}
   244 *}
   243 
   245 
   244 translations
   246 translations
   245   "x" <= "_match Fixrec.return (_var x)"
   247   "x" <= "_match Fixrec.return (_var x)"
   246 
   248 
   247 
   249 
   248 subsection {* Pattern combinators for data constructors *}
   250 subsection {* Pattern combinators for data constructors *}
   249 
   251 
   250 types ('a, 'b) pat = "'a \<rightarrow> 'b maybe"
   252 types ('a, 'b) pat = "'a \<rightarrow> 'b maybe"
   251 
   253 
   252 constdefs
   254 definition
   253   cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat"
   255   cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat" where
   254   "cpair_pat p1 p2 \<equiv> \<Lambda>\<langle>x, y\<rangle>.
   256   "cpair_pat p1 p2 = (\<Lambda>\<langle>x, y\<rangle>.
   255     bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. return\<cdot>\<langle>a, b\<rangle>))"
   257     bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. return\<cdot>\<langle>a, b\<rangle>)))"
   256 
   258 
       
   259 definition
   257   spair_pat ::
   260   spair_pat ::
   258   "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat"
   261   "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat" where
   259   "spair_pat p1 p2 \<equiv> \<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>\<langle>x, y\<rangle>"
   262   "spair_pat p1 p2 = (\<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>\<langle>x, y\<rangle>)"
   260 
   263 
   261   sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat"
   264 definition
   262   "sinl_pat p \<equiv> sscase\<cdot>p\<cdot>(\<Lambda> x. fail)"
   265   sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
   263 
   266   "sinl_pat p = sscase\<cdot>p\<cdot>(\<Lambda> x. fail)"
   264   sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat"
   267 
   265   "sinr_pat p \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>p"
   268 definition
   266 
   269   sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
   267   up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat"
   270   "sinr_pat p = sscase\<cdot>(\<Lambda> x. fail)\<cdot>p"
   268   "up_pat p \<equiv> fup\<cdot>p"
   271 
   269 
   272 definition
   270   TT_pat :: "(tr, unit) pat"
   273   up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat" where
   271   "TT_pat \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi"
   274   "up_pat p = fup\<cdot>p"
   272 
   275 
   273   FF_pat :: "(tr, unit) pat"
   276 definition
   274   "FF_pat \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi"
   277   TT_pat :: "(tr, unit) pat" where
   275 
   278   "TT_pat = (\<Lambda> b. If b then return\<cdot>() else fail fi)"
   276   ONE_pat :: "(one, unit) pat"
   279 
   277   "ONE_pat \<equiv> \<Lambda> ONE. return\<cdot>()"
   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>())"
   278 
   287 
   279 text {* Parse translations (patterns) *}
   288 text {* Parse translations (patterns) *}
   280 translations
   289 translations
   281   "_pat (cpair\<cdot>x\<cdot>y)" => "cpair_pat (_pat x) (_pat y)"
   290   "_pat (CONST cpair\<cdot>x\<cdot>y)" => "CONST cpair_pat (_pat x) (_pat y)"
   282   "_pat (spair\<cdot>x\<cdot>y)" => "spair_pat (_pat x) (_pat y)"
   291   "_pat (CONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
   283   "_pat (sinl\<cdot>x)" => "sinl_pat (_pat x)"
   292   "_pat (CONST sinl\<cdot>x)" => "CONST sinl_pat (_pat x)"
   284   "_pat (sinr\<cdot>x)" => "sinr_pat (_pat x)"
   293   "_pat (CONST sinr\<cdot>x)" => "CONST sinr_pat (_pat x)"
   285   "_pat (up\<cdot>x)" => "up_pat (_pat x)"
   294   "_pat (CONST up\<cdot>x)" => "CONST up_pat (_pat x)"
   286   "_pat TT" => "TT_pat"
   295   "_pat (CONST TT)" => "CONST TT_pat"
   287   "_pat FF" => "FF_pat"
   296   "_pat (CONST FF)" => "CONST FF_pat"
   288   "_pat ONE" => "ONE_pat"
   297   "_pat (CONST ONE)" => "CONST ONE_pat"
   289 
   298 
   290 text {* Parse translations (variables) *}
   299 text {* Parse translations (variables) *}
   291 translations
   300 translations
   292   "_var (cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
   301   "_var (CONST cpair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
   293   "_var (spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
   302   "_var (CONST spair\<cdot>x\<cdot>y) r" => "_var (_args x y) r"
   294   "_var (sinl\<cdot>x) r" => "_var x r"
   303   "_var (CONST sinl\<cdot>x) r" => "_var x r"
   295   "_var (sinr\<cdot>x) r" => "_var x r"
   304   "_var (CONST sinr\<cdot>x) r" => "_var x r"
   296   "_var (up\<cdot>x) r" => "_var x r"
   305   "_var (CONST up\<cdot>x) r" => "_var x r"
   297   "_var TT r" => "_var () r"
   306   "_var (CONST TT) r" => "_var () r"
   298   "_var FF r" => "_var () r"
   307   "_var (CONST FF) r" => "_var () r"
   299   "_var ONE r" => "_var () r"
   308   "_var (CONST ONE) r" => "_var () r"
   300 
   309 
   301 text {* Print translations *}
   310 text {* Print translations *}
   302 translations
   311 translations
   303   "cpair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
   312   "CONST cpair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
   304       <= "_match (cpair_pat p1 p2) (_args v1 v2)"
   313       <= "_match (CONST cpair_pat p1 p2) (_args v1 v2)"
   305   "spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
   314   "CONST spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
   306       <= "_match (spair_pat p1 p2) (_args v1 v2)"
   315       <= "_match (CONST spair_pat p1 p2) (_args v1 v2)"
   307   "sinl\<cdot>(_match p1 v1)" <= "_match (sinl_pat p1) v1"
   316   "CONST sinl\<cdot>(_match p1 v1)" <= "_match (CONST sinl_pat p1) v1"
   308   "sinr\<cdot>(_match p1 v1)" <= "_match (sinr_pat p1) v1"
   317   "CONST sinr\<cdot>(_match p1 v1)" <= "_match (CONST sinr_pat p1) v1"
   309   "up\<cdot>(_match p1 v1)" <= "_match (up_pat p1) v1"
   318   "CONST up\<cdot>(_match p1 v1)" <= "_match (CONST up_pat p1) v1"
   310   "TT" <= "_match TT_pat ()"
   319   "CONST TT" <= "_match (CONST TT_pat) ()"
   311   "FF" <= "_match FF_pat ()"
   320   "CONST FF" <= "_match (CONST FF_pat) ()"
   312   "ONE" <= "_match ONE_pat ()"
   321   "CONST ONE" <= "_match (CONST ONE_pat) ()"
   313 
   322 
   314 lemma cpair_pat1:
   323 lemma cpair_pat1:
   315   "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = \<bottom>"
   324   "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>\<langle>x, y\<rangle> = \<bottom>"
   316 apply (simp add: branch_def cpair_pat_def)
   325 apply (simp add: branch_def cpair_pat_def)
   317 apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
   326 apply (rule_tac p="p\<cdot>x" in maybeE, simp_all)
   380 
   389 
   381 syntax
   390 syntax
   382   "_as_pat" :: "[idt, 'a] \<Rightarrow> 'a" (infixr "\<as>" 10)
   391   "_as_pat" :: "[idt, 'a] \<Rightarrow> 'a" (infixr "\<as>" 10)
   383   "_lazy_pat" :: "'a \<Rightarrow> 'a" ("\<lazy> _" [1000] 1000)
   392   "_lazy_pat" :: "'a \<Rightarrow> 'a" ("\<lazy> _" [1000] 1000)
   384 
   393 
   385 constdefs
   394 definition
   386   wild_pat :: "'a \<rightarrow> unit maybe"
   395   wild_pat :: "'a \<rightarrow> unit maybe" where
   387   "wild_pat \<equiv> \<Lambda> x. return\<cdot>()"
   396   "wild_pat = (\<Lambda> x. return\<cdot>())"
   388 
   397 
   389   as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe"
   398 definition
   390   "as_pat p \<equiv> \<Lambda> x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. return\<cdot>\<langle>x, a\<rangle>)"
   399   as_pat :: "('a \<rightarrow> 'b maybe) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) maybe" where
   391 
   400   "as_pat p = (\<Lambda> x. bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. return\<cdot>\<langle>x, a\<rangle>))"
   392   lazy_pat :: "('a \<rightarrow> 'b::pcpo maybe) \<Rightarrow> ('a \<rightarrow> 'b maybe)"
   401 
   393   "lazy_pat p \<equiv> \<Lambda> x. return\<cdot>(run\<cdot>(p\<cdot>x))"
   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)))"
   394 
   405 
   395 text {* Parse translations (patterns) *}
   406 text {* Parse translations (patterns) *}
   396 translations
   407 translations
   397   "_pat _" => "wild_pat"
   408   "_pat _" => "CONST wild_pat"
   398   "_pat (_as_pat x y)" => "as_pat (_pat y)"
   409   "_pat (_as_pat x y)" => "CONST as_pat (_pat y)"
   399   "_pat (_lazy_pat x)" => "lazy_pat (_pat x)"
   410   "_pat (_lazy_pat x)" => "CONST lazy_pat (_pat x)"
   400 
   411 
   401 text {* Parse translations (variables) *}
   412 text {* Parse translations (variables) *}
   402 translations
   413 translations
   403   "_var _ r" => "_var () r"
   414   "_var _ r" => "_var () r"
   404   "_var (_as_pat x y) r" => "_var (_args x y) r"
   415   "_var (_as_pat x y) r" => "_var (_args x y) r"
   405   "_var (_lazy_pat x) r" => "_var x r"
   416   "_var (_lazy_pat x) r" => "_var x r"
   406 
   417 
   407 text {* Print translations *}
   418 text {* Print translations *}
   408 translations
   419 translations
   409   "_" <= "_match wild_pat ()"
   420   "_" <= "_match (CONST wild_pat) ()"
   410   "_as_pat x (_match p v)" <= "_match (as_pat p) (_args (_var x) v)"
   421   "_as_pat x (_match p v)" <= "_match (CONST as_pat p) (_args (_var x) v)"
   411   "_lazy_pat (_match p v)" <= "_match (lazy_pat p) v"
   422   "_lazy_pat (_match p v)" <= "_match (CONST lazy_pat p) v"
   412 
   423 
   413 text {* Lazy patterns in lambda abstractions *}
   424 text {* Lazy patterns in lambda abstractions *}
   414 translations
   425 translations
   415   "_cabs (_lazy_pat p) r" == "run oo (_Case1 (_lazy_pat p) r)"
   426   "_cabs (_lazy_pat p) r" == "CONST run oo (_Case1 (_lazy_pat p) r)"
   416 
   427 
   417 lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = return\<cdot>r"
   428 lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = return\<cdot>r"
   418 by (simp add: branch_def wild_pat_def)
   429 by (simp add: branch_def wild_pat_def)
   419 
   430 
   420 lemma as_pat [simp]:
   431 lemma as_pat [simp]:
   434 
   445 
   435 subsection {* Match functions for built-in types *}
   446 subsection {* Match functions for built-in types *}
   436 
   447 
   437 defaultsort pcpo
   448 defaultsort pcpo
   438 
   449 
   439 constdefs
   450 definition
   440   match_UU :: "'a \<rightarrow> unit maybe"
   451   match_UU :: "'a \<rightarrow> unit maybe" where
   441   "match_UU \<equiv> \<Lambda> x. fail"
   452   "match_UU = (\<Lambda> x. fail)"
   442 
   453 
   443   match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe"
   454 definition
   444   "match_cpair \<equiv> csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
   455   match_cpair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<times> 'b) maybe" where
   445 
   456   "match_cpair = csplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
   446   match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe"
   457 
   447   "match_spair \<equiv> ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
   458 definition
   448 
   459   match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<times> 'b) maybe" where
   449   match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe"
   460   "match_spair = ssplit\<cdot>(\<Lambda> x y. return\<cdot><x,y>)"
   450   "match_sinl \<equiv> sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
   461 
   451 
   462 definition
   452   match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe"
   463   match_sinl :: "'a \<oplus> 'b \<rightarrow> 'a maybe" where
   453   "match_sinr \<equiv> sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
   464   "match_sinl = sscase\<cdot>return\<cdot>(\<Lambda> y. fail)"
   454 
   465 
   455   match_up :: "'a::cpo u \<rightarrow> 'a maybe"
   466 definition
   456   "match_up \<equiv> fup\<cdot>return"
   467   match_sinr :: "'a \<oplus> 'b \<rightarrow> 'b maybe" where
   457 
   468   "match_sinr = sscase\<cdot>(\<Lambda> x. fail)\<cdot>return"
   458   match_ONE :: "one \<rightarrow> unit maybe"
   469 
   459   "match_ONE \<equiv> \<Lambda> ONE. return\<cdot>()"
   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>())"
   460  
   477  
   461   match_TT :: "tr \<rightarrow> unit maybe"
   478 definition
   462   "match_TT \<equiv> \<Lambda> b. If b then return\<cdot>() else fail fi"
   479   match_TT :: "tr \<rightarrow> unit maybe" where
       
   480   "match_TT = (\<Lambda> b. If b then return\<cdot>() else fail fi)"
   463  
   481  
   464   match_FF :: "tr \<rightarrow> unit maybe"
   482 definition
   465   "match_FF \<equiv> \<Lambda> b. If b then fail else return\<cdot>() fi"
   483   match_FF :: "tr \<rightarrow> unit maybe" where
       
   484   "match_FF = (\<Lambda> b. If b then fail else return\<cdot>() fi)"
   466 
   485 
   467 lemma match_UU_simps [simp]:
   486 lemma match_UU_simps [simp]:
   468   "match_UU\<cdot>x = fail"
   487   "match_UU\<cdot>x = fail"
   469 by (simp add: match_UU_def)
   488 by (simp add: match_UU_def)
   470 
   489 
   530 text {* lemma for proving rewrite rules *}
   549 text {* lemma for proving rewrite rules *}
   531 
   550 
   532 lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
   551 lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
   533 by simp
   552 by simp
   534 
   553 
   535 ML {*
       
   536 val cpair_equalI = thm "cpair_equalI";
       
   537 val cpair_eqD1 = thm "cpair_eqD1";
       
   538 val cpair_eqD2 = thm "cpair_eqD2";
       
   539 val ssubst_lhs = thm "ssubst_lhs";
       
   540 val branch_def = thm "branch_def";
       
   541 *}
       
   542 
   554 
   543 subsection {* Initializing the fixrec package *}
   555 subsection {* Initializing the fixrec package *}
   544 
   556 
   545 use "Tools/fixrec_package.ML"
   557 use "Tools/fixrec_package.ML"
   546 
   558