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