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