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