src/HOL/HOLCF/Fixrec.thy
 author huffman Tue Oct 09 17:33:46 2012 +0200 (2012-10-09) changeset 49759 ecf1d5d87e5e parent 48891 c0eafbd55de3 child 58880 0baae4311a9f permissions -rw-r--r--
removed support for set constant definitions in HOLCF {cpo,pcpo,domain}def commands;
removed '(open)', '(set_name)' and '(open set_name)' options
1 (*  Title:      HOL/HOLCF/Fixrec.thy
2     Author:     Amber Telfer and Brian Huffman
3 *)
5 header "Package for defining recursive functions in HOLCF"
7 theory Fixrec
8 imports Plain_HOLCF
9 keywords "fixrec" :: thy_decl
10 begin
12 subsection {* Pattern-match monad *}
14 default_sort cpo
16 pcpodef 'a match = "UNIV::(one ++ 'a u) set"
17 by simp_all
19 definition
20   fail :: "'a match" where
21   "fail = Abs_match (sinl\<cdot>ONE)"
23 definition
24   succeed :: "'a \<rightarrow> 'a match" where
25   "succeed = (\<Lambda> x. Abs_match (sinr\<cdot>(up\<cdot>x)))"
27 lemma matchE [case_names bottom fail succeed, cases type: match]:
28   "\<lbrakk>p = \<bottom> \<Longrightarrow> Q; p = fail \<Longrightarrow> Q; \<And>x. p = succeed\<cdot>x \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
29 unfolding fail_def succeed_def
30 apply (cases p, rename_tac r)
31 apply (rule_tac p=r in ssumE, simp add: Abs_match_strict)
32 apply (rule_tac p=x in oneE, simp, simp)
33 apply (rule_tac p=y in upE, simp, simp add: cont_Abs_match)
34 done
36 lemma succeed_defined [simp]: "succeed\<cdot>x \<noteq> \<bottom>"
37 by (simp add: succeed_def cont_Abs_match Abs_match_bottom_iff)
39 lemma fail_defined [simp]: "fail \<noteq> \<bottom>"
40 by (simp add: fail_def Abs_match_bottom_iff)
42 lemma succeed_eq [simp]: "(succeed\<cdot>x = succeed\<cdot>y) = (x = y)"
43 by (simp add: succeed_def cont_Abs_match Abs_match_inject)
45 lemma succeed_neq_fail [simp]:
46   "succeed\<cdot>x \<noteq> fail" "fail \<noteq> succeed\<cdot>x"
47 by (simp_all add: succeed_def fail_def cont_Abs_match Abs_match_inject)
49 subsubsection {* Run operator *}
51 definition
52   run :: "'a match \<rightarrow> 'a::pcpo" where
53   "run = (\<Lambda> m. sscase\<cdot>\<bottom>\<cdot>(fup\<cdot>ID)\<cdot>(Rep_match m))"
55 text {* rewrite rules for run *}
57 lemma run_strict [simp]: "run\<cdot>\<bottom> = \<bottom>"
58 unfolding run_def
59 by (simp add: cont_Rep_match Rep_match_strict)
61 lemma run_fail [simp]: "run\<cdot>fail = \<bottom>"
62 unfolding run_def fail_def
63 by (simp add: cont_Rep_match Abs_match_inverse)
65 lemma run_succeed [simp]: "run\<cdot>(succeed\<cdot>x) = x"
66 unfolding run_def succeed_def
67 by (simp add: cont_Rep_match cont_Abs_match Abs_match_inverse)
69 subsubsection {* Monad plus operator *}
71 definition
72   mplus :: "'a match \<rightarrow> 'a match \<rightarrow> 'a match" where
73   "mplus = (\<Lambda> m1 m2. sscase\<cdot>(\<Lambda> _. m2)\<cdot>(\<Lambda> _. m1)\<cdot>(Rep_match m1))"
75 abbreviation
76   mplus_syn :: "['a match, 'a match] \<Rightarrow> 'a match"  (infixr "+++" 65)  where
77   "m1 +++ m2 == mplus\<cdot>m1\<cdot>m2"
79 text {* rewrite rules for mplus *}
81 lemma mplus_strict [simp]: "\<bottom> +++ m = \<bottom>"
82 unfolding mplus_def
83 by (simp add: cont_Rep_match Rep_match_strict)
85 lemma mplus_fail [simp]: "fail +++ m = m"
86 unfolding mplus_def fail_def
87 by (simp add: cont_Rep_match Abs_match_inverse)
89 lemma mplus_succeed [simp]: "succeed\<cdot>x +++ m = succeed\<cdot>x"
90 unfolding mplus_def succeed_def
91 by (simp add: cont_Rep_match cont_Abs_match Abs_match_inverse)
93 lemma mplus_fail2 [simp]: "m +++ fail = m"
94 by (cases m, simp_all)
96 lemma mplus_assoc: "(x +++ y) +++ z = x +++ (y +++ z)"
97 by (cases x, simp_all)
99 subsection {* Match functions for built-in types *}
101 default_sort pcpo
103 definition
104   match_bottom :: "'a \<rightarrow> 'c match \<rightarrow> 'c match"
105 where
106   "match_bottom = (\<Lambda> x k. seq\<cdot>x\<cdot>fail)"
108 definition
109   match_Pair :: "'a::cpo \<times> 'b::cpo \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
110 where
111   "match_Pair = (\<Lambda> x k. csplit\<cdot>k\<cdot>x)"
113 definition
114   match_spair :: "'a \<otimes> 'b \<rightarrow> ('a \<rightarrow> 'b \<rightarrow> 'c match) \<rightarrow> 'c match"
115 where
116   "match_spair = (\<Lambda> x k. ssplit\<cdot>k\<cdot>x)"
118 definition
119   match_sinl :: "'a \<oplus> 'b \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
120 where
121   "match_sinl = (\<Lambda> x k. sscase\<cdot>k\<cdot>(\<Lambda> b. fail)\<cdot>x)"
123 definition
124   match_sinr :: "'a \<oplus> 'b \<rightarrow> ('b \<rightarrow> 'c match) \<rightarrow> 'c match"
125 where
126   "match_sinr = (\<Lambda> x k. sscase\<cdot>(\<Lambda> a. fail)\<cdot>k\<cdot>x)"
128 definition
129   match_up :: "'a::cpo u \<rightarrow> ('a \<rightarrow> 'c match) \<rightarrow> 'c match"
130 where
131   "match_up = (\<Lambda> x k. fup\<cdot>k\<cdot>x)"
133 definition
134   match_ONE :: "one \<rightarrow> 'c match \<rightarrow> 'c match"
135 where
136   "match_ONE = (\<Lambda> ONE k. k)"
138 definition
139   match_TT :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
140 where
141   "match_TT = (\<Lambda> x k. If x then k else fail)"
143 definition
144   match_FF :: "tr \<rightarrow> 'c match \<rightarrow> 'c match"
145 where
146   "match_FF = (\<Lambda> x k. If x then fail else k)"
148 lemma match_bottom_simps [simp]:
149   "match_bottom\<cdot>x\<cdot>k = (if x = \<bottom> then \<bottom> else fail)"
150 by (simp add: match_bottom_def)
152 lemma match_Pair_simps [simp]:
153   "match_Pair\<cdot>(x, y)\<cdot>k = k\<cdot>x\<cdot>y"
154 by (simp_all add: match_Pair_def)
156 lemma match_spair_simps [simp]:
157   "\<lbrakk>x \<noteq> \<bottom>; y \<noteq> \<bottom>\<rbrakk> \<Longrightarrow> match_spair\<cdot>(:x, y:)\<cdot>k = k\<cdot>x\<cdot>y"
158   "match_spair\<cdot>\<bottom>\<cdot>k = \<bottom>"
159 by (simp_all add: match_spair_def)
161 lemma match_sinl_simps [simp]:
162   "x \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinl\<cdot>x)\<cdot>k = k\<cdot>x"
163   "y \<noteq> \<bottom> \<Longrightarrow> match_sinl\<cdot>(sinr\<cdot>y)\<cdot>k = fail"
164   "match_sinl\<cdot>\<bottom>\<cdot>k = \<bottom>"
165 by (simp_all add: match_sinl_def)
167 lemma match_sinr_simps [simp]:
168   "x \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinl\<cdot>x)\<cdot>k = fail"
169   "y \<noteq> \<bottom> \<Longrightarrow> match_sinr\<cdot>(sinr\<cdot>y)\<cdot>k = k\<cdot>y"
170   "match_sinr\<cdot>\<bottom>\<cdot>k = \<bottom>"
171 by (simp_all add: match_sinr_def)
173 lemma match_up_simps [simp]:
174   "match_up\<cdot>(up\<cdot>x)\<cdot>k = k\<cdot>x"
175   "match_up\<cdot>\<bottom>\<cdot>k = \<bottom>"
176 by (simp_all add: match_up_def)
178 lemma match_ONE_simps [simp]:
179   "match_ONE\<cdot>ONE\<cdot>k = k"
180   "match_ONE\<cdot>\<bottom>\<cdot>k = \<bottom>"
181 by (simp_all add: match_ONE_def)
183 lemma match_TT_simps [simp]:
184   "match_TT\<cdot>TT\<cdot>k = k"
185   "match_TT\<cdot>FF\<cdot>k = fail"
186   "match_TT\<cdot>\<bottom>\<cdot>k = \<bottom>"
187 by (simp_all add: match_TT_def)
189 lemma match_FF_simps [simp]:
190   "match_FF\<cdot>FF\<cdot>k = k"
191   "match_FF\<cdot>TT\<cdot>k = fail"
192   "match_FF\<cdot>\<bottom>\<cdot>k = \<bottom>"
193 by (simp_all add: match_FF_def)
195 subsection {* Mutual recursion *}
197 text {*
198   The following rules are used to prove unfolding theorems from
199   fixed-point definitions of mutually recursive functions.
200 *}
202 lemma Pair_equalI: "\<lbrakk>x \<equiv> fst p; y \<equiv> snd p\<rbrakk> \<Longrightarrow> (x, y) \<equiv> p"
203 by simp
205 lemma Pair_eqD1: "(x, y) = (x', y') \<Longrightarrow> x = x'"
206 by simp
208 lemma Pair_eqD2: "(x, y) = (x', y') \<Longrightarrow> y = y'"
209 by simp
211 lemma def_cont_fix_eq:
212   "\<lbrakk>f \<equiv> fix\<cdot>(Abs_cfun F); cont F\<rbrakk> \<Longrightarrow> f = F f"
213 by (simp, subst fix_eq, simp)
215 lemma def_cont_fix_ind:
216   "\<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"
217 by (simp add: fix_ind)
219 text {* lemma for proving rewrite rules *}
221 lemma ssubst_lhs: "\<lbrakk>t = s; P s = Q\<rbrakk> \<Longrightarrow> P t = Q"
222 by simp
225 subsection {* Initializing the fixrec package *}
227 ML_file "Tools/holcf_library.ML"
228 ML_file "Tools/fixrec.ML"
230 method_setup fixrec_simp = {*
231   Scan.succeed (SIMPLE_METHOD' o Fixrec.fixrec_simp_tac)
232 *} "pattern prover for fixrec constants"
234 setup {*