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