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