2 (* Title: HOL/Tools/Presburger/presburger.ML
4 Author: Amine Chaieb, TU Muenchen
9 val cooper_tac: bool -> thm list -> thm list -> Proof.context -> int -> Tactical.tactic
12 structure Presburger : PRESBURGER =
16 val comp_ss = HOL_ss addsimps @{thms "Groebner_Basis.comp_arith"};
18 fun strip_imp_cprems ct =
20 Const ("==>", _) $ _ $ _ => Thm.dest_arg1 ct :: strip_imp_cprems (Thm.dest_arg ct)
23 val cprems_of = strip_imp_cprems o cprop_of;
27 Const ("op -->", _) $ _ $ _ => Thm.dest_arg1 ct :: strip_objimp (Thm.dest_arg ct)
32 Const ("All", _) $ Abs (xn,xT,p) =>
33 let val (a,(v,t')) = (apsnd (Thm.dest_abs (SOME xn)) o Thm.dest_comb) ct
34 in apfst (cons (a,v)) (strip_objall t')
40 HOL_basic_ss addsimps map (fn th => th RS sym) @{thms "all_simps"}
42 fun thin_prems_tac P i = simp_tac all_maxscope_ss i THEN
43 (fn st => case try (nth (cprems_of st)) (i - 1) of
47 val (qvs, p) = strip_objall (Thm.dest_arg p')
48 val (ps, c) = split_last (strip_objimp p)
50 val q = if P c then c else @{cterm "False"}
51 val ng = fold_rev (fn (a,v) => fn t => Thm.capply a (Thm.cabs v t)) qvs
52 (fold_rev (fn p => fn q => Thm.capply (Thm.capply @{cterm "op -->"} p) q) qs q)
53 val g = Thm.capply (Thm.capply @{cterm "op ==>"} (Thm.capply @{cterm "Trueprop"} ng)) p'
54 val ntac = (case qs of [] => q aconvc @{cterm "False"}
57 if ntac then no_tac st
58 else rtac (Goal.prove_internal [] g (K (blast_tac HOL_cs 1))) i st
64 if not (typ_of (ctyp_of_term t) mem [HOLogic.intT, HOLogic.natT]) then false
65 else case term_of t of
66 c$_$_ => not (member (op aconv) cts c)
67 | c$_ => not (member (op aconv) cts c)
68 | c => not (member (op aconv) cts c)
72 let fun h acc t = case t of
73 Const _ => insert (op aconv) t acc
74 | a$b => h (h acc a) b
75 | Abs (_,_,t) => h acc t
79 fun is_relevant ctxt ct =
80 gen_subset (op aconv) (term_constants (term_of ct) , snd (CooperData.get ctxt))
81 andalso forall (fn Free (_,T) => T = HOLogic.intT) (term_frees (term_of ct))
82 andalso forall (fn Var (_,T) => T = HOLogic.intT) (term_vars (term_of ct));
84 fun int_nat_terms ctxt ct =
86 val cts = snd (CooperData.get ctxt)
87 fun h acc t = if ty cts t then insert (op aconvc) t acc else
89 _$_ => h (h acc (Thm.dest_arg t)) (Thm.dest_fun t)
90 | Abs(_,_,_) => Thm.dest_abs NONE t ||> h acc |> uncurry (remove (op aconvc))
95 fun generalize_tac ctxt f i st =
96 case try (nth (cprems_of st)) (i - 1) of
100 fun all T = Drule.cterm_rule (instantiate' [SOME T] []) @{cpat "all"}
101 fun gen x t = Thm.capply (all (ctyp_of_term x)) (Thm.cabs x t)
102 val ts = sort (fn (a,b) => Term.fast_term_ord (term_of a, term_of b)) (f p)
103 val p' = fold_rev gen ts p
104 in Seq.of_list [implies_intr p' (implies_elim st (fold forall_elim ts (assume p')))]
109 addsimps simp_thms @ [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}]
110 @ map (fn r => r RS sym)
111 [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"},
113 addsplits [@{thm "zdiff_int_split"}]
115 val ss2 = HOL_basic_ss
116 addsimps [@{thm "nat_0_le"}, @{thm "int_nat_number_of"},
117 @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"},
118 @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}, @{thm "Suc_plus1"}]
119 addcongs [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
120 val div_mod_ss = HOL_basic_ss addsimps simp_thms
121 @ map (symmetric o mk_meta_eq)
122 [@{thm "dvd_eq_mod_eq_0"}, @{thm "zdvd_iff_zmod_eq_0"}, @{thm "mod_add1_eq"},
123 @{thm "mod_add_left_eq"}, @{thm "mod_add_right_eq"},
124 @{thm "zmod_zadd1_eq"}, @{thm "zmod_zadd_left_eq"},
125 @{thm "zmod_zadd_right_eq"}, @{thm "div_add1_eq"}, @{thm "zdiv_zadd1_eq"}]
126 @ [@{thm "mod_self"}, @{thm "zmod_self"}, @{thm "DIVISION_BY_ZERO_MOD"},
127 @{thm "DIVISION_BY_ZERO_DIV"}, @{thm "DIVISION_BY_ZERO"} RS conjunct1,
128 @{thm "DIVISION_BY_ZERO"} RS conjunct2, @{thm "zdiv_zero"}, @{thm "zmod_zero"},
129 @{thm "div_0"}, @{thm "mod_0"}, @{thm "zdiv_1"}, @{thm "zmod_1"}, @{thm "div_1"},
130 @{thm "mod_1"}, @{thm "Suc_plus1"}]
132 addsimprocs [cancel_div_mod_proc]
133 val splits_ss = comp_ss addsimps [@{thm "mod_div_equality'"}] addsplits
134 [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"},
135 @{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}]
137 fun nat_to_int_tac ctxt i =
138 simp_tac (Simplifier.context ctxt ss1) i THEN
139 simp_tac (Simplifier.context ctxt ss2) i THEN
140 TRY (simp_tac (Simplifier.context ctxt comp_ss) i);
142 fun div_mod_tac ctxt i = simp_tac (Simplifier.context ctxt div_mod_ss) i;
143 fun splits_tac ctxt i = simp_tac (Simplifier.context ctxt splits_ss) i;
147 fun eta_beta_tac ctxt i st = case try (nth (cprems_of st)) (i - 1) of
151 val eq = (eta_conv (ProofContext.theory_of ctxt) then_conv Thm.beta_conversion true) p
152 val p' = Thm.rhs_of eq
153 val th = implies_intr p' (equal_elim (symmetric eq) (assume p'))
159 fun core_cooper_tac ctxt i st =
160 case try (nth (cprems_of st)) (i - 1) of
166 then linzqe_oracle (ProofContext.theory_of ctxt)
167 (Envir.beta_norm (Pattern.eta_long [] (term_of (Thm.dest_arg p))))
168 else arg_conv (Cooper.cooper_conv ctxt) p
169 val p' = Thm.rhs_of cpth
170 val th = implies_intr p' (equal_elim (symmetric cpth) (assume p'))
172 handle Cooper.COOPER _ => no_tac st;
174 fun nogoal_tac i st = case try (nth (cprems_of st)) (i - 1) of
176 | SOME _ => all_tac st
178 fun finish_tac q i st = case try (nth (cprems_of st)) (i - 1) of
180 | SOME _ => (if q then I else TRY) (rtac TrueI i) st
182 fun cooper_tac elim add_ths del_ths ctxt i =
183 let val ss = fst (CooperData.get ctxt) delsimps del_ths addsimps add_ths
186 THEN (EVERY o (map TRY))
187 [ObjectLogic.full_atomize_tac i,
190 generalize_tac ctxt (int_nat_terms ctxt) i,
191 ObjectLogic.full_atomize_tac i,
196 nat_to_int_tac ctxt i,
197 thin_prems_tac (is_relevant ctxt) i]
198 THEN core_cooper_tac ctxt i THEN finish_tac elim i