src/HOLCF/Fixrec.thy
author huffman
Wed Oct 27 14:15:54 2010 -0700 (2010-10-27)
changeset 40322 707eb30e8a53
parent 40046 ba2e41c8b725
child 40327 1dfdbd66093a
permissions -rw-r--r--
make syntax of continuous if-then-else consistent with HOL if-then-else
     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 {* Pattern-match monad *}
    15 
    16 default_sort cpo
    17 
    18 pcpodef (open) 'a match = "UNIV::(one ++ 'a u) set"
    19 by simp_all
    20 
    21 definition
    22   fail :: "'a match" where
    23   "fail = Abs_match (sinl\<cdot>ONE)"
    24 
    25 definition
    26   succeed :: "'a \<rightarrow> 'a match" where
    27   "succeed = (\<Lambda> x. Abs_match (sinr\<cdot>(up\<cdot>x)))"
    28 
    29 definition
    30   match_case :: "'b \<rightarrow> ('a \<rightarrow> 'b) \<rightarrow> 'a match \<rightarrow> 'b::pcpo" where
    31   "match_case = (\<Lambda> f r m. sscase\<cdot>(\<Lambda> x. f)\<cdot>(fup\<cdot>r)\<cdot>(Rep_match m))"
    32 
    33 lemma matchE [case_names bottom fail succeed, cases type: match]:
    34   "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = succeed\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
    35 unfolding fail_def succeed_def
    36 apply (cases p, rename_tac r)
    37 apply (rule_tac p=r in ssumE, simp add: Abs_match_strict)
    38 apply (rule_tac p=x in oneE, simp, simp)
    39 apply (rule_tac p=y in upE, simp, simp add: cont_Abs_match)
    40 done
    41 
    42 lemma succeed_defined [simp]: "succeed\<cdot>x \<noteq> \<bottom>"
    43 by (simp add: succeed_def cont_Abs_match Abs_match_defined)
    44 
    45 lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
    46 by (simp add: fail_def Abs_match_defined)
    47 
    48 lemma succeed_eq [simp]: "(succeed\<cdot>x = succeed\<cdot>y) = (x = y)"
    49 by (simp add: succeed_def cont_Abs_match Abs_match_inject)
    50 
    51 lemma succeed_neq_fail [simp]:
    52   "succeed\<cdot>x \<noteq> fail" "fail \<noteq> succeed\<cdot>x"
    53 by (simp_all add: succeed_def fail_def cont_Abs_match Abs_match_inject)
    54 
    55 lemma match_case_simps [simp]:
    56   "match_case\<cdot>f\<cdot>r\<cdot>\<bottom> = \<bottom>"
    57   "match_case\<cdot>f\<cdot>r\<cdot>fail = f"
    58   "match_case\<cdot>f\<cdot>r\<cdot>(succeed\<cdot>x) = r\<cdot>x"
    59 by (simp_all add: succeed_def fail_def match_case_def cont_Rep_match
    60                   cont2cont_LAM
    61                   cont_Abs_match Abs_match_inverse Rep_match_strict)
    62 
    63 translations
    64   "case m of XCONST fail \<Rightarrow> t1 | XCONST succeed\<cdot>x \<Rightarrow> t2"
    65     == "CONST match_case\<cdot>t1\<cdot>(\<Lambda> x. t2)\<cdot>m"
    66 
    67 subsubsection {* Run operator *}
    68 
    69 definition
    70   run :: "'a match \<rightarrow> 'a::pcpo" where
    71   "run = match_case\<cdot>\<bottom>\<cdot>ID"
    72 
    73 text {* rewrite rules for run *}
    74 
    75 lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
    76 by (simp add: run_def)
    77 
    78 lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
    79 by (simp add: run_def)
    80 
    81 lemma run_succeed [simp]: "run\<cdot>(succeed\<cdot>x) = x"
    82 by (simp add: run_def)
    83 
    84 subsubsection {* Monad plus operator *}
    85 
    86 definition
    87   mplus :: "'a match \<rightarrow> 'a match \<rightarrow> 'a match" where
    88   "mplus = (\<Lambda> m1 m2. case m1 of fail \<Rightarrow> m2 | succeed\<cdot>x \<Rightarrow> m1)"
    89 
    90 abbreviation
    91   mplus_syn :: "['a match, 'a match] \<Rightarrow> 'a match"  (infixr "+++" 65)  where
    92   "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
    93 
    94 text {* rewrite rules for mplus *}
    95 
    96 lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
    97 by (simp add: mplus_def)
    98 
    99 lemma mplus_fail [simp]: "fail +++ m = m"
   100 by (simp add: mplus_def)
   101 
   102 lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
   103 by (simp add: mplus_def)
   104 
   105 lemma mplus_fail2 [simp]: "m +++ fail = m"
   106 by (cases m, simp_all)
   107 
   108 lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
   109 by (cases x, simp_all)
   110 
   111 subsection {* Match functions for built-in types *}
   112 
   113 default_sort pcpo
   114 
   115 definition
   116   match_UU :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
   117 where
   118   "match_UU = (\<Lambda> x k. strict\<cdot>x\<cdot>fail)"
   119 
   120 definition
   121   match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   122 where
   123   "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
   124 
   125 definition
   126   match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   127 where
   128   "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
   129 
   130 definition
   131   match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   132 where
   133   "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
   134 
   135 definition
   136   match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
   137 where
   138   "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
   139 
   140 definition
   141   match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   142 where
   143   "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
   144 
   145 definition
   146   match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
   147 where
   148   "match_ONE = (\<Lambda> ONE k. k)"
   149 
   150 definition
   151   match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   152 where
   153   "match_TT = (\<Lambda> x k. If x then k else fail)"
   154  
   155 definition
   156   match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   157 where
   158   "match_FF = (\<Lambda> x k. If x then fail else k)"
   159 
   160 lemma match_UU_simps [simp]:
   161   "match_UU\<cdot>\<bottom>\<cdot>k = \<bottom>"
   162   "x \<noteq> \<bottom> \<Longrightarrow> match_UU\<cdot>x\<cdot>k = fail"
   163 by (simp_all add: match_UU_def)
   164 
   165 lemma match_Pair_simps [simp]:
   166   "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
   167 by (simp_all add: match_Pair_def)
   168 
   169 lemma match_spair_simps [simp]:
   170   "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
   171   "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
   172 by (simp_all add: match_spair_def)
   173 
   174 lemma match_sinl_simps [simp]:
   175   "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
   176   "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
   177   "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
   178 by (simp_all add: match_sinl_def)
   179 
   180 lemma match_sinr_simps [simp]:
   181   "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
   182   "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
   183   "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
   184 by (simp_all add: match_sinr_def)
   185 
   186 lemma match_up_simps [simp]:
   187   "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
   188   "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
   189 by (simp_all add: match_up_def)
   190 
   191 lemma match_ONE_simps [simp]:
   192   "match_ONE\<cdot>ONE\<cdot>k = k"
   193   "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
   194 by (simp_all add: match_ONE_def)
   195 
   196 lemma match_TT_simps [simp]:
   197   "match_TT\<cdot>TT\<cdot>k = k"
   198   "match_TT\<cdot>FF\<cdot>k = fail"
   199   "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
   200 by (simp_all add: match_TT_def)
   201 
   202 lemma match_FF_simps [simp]:
   203   "match_FF\<cdot>FF\<cdot>k = k"
   204   "match_FF\<cdot>TT\<cdot>k = fail"
   205   "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
   206 by (simp_all add: match_FF_def)
   207 
   208 subsection {* Mutual recursion *}
   209 
   210 text {*
   211   The following rules are used to prove unfolding theorems from
   212   fixed-point definitions of mutually recursive functions.
   213 *}
   214 
   215 lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
   216 by simp
   217 
   218 lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
   219 by simp
   220 
   221 lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
   222 by simp
   223 
   224 lemma def_cont_fix_eq:
   225   "\<lbrakk>f \<equiv> fix\<cdot>(Abs_CFun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
   226 by (simp, subst fix_eq, simp)
   227 
   228 lemma def_cont_fix_ind:
   229   "\<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"
   230 by (simp add: fix_ind)
   231 
   232 text {* lemma for proving rewrite rules *}
   233 
   234 lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
   235 by simp
   236 
   237 
   238 subsection {* Initializing the fixrec package *}
   239 
   240 use "Tools/holcf_library.ML"
   241 use "Tools/fixrec.ML"
   242 
   243 setup {* Fixrec.setup *}
   244 
   245 setup {*
   246   Fixrec.add_matchers
   247     [ (@{const_name up}, @{const_name match_up}),
   248       (@{const_name sinl}, @{const_name match_sinl}),
   249       (@{const_name sinr}, @{const_name match_sinr}),
   250       (@{const_name spair}, @{const_name match_spair}),
   251       (@{const_name Pair}, @{const_name match_Pair}),
   252       (@{const_name ONE}, @{const_name match_ONE}),
   253       (@{const_name TT}, @{const_name match_TT}),
   254       (@{const_name FF}, @{const_name match_FF}),
   255       (@{const_name UU}, @{const_name match_UU}) ]
   256 *}
   257 
   258 hide_const (open) succeed fail run
   259 
   260 end