src/HOL/Bali/Basis.thy
author wenzelm
Sat Jul 28 20:40:22 2007 +0200 (2007-07-28)
changeset 24019 67bde7cfcf10
parent 22781 18fbba942a80
child 24038 18182c4aec9e
permissions -rw-r--r--
tuned ML/simproc declarations;
     1 (*  Title:      HOL/Bali/Basis.thy
     2     ID:         $Id$
     3     Author:     David von Oheimb
     4 
     5 *)
     6 header {* Definitions extending HOL as logical basis of Bali *}
     7 
     8 theory Basis imports Main begin
     9 
    10 ML {*
    11 Unify.search_bound := 40;
    12 Unify.trace_bound  := 40;
    13 *}
    14 
    15 section "misc"
    16 
    17 declare same_fstI [intro!] (*### TO HOL/Wellfounded_Relations *)
    18 
    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]
    23 
    24 (*###to be phased out *)
    25 ML {*
    26 bind_thm ("make_imp", rearrange_prems [1,0] mp)
    27 *}
    28 
    29 lemma Collect_split_eq: "{p. P (split f p)} = {(a,b). P (f a b)}"
    30 apply auto
    31 done
    32 
    33 lemma subset_insertD: 
    34   "A <= insert x B ==> A <= B & x ~: A | (EX B'. A = insert x B' & B' <= B)"
    35 apply (case_tac "x:A")
    36 apply (rule disjI2)
    37 apply (rule_tac x = "A-{x}" in exI)
    38 apply fast+
    39 done
    40 
    41 syntax
    42   "3" :: nat   ("3") 
    43   "4" :: nat   ("4")
    44 translations
    45  "3" == "Suc 2"
    46  "4" == "Suc 3"
    47 
    48 (*unused*)
    49 lemma range_bool_domain: "range f = {f True, f False}"
    50 apply auto
    51 apply (case_tac "xa")
    52 apply auto
    53 done
    54 
    55 (* irrefl_tranclI in Transitive_Closure.thy is more general *)
    56 lemma irrefl_tranclI': "r^-1 Int r^+ = {} ==> !x. (x, x) ~: r^+"
    57 by(blast elim: tranclE dest: trancl_into_rtrancl)
    58 
    59 
    60 lemma trancl_rtrancl_trancl:
    61 "\<lbrakk>(x,y)\<in>r^+; (y,z)\<in>r^*\<rbrakk> \<Longrightarrow> (x,z)\<in>r^+"
    62 by (auto dest: tranclD rtrancl_trans rtrancl_into_trancl2)
    63 
    64 lemma rtrancl_into_trancl3:
    65 "\<lbrakk>(a,b)\<in>r^*; a\<noteq>b\<rbrakk> \<Longrightarrow> (a,b)\<in>r^+" 
    66 apply (drule rtranclD)
    67 apply auto
    68 done
    69 
    70 lemma rtrancl_into_rtrancl2: 
    71   "\<lbrakk> (a, b) \<in>  r; (b, c) \<in> r^* \<rbrakk> \<Longrightarrow> (a, c) \<in>  r^*"
    72 by (auto intro: r_into_rtrancl rtrancl_trans)
    73 
    74 lemma triangle_lemma:
    75  "\<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> 
    76  \<Longrightarrow> (x,y)\<in>r\<^sup>* \<or> (y,x)\<in>r\<^sup>*"
    77 proof -
    78   note converse_rtrancl_induct = converse_rtrancl_induct [consumes 1]
    79   note converse_rtranclE = converse_rtranclE [consumes 1] 
    80   assume unique: "\<And> a b c. \<lbrakk>(a,b)\<in>r; (a,c)\<in>r\<rbrakk> \<Longrightarrow> b=c"
    81   assume "(a,x)\<in>r\<^sup>*" 
    82   then show "(a,y)\<in>r\<^sup>* \<Longrightarrow> (x,y)\<in>r\<^sup>* \<or> (y,x)\<in>r\<^sup>*"
    83   proof (induct rule: converse_rtrancl_induct)
    84     assume "(x,y)\<in>r\<^sup>*"
    85     then show ?thesis 
    86       by blast
    87   next
    88     fix a v
    89     assume a_v_r: "(a, v) \<in> r" and
    90           v_x_rt: "(v, x) \<in> r\<^sup>*" and
    91           a_y_rt: "(a, y) \<in> r\<^sup>*"  and
    92              hyp: "(v, y) \<in> r\<^sup>* \<Longrightarrow> (x, y) \<in> r\<^sup>* \<or> (y, x) \<in> r\<^sup>*"
    93     from a_y_rt 
    94     show "(x, y) \<in> r\<^sup>* \<or> (y, x) \<in> r\<^sup>*"
    95     proof (cases rule: converse_rtranclE)
    96       assume "a=y"
    97       with a_v_r v_x_rt have "(y,x) \<in> r\<^sup>*"
    98 	by (auto intro: r_into_rtrancl rtrancl_trans)
    99       then show ?thesis 
   100 	by blast
   101     next
   102       fix w 
   103       assume a_w_r: "(a, w) \<in> r" and
   104             w_y_rt: "(w, y) \<in> r\<^sup>*"
   105       from a_v_r a_w_r unique 
   106       have "v=w" 
   107 	by auto
   108       with w_y_rt hyp 
   109       show ?thesis
   110 	by blast
   111     qed
   112   qed
   113 qed
   114 
   115 
   116 lemma rtrancl_cases [consumes 1, case_names Refl Trancl]:
   117  "\<lbrakk>(a,b)\<in>r\<^sup>*;  a = b \<Longrightarrow> P; (a,b)\<in>r\<^sup>+ \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P"
   118 apply (erule rtranclE)
   119 apply (auto dest: rtrancl_into_trancl1)
   120 done
   121 
   122 (* ### To Transitive_Closure *)
   123 theorems converse_rtrancl_induct 
   124  = converse_rtrancl_induct [consumes 1,case_names Id Step]
   125 
   126 theorems converse_trancl_induct 
   127          = converse_trancl_induct [consumes 1,case_names Single Step]
   128 
   129 (* context (theory "Set") *)
   130 lemma Ball_weaken:"\<lbrakk>Ball s P;\<And> x. P x\<longrightarrow>Q x\<rbrakk>\<Longrightarrow>Ball s Q"
   131 by auto
   132 
   133 (* context (theory "Finite") *)
   134 lemma finite_SetCompr2: "[| finite (Collect P); !y. P y --> finite (range (f y)) |] ==>  
   135   finite {f y x |x y. P y}"
   136 apply (subgoal_tac "{f y x |x y. P y} = UNION (Collect P) (%y. range (f y))")
   137 prefer 2 apply  fast
   138 apply (erule ssubst)
   139 apply (erule finite_UN_I)
   140 apply fast
   141 done
   142 
   143 
   144 (* ### TO theory "List" *)
   145 lemma list_all2_trans: "\<forall> a b c. P1 a b \<longrightarrow> P2 b c \<longrightarrow> P3 a c \<Longrightarrow>
   146  \<forall>xs2 xs3. list_all2 P1 xs1 xs2 \<longrightarrow> list_all2 P2 xs2 xs3 \<longrightarrow> list_all2 P3 xs1 xs3"
   147 apply (induct_tac "xs1")
   148 apply simp
   149 apply (rule allI)
   150 apply (induct_tac "xs2")
   151 apply simp
   152 apply (rule allI)
   153 apply (induct_tac "xs3")
   154 apply auto
   155 done
   156 
   157 
   158 section "pairs"
   159 
   160 lemma surjective_pairing5: "p = (fst p, fst (snd p), fst (snd (snd p)), fst (snd (snd (snd p))), 
   161   snd (snd (snd (snd p))))"
   162 apply auto
   163 done
   164 
   165 lemma fst_splitE [elim!]: 
   166 "[| fst s' = x';  !!x s. [| s' = (x,s);  x = x' |] ==> Q |] ==> Q"
   167 apply (cut_tac p = "s'" in surjective_pairing)
   168 apply auto
   169 done
   170 
   171 lemma fst_in_set_lemma [rule_format (no_asm)]: "(x, y) : set l --> x : fst ` set l"
   172 apply (induct_tac "l")
   173 apply  auto
   174 done
   175 
   176 
   177 section "quantifiers"
   178 
   179 lemma All_Ex_refl_eq2 [simp]: 
   180  "(!x. (? b. x = f b & Q b) \<longrightarrow> P x) = (!b. Q b --> P (f b))"
   181 apply auto
   182 done
   183 
   184 lemma ex_ex_miniscope1 [simp]:
   185   "(EX w v. P w v & Q v) = (EX v. (EX w. P w v) & Q v)"
   186 apply auto
   187 done
   188 
   189 lemma ex_miniscope2 [simp]:
   190   "(EX v. P v & Q & R v) = (Q & (EX v. P v & R v))" 
   191 apply auto
   192 done
   193 
   194 lemma ex_reorder31: "(\<exists>z x y. P x y z) = (\<exists>x y z. P x y z)"
   195 apply auto
   196 done
   197 
   198 lemma All_Ex_refl_eq1 [simp]: "(!x. (? b. x = f b) --> P x) = (!b. P (f b))"
   199 apply auto
   200 done
   201 
   202 
   203 section "sums"
   204 
   205 hide const In0 In1
   206 
   207 syntax
   208   fun_sum :: "('a => 'c) => ('b => 'c) => (('a+'b) => 'c)" (infixr "'(+')"80)
   209 translations
   210  "fun_sum" == "CONST sum_case"
   211 
   212 consts    the_Inl  :: "'a + 'b \<Rightarrow> 'a"
   213           the_Inr  :: "'a + 'b \<Rightarrow> 'b"
   214 primrec  "the_Inl (Inl a) = a"
   215 primrec  "the_Inr (Inr b) = b"
   216 
   217 datatype ('a, 'b, 'c) sum3 = In1 'a | In2 'b | In3 'c
   218 
   219 consts    the_In1  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'a"
   220           the_In2  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'b"
   221           the_In3  :: "('a, 'b, 'c) sum3 \<Rightarrow> 'c"
   222 primrec  "the_In1 (In1 a) = a"
   223 primrec  "the_In2 (In2 b) = b"
   224 primrec  "the_In3 (In3 c) = c"
   225 
   226 syntax
   227 	 In1l	:: "'al \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   228 	 In1r	:: "'ar \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   229 translations
   230 	"In1l e" == "In1 (Inl e)"
   231 	"In1r c" == "In1 (Inr c)"
   232 
   233 syntax the_In1l :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'al"
   234        the_In1r :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'ar"
   235 translations
   236    "the_In1l" == "the_Inl \<circ> the_In1"
   237    "the_In1r" == "the_Inr \<circ> the_In1"
   238 
   239 ML {*
   240 fun sum3_instantiate thm = map (fn s => simplify(simpset()delsimps[@{thm not_None_eq}])
   241  (read_instantiate [("t","In"^s^" ?x")] thm)) ["1l","2","3","1r"]
   242 *}
   243 (* e.g. lemmas is_stmt_rews = is_stmt_def [of "In1l x", simplified] *)
   244 
   245 translations
   246   "option"<= (type) "Datatype.option"
   247   "list"  <= (type) "List.list"
   248   "sum3"  <= (type) "Basis.sum3"
   249 
   250 
   251 section "quantifiers for option type"
   252 
   253 syntax
   254   Oall :: "[pttrn, 'a option, bool] => bool"   ("(3! _:_:/ _)" [0,0,10] 10)
   255   Oex  :: "[pttrn, 'a option, bool] => bool"   ("(3? _:_:/ _)" [0,0,10] 10)
   256 
   257 syntax (symbols)
   258   Oall :: "[pttrn, 'a option, bool] => bool"   ("(3\<forall>_\<in>_:/ _)"  [0,0,10] 10)
   259   Oex  :: "[pttrn, 'a option, bool] => bool"   ("(3\<exists>_\<in>_:/ _)"  [0,0,10] 10)
   260 
   261 translations
   262   "! x:A: P"    == "! x:o2s A. P"
   263   "? x:A: P"    == "? x:o2s A. P"
   264 
   265 section "Special map update"
   266 
   267 text{* Deemed too special for theory Map. *}
   268 
   269 constdefs
   270   chg_map :: "('b => 'b) => 'a => ('a ~=> 'b) => ('a ~=> 'b)"
   271  "chg_map f a m == case m a of None => m | Some b => m(a|->f b)"
   272 
   273 lemma chg_map_new[simp]: "m a = None   ==> chg_map f a m = m"
   274 by (unfold chg_map_def, auto)
   275 
   276 lemma chg_map_upd[simp]: "m a = Some b ==> chg_map f a m = m(a|->f b)"
   277 by (unfold chg_map_def, auto)
   278 
   279 lemma chg_map_other [simp]: "a \<noteq> b \<Longrightarrow> chg_map f a m b = m b"
   280 by (auto simp: chg_map_def split add: option.split)
   281 
   282 
   283 section "unique association lists"
   284 
   285 constdefs
   286   unique   :: "('a \<times> 'b) list \<Rightarrow> bool"
   287  "unique \<equiv> distinct \<circ> map fst"
   288 
   289 lemma uniqueD [rule_format (no_asm)]: 
   290 "unique l--> (!x y. (x,y):set l --> (!x' y'. (x',y'):set l --> x=x'-->  y=y'))"
   291 apply (unfold unique_def o_def)
   292 apply (induct_tac "l")
   293 apply  (auto dest: fst_in_set_lemma)
   294 done
   295 
   296 lemma unique_Nil [simp]: "unique []"
   297 apply (unfold unique_def)
   298 apply (simp (no_asm))
   299 done
   300 
   301 lemma unique_Cons [simp]: "unique ((x,y)#l) = (unique l & (!y. (x,y) ~: set l))"
   302 apply (unfold unique_def)
   303 apply  (auto dest: fst_in_set_lemma)
   304 done
   305 
   306 lemmas unique_ConsI = conjI [THEN unique_Cons [THEN iffD2], standard]
   307 
   308 lemma unique_single [simp]: "!!p. unique [p]"
   309 apply auto
   310 done
   311 
   312 lemma unique_ConsD: "unique (x#xs) ==> unique xs"
   313 apply (simp add: unique_def)
   314 done
   315 
   316 lemma unique_append [rule_format (no_asm)]: "unique l' ==> unique l -->  
   317   (!(x,y):set l. !(x',y'):set l'. x' ~= x) --> unique (l @ l')"
   318 apply (induct_tac "l")
   319 apply  (auto dest: fst_in_set_lemma)
   320 done
   321 
   322 lemma unique_map_inj [rule_format (no_asm)]: "unique l --> inj f --> unique (map (%(k,x). (f k, g k x)) l)"
   323 apply (induct_tac "l")
   324 apply  (auto dest: fst_in_set_lemma simp add: inj_eq)
   325 done
   326 
   327 lemma map_of_SomeI [rule_format (no_asm)]: "unique l --> (k, x) : set l --> map_of l k = Some x"
   328 apply (induct_tac "l")
   329 apply auto
   330 done
   331 
   332 
   333 section "list patterns"
   334 
   335 consts
   336   lsplit         :: "[['a, 'a list] => 'b, 'a list] => 'b"
   337 defs
   338   lsplit_def:    "lsplit == %f l. f (hd l) (tl l)"
   339 (*  list patterns -- extends pre-defined type "pttrn" used in abstractions *)
   340 syntax
   341   "_lpttrn"    :: "[pttrn,pttrn] => pttrn"     ("_#/_" [901,900] 900)
   342 translations
   343   "%y#x#xs. b"  == "lsplit (%y x#xs. b)"
   344   "%x#xs  . b"  == "lsplit (%x xs  . b)"
   345 
   346 lemma lsplit [simp]: "lsplit c (x#xs) = c x xs"
   347 apply (unfold lsplit_def)
   348 apply (simp (no_asm))
   349 done
   350 
   351 lemma lsplit2 [simp]: "lsplit P (x#xs) y z = P x xs y z"
   352 apply (unfold lsplit_def)
   353 apply simp
   354 done 
   355 
   356 
   357 end