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