src/HOL/Bali/Basis.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (20 months ago)
changeset 67022 49309fe530fd
parent 66809 f6a30d48aab0
child 67613 ce654b0e6d69
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
     1 (*  Title:      HOL/Bali/Basis.thy
     2     Author:     David von Oheimb
     3 *)
     4 subsection \<open>Definitions extending HOL as logical basis of Bali\<close>
     5 
     6 theory Basis
     7 imports Main
     8 begin
     9 
    10 subsubsection "misc"
    11 
    12 ML \<open>fun strip_tac ctxt i = REPEAT (resolve_tac ctxt [impI, allI] i)\<close>
    13 
    14 declare if_split_asm  [split] option.split [split] option.split_asm [split]
    15 setup \<open>map_theory_simpset (fn ctxt => ctxt addloop ("split_all_tac", split_all_tac))\<close>
    16 declare if_weak_cong [cong del] option.case_cong_weak [cong del]
    17 declare length_Suc_conv [iff]
    18 
    19 lemma Collect_split_eq: "{p. P (case_prod f p)} = {(a,b). P (f a b)}"
    20   by auto
    21 
    22 lemma subset_insertD: "A \<subseteq> insert x B \<Longrightarrow> A \<subseteq> B \<and> x \<notin> A \<or> (\<exists>B'. A = insert x B' \<and> B' \<subseteq> B)"
    23   apply (case_tac "x \<in> A")
    24    apply (rule disjI2)
    25    apply (rule_tac x = "A - {x}" in exI)
    26    apply fast+
    27   done
    28 
    29 abbreviation nat3 :: nat  ("3") where "3 \<equiv> Suc 2"
    30 abbreviation nat4 :: nat  ("4") where "4 \<equiv> Suc 3"
    31 
    32 (* irrefl_tranclI in Transitive_Closure.thy is more general *)
    33 lemma irrefl_tranclI': "r\<inverse> \<inter> r\<^sup>+ = {} \<Longrightarrow> \<forall>x. (x, x) \<notin> r\<^sup>+"
    34   by (blast elim: tranclE dest: trancl_into_rtrancl)
    35 
    36 
    37 lemma trancl_rtrancl_trancl: "\<lbrakk>(x, y) \<in> r\<^sup>+; (y, z) \<in> r\<^sup>*\<rbrakk> \<Longrightarrow> (x, z) \<in> r\<^sup>+"
    38   by (auto dest: tranclD rtrancl_trans rtrancl_into_trancl2)
    39 
    40 lemma rtrancl_into_trancl3: "\<lbrakk>(a, b) \<in> r\<^sup>*; a \<noteq> b\<rbrakk> \<Longrightarrow> (a, b) \<in> r\<^sup>+"
    41   apply (drule rtranclD)
    42   apply auto
    43   done
    44 
    45 lemma rtrancl_into_rtrancl2: "\<lbrakk>(a, b) \<in>  r; (b, c) \<in> r\<^sup>*\<rbrakk> \<Longrightarrow> (a, c) \<in> r\<^sup>*"
    46   by (auto intro: rtrancl_trans)
    47 
    48 lemma triangle_lemma:
    49   assumes unique: "\<And>a b c. \<lbrakk>(a,b)\<in>r; (a,c)\<in>r\<rbrakk> \<Longrightarrow> b = c"
    50     and ax: "(a,x)\<in>r\<^sup>*" and ay: "(a,y)\<in>r\<^sup>*"
    51   shows "(x,y)\<in>r\<^sup>* \<or> (y,x)\<in>r\<^sup>*"
    52   using ax ay
    53 proof (induct rule: converse_rtrancl_induct)
    54   assume "(x,y)\<in>r\<^sup>*"
    55   then show ?thesis by blast
    56 next
    57   fix a v
    58   assume a_v_r: "(a, v) \<in> r"
    59     and v_x_rt: "(v, x) \<in> r\<^sup>*"
    60     and a_y_rt: "(a, y) \<in> r\<^sup>*"
    61     and hyp: "(v, y) \<in> r\<^sup>* \<Longrightarrow> (x, y) \<in> r\<^sup>* \<or> (y, x) \<in> r\<^sup>*"
    62   from a_y_rt show "(x, y) \<in> r\<^sup>* \<or> (y, x) \<in> r\<^sup>*"
    63   proof (cases rule: converse_rtranclE)
    64     assume "a = y"
    65     with a_v_r v_x_rt have "(y,x) \<in> r\<^sup>*"
    66       by (auto intro: rtrancl_trans)
    67     then show ?thesis by blast
    68   next
    69     fix w
    70     assume a_w_r: "(a, w) \<in> r"
    71       and w_y_rt: "(w, y) \<in> r\<^sup>*"
    72     from a_v_r a_w_r unique have "v=w" by auto
    73     with w_y_rt hyp show ?thesis by blast
    74   qed
    75 qed
    76 
    77 
    78 lemma rtrancl_cases:
    79   assumes "(a,b)\<in>r\<^sup>*"
    80   obtains (Refl) "a = b"
    81     | (Trancl) "(a,b)\<in>r\<^sup>+"
    82   apply (rule rtranclE [OF assms])
    83    apply (auto dest: rtrancl_into_trancl1)
    84   done
    85 
    86 lemma Ball_weaken: "\<lbrakk>Ball s P; \<And> x. P x\<longrightarrow>Q x\<rbrakk>\<Longrightarrow>Ball s Q"
    87   by auto
    88 
    89 lemma finite_SetCompr2:
    90   "finite (Collect P) \<Longrightarrow> \<forall>y. P y \<longrightarrow> finite (range (f y)) \<Longrightarrow>
    91     finite {f y x |x y. P y}"
    92   apply (subgoal_tac "{f y x |x y. P y} = UNION (Collect P) (\<lambda>y. range (f y))")
    93    prefer 2 apply fast
    94   apply (erule ssubst)
    95   apply (erule finite_UN_I)
    96   apply fast
    97   done
    98 
    99 lemma list_all2_trans: "\<forall>a b c. P1 a b \<longrightarrow> P2 b c \<longrightarrow> P3 a c \<Longrightarrow>
   100     \<forall>xs2 xs3. list_all2 P1 xs1 xs2 \<longrightarrow> list_all2 P2 xs2 xs3 \<longrightarrow> list_all2 P3 xs1 xs3"
   101   apply (induct_tac xs1)
   102    apply simp
   103   apply (rule allI)
   104   apply (induct_tac xs2)
   105    apply simp
   106   apply (rule allI)
   107   apply (induct_tac xs3)
   108    apply auto
   109   done
   110 
   111 
   112 subsubsection "pairs"
   113 
   114 lemma surjective_pairing5:
   115   "p = (fst p, fst (snd p), fst (snd (snd p)), fst (snd (snd (snd p))),
   116     snd (snd (snd (snd p))))"
   117   by auto
   118 
   119 lemma fst_splitE [elim!]:
   120   assumes "fst s' = x'"
   121   obtains x s where "s' = (x,s)" and "x = x'"
   122   using assms by (cases s') auto
   123 
   124 lemma fst_in_set_lemma: "(x, y) : set l \<Longrightarrow> x : fst ` set l"
   125   by (induct l) auto
   126 
   127 
   128 subsubsection "quantifiers"
   129 
   130 lemma All_Ex_refl_eq2 [simp]: "(\<forall>x. (\<exists>b. x = f b \<and> Q b) \<longrightarrow> P x) = (\<forall>b. Q b \<longrightarrow> P (f b))"
   131   by auto
   132 
   133 lemma ex_ex_miniscope1 [simp]: "(\<exists>w v. P w v \<and> Q v) = (\<exists>v. (\<exists>w. P w v) \<and> Q v)"
   134   by auto
   135 
   136 lemma ex_miniscope2 [simp]: "(\<exists>v. P v \<and> Q \<and> R v) = (Q \<and> (\<exists>v. P v \<and> R v))"
   137   by auto
   138 
   139 lemma ex_reorder31: "(\<exists>z x y. P x y z) = (\<exists>x y z. P x y z)"
   140   by auto
   141 
   142 lemma All_Ex_refl_eq1 [simp]: "(\<forall>x. (\<exists>b. x = f b) \<longrightarrow> P x) = (\<forall>b. P (f b))"
   143   by auto
   144 
   145 
   146 subsubsection "sums"
   147 
   148 notation case_sum  (infixr "'(+')" 80)
   149 
   150 primrec the_Inl :: "'a + 'b \<Rightarrow> 'a"
   151   where "the_Inl (Inl a) = a"
   152 
   153 primrec the_Inr :: "'a + 'b \<Rightarrow> 'b"
   154   where "the_Inr (Inr b) = b"
   155 
   156 datatype ('a, 'b, 'c) sum3 = In1 'a | In2 'b | In3 'c
   157 
   158 primrec the_In1 :: "('a, 'b, 'c) sum3 \<Rightarrow> 'a"
   159   where "the_In1 (In1 a) = a"
   160 
   161 primrec the_In2 :: "('a, 'b, 'c) sum3 \<Rightarrow> 'b"
   162   where "the_In2 (In2 b) = b"
   163 
   164 primrec the_In3 :: "('a, 'b, 'c) sum3 \<Rightarrow> 'c"
   165   where "the_In3 (In3 c) = c"
   166 
   167 abbreviation In1l :: "'al \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   168   where "In1l e \<equiv> In1 (Inl e)"
   169 
   170 abbreviation In1r :: "'ar \<Rightarrow> ('al + 'ar, 'b, 'c) sum3"
   171   where "In1r c \<equiv> In1 (Inr c)"
   172 
   173 abbreviation the_In1l :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'al"
   174   where "the_In1l \<equiv> the_Inl \<circ> the_In1"
   175 
   176 abbreviation the_In1r :: "('al + 'ar, 'b, 'c) sum3 \<Rightarrow> 'ar"
   177   where "the_In1r \<equiv> the_Inr \<circ> the_In1"
   178 
   179 ML \<open>
   180 fun sum3_instantiate ctxt thm =
   181   map (fn s =>
   182     simplify (ctxt delsimps @{thms not_None_eq})
   183       (Rule_Insts.read_instantiate ctxt [((("t", 0), Position.none), "In" ^ s ^ " x")] ["x"] thm))
   184     ["1l","2","3","1r"]
   185 \<close>
   186 (* e.g. lemmas is_stmt_rews = is_stmt_def [of "In1l x", simplified] *)
   187 
   188 
   189 subsubsection "quantifiers for option type"
   190 
   191 syntax
   192   "_Oall" :: "[pttrn, 'a option, bool] \<Rightarrow> bool"   ("(3! _:_:/ _)" [0,0,10] 10)
   193   "_Oex"  :: "[pttrn, 'a option, bool] \<Rightarrow> bool"   ("(3? _:_:/ _)" [0,0,10] 10)
   194 
   195 syntax (symbols)
   196   "_Oall" :: "[pttrn, 'a option, bool] \<Rightarrow> bool"   ("(3\<forall>_\<in>_:/ _)"  [0,0,10] 10)
   197   "_Oex"  :: "[pttrn, 'a option, bool] \<Rightarrow> bool"   ("(3\<exists>_\<in>_:/ _)"  [0,0,10] 10)
   198 
   199 translations
   200   "\<forall>x\<in>A: P" \<rightleftharpoons> "\<forall>x\<in>CONST set_option A. P"
   201   "\<exists>x\<in>A: P" \<rightleftharpoons> "\<exists>x\<in>CONST set_option A. P"
   202 
   203 
   204 subsubsection "Special map update"
   205 
   206 text\<open>Deemed too special for theory Map.\<close>
   207 
   208 definition chg_map :: "('b \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> ('a \<rightharpoonup> 'b) \<Rightarrow> ('a \<rightharpoonup> 'b)"
   209   where "chg_map f a m = (case m a of None \<Rightarrow> m | Some b \<Rightarrow> m(a\<mapsto>f b))"
   210 
   211 lemma chg_map_new[simp]: "m a = None \<Longrightarrow> chg_map f a m = m"
   212   unfolding chg_map_def by auto
   213 
   214 lemma chg_map_upd[simp]: "m a = Some b \<Longrightarrow> chg_map f a m = m(a\<mapsto>f b)"
   215   unfolding chg_map_def by auto
   216 
   217 lemma chg_map_other [simp]: "a \<noteq> b \<Longrightarrow> chg_map f a m b = m b"
   218   by (auto simp: chg_map_def)
   219 
   220 
   221 subsubsection "unique association lists"
   222 
   223 definition unique :: "('a \<times> 'b) list \<Rightarrow> bool"
   224   where "unique = distinct \<circ> map fst"
   225 
   226 lemma uniqueD: "unique l \<Longrightarrow> (x, y) \<in> set l \<Longrightarrow> (x', y') \<in> set l \<Longrightarrow> x = x' \<Longrightarrow> y = y'"
   227   unfolding unique_def o_def
   228   by (induct l) (auto dest: fst_in_set_lemma)
   229 
   230 lemma unique_Nil [simp]: "unique []"
   231   by (simp add: unique_def)
   232 
   233 lemma unique_Cons [simp]: "unique ((x,y)#l) = (unique l \<and> (\<forall>y. (x,y) \<notin> set l))"
   234   by (auto simp: unique_def dest: fst_in_set_lemma)
   235 
   236 lemma unique_ConsD: "unique (x#xs) \<Longrightarrow> unique xs"
   237   by (simp add: unique_def)
   238 
   239 lemma unique_single [simp]: "\<And>p. unique [p]"
   240   by simp
   241 
   242 lemma unique_append [rule_format (no_asm)]: "unique l' \<Longrightarrow> unique l \<Longrightarrow>
   243     (\<forall>(x,y)\<in>set l. \<forall>(x',y')\<in>set l'. x' \<noteq> x) \<longrightarrow> unique (l @ l')"
   244   by (induct l) (auto dest: fst_in_set_lemma)
   245 
   246 lemma unique_map_inj: "unique l \<Longrightarrow> inj f \<Longrightarrow> unique (map (\<lambda>(k,x). (f k, g k x)) l)"
   247   by (induct l) (auto dest: fst_in_set_lemma simp add: inj_eq)
   248 
   249 lemma map_of_SomeI: "unique l \<Longrightarrow> (k, x) : set l \<Longrightarrow> map_of l k = Some x"
   250   by (induct l) auto
   251 
   252 
   253 subsubsection "list patterns"
   254 
   255 definition lsplit :: "[['a, 'a list] \<Rightarrow> 'b, 'a list] \<Rightarrow> 'b"
   256   where "lsplit = (\<lambda>f l. f (hd l) (tl l))"
   257 
   258 text \<open>list patterns -- extends pre-defined type "pttrn" used in abstractions\<close>
   259 syntax
   260   "_lpttrn" :: "[pttrn, pttrn] \<Rightarrow> pttrn"    ("_#/_" [901,900] 900)
   261 translations
   262   "\<lambda>y # x # xs. b" \<rightleftharpoons> "CONST lsplit (\<lambda>y x # xs. b)"
   263   "\<lambda>x # xs. b" \<rightleftharpoons> "CONST lsplit (\<lambda>x xs. b)"
   264 
   265 lemma lsplit [simp]: "lsplit c (x#xs) = c x xs"
   266   by (simp add: lsplit_def)
   267 
   268 lemma lsplit2 [simp]: "lsplit P (x#xs) y z = P x xs y z"
   269   by (simp add: lsplit_def)
   270 
   271 end