src/HOLCF/ex/Pattern_Match.thy
changeset 40774 0437dbc127b3
parent 40773 6c12f5e24e34
child 40775 ed7a4eadb2f6
     1.1 --- a/src/HOLCF/ex/Pattern_Match.thy	Sat Nov 27 14:34:54 2010 -0800
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,609 +0,0 @@
     1.4 -(*  Title:      HOLCF/ex/Pattern_Match.thy
     1.5 -    Author:     Brian Huffman
     1.6 -*)
     1.7 -
     1.8 -header {* An experimental pattern-matching notation *}
     1.9 -
    1.10 -theory Pattern_Match
    1.11 -imports HOLCF
    1.12 -begin
    1.13 -
    1.14 -default_sort pcpo
    1.15 -
    1.16 -text {* FIXME: Find a proper way to un-hide constants. *}
    1.17 -
    1.18 -abbreviation fail :: "'a match"
    1.19 -where "fail \<equiv> Fixrec.fail"
    1.20 -
    1.21 -abbreviation succeed :: "'a \<rightarrow> 'a match"
    1.22 -where "succeed \<equiv> Fixrec.succeed"
    1.23 -
    1.24 -abbreviation run :: "'a match \<rightarrow> 'a"
    1.25 -where "run \<equiv> Fixrec.run"
    1.26 -
    1.27 -subsection {* Fatbar combinator *}
    1.28 -
    1.29 -definition
    1.30 -  fatbar :: "('a \<rightarrow> 'b match) \<rightarrow> ('a \<rightarrow> 'b match) \<rightarrow> ('a \<rightarrow> 'b match)" where
    1.31 -  "fatbar = (\<Lambda> a b x. a\<cdot>x +++ b\<cdot>x)"
    1.32 -
    1.33 -abbreviation
    1.34 -  fatbar_syn :: "['a \<rightarrow> 'b match, 'a \<rightarrow> 'b match] \<Rightarrow> 'a \<rightarrow> 'b match" (infixr "\<parallel>" 60)  where
    1.35 -  "m1 \<parallel> m2 == fatbar\<cdot>m1\<cdot>m2"
    1.36 -
    1.37 -lemma fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> (m \<parallel> ms)\<cdot>x = \<bottom>"
    1.38 -by (simp add: fatbar_def)
    1.39 -
    1.40 -lemma fatbar2: "m\<cdot>x = fail \<Longrightarrow> (m \<parallel> ms)\<cdot>x = ms\<cdot>x"
    1.41 -by (simp add: fatbar_def)
    1.42 -
    1.43 -lemma fatbar3: "m\<cdot>x = succeed\<cdot>y \<Longrightarrow> (m \<parallel> ms)\<cdot>x = succeed\<cdot>y"
    1.44 -by (simp add: fatbar_def)
    1.45 -
    1.46 -lemmas fatbar_simps = fatbar1 fatbar2 fatbar3
    1.47 -
    1.48 -lemma run_fatbar1: "m\<cdot>x = \<bottom> \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = \<bottom>"
    1.49 -by (simp add: fatbar_def)
    1.50 -
    1.51 -lemma run_fatbar2: "m\<cdot>x = fail \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = run\<cdot>(ms\<cdot>x)"
    1.52 -by (simp add: fatbar_def)
    1.53 -
    1.54 -lemma run_fatbar3: "m\<cdot>x = succeed\<cdot>y \<Longrightarrow> run\<cdot>((m \<parallel> ms)\<cdot>x) = y"
    1.55 -by (simp add: fatbar_def)
    1.56 -
    1.57 -lemmas run_fatbar_simps [simp] = run_fatbar1 run_fatbar2 run_fatbar3
    1.58 -
    1.59 -subsection {* Bind operator for match monad *}
    1.60 -
    1.61 -definition match_bind :: "'a match \<rightarrow> ('a \<rightarrow> 'b match) \<rightarrow> 'b match" where
    1.62 -  "match_bind = (\<Lambda> m k. sscase\<cdot>(\<Lambda> _. fail)\<cdot>(fup\<cdot>k)\<cdot>(Rep_match m))"
    1.63 -
    1.64 -lemma match_bind_simps [simp]:
    1.65 -  "match_bind\<cdot>\<bottom>\<cdot>k = \<bottom>"
    1.66 -  "match_bind\<cdot>fail\<cdot>k = fail"
    1.67 -  "match_bind\<cdot>(succeed\<cdot>x)\<cdot>k = k\<cdot>x"
    1.68 -unfolding match_bind_def fail_def succeed_def
    1.69 -by (simp_all add: cont2cont_Rep_match cont_Abs_match
    1.70 -  Rep_match_strict Abs_match_inverse)
    1.71 -
    1.72 -subsection {* Case branch combinator *}
    1.73 -
    1.74 -definition
    1.75 -  branch :: "('a \<rightarrow> 'b match) \<Rightarrow> ('b \<rightarrow> 'c) \<rightarrow> ('a \<rightarrow> 'c match)" where
    1.76 -  "branch p \<equiv> \<Lambda> r x. match_bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> y. succeed\<cdot>(r\<cdot>y))"
    1.77 -
    1.78 -lemma branch_simps:
    1.79 -  "p\<cdot>x = \<bottom> \<Longrightarrow> branch p\<cdot>r\<cdot>x = \<bottom>"
    1.80 -  "p\<cdot>x = fail \<Longrightarrow> branch p\<cdot>r\<cdot>x = fail"
    1.81 -  "p\<cdot>x = succeed\<cdot>y \<Longrightarrow> branch p\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>y)"
    1.82 -by (simp_all add: branch_def)
    1.83 -
    1.84 -lemma branch_succeed [simp]: "branch succeed\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>x)"
    1.85 -by (simp add: branch_def)
    1.86 -
    1.87 -subsection {* Cases operator *}
    1.88 -
    1.89 -definition
    1.90 -  cases :: "'a match \<rightarrow> 'a::pcpo" where
    1.91 -  "cases = Fixrec.run"
    1.92 -
    1.93 -text {* rewrite rules for cases *}
    1.94 -
    1.95 -lemma cases_strict [simp]: "cases\<cdot>\<bottom> = \<bottom>"
    1.96 -by (simp add: cases_def)
    1.97 -
    1.98 -lemma cases_fail [simp]: "cases\<cdot>fail = \<bottom>"
    1.99 -by (simp add: cases_def)
   1.100 -
   1.101 -lemma cases_succeed [simp]: "cases\<cdot>(succeed\<cdot>x) = x"
   1.102 -by (simp add: cases_def)
   1.103 -
   1.104 -subsection {* Case syntax *}
   1.105 -
   1.106 -nonterminals
   1.107 -  Case_syn  Cases_syn
   1.108 -
   1.109 -syntax
   1.110 -  "_Case_syntax":: "['a, Cases_syn] => 'b"               ("(Case _ of/ _)" 10)
   1.111 -  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ =>/ _)" 10)
   1.112 -  ""            :: "Case_syn => Cases_syn"               ("_")
   1.113 -  "_Case2"      :: "[Case_syn, Cases_syn] => Cases_syn"  ("_/ | _")
   1.114 -
   1.115 -syntax (xsymbols)
   1.116 -  "_Case1"      :: "['a, 'b] => Case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
   1.117 -
   1.118 -translations
   1.119 -  "_Case_syntax x ms" == "CONST cases\<cdot>(ms\<cdot>x)"
   1.120 -  "_Case2 m ms" == "m \<parallel> ms"
   1.121 -
   1.122 -text {* Parsing Case expressions *}
   1.123 -
   1.124 -syntax
   1.125 -  "_pat" :: "'a"
   1.126 -  "_variable" :: "'a"
   1.127 -  "_noargs" :: "'a"
   1.128 -
   1.129 -translations
   1.130 -  "_Case1 p r" => "CONST branch (_pat p)\<cdot>(_variable p r)"
   1.131 -  "_variable (_args x y) r" => "CONST csplit\<cdot>(_variable x (_variable y r))"
   1.132 -  "_variable _noargs r" => "CONST unit_when\<cdot>r"
   1.133 -
   1.134 -parse_translation {*
   1.135 -(* rewrite (_pat x) => (succeed) *)
   1.136 -(* rewrite (_variable x t) => (Abs_cfun (%x. t)) *)
   1.137 - [(@{syntax_const "_pat"}, fn _ => Syntax.const @{const_syntax Fixrec.succeed}),
   1.138 -  mk_binder_tr (@{syntax_const "_variable"}, @{const_syntax Abs_cfun})];
   1.139 -*}
   1.140 -
   1.141 -text {* Printing Case expressions *}
   1.142 -
   1.143 -syntax
   1.144 -  "_match" :: "'a"
   1.145 -
   1.146 -print_translation {*
   1.147 -  let
   1.148 -    fun dest_LAM (Const (@{const_syntax Rep_cfun},_) $ Const (@{const_syntax unit_when},_) $ t) =
   1.149 -          (Syntax.const @{syntax_const "_noargs"}, t)
   1.150 -    |   dest_LAM (Const (@{const_syntax Rep_cfun},_) $ Const (@{const_syntax csplit},_) $ t) =
   1.151 -          let
   1.152 -            val (v1, t1) = dest_LAM t;
   1.153 -            val (v2, t2) = dest_LAM t1;
   1.154 -          in (Syntax.const @{syntax_const "_args"} $ v1 $ v2, t2) end
   1.155 -    |   dest_LAM (Const (@{const_syntax Abs_cfun},_) $ t) =
   1.156 -          let
   1.157 -            val abs =
   1.158 -              case t of Abs abs => abs
   1.159 -                | _ => ("x", dummyT, incr_boundvars 1 t $ Bound 0);
   1.160 -            val (x, t') = atomic_abs_tr' abs;
   1.161 -          in (Syntax.const @{syntax_const "_variable"} $ x, t') end
   1.162 -    |   dest_LAM _ = raise Match; (* too few vars: abort translation *)
   1.163 -
   1.164 -    fun Case1_tr' [Const(@{const_syntax branch},_) $ p, r] =
   1.165 -          let val (v, t) = dest_LAM r in
   1.166 -            Syntax.const @{syntax_const "_Case1"} $
   1.167 -              (Syntax.const @{syntax_const "_match"} $ p $ v) $ t
   1.168 -          end;
   1.169 -
   1.170 -  in [(@{const_syntax Rep_cfun}, Case1_tr')] end;
   1.171 -*}
   1.172 -
   1.173 -translations
   1.174 -  "x" <= "_match (CONST succeed) (_variable x)"
   1.175 -
   1.176 -
   1.177 -subsection {* Pattern combinators for data constructors *}
   1.178 -
   1.179 -types ('a, 'b) pat = "'a \<rightarrow> 'b match"
   1.180 -
   1.181 -definition
   1.182 -  cpair_pat :: "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a \<times> 'b, 'c \<times> 'd) pat" where
   1.183 -  "cpair_pat p1 p2 = (\<Lambda>(x, y).
   1.184 -    match_bind\<cdot>(p1\<cdot>x)\<cdot>(\<Lambda> a. match_bind\<cdot>(p2\<cdot>y)\<cdot>(\<Lambda> b. succeed\<cdot>(a, b))))"
   1.185 -
   1.186 -definition
   1.187 -  spair_pat ::
   1.188 -  "('a, 'c) pat \<Rightarrow> ('b, 'd) pat \<Rightarrow> ('a::pcpo \<otimes> 'b::pcpo, 'c \<times> 'd) pat" where
   1.189 -  "spair_pat p1 p2 = (\<Lambda>(:x, y:). cpair_pat p1 p2\<cdot>(x, y))"
   1.190 -
   1.191 -definition
   1.192 -  sinl_pat :: "('a, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
   1.193 -  "sinl_pat p = sscase\<cdot>p\<cdot>(\<Lambda> x. fail)"
   1.194 -
   1.195 -definition
   1.196 -  sinr_pat :: "('b, 'c) pat \<Rightarrow> ('a::pcpo \<oplus> 'b::pcpo, 'c) pat" where
   1.197 -  "sinr_pat p = sscase\<cdot>(\<Lambda> x. fail)\<cdot>p"
   1.198 -
   1.199 -definition
   1.200 -  up_pat :: "('a, 'b) pat \<Rightarrow> ('a u, 'b) pat" where
   1.201 -  "up_pat p = fup\<cdot>p"
   1.202 -
   1.203 -definition
   1.204 -  TT_pat :: "(tr, unit) pat" where
   1.205 -  "TT_pat = (\<Lambda> b. If b then succeed\<cdot>() else fail)"
   1.206 -
   1.207 -definition
   1.208 -  FF_pat :: "(tr, unit) pat" where
   1.209 -  "FF_pat = (\<Lambda> b. If b then fail else succeed\<cdot>())"
   1.210 -
   1.211 -definition
   1.212 -  ONE_pat :: "(one, unit) pat" where
   1.213 -  "ONE_pat = (\<Lambda> ONE. succeed\<cdot>())"
   1.214 -
   1.215 -text {* Parse translations (patterns) *}
   1.216 -translations
   1.217 -  "_pat (XCONST Pair x y)" => "CONST cpair_pat (_pat x) (_pat y)"
   1.218 -  "_pat (XCONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
   1.219 -  "_pat (XCONST sinl\<cdot>x)" => "CONST sinl_pat (_pat x)"
   1.220 -  "_pat (XCONST sinr\<cdot>x)" => "CONST sinr_pat (_pat x)"
   1.221 -  "_pat (XCONST up\<cdot>x)" => "CONST up_pat (_pat x)"
   1.222 -  "_pat (XCONST TT)" => "CONST TT_pat"
   1.223 -  "_pat (XCONST FF)" => "CONST FF_pat"
   1.224 -  "_pat (XCONST ONE)" => "CONST ONE_pat"
   1.225 -
   1.226 -text {* CONST version is also needed for constructors with special syntax *}
   1.227 -translations
   1.228 -  "_pat (CONST Pair x y)" => "CONST cpair_pat (_pat x) (_pat y)"
   1.229 -  "_pat (CONST spair\<cdot>x\<cdot>y)" => "CONST spair_pat (_pat x) (_pat y)"
   1.230 -
   1.231 -text {* Parse translations (variables) *}
   1.232 -translations
   1.233 -  "_variable (XCONST Pair x y) r" => "_variable (_args x y) r"
   1.234 -  "_variable (XCONST spair\<cdot>x\<cdot>y) r" => "_variable (_args x y) r"
   1.235 -  "_variable (XCONST sinl\<cdot>x) r" => "_variable x r"
   1.236 -  "_variable (XCONST sinr\<cdot>x) r" => "_variable x r"
   1.237 -  "_variable (XCONST up\<cdot>x) r" => "_variable x r"
   1.238 -  "_variable (XCONST TT) r" => "_variable _noargs r"
   1.239 -  "_variable (XCONST FF) r" => "_variable _noargs r"
   1.240 -  "_variable (XCONST ONE) r" => "_variable _noargs r"
   1.241 -
   1.242 -translations
   1.243 -  "_variable (CONST Pair x y) r" => "_variable (_args x y) r"
   1.244 -  "_variable (CONST spair\<cdot>x\<cdot>y) r" => "_variable (_args x y) r"
   1.245 -
   1.246 -text {* Print translations *}
   1.247 -translations
   1.248 -  "CONST Pair (_match p1 v1) (_match p2 v2)"
   1.249 -      <= "_match (CONST cpair_pat p1 p2) (_args v1 v2)"
   1.250 -  "CONST spair\<cdot>(_match p1 v1)\<cdot>(_match p2 v2)"
   1.251 -      <= "_match (CONST spair_pat p1 p2) (_args v1 v2)"
   1.252 -  "CONST sinl\<cdot>(_match p1 v1)" <= "_match (CONST sinl_pat p1) v1"
   1.253 -  "CONST sinr\<cdot>(_match p1 v1)" <= "_match (CONST sinr_pat p1) v1"
   1.254 -  "CONST up\<cdot>(_match p1 v1)" <= "_match (CONST up_pat p1) v1"
   1.255 -  "CONST TT" <= "_match (CONST TT_pat) _noargs"
   1.256 -  "CONST FF" <= "_match (CONST FF_pat) _noargs"
   1.257 -  "CONST ONE" <= "_match (CONST ONE_pat) _noargs"
   1.258 -
   1.259 -lemma cpair_pat1:
   1.260 -  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>(x, y) = \<bottom>"
   1.261 -apply (simp add: branch_def cpair_pat_def)
   1.262 -apply (cases "p\<cdot>x", simp_all)
   1.263 -done
   1.264 -
   1.265 -lemma cpair_pat2:
   1.266 -  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>(x, y) = fail"
   1.267 -apply (simp add: branch_def cpair_pat_def)
   1.268 -apply (cases "p\<cdot>x", simp_all)
   1.269 -done
   1.270 -
   1.271 -lemma cpair_pat3:
   1.272 -  "branch p\<cdot>r\<cdot>x = succeed\<cdot>s \<Longrightarrow>
   1.273 -   branch (cpair_pat p q)\<cdot>(csplit\<cdot>r)\<cdot>(x, y) = branch q\<cdot>s\<cdot>y"
   1.274 -apply (simp add: branch_def cpair_pat_def)
   1.275 -apply (cases "p\<cdot>x", simp_all)
   1.276 -apply (cases "q\<cdot>y", simp_all)
   1.277 -done
   1.278 -
   1.279 -lemmas cpair_pat [simp] =
   1.280 -  cpair_pat1 cpair_pat2 cpair_pat3
   1.281 -
   1.282 -lemma spair_pat [simp]:
   1.283 -  "branch (spair_pat p1 p2)\<cdot>r\<cdot>\<bottom> = \<bottom>"
   1.284 -  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk>
   1.285 -     \<Longrightarrow> branch (spair_pat p1 p2)\<cdot>r\<cdot>(:x, y:) =
   1.286 -         branch (cpair_pat p1 p2)\<cdot>r\<cdot>(x, y)"
   1.287 -by (simp_all add: branch_def spair_pat_def)
   1.288 -
   1.289 -lemma sinl_pat [simp]:
   1.290 -  "branch (sinl_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
   1.291 -  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = branch p\<cdot>r\<cdot>x"
   1.292 -  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinl_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = fail"
   1.293 -by (simp_all add: branch_def sinl_pat_def)
   1.294 -
   1.295 -lemma sinr_pat [simp]:
   1.296 -  "branch (sinr_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
   1.297 -  "x \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinl\<cdot>x) = fail"
   1.298 -  "y \<noteq> \<bottom> \<Longrightarrow> branch (sinr_pat p)\<cdot>r\<cdot>(sinr\<cdot>y) = branch p\<cdot>r\<cdot>y"
   1.299 -by (simp_all add: branch_def sinr_pat_def)
   1.300 -
   1.301 -lemma up_pat [simp]:
   1.302 -  "branch (up_pat p)\<cdot>r\<cdot>\<bottom> = \<bottom>"
   1.303 -  "branch (up_pat p)\<cdot>r\<cdot>(up\<cdot>x) = branch p\<cdot>r\<cdot>x"
   1.304 -by (simp_all add: branch_def up_pat_def)
   1.305 -
   1.306 -lemma TT_pat [simp]:
   1.307 -  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
   1.308 -  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = succeed\<cdot>r"
   1.309 -  "branch TT_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = fail"
   1.310 -by (simp_all add: branch_def TT_pat_def)
   1.311 -
   1.312 -lemma FF_pat [simp]:
   1.313 -  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
   1.314 -  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>TT = fail"
   1.315 -  "branch FF_pat\<cdot>(unit_when\<cdot>r)\<cdot>FF = succeed\<cdot>r"
   1.316 -by (simp_all add: branch_def FF_pat_def)
   1.317 -
   1.318 -lemma ONE_pat [simp]:
   1.319 -  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>\<bottom> = \<bottom>"
   1.320 -  "branch ONE_pat\<cdot>(unit_when\<cdot>r)\<cdot>ONE = succeed\<cdot>r"
   1.321 -by (simp_all add: branch_def ONE_pat_def)
   1.322 -
   1.323 -
   1.324 -subsection {* Wildcards, as-patterns, and lazy patterns *}
   1.325 -
   1.326 -definition
   1.327 -  wild_pat :: "'a \<rightarrow> unit match" where
   1.328 -  "wild_pat = (\<Lambda> x. succeed\<cdot>())"
   1.329 -
   1.330 -definition
   1.331 -  as_pat :: "('a \<rightarrow> 'b match) \<Rightarrow> 'a \<rightarrow> ('a \<times> 'b) match" where
   1.332 -  "as_pat p = (\<Lambda> x. match_bind\<cdot>(p\<cdot>x)\<cdot>(\<Lambda> a. succeed\<cdot>(x, a)))"
   1.333 -
   1.334 -definition
   1.335 -  lazy_pat :: "('a \<rightarrow> 'b::pcpo match) \<Rightarrow> ('a \<rightarrow> 'b match)" where
   1.336 -  "lazy_pat p = (\<Lambda> x. succeed\<cdot>(cases\<cdot>(p\<cdot>x)))"
   1.337 -
   1.338 -text {* Parse translations (patterns) *}
   1.339 -translations
   1.340 -  "_pat _" => "CONST wild_pat"
   1.341 -
   1.342 -text {* Parse translations (variables) *}
   1.343 -translations
   1.344 -  "_variable _ r" => "_variable _noargs r"
   1.345 -
   1.346 -text {* Print translations *}
   1.347 -translations
   1.348 -  "_" <= "_match (CONST wild_pat) _noargs"
   1.349 -
   1.350 -lemma wild_pat [simp]: "branch wild_pat\<cdot>(unit_when\<cdot>r)\<cdot>x = succeed\<cdot>r"
   1.351 -by (simp add: branch_def wild_pat_def)
   1.352 -
   1.353 -lemma as_pat [simp]:
   1.354 -  "branch (as_pat p)\<cdot>(csplit\<cdot>r)\<cdot>x = branch p\<cdot>(r\<cdot>x)\<cdot>x"
   1.355 -apply (simp add: branch_def as_pat_def)
   1.356 -apply (cases "p\<cdot>x", simp_all)
   1.357 -done
   1.358 -
   1.359 -lemma lazy_pat [simp]:
   1.360 -  "branch p\<cdot>r\<cdot>x = \<bottom> \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>\<bottom>)"
   1.361 -  "branch p\<cdot>r\<cdot>x = fail \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = succeed\<cdot>(r\<cdot>\<bottom>)"
   1.362 -  "branch p\<cdot>r\<cdot>x = succeed\<cdot>s \<Longrightarrow> branch (lazy_pat p)\<cdot>r\<cdot>x = succeed\<cdot>s"
   1.363 -apply (simp_all add: branch_def lazy_pat_def)
   1.364 -apply (cases "p\<cdot>x", simp_all)+
   1.365 -done
   1.366 -
   1.367 -subsection {* Examples *}
   1.368 -
   1.369 -term "Case t of (:up\<cdot>(sinl\<cdot>x), sinr\<cdot>y:) \<Rightarrow> (x, y)"
   1.370 -
   1.371 -term "\<Lambda> t. Case t of up\<cdot>(sinl\<cdot>a) \<Rightarrow> a | up\<cdot>(sinr\<cdot>b) \<Rightarrow> b"
   1.372 -
   1.373 -term "\<Lambda> t. Case t of (:up\<cdot>(sinl\<cdot>_), sinr\<cdot>x:) \<Rightarrow> x"
   1.374 -
   1.375 -subsection {* ML code for generating definitions *}
   1.376 -
   1.377 -ML {*
   1.378 -local open HOLCF_Library in
   1.379 -
   1.380 -infixr 6 ->>;
   1.381 -infix 9 ` ;
   1.382 -
   1.383 -val beta_rules =
   1.384 -  @{thms beta_cfun cont_id cont_const cont2cont_APP cont2cont_LAM'} @
   1.385 -  @{thms cont2cont_fst cont2cont_snd cont2cont_Pair};
   1.386 -
   1.387 -val beta_ss = HOL_basic_ss addsimps (simp_thms @ beta_rules);
   1.388 -
   1.389 -fun define_consts
   1.390 -    (specs : (binding * term * mixfix) list)
   1.391 -    (thy : theory)
   1.392 -    : (term list * thm list) * theory =
   1.393 -  let
   1.394 -    fun mk_decl (b, t, mx) = (b, fastype_of t, mx);
   1.395 -    val decls = map mk_decl specs;
   1.396 -    val thy = Cont_Consts.add_consts decls thy;
   1.397 -    fun mk_const (b, T, mx) = Const (Sign.full_name thy b, T);
   1.398 -    val consts = map mk_const decls;
   1.399 -    fun mk_def c (b, t, mx) =
   1.400 -      (Binding.suffix_name "_def" b, Logic.mk_equals (c, t));
   1.401 -    val defs = map2 mk_def consts specs;
   1.402 -    val (def_thms, thy) =
   1.403 -      Global_Theory.add_defs false (map Thm.no_attributes defs) thy;
   1.404 -  in
   1.405 -    ((consts, def_thms), thy)
   1.406 -  end;
   1.407 -
   1.408 -fun prove
   1.409 -    (thy : theory)
   1.410 -    (defs : thm list)
   1.411 -    (goal : term)
   1.412 -    (tacs : {prems: thm list, context: Proof.context} -> tactic list)
   1.413 -    : thm =
   1.414 -  let
   1.415 -    fun tac {prems, context} =
   1.416 -      rewrite_goals_tac defs THEN
   1.417 -      EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
   1.418 -  in
   1.419 -    Goal.prove_global thy [] [] goal tac
   1.420 -  end;
   1.421 -
   1.422 -fun get_vars_avoiding
   1.423 -    (taken : string list)
   1.424 -    (args : (bool * typ) list)
   1.425 -    : (term list * term list) =
   1.426 -  let
   1.427 -    val Ts = map snd args;
   1.428 -    val ns = Name.variant_list taken (Datatype_Prop.make_tnames Ts);
   1.429 -    val vs = map Free (ns ~~ Ts);
   1.430 -    val nonlazy = map snd (filter_out (fst o fst) (args ~~ vs));
   1.431 -  in
   1.432 -    (vs, nonlazy)
   1.433 -  end;
   1.434 -
   1.435 -(******************************************************************************)
   1.436 -(************** definitions and theorems for pattern combinators **************)
   1.437 -(******************************************************************************)
   1.438 -
   1.439 -fun add_pattern_combinators
   1.440 -    (bindings : binding list)
   1.441 -    (spec : (term * (bool * typ) list) list)
   1.442 -    (lhsT : typ)
   1.443 -    (exhaust : thm)
   1.444 -    (case_const : typ -> term)
   1.445 -    (case_rews : thm list)
   1.446 -    (thy : theory) =
   1.447 -  let
   1.448 -
   1.449 -    (* utility functions *)
   1.450 -    fun mk_pair_pat (p1, p2) =
   1.451 -      let
   1.452 -        val T1 = fastype_of p1;
   1.453 -        val T2 = fastype_of p2;
   1.454 -        val (U1, V1) = apsnd dest_matchT (dest_cfunT T1);
   1.455 -        val (U2, V2) = apsnd dest_matchT (dest_cfunT T2);
   1.456 -        val pat_typ = [T1, T2] --->
   1.457 -            (mk_prodT (U1, U2) ->> mk_matchT (mk_prodT (V1, V2)));
   1.458 -        val pat_const = Const (@{const_name cpair_pat}, pat_typ);
   1.459 -      in
   1.460 -        pat_const $ p1 $ p2
   1.461 -      end;
   1.462 -    fun mk_tuple_pat [] = succeed_const HOLogic.unitT
   1.463 -      | mk_tuple_pat ps = foldr1 mk_pair_pat ps;
   1.464 -    fun branch_const (T,U,V) = 
   1.465 -      Const (@{const_name branch},
   1.466 -        (T ->> mk_matchT U) --> (U ->> V) ->> T ->> mk_matchT V);
   1.467 -
   1.468 -    (* define pattern combinators *)
   1.469 -    local
   1.470 -      val tns = map (fst o dest_TFree) (snd (dest_Type lhsT));
   1.471 -
   1.472 -      fun pat_eqn (i, (bind, (con, args))) : binding * term * mixfix =
   1.473 -        let
   1.474 -          val pat_bind = Binding.suffix_name "_pat" bind;
   1.475 -          val Ts = map snd args;
   1.476 -          val Vs =
   1.477 -              (map (K "'t") args)
   1.478 -              |> Datatype_Prop.indexify_names
   1.479 -              |> Name.variant_list tns
   1.480 -              |> map (fn t => TFree (t, @{sort pcpo}));
   1.481 -          val patNs = Datatype_Prop.indexify_names (map (K "pat") args);
   1.482 -          val patTs = map2 (fn T => fn V => T ->> mk_matchT V) Ts Vs;
   1.483 -          val pats = map Free (patNs ~~ patTs);
   1.484 -          val fail = mk_fail (mk_tupleT Vs);
   1.485 -          val (vs, nonlazy) = get_vars_avoiding patNs args;
   1.486 -          val rhs = big_lambdas vs (mk_tuple_pat pats ` mk_tuple vs);
   1.487 -          fun one_fun (j, (_, args')) =
   1.488 -            let
   1.489 -              val (vs', nonlazy) = get_vars_avoiding patNs args';
   1.490 -            in if i = j then rhs else big_lambdas vs' fail end;
   1.491 -          val funs = map_index one_fun spec;
   1.492 -          val body = list_ccomb (case_const (mk_matchT (mk_tupleT Vs)), funs);
   1.493 -        in
   1.494 -          (pat_bind, lambdas pats body, NoSyn)
   1.495 -        end;
   1.496 -    in
   1.497 -      val ((pat_consts, pat_defs), thy) =
   1.498 -          define_consts (map_index pat_eqn (bindings ~~ spec)) thy
   1.499 -    end;
   1.500 -
   1.501 -    (* syntax translations for pattern combinators *)
   1.502 -    local
   1.503 -      open Syntax
   1.504 -      fun syntax c = Syntax.mark_const (fst (dest_Const c));
   1.505 -      fun app s (l, r) = Syntax.mk_appl (Constant s) [l, r];
   1.506 -      val capp = app @{const_syntax Rep_cfun};
   1.507 -      val capps = Library.foldl capp
   1.508 -
   1.509 -      fun app_var x = Syntax.mk_appl (Constant "_variable") [x, Variable "rhs"];
   1.510 -      fun app_pat x = Syntax.mk_appl (Constant "_pat") [x];
   1.511 -      fun args_list [] = Constant "_noargs"
   1.512 -        | args_list xs = foldr1 (app "_args") xs;
   1.513 -      fun one_case_trans (pat, (con, args)) =
   1.514 -        let
   1.515 -          val cname = Constant (syntax con);
   1.516 -          val pname = Constant (syntax pat);
   1.517 -          val ns = 1 upto length args;
   1.518 -          val xs = map (fn n => Variable ("x"^(string_of_int n))) ns;
   1.519 -          val ps = map (fn n => Variable ("p"^(string_of_int n))) ns;
   1.520 -          val vs = map (fn n => Variable ("v"^(string_of_int n))) ns;
   1.521 -        in
   1.522 -          [ParseRule (app_pat (capps (cname, xs)),
   1.523 -                      mk_appl pname (map app_pat xs)),
   1.524 -           ParseRule (app_var (capps (cname, xs)),
   1.525 -                      app_var (args_list xs)),
   1.526 -           PrintRule (capps (cname, ListPair.map (app "_match") (ps,vs)),
   1.527 -                      app "_match" (mk_appl pname ps, args_list vs))]
   1.528 -        end;
   1.529 -      val trans_rules : Syntax.ast Syntax.trrule list =
   1.530 -          maps one_case_trans (pat_consts ~~ spec);
   1.531 -    in
   1.532 -      val thy = Sign.add_trrules_i trans_rules thy;
   1.533 -    end;
   1.534 -
   1.535 -    (* prove strictness and reduction rules of pattern combinators *)
   1.536 -    local
   1.537 -      val tns = map (fst o dest_TFree) (snd (dest_Type lhsT));
   1.538 -      val rn = Name.variant tns "'r";
   1.539 -      val R = TFree (rn, @{sort pcpo});
   1.540 -      fun pat_lhs (pat, args) =
   1.541 -        let
   1.542 -          val Ts = map snd args;
   1.543 -          val Vs =
   1.544 -              (map (K "'t") args)
   1.545 -              |> Datatype_Prop.indexify_names
   1.546 -              |> Name.variant_list (rn::tns)
   1.547 -              |> map (fn t => TFree (t, @{sort pcpo}));
   1.548 -          val patNs = Datatype_Prop.indexify_names (map (K "pat") args);
   1.549 -          val patTs = map2 (fn T => fn V => T ->> mk_matchT V) Ts Vs;
   1.550 -          val pats = map Free (patNs ~~ patTs);
   1.551 -          val k = Free ("rhs", mk_tupleT Vs ->> R);
   1.552 -          val branch1 = branch_const (lhsT, mk_tupleT Vs, R);
   1.553 -          val fun1 = (branch1 $ list_comb (pat, pats)) ` k;
   1.554 -          val branch2 = branch_const (mk_tupleT Ts, mk_tupleT Vs, R);
   1.555 -          val fun2 = (branch2 $ mk_tuple_pat pats) ` k;
   1.556 -          val taken = "rhs" :: patNs;
   1.557 -        in (fun1, fun2, taken) end;
   1.558 -      fun pat_strict (pat, (con, args)) =
   1.559 -        let
   1.560 -          val (fun1, fun2, taken) = pat_lhs (pat, args);
   1.561 -          val defs = @{thm branch_def} :: pat_defs;
   1.562 -          val goal = mk_trp (mk_strict fun1);
   1.563 -          val rules = @{thms match_bind_simps} @ case_rews;
   1.564 -          val tacs = [simp_tac (beta_ss addsimps rules) 1];
   1.565 -        in prove thy defs goal (K tacs) end;
   1.566 -      fun pat_apps (i, (pat, (con, args))) =
   1.567 -        let
   1.568 -          val (fun1, fun2, taken) = pat_lhs (pat, args);
   1.569 -          fun pat_app (j, (con', args')) =
   1.570 -            let
   1.571 -              val (vs, nonlazy) = get_vars_avoiding taken args';
   1.572 -              val con_app = list_ccomb (con', vs);
   1.573 -              val assms = map (mk_trp o mk_defined) nonlazy;
   1.574 -              val rhs = if i = j then fun2 ` mk_tuple vs else mk_fail R;
   1.575 -              val concl = mk_trp (mk_eq (fun1 ` con_app, rhs));
   1.576 -              val goal = Logic.list_implies (assms, concl);
   1.577 -              val defs = @{thm branch_def} :: pat_defs;
   1.578 -              val rules = @{thms match_bind_simps} @ case_rews;
   1.579 -              val tacs = [asm_simp_tac (beta_ss addsimps rules) 1];
   1.580 -            in prove thy defs goal (K tacs) end;
   1.581 -        in map_index pat_app spec end;
   1.582 -    in
   1.583 -      val pat_stricts = map pat_strict (pat_consts ~~ spec);
   1.584 -      val pat_apps = flat (map_index pat_apps (pat_consts ~~ spec));
   1.585 -    end;
   1.586 -
   1.587 -  in
   1.588 -    (pat_stricts @ pat_apps, thy)
   1.589 -  end
   1.590 -
   1.591 -end
   1.592 -*}
   1.593 -
   1.594 -(*
   1.595 -Cut from HOLCF/Tools/domain_constructors.ML
   1.596 -in function add_domain_constructors:
   1.597 -
   1.598 -    ( * define and prove theorems for pattern combinators * )
   1.599 -    val (pat_thms : thm list, thy : theory) =
   1.600 -      let
   1.601 -        val bindings = map #1 spec;
   1.602 -        fun prep_arg (lazy, sel, T) = (lazy, T);
   1.603 -        fun prep_con c (b, args, mx) = (c, map prep_arg args);
   1.604 -        val pat_spec = map2 prep_con con_consts spec;
   1.605 -      in
   1.606 -        add_pattern_combinators bindings pat_spec lhsT
   1.607 -          exhaust case_const cases thy
   1.608 -      end
   1.609 -
   1.610 -*)
   1.611 -
   1.612 -end