1 (* Title: HOL/Bali/Basis.thy
3 Author: David von Oheimb
6 header {* Definitions extending HOL as logical basis of Bali *}
8 theory Basis imports Main begin
11 Unify.search_bound := 40;
12 Unify.trace_bound := 40;
17 declare same_fstI [intro!] (*### TO HOL/Wellfounded_Relations *)
19 declare split_if_asm [split] option.split [split] option.split_asm [split]
20 declaration {* K (Simplifier.map_ss (fn ss => ss addloop ("split_all_tac", split_all_tac))) *}
21 declare if_weak_cong [cong del] option.weak_case_cong [cong del]
22 declare length_Suc_conv [iff]
24 lemma Collect_split_eq: "{p. P (split f p)} = {(a,b). P (f a b)}"
29 "A <= insert x B ==> A <= B & x ~: A | (EX B'. A = insert x B' & B' <= B)"
30 apply (case_tac "x:A")
32 apply (rule_tac x = "A-{x}" in exI)
44 lemma range_bool_domain: "range f = {f True, f False}"
50 (* irrefl_tranclI in Transitive_Closure.thy is more general *)
51 lemma irrefl_tranclI': "r^-1 Int r^+ = {} ==> !x. (x, x) ~: r^+"
52 by(blast elim: tranclE dest: trancl_into_rtrancl)
55 lemma trancl_rtrancl_trancl:
56 "\<lbrakk>(x,y)\<in>r^+; (y,z)\<in>r^*\<rbrakk> \<Longrightarrow> (x,z)\<in>r^+"
57 by (auto dest: tranclD rtrancl_trans rtrancl_into_trancl2)
59 lemma rtrancl_into_trancl3:
60 "\<lbrakk>(a,b)\<in>r^*; a\<noteq>b\<rbrakk> \<Longrightarrow> (a,b)\<in>r^+"
61 apply (drule rtranclD)
65 lemma rtrancl_into_rtrancl2:
66 "\<lbrakk> (a, b) \<in> r; (b, c) \<in> r^* \<rbrakk> \<Longrightarrow> (a, c) \<in> r^*"
67 by (auto intro: r_into_rtrancl rtrancl_trans)
70 "\<lbrakk> \<And> a b c. \<lbrakk>(a,b)\<in>r; (a,c)\<in>r\<rbrakk> \<Longrightarrow> b=c; (a,x)\<in>r\<^sup>*; (a,y)\<in>r\<^sup>*\<rbrakk>
71 \<Longrightarrow> (x,y)\<in>r\<^sup>* \<or> (y,x)\<in>r\<^sup>*"
73 note converse_rtrancl_induct = converse_rtrancl_induct [consumes 1]
74 note converse_rtranclE = converse_rtranclE [consumes 1]
75 assume unique: "\<And> a b c. \<lbrakk>(a,b)\<in>r; (a,c)\<in>r\<rbrakk> \<Longrightarrow> b=c"
76 assume "(a,x)\<in>r\<^sup>*"
77 then show "(a,y)\<in>r\<^sup>* \<Longrightarrow> (x,y)\<in>r\<^sup>* \<or> (y,x)\<in>r\<^sup>*"
78 proof (induct rule: converse_rtrancl_induct)
79 assume "(x,y)\<in>r\<^sup>*"
84 assume a_v_r: "(a, v) \<in> r" and
85 v_x_rt: "(v, x) \<in> r\<^sup>*" and
86 a_y_rt: "(a, y) \<in> r\<^sup>*" and
87 hyp: "(v, y) \<in> r\<^sup>* \<Longrightarrow> (x, y) \<in> r\<^sup>* \<or> (y, x) \<in> r\<^sup>*"
89 show "(x, y) \<in> r\<^sup>* \<or> (y, x) \<in> r\<^sup>*"
90 proof (cases rule: converse_rtranclE)
92 with a_v_r v_x_rt have "(y,x) \<in> r\<^sup>*"
93 by (auto intro: r_into_rtrancl rtrancl_trans)
98 assume a_w_r: "(a, w) \<in> r" and
99 w_y_rt: "(w, y) \<in> r\<^sup>*"
100 from a_v_r a_w_r unique
111 lemma rtrancl_cases [consumes 1, case_names Refl Trancl]:
112 "\<lbrakk>(a,b)\<in>r\<^sup>*; a = b \<Longrightarrow> P; (a,b)\<in>r\<^sup>+ \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
113 apply (erule rtranclE)
114 apply (auto dest: rtrancl_into_trancl1)
117 (* ### To Transitive_Closure *)
118 theorems converse_rtrancl_induct
119 = converse_rtrancl_induct [consumes 1,case_names Id Step]
121 theorems converse_trancl_induct
122 = converse_trancl_induct [consumes 1,case_names Single Step]
124 (* context (theory "Set") *)
125 lemma Ball_weaken:"\<lbrakk>Ball s P;\<And> x. P x\<longrightarrow>Q x\<rbrakk>\<Longrightarrow>Ball s Q"
128 (* context (theory "Finite") *)
129 lemma finite_SetCompr2: "[| finite (Collect P); !y. P y --> finite (range (f y)) |] ==>
130 finite {f y x |x y. P y}"
131 apply (subgoal_tac "{f y x |x y. P y} = UNION (Collect P) (%y. range (f y))")
134 apply (erule finite_UN_I)
139 (* ### TO theory "List" *)
140 lemma list_all2_trans: "\<forall> a b c. P1 a b \<longrightarrow> P2 b c \<longrightarrow> P3 a c \<Longrightarrow>
141 \<forall>xs2 xs3. list_all2 P1 xs1 xs2 \<longrightarrow> list_all2 P2 xs2 xs3 \<longrightarrow> list_all2 P3 xs1 xs3"
142 apply (induct_tac "xs1")
145 apply (induct_tac "xs2")
148 apply (induct_tac "xs3")
155 lemma surjective_pairing5: "p = (fst p, fst (snd p), fst (snd (snd p)), fst (snd (snd (snd p))),
156 snd (snd (snd (snd p))))"
160 lemma fst_splitE [elim!]:
161 "[| fst s' = x'; !!x s. [| s' = (x,s); x = x' |] ==> Q |] ==> Q"
162 apply (cut_tac p = "s'" in surjective_pairing)
166 lemma fst_in_set_lemma [rule_format (no_asm)]: "(x, y) : set l --> x : fst ` set l"
167 apply (induct_tac "l")
172 section "quantifiers"
174 lemma All_Ex_refl_eq2 [simp]:
175 "(!x. (? b. x = f b & Q b) \<longrightarrow> P x) = (!b. Q b --> P (f b))"
179 lemma ex_ex_miniscope1 [simp]:
180 "(EX w v. P w v & Q v) = (EX v. (EX w. P w v) & Q v)"
184 lemma ex_miniscope2 [simp]:
185 "(EX v. P v & Q & R v) = (Q & (EX v. P v & R v))"
189 lemma ex_reorder31: "(\<exists>z x y. P x y z) = (\<exists>x y z. P x y z)"
193 lemma All_Ex_refl_eq1 [simp]: "(!x. (? b. x = f b) --> P x) = (!b. P (f b))"
203 fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80)
205 "fun_sum" == "CONST sum_case"
207 consts the_Inl :: "'a + 'b \<Rightarrow> 'a"
208 the_Inr :: "'a + 'b \<Rightarrow> 'b"
209 primrec "the_Inl (Inl a) = a"
210 primrec "the_Inr (Inr b) = b"
212 datatype ('a, 'b, 'c) sum3 = In1 'a | In2 'b | In3 'c
214 consts the_In1 :: "('a, 'b, 'c) sum3 \<Rightarrow> 'a"
215 the_In2 :: "('a, 'b, 'c) sum3 \<Rightarrow> 'b"
216 the_In3 :: "('a, 'b, 'c) sum3 \<Rightarrow> 'c"
217 primrec "the_In1 (In1 a) = a"
218 primrec "the_In2 (In2 b) = b"
219 primrec "the_In3 (In3 c) = c"
222 In1l :: "'al \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
223 In1r :: "'ar \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
225 "In1l e" == "In1 (Inl e)"
226 "In1r c" == "In1 (Inr c)"
228 syntax the_In1l :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'al"
229 the_In1r :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'ar"
231 "the_In1l" == "the_Inl \<circ> the_In1"
232 "the_In1r" == "the_Inr \<circ> the_In1"
235 fun sum3_instantiate thm = map (fn s => simplify(simpset()delsimps[@{thm not_None_eq}])
236 (read_instantiate [("t","In"^s^" ?x")] thm)) ["1l","2","3","1r"]
238 (* e.g. lemmas is_stmt_rews = is_stmt_def [of "In1l x", simplified] *)
241 "option"<= (type) "Option.option"
242 "list" <= (type) "List.list"
243 "sum3" <= (type) "Basis.sum3"
246 section "quantifiers for option type"
249 Oall :: "[pttrn, 'a option, bool] => bool" ("(3! _:_:/ _)" [0,0,10] 10)
250 Oex :: "[pttrn, 'a option, bool] => bool" ("(3? _:_:/ _)" [0,0,10] 10)
253 Oall :: "[pttrn, 'a option, bool] => bool" ("(3\<forall>_\<in>_:/ _)" [0,0,10] 10)
254 Oex :: "[pttrn, 'a option, bool] => bool" ("(3\<exists>_\<in>_:/ _)" [0,0,10] 10)
257 "! x:A: P" == "! x:o2s A. P"
258 "? x:A: P" == "? x:o2s A. P"
260 section "Special map update"
262 text{* Deemed too special for theory Map. *}
265 chg_map :: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)"
266 "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
268 lemma chg_map_new[simp]: "m a = None ==> chg_map f a m = m"
269 by (unfold chg_map_def, auto)
271 lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
272 by (unfold chg_map_def, auto)
274 lemma chg_map_other [simp]: "a \<noteq> b \<Longrightarrow> chg_map f a m b = m b"
275 by (auto simp: chg_map_def split add: option.split)
278 section "unique association lists"
281 unique :: "('a \<times> 'b) list \<Rightarrow> bool"
282 "unique \<equiv> distinct \<circ> map fst"
284 lemma uniqueD [rule_format (no_asm)]:
285 "unique l--> (!x y. (x,y):set l --> (!x' y'. (x',y'):set l --> x=x'--> y=y'))"
286 apply (unfold unique_def o_def)
287 apply (induct_tac "l")
288 apply (auto dest: fst_in_set_lemma)
291 lemma unique_Nil [simp]: "unique []"
292 apply (unfold unique_def)
293 apply (simp (no_asm))
296 lemma unique_Cons [simp]: "unique ((x,y)#l) = (unique l & (!y. (x,y) ~: set l))"
297 apply (unfold unique_def)
298 apply (auto dest: fst_in_set_lemma)
301 lemmas unique_ConsI = conjI [THEN unique_Cons [THEN iffD2], standard]
303 lemma unique_single [simp]: "!!p. unique [p]"
307 lemma unique_ConsD: "unique (x#xs) ==> unique xs"
308 apply (simp add: unique_def)
311 lemma unique_append [rule_format (no_asm)]: "unique l' ==> unique l -->
312 (!(x,y):set l. !(x',y'):set l'. x' ~= x) --> unique (l @ l')"
313 apply (induct_tac "l")
314 apply (auto dest: fst_in_set_lemma)
317 lemma unique_map_inj [rule_format (no_asm)]: "unique l --> inj f --> unique (map (%(k,x). (f k, g k x)) l)"
318 apply (induct_tac "l")
319 apply (auto dest: fst_in_set_lemma simp add: inj_eq)
322 lemma map_of_SomeI [rule_format (no_asm)]: "unique l --> (k, x) : set l --> map_of l k = Some x"
323 apply (induct_tac "l")
328 section "list patterns"
331 lsplit :: "[['a, 'a list] => 'b, 'a list] => 'b"
333 lsplit_def: "lsplit == %f l. f (hd l) (tl l)"
334 (* list patterns -- extends pre-defined type "pttrn" used in abstractions *)
336 "_lpttrn" :: "[pttrn,pttrn] => pttrn" ("_#/_" [901,900] 900)
338 "%y#x#xs. b" == "lsplit (%y x#xs. b)"
339 "%x#xs . b" == "lsplit (%x xs . b)"
341 lemma lsplit [simp]: "lsplit c (x#xs) = c x xs"
342 apply (unfold lsplit_def)
343 apply (simp (no_asm))
346 lemma lsplit2 [simp]: "lsplit P (x#xs) y z = P x xs y z"
347 apply (unfold lsplit_def)