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