src/HOL/HOLCF/Fixrec.thy
changeset 40774 0437dbc127b3
parent 40768 50a80cf4b7ef
child 40795 c52cd8bc426d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/HOLCF/Fixrec.thy	Sat Nov 27 16:08:10 2010 -0800
     1.3 @@ -0,0 +1,252 @@
     1.4 +(*  Title:      HOLCF/Fixrec.thy
     1.5 +    Author:     Amber Telfer and Brian Huffman
     1.6 +*)
     1.7 +
     1.8 +header "Package for defining recursive functions in HOLCF"
     1.9 +
    1.10 +theory Fixrec
    1.11 +imports Plain_HOLCF
    1.12 +uses
    1.13 +  ("Tools/holcf_library.ML")
    1.14 +  ("Tools/fixrec.ML")
    1.15 +begin
    1.16 +
    1.17 +subsection {* Pattern-match monad *}
    1.18 +
    1.19 +default_sort cpo
    1.20 +
    1.21 +pcpodef (open) 'a match = "UNIV::(one ++ 'a u) set"
    1.22 +by simp_all
    1.23 +
    1.24 +definition
    1.25 +  fail :: "'a match" where
    1.26 +  "fail = Abs_match (sinl\<cdot>ONE)"
    1.27 +
    1.28 +definition
    1.29 +  succeed :: "'a \<rightarrow> 'a match" where
    1.30 +  "succeed = (\<Lambda> x. Abs_match (sinr\<cdot>(up\<cdot>x)))"
    1.31 +
    1.32 +lemma matchE [case_names bottom fail succeed, cases type: match]:
    1.33 +  "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = succeed\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
    1.34 +unfolding fail_def succeed_def
    1.35 +apply (cases p, rename_tac r)
    1.36 +apply (rule_tac p=r in ssumE, simp add: Abs_match_strict)
    1.37 +apply (rule_tac p=x in oneE, simp, simp)
    1.38 +apply (rule_tac p=y in upE, simp, simp add: cont_Abs_match)
    1.39 +done
    1.40 +
    1.41 +lemma succeed_defined [simp]: "succeed\<cdot>x \<noteq> \<bottom>"
    1.42 +by (simp add: succeed_def cont_Abs_match Abs_match_defined)
    1.43 +
    1.44 +lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
    1.45 +by (simp add: fail_def Abs_match_defined)
    1.46 +
    1.47 +lemma succeed_eq [simp]: "(succeed\<cdot>x = succeed\<cdot>y) = (x = y)"
    1.48 +by (simp add: succeed_def cont_Abs_match Abs_match_inject)
    1.49 +
    1.50 +lemma succeed_neq_fail [simp]:
    1.51 +  "succeed\<cdot>x \<noteq> fail" "fail \<noteq> succeed\<cdot>x"
    1.52 +by (simp_all add: succeed_def fail_def cont_Abs_match Abs_match_inject)
    1.53 +
    1.54 +subsubsection {* Run operator *}
    1.55 +
    1.56 +definition
    1.57 +  run :: "'a match \<rightarrow> 'a::pcpo" where
    1.58 +  "run = (\<Lambda> m. sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)\<cdot>(Rep_match m))"
    1.59 +
    1.60 +text {* rewrite rules for run *}
    1.61 +
    1.62 +lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
    1.63 +unfolding run_def
    1.64 +by (simp add: cont_Rep_match Rep_match_strict)
    1.65 +
    1.66 +lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
    1.67 +unfolding run_def fail_def
    1.68 +by (simp add: cont_Rep_match Abs_match_inverse)
    1.69 +
    1.70 +lemma run_succeed [simp]: "run\<cdot>(succeed\<cdot>x) = x"
    1.71 +unfolding run_def succeed_def
    1.72 +by (simp add: cont_Rep_match cont_Abs_match Abs_match_inverse)
    1.73 +
    1.74 +subsubsection {* Monad plus operator *}
    1.75 +
    1.76 +definition
    1.77 +  mplus :: "'a match \<rightarrow> 'a match \<rightarrow> 'a match" where
    1.78 +  "mplus = (\<Lambda> m1 m2. sscase\<cdot>(\<Lambda> _. m2)\<cdot>(\<Lambda> _. m1)\<cdot>(Rep_match m1))"
    1.79 +
    1.80 +abbreviation
    1.81 +  mplus_syn :: "['a match, 'a match] \<Rightarrow> 'a match"  (infixr "+++" 65)  where
    1.82 +  "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
    1.83 +
    1.84 +text {* rewrite rules for mplus *}
    1.85 +
    1.86 +lemmas cont2cont_Rep_match = cont_Rep_match [THEN cont_compose]
    1.87 +
    1.88 +lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
    1.89 +unfolding mplus_def
    1.90 +by (simp add: cont2cont_Rep_match Rep_match_strict)
    1.91 +
    1.92 +lemma mplus_fail [simp]: "fail +++ m = m"
    1.93 +unfolding mplus_def fail_def
    1.94 +by (simp add: cont2cont_Rep_match Abs_match_inverse)
    1.95 +
    1.96 +lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
    1.97 +unfolding mplus_def succeed_def
    1.98 +by (simp add: cont2cont_Rep_match cont_Abs_match Abs_match_inverse)
    1.99 +
   1.100 +lemma mplus_fail2 [simp]: "m +++ fail = m"
   1.101 +by (cases m, simp_all)
   1.102 +
   1.103 +lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
   1.104 +by (cases x, simp_all)
   1.105 +
   1.106 +subsection {* Match functions for built-in types *}
   1.107 +
   1.108 +default_sort pcpo
   1.109 +
   1.110 +definition
   1.111 +  match_bottom :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
   1.112 +where
   1.113 +  "match_bottom = (\<Lambda> x k. seq\<cdot>x\<cdot>fail)"
   1.114 +
   1.115 +definition
   1.116 +  match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   1.117 +where
   1.118 +  "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
   1.119 +
   1.120 +definition
   1.121 +  match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
   1.122 +where
   1.123 +  "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
   1.124 +
   1.125 +definition
   1.126 +  match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   1.127 +where
   1.128 +  "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
   1.129 +
   1.130 +definition
   1.131 +  match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
   1.132 +where
   1.133 +  "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
   1.134 +
   1.135 +definition
   1.136 +  match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
   1.137 +where
   1.138 +  "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
   1.139 +
   1.140 +definition
   1.141 +  match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
   1.142 +where
   1.143 +  "match_ONE = (\<Lambda> ONE k. k)"
   1.144 +
   1.145 +definition
   1.146 +  match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   1.147 +where
   1.148 +  "match_TT = (\<Lambda> x k. If x then k else fail)"
   1.149 + 
   1.150 +definition
   1.151 +  match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
   1.152 +where
   1.153 +  "match_FF = (\<Lambda> x k. If x then fail else k)"
   1.154 +
   1.155 +lemma match_bottom_simps [simp]:
   1.156 +  "match_bottom\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.157 +  "x \<noteq> \<bottom> \<Longrightarrow> match_bottom\<cdot>x\<cdot>k = fail"
   1.158 +by (simp_all add: match_bottom_def)
   1.159 +
   1.160 +lemma match_Pair_simps [simp]:
   1.161 +  "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
   1.162 +by (simp_all add: match_Pair_def)
   1.163 +
   1.164 +lemma match_spair_simps [simp]:
   1.165 +  "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
   1.166 +  "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.167 +by (simp_all add: match_spair_def)
   1.168 +
   1.169 +lemma match_sinl_simps [simp]:
   1.170 +  "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
   1.171 +  "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
   1.172 +  "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.173 +by (simp_all add: match_sinl_def)
   1.174 +
   1.175 +lemma match_sinr_simps [simp]:
   1.176 +  "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
   1.177 +  "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
   1.178 +  "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.179 +by (simp_all add: match_sinr_def)
   1.180 +
   1.181 +lemma match_up_simps [simp]:
   1.182 +  "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
   1.183 +  "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.184 +by (simp_all add: match_up_def)
   1.185 +
   1.186 +lemma match_ONE_simps [simp]:
   1.187 +  "match_ONE\<cdot>ONE\<cdot>k = k"
   1.188 +  "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.189 +by (simp_all add: match_ONE_def)
   1.190 +
   1.191 +lemma match_TT_simps [simp]:
   1.192 +  "match_TT\<cdot>TT\<cdot>k = k"
   1.193 +  "match_TT\<cdot>FF\<cdot>k = fail"
   1.194 +  "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.195 +by (simp_all add: match_TT_def)
   1.196 +
   1.197 +lemma match_FF_simps [simp]:
   1.198 +  "match_FF\<cdot>FF\<cdot>k = k"
   1.199 +  "match_FF\<cdot>TT\<cdot>k = fail"
   1.200 +  "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
   1.201 +by (simp_all add: match_FF_def)
   1.202 +
   1.203 +subsection {* Mutual recursion *}
   1.204 +
   1.205 +text {*
   1.206 +  The following rules are used to prove unfolding theorems from
   1.207 +  fixed-point definitions of mutually recursive functions.
   1.208 +*}
   1.209 +
   1.210 +lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
   1.211 +by simp
   1.212 +
   1.213 +lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
   1.214 +by simp
   1.215 +
   1.216 +lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
   1.217 +by simp
   1.218 +
   1.219 +lemma def_cont_fix_eq:
   1.220 +  "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
   1.221 +by (simp, subst fix_eq, simp)
   1.222 +
   1.223 +lemma def_cont_fix_ind:
   1.224 +  "\<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"
   1.225 +by (simp add: fix_ind)
   1.226 +
   1.227 +text {* lemma for proving rewrite rules *}
   1.228 +
   1.229 +lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
   1.230 +by simp
   1.231 +
   1.232 +
   1.233 +subsection {* Initializing the fixrec package *}
   1.234 +
   1.235 +use "Tools/holcf_library.ML"
   1.236 +use "Tools/fixrec.ML"
   1.237 +
   1.238 +setup {* Fixrec.setup *}
   1.239 +
   1.240 +setup {*
   1.241 +  Fixrec.add_matchers
   1.242 +    [ (@{const_name up}, @{const_name match_up}),
   1.243 +      (@{const_name sinl}, @{const_name match_sinl}),
   1.244 +      (@{const_name sinr}, @{const_name match_sinr}),
   1.245 +      (@{const_name spair}, @{const_name match_spair}),
   1.246 +      (@{const_name Pair}, @{const_name match_Pair}),
   1.247 +      (@{const_name ONE}, @{const_name match_ONE}),
   1.248 +      (@{const_name TT}, @{const_name match_TT}),
   1.249 +      (@{const_name FF}, @{const_name match_FF}),
   1.250 +      (@{const_name UU}, @{const_name match_bottom}) ]
   1.251 +*}
   1.252 +
   1.253 +hide_const (open) succeed fail run
   1.254 +
   1.255 +end