File ‹Tools/lin_arith.ML›
signature LIN_ARITH =
sig
val pre_tac: Proof.context -> int -> tactic
val simple_tac: Proof.context -> int -> tactic
val tac: Proof.context -> int -> tactic
val simproc: Simplifier.proc
val add_inj_thms: thm list -> Context.generic -> Context.generic
val add_lessD: thm -> Context.generic -> Context.generic
val add_simps: thm list -> Context.generic -> Context.generic
val add_simprocs: simproc list -> Context.generic -> Context.generic
val add_inj_const: string * typ -> Context.generic -> Context.generic
val add_discrete_type: string -> Context.generic -> Context.generic
val set_number_of: (Proof.context -> typ -> int -> cterm) -> Context.generic -> Context.generic
val global_setup: theory -> theory
val init_arith_data: Context.generic -> Context.generic
val split_limit: int Config.T
val neq_limit: int Config.T
val trace: bool Config.T
end;
structure Lin_Arith: LIN_ARITH =
struct
structure LA_Logic: LIN_ARITH_LOGIC =
struct
val ccontr = @{thm ccontr};
val conjI = conjI;
val notI = notI;
val sym = sym;
val trueI = TrueI;
val not_lessD = @{thm linorder_not_less} RS iffD1;
val not_leD = @{thm linorder_not_le} RS iffD1;
fun mk_Eq thm = thm RS @{thm Eq_FalseI} handle THM _ => thm RS @{thm Eq_TrueI};
val mk_Trueprop = HOLogic.mk_Trueprop;
fun atomize thm = case Thm.prop_of thm of
Const (\<^const_name>‹Trueprop›, _) $ (Const (\<^const_name>‹HOL.conj›, _) $ _ $ _) =>
atomize (thm RS conjunct1) @ atomize (thm RS conjunct2)
| _ => [thm];
fun neg_prop ((TP as Const(\<^const_name>‹Trueprop›, _)) $ (Const (\<^const_name>‹Not›, _) $ t)) = TP $ t
| neg_prop ((TP as Const(\<^const_name>‹Trueprop›, _)) $ t) = TP $ (HOLogic.Not $t)
| neg_prop t = raise TERM ("neg_prop", [t]);
fun is_False thm =
let val _ $ t = Thm.prop_of thm
in t = \<^term>‹False› end;
fun is_nat t = (fastype_of1 t = HOLogic.natT);
fun mk_nat_thm thy t =
\<^instantiate>‹n = ‹Thm.global_cterm_of thy t› in
lemma (open) ‹0 ≤ n› for n :: nat by (rule le0)›;
end;
structure Lin_Arith_Data = Generic_Data
(
type T = {splits: thm list,
inj_consts: (string * typ) list,
discrete: string list};
val empty = {splits = [], inj_consts = [], discrete = []};
fun merge
({splits = splits1, inj_consts = inj_consts1, discrete = discrete1},
{splits = splits2, inj_consts = inj_consts2, discrete = discrete2}) : T =
{splits = Thm.merge_thms (splits1, splits2),
inj_consts = Library.merge (op =) (inj_consts1, inj_consts2),
discrete = Library.merge (op =) (discrete1, discrete2)};
);
val get_arith_data = Lin_Arith_Data.get o Context.Proof;
fun get_splits ctxt =
#splits (get_arith_data ctxt)
|> map (Thm.transfer' ctxt);
fun add_split thm = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
{splits = update Thm.eq_thm_prop (Thm.trim_context thm) splits,
inj_consts = inj_consts, discrete = discrete});
fun add_discrete_type d = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
{splits = splits, inj_consts = inj_consts,
discrete = update (op =) d discrete});
fun add_inj_const c = Lin_Arith_Data.map (fn {splits, inj_consts, discrete} =>
{splits = splits, inj_consts = update (op =) c inj_consts,
discrete = discrete});
val split_limit = Attrib.setup_config_int \<^binding>‹linarith_split_limit› (K 9);
val neq_limit = Attrib.setup_config_int \<^binding>‹linarith_neq_limit› (K 9);
val trace = Attrib.setup_config_bool \<^binding>‹linarith_trace› (K false);
fun nnf_simpset ctxt =
(empty_simpset ctxt
|> Simplifier.set_mkeqTrue mk_eq_True
|> Simplifier.set_mksimps (mksimps mksimps_pairs))
addsimps @{thms imp_conv_disj iff_conv_conj_imp de_Morgan_disj
de_Morgan_conj not_all not_ex not_not}
fun prem_nnf_tac ctxt = full_simp_tac (nnf_simpset ctxt)
structure LA_Data: LIN_ARITH_DATA =
struct
val neq_limit = neq_limit;
val trace = trace;
type decomp =
((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat * bool);
fun nT (Type ("fun", [N, _])) = (N = HOLogic.natT)
| nT _ = false;
fun add_atom (t : term) (m : Rat.rat) (p : (term * Rat.rat) list, i : Rat.rat) :
(term * Rat.rat) list * Rat.rat =
case AList.lookup Envir.aeconv p t of
NONE => ((t, m) :: p, i)
| SOME n => (AList.update Envir.aeconv (t, Rat.add n m) p, i);
fun of_field_sort thy U = Sign.of_sort thy (U, \<^sort>‹inverse›);
fun demult thy (inj_consts : (string * typ) list) : term * Rat.rat -> term option * Rat.rat =
let
fun demult ((mC as Const (\<^const_name>‹Groups.times›, _)) $ s $ t, m) =
(case s of Const (\<^const_name>‹Groups.times›, _) $ s1 $ s2 =>
demult (mC $ s1 $ (mC $ s2 $ t), m)
| _ =>
(case demult (s, m) of
(SOME s', m') =>
(case demult (t, m') of
(SOME t', m'') => (SOME (mC $ s' $ t'), m'')
| (NONE, m'') => (SOME s', m''))
| (NONE, m') => demult (t, m')))
| demult (atom as (mC as Const (\<^const_name>‹Rings.divide›, T)) $ s $ t, m) =
if of_field_sort thy (domain_type T) then
let
val (os',m') = demult (s, m);
val (ot',p) = demult (t, @1)
in (case (os',ot') of
(SOME s', SOME t') => SOME (mC $ s' $ t')
| (SOME s', NONE) => SOME s'
| (NONE, SOME t') =>
SOME (mC $ Const (\<^const_name>‹Groups.one›, domain_type (snd (dest_Const mC))) $ t')
| (NONE, NONE) => NONE,
Rat.mult m' (Rat.inv p))
end
else (SOME atom, m)
| demult (Const (\<^const_name>‹Groups.uminus›, _) $ t, m) = demult (t, ~ m)
| demult (Const (\<^const_name>‹Groups.zero›, _), _) = (NONE, @0)
| demult (Const (\<^const_name>‹Groups.one›, _), m) = (NONE, m)
| demult (t as Const ("Num.numeral_class.numeral", _) $ n, m) =
((NONE, Rat.mult m (Rat.of_int (HOLogic.dest_numeral n)))
handle TERM _ => (SOME t, m))
| demult (t as Const (\<^const_name>‹Suc›, _) $ _, m) =
((NONE, Rat.mult m (Rat.of_int (HOLogic.dest_nat t)))
handle TERM _ => (SOME t, m))
| demult (t as Const f $ x, m) =
if member (op =) inj_consts f then demult (x, m) else (SOME t, m)
| demult (atom, m) = (SOME atom, m)
in demult end;
fun decomp0 thy (inj_consts : (string * typ) list) (rel : string, lhs : term, rhs : term) :
((term * Rat.rat) list * Rat.rat * string * (term * Rat.rat) list * Rat.rat) option =
let
fun poly (Const (\<^const_name>‹Groups.plus›, _) $ s $ t,
m : Rat.rat, pi : (term * Rat.rat) list * Rat.rat) = poly (s, m, poly (t, m, pi))
| poly (all as Const (\<^const_name>‹Groups.minus›, T) $ s $ t, m, pi) =
if nT T then add_atom all m pi else poly (s, m, poly (t, ~ m, pi))
| poly (all as Const (\<^const_name>‹Groups.uminus›, T) $ t, m, pi) =
if nT T then add_atom all m pi else poly (t, ~ m, pi)
| poly (Const (\<^const_name>‹Groups.zero›, _), _, pi) =
pi
| poly (Const (\<^const_name>‹Groups.one›, _), m, (p, i)) =
(p, Rat.add i m)
| poly (all as Const ("Num.numeral_class.numeral", _) $ t, m, pi as (p, i)) =
(let val k = HOLogic.dest_numeral t
in (p, Rat.add i (Rat.mult m (Rat.of_int k))) end
handle TERM _ => add_atom all m pi)
| poly (Const (\<^const_name>‹Suc›, _) $ t, m, (p, i)) =
poly (t, m, (p, Rat.add i m))
| poly (all as Const (\<^const_name>‹Groups.times›, _) $ _ $ _, m, pi as (p, i)) =
(case demult thy inj_consts (all, m) of
(NONE, m') => (p, Rat.add i m')
| (SOME u, m') => add_atom u m' pi)
| poly (all as Const (\<^const_name>‹Rings.divide›, T) $ _ $ _, m, pi as (p, i)) =
if of_field_sort thy (domain_type T) then
(case demult thy inj_consts (all, m) of
(NONE, m') => (p, Rat.add i m')
| (SOME u, m') => add_atom u m' pi)
else add_atom all m pi
| poly (all as Const f $ x, m, pi) =
if member (op =) inj_consts f then poly (x, m, pi) else add_atom all m pi
| poly (all, m, pi) =
add_atom all m pi
val (p, i) = poly (lhs, @1, ([], @0))
val (q, j) = poly (rhs, @1, ([], @0))
in
case rel of
\<^const_name>‹Orderings.less› => SOME (p, i, "<", q, j)
| \<^const_name>‹Orderings.less_eq› => SOME (p, i, "<=", q, j)
| \<^const_name>‹HOL.eq› => SOME (p, i, "=", q, j)
| _ => NONE
end handle General.Div => NONE;
fun of_lin_arith_sort thy U =
Sign.of_sort thy (U, \<^sort>‹Rings.linordered_idom›);
fun allows_lin_arith thy (discrete : string list) (U as Type (D, [])) : bool * bool =
if of_lin_arith_sort thy U then (true, member (op =) discrete D)
else if member (op =) discrete D then (true, true) else (false, false)
| allows_lin_arith sg _ U = (of_lin_arith_sort sg U, false);
fun decomp_typecheck thy (discrete, inj_consts) (T : typ, xxx) : decomp option =
case T of
Type ("fun", [U, _]) =>
(case allows_lin_arith thy discrete U of
(true, d) =>
(case decomp0 thy inj_consts xxx of
NONE => NONE
| SOME (p, i, rel, q, j) => SOME (p, i, rel, q, j, d))
| (false, _) =>
NONE)
| _ => NONE;
fun negate (SOME (x, i, rel, y, j, d)) = SOME (x, i, "~" ^ rel, y, j, d)
| negate NONE = NONE;
fun decomp_negation thy data
((Const (\<^const_name>‹Trueprop›, _)) $ (Const (rel, T) $ lhs $ rhs)) : decomp option =
decomp_typecheck thy data (T, (rel, lhs, rhs))
| decomp_negation thy data
((Const (\<^const_name>‹Trueprop›, _)) $ (Const (\<^const_name>‹Not›, _) $ (Const (rel, T) $ lhs $ rhs))) =
negate (decomp_typecheck thy data (T, (rel, lhs, rhs)))
| decomp_negation _ _ _ =
NONE;
fun decomp ctxt : term -> decomp option =
let
val thy = Proof_Context.theory_of ctxt
val {discrete, inj_consts, ...} = get_arith_data ctxt
in decomp_negation thy (discrete, inj_consts) end;
fun domain_is_nat (_ $ (Const (_, T) $ _ $ _)) = nT T
| domain_is_nat (_ $ (Const (\<^const_name>‹Not›, _) $ (Const (_, T) $ _ $ _))) = nT T
| domain_is_nat _ = false;
fun apply t u = t $ u
fun with2 f c t u cx = f t cx ||>> f u |>> (fn (t, u) => c $ t $ u)
fun abstract_atom (t as Free _) cx = (t, cx)
| abstract_atom (t as Const _) cx = (t, cx)
| abstract_atom t (cx as (terms, ctxt)) =
(case AList.lookup Envir.aeconv terms t of
SOME u => (u, cx)
| NONE =>
let
val (n, ctxt') = yield_singleton Variable.variant_fixes "" ctxt
val u = Free (n, fastype_of t)
in (u, ((t, u) :: terms, ctxt')) end)
fun abstract_num t cx = if can HOLogic.dest_number t then (t, cx) else abstract_atom t cx
fun is_field_sort (_, ctxt) T = of_field_sort (Proof_Context.theory_of ctxt) (domain_type T)
fun is_inj_const (_, ctxt) f =
let val {inj_consts, ...} = get_arith_data ctxt
in member (op =) inj_consts f end
fun abstract_arith ((c as Const (\<^const_name>‹Groups.plus›, _)) $ u1 $ u2) cx =
with2 abstract_arith c u1 u2 cx
| abstract_arith (t as (c as Const (\<^const_name>‹Groups.minus›, T)) $ u1 $ u2) cx =
if nT T then abstract_atom t cx else with2 abstract_arith c u1 u2 cx
| abstract_arith (t as (c as Const (\<^const_name>‹Groups.uminus›, T)) $ u) cx =
if nT T then abstract_atom t cx else abstract_arith u cx |>> apply c
| abstract_arith ((c as Const (\<^const_name>‹Suc›, _)) $ u) cx = abstract_arith u cx |>> apply c
| abstract_arith ((c as Const (\<^const_name>‹Groups.times›, _)) $ u1 $ u2) cx =
with2 abstract_arith c u1 u2 cx
| abstract_arith (t as (c as Const (\<^const_name>‹Rings.divide›, T)) $ u1 $ u2) cx =
if is_field_sort cx T then with2 abstract_arith c u1 u2 cx else abstract_atom t cx
| abstract_arith (t as (c as Const f) $ u) cx =
if is_inj_const cx f then abstract_arith u cx |>> apply c else abstract_num t cx
| abstract_arith t cx = abstract_num t cx
fun is_lin_arith_rel \<^const_name>‹Orderings.less› = true
| is_lin_arith_rel \<^const_name>‹Orderings.less_eq› = true
| is_lin_arith_rel \<^const_name>‹HOL.eq› = true
| is_lin_arith_rel _ = false
fun is_lin_arith_type (_, ctxt) T =
let val {discrete, ...} = get_arith_data ctxt
in fst (allows_lin_arith (Proof_Context.theory_of ctxt) discrete T) end
fun abstract_rel (t as (r as Const (rel, Type ("fun", [U, _]))) $ lhs $ rhs) cx =
if is_lin_arith_rel rel andalso is_lin_arith_type cx U then with2 abstract_arith r lhs rhs cx
else abstract_atom t cx
| abstract_rel t cx = abstract_atom t cx
fun abstract_neg ((c as Const (\<^const_name>‹Not›, _)) $ t) cx = abstract_rel t cx |>> apply c
| abstract_neg t cx = abstract_rel t cx
fun abstract ((c as Const (\<^const_name>‹Trueprop›, _)) $ t) cx = abstract_neg t cx |>> apply c
| abstract t cx = abstract_atom t cx
fun is_split_thm ctxt thm =
(case Thm.concl_of thm of _ $ (_ $ (_ $ lhs) $ _) =>
(case head_of lhs of
Const (a, _) =>
member (op =)
[\<^const_name>‹Orderings.max›,
\<^const_name>‹Orderings.min›,
\<^const_name>‹Groups.abs›,
\<^const_name>‹Groups.minus›,
"Int.nat" ,
\<^const_name>‹Rings.modulo›,
\<^const_name>‹Rings.divide›] a
| _ =>
(if Context_Position.is_visible ctxt then
warning ("Lin. Arith.: wrong format for split rule " ^ Thm.string_of_thm ctxt thm)
else (); false))
| _ =>
(if Context_Position.is_visible ctxt then
warning ("Lin. Arith.: wrong format for split rule " ^ Thm.string_of_thm ctxt thm)
else (); false));
fun subst_term ([] : (term * term) list) (t : term) = t
| subst_term pairs t =
(case AList.lookup Envir.aeconv pairs t of
SOME new =>
new
| NONE =>
(case t of Abs (a, T, body) =>
let val pairs' = map (apply2 (incr_boundvars 1)) pairs
in Abs (a, T, subst_term pairs' body) end
| t1 $ t2 => subst_term pairs t1 $ subst_term pairs t2
| _ => t));
fun split_once_items ctxt (Ts : typ list, terms : term list) :
(typ list * term list) list option =
let
val thy = Proof_Context.theory_of ctxt
fun REPEAT_DETERM_etac_rev_mp tms =
fold (curry HOLogic.mk_imp) (map HOLogic.dest_Trueprop tms) \<^term>‹False›
val split_thms = filter (is_split_thm ctxt) (get_splits ctxt)
val cmap = Splitter.cmap_of_split_thms split_thms
val goal_tm = REPEAT_DETERM_etac_rev_mp terms
val splits = Splitter.split_posns cmap thy Ts goal_tm
val split_limit = Config.get ctxt split_limit
in
if length splits > split_limit then (
tracing ("linarith_split_limit exceeded (current value is " ^
string_of_int split_limit ^ ")");
NONE
) else case splits of
[] =>
NONE
| (_, _::_, _, _, _) :: _ =>
NONE
| (_, [], _, split_type, split_term) :: _ =>
(case strip_comb split_term of
(Const (\<^const_name>‹Orderings.max›, _), [t1, t2]) =>
let
val rev_terms = rev terms
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
val terms2 = map (subst_term [(split_term, t2)]) rev_terms
val t1_leq_t2 = Const (\<^const_name>‹Orderings.less_eq›,
split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t1_leq_t2) :: terms2 @ [not_false]
val subgoal2 = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms1 @ [not_false]
in
SOME [(Ts, subgoal1), (Ts, subgoal2)]
end
| (Const (\<^const_name>‹Orderings.min›, _), [t1, t2]) =>
let
val rev_terms = rev terms
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
val terms2 = map (subst_term [(split_term, t2)]) rev_terms
val t1_leq_t2 = Const (\<^const_name>‹Orderings.less_eq›,
split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
val not_t1_leq_t2 = HOLogic.Not $ t1_leq_t2
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t1_leq_t2) :: terms1 @ [not_false]
val subgoal2 = (HOLogic.mk_Trueprop not_t1_leq_t2) :: terms2 @ [not_false]
in
SOME [(Ts, subgoal1), (Ts, subgoal2)]
end
| (Const (\<^const_name>‹Groups.abs›, _), [t1]) =>
let
val rev_terms = rev terms
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
val terms2 = map (subst_term [(split_term, Const (\<^const_name>‹Groups.uminus›,
split_type --> split_type) $ t1)]) rev_terms
val zero = Const (\<^const_name>‹Groups.zero›, split_type)
val zero_leq_t1 = Const (\<^const_name>‹Orderings.less_eq›,
split_type --> split_type --> HOLogic.boolT) $ zero $ t1
val t1_lt_zero = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type --> HOLogic.boolT) $ t1 $ zero
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop zero_leq_t1) :: terms1 @ [not_false]
val subgoal2 = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
in
SOME [(Ts, subgoal1), (Ts, subgoal2)]
end
| (Const (\<^const_name>‹Groups.minus›, _), [t1, t2]) =>
let
val rev_terms = rev terms
val zero = Const (\<^const_name>‹Groups.zero›, split_type)
val d = Bound 0
val terms1 = map (subst_term [(split_term, zero)]) rev_terms
val terms2 = map (subst_term [(incr_boundvars 1 split_term, d)])
(map (incr_boundvars 1) rev_terms)
val t1' = incr_boundvars 1 t1
val t2' = incr_boundvars 1 t2
val t1_lt_t2 = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type --> HOLogic.boolT) $ t1 $ t2
val t1_eq_t2_plus_d = Const (\<^const_name>‹HOL.eq›, split_type --> split_type --> HOLogic.boolT) $ t1' $
(Const (\<^const_name>‹Groups.plus›,
split_type --> split_type --> split_type) $ t2' $ d)
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t1_lt_t2) :: terms1 @ [not_false]
val subgoal2 = (HOLogic.mk_Trueprop t1_eq_t2_plus_d) :: terms2 @ [not_false]
in
SOME [(Ts, subgoal1), (split_type :: Ts, subgoal2)]
end
| (Const ("Int.nat", _), [t1]) =>
let
val rev_terms = rev terms
val zero_int = Const (\<^const_name>‹Groups.zero›, HOLogic.intT)
val zero_nat = Const (\<^const_name>‹Groups.zero›, HOLogic.natT)
val n = Bound 0
val terms1 = map (subst_term [(incr_boundvars 1 split_term, n)])
(map (incr_boundvars 1) rev_terms)
val terms2 = map (subst_term [(split_term, zero_nat)]) rev_terms
val t1' = incr_boundvars 1 t1
val t1_eq_nat_n = Const (\<^const_name>‹HOL.eq›, HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1' $
(Const (\<^const_name>‹of_nat›, HOLogic.natT --> HOLogic.intT) $ n)
val t1_lt_zero = Const (\<^const_name>‹Orderings.less›,
HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $ t1 $ zero_int
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t1_eq_nat_n) :: terms1 @ [not_false]
val subgoal2 = (HOLogic.mk_Trueprop t1_lt_zero) :: terms2 @ [not_false]
in
SOME [(HOLogic.natT :: Ts, subgoal1), (Ts, subgoal2)]
end
| (Const (\<^const_name>‹Rings.modulo›, Type ("fun", [\<^typ>‹nat›, _])), [t1, t2]) =>
let
val rev_terms = rev terms
val zero = Const (\<^const_name>‹Groups.zero›, split_type)
val i = Bound 1
val j = Bound 0
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
val terms2 = map (subst_term [(incr_boundvars 2 split_term, j)])
(map (incr_boundvars 2) rev_terms)
val t1' = incr_boundvars 2 t1
val t2' = incr_boundvars 2 t2
val t2_eq_zero = Const (\<^const_name>‹HOL.eq›,
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
val t2_neq_zero = HOLogic.mk_not (Const (\<^const_name>‹HOL.eq›,
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
val j_lt_t2 = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
val t1_eq_t2_times_i_plus_j = Const (\<^const_name>‹HOL.eq›, split_type --> split_type --> HOLogic.boolT) $ t1' $
(Const (\<^const_name>‹Groups.plus›, split_type --> split_type --> split_type) $
(Const (\<^const_name>‹Groups.times›,
split_type --> split_type --> split_type) $ t2' $ i) $ j)
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
val subgoal2 = (map HOLogic.mk_Trueprop
[t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
@ terms2 @ [not_false]
in
SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
end
| (Const (\<^const_name>‹Rings.divide›, Type ("fun", [\<^typ>‹nat›, _])), [t1, t2]) =>
let
val rev_terms = rev terms
val zero = Const (\<^const_name>‹Groups.zero›, split_type)
val i = Bound 1
val j = Bound 0
val terms1 = map (subst_term [(split_term, zero)]) rev_terms
val terms2 = map (subst_term [(incr_boundvars 2 split_term, i)])
(map (incr_boundvars 2) rev_terms)
val t1' = incr_boundvars 2 t1
val t2' = incr_boundvars 2 t2
val t2_eq_zero = Const (\<^const_name>‹HOL.eq›,
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
val t2_neq_zero = HOLogic.mk_not (Const (\<^const_name>‹HOL.eq›,
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero)
val j_lt_t2 = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
val t1_eq_t2_times_i_plus_j = Const (\<^const_name>‹HOL.eq›, split_type --> split_type --> HOLogic.boolT) $ t1' $
(Const (\<^const_name>‹Groups.plus›, split_type --> split_type --> split_type) $
(Const (\<^const_name>‹Groups.times›,
split_type --> split_type --> split_type) $ t2' $ i) $ j)
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
val subgoal2 = (map HOLogic.mk_Trueprop
[t2_neq_zero, j_lt_t2, t1_eq_t2_times_i_plus_j])
@ terms2 @ [not_false]
in
SOME [(Ts, subgoal1), (split_type :: split_type :: Ts, subgoal2)]
end
| (Const (\<^const_name>‹Rings.modulo›,
Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
let
val rev_terms = rev terms
val zero = Const (\<^const_name>‹Groups.zero›, split_type)
val i = Bound 1
val j = Bound 0
val terms1 = map (subst_term [(split_term, t1)]) rev_terms
val terms2_3 = map (subst_term [(incr_boundvars 2 split_term, j)])
(map (incr_boundvars 2) rev_terms)
val t1' = incr_boundvars 2 t1
val t2' = incr_boundvars 2 t2
val t2_eq_zero = Const (\<^const_name>‹HOL.eq›,
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
val zero_lt_t2 = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
val t2_lt_zero = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
val zero_leq_j = Const (\<^const_name>‹Orderings.less_eq›,
split_type --> split_type --> HOLogic.boolT) $ zero $ j
val j_leq_zero = Const (\<^const_name>‹Orderings.less_eq›,
split_type --> split_type --> HOLogic.boolT) $ j $ zero
val j_lt_t2 = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
val t2_lt_j = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type--> HOLogic.boolT) $ t2' $ j
val t1_eq_t2_times_i_plus_j = Const (\<^const_name>‹HOL.eq›, split_type --> split_type --> HOLogic.boolT) $ t1' $
(Const (\<^const_name>‹Groups.plus›, split_type --> split_type --> split_type) $
(Const (\<^const_name>‹Groups.times›,
split_type --> split_type --> split_type) $ t2' $ i) $ j)
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
val subgoal2 = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
@ hd terms2_3
:: (if tl terms2_3 = [] then [not_false] else [])
@ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
val subgoal3 = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
@ hd terms2_3
:: (if tl terms2_3 = [] then [not_false] else [])
@ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
val Ts' = split_type :: split_type :: Ts
in
SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
end
| (Const (\<^const_name>‹Rings.divide›,
Type ("fun", [Type ("Int.int", []), _])), [t1, t2]) =>
let
val rev_terms = rev terms
val zero = Const (\<^const_name>‹Groups.zero›, split_type)
val i = Bound 1
val j = Bound 0
val terms1 = map (subst_term [(split_term, zero)]) rev_terms
val terms2_3 = map (subst_term [(incr_boundvars 2 split_term, i)])
(map (incr_boundvars 2) rev_terms)
val t1' = incr_boundvars 2 t1
val t2' = incr_boundvars 2 t2
val t2_eq_zero = Const (\<^const_name>‹HOL.eq›,
split_type --> split_type --> HOLogic.boolT) $ t2 $ zero
val zero_lt_t2 = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type --> HOLogic.boolT) $ zero $ t2'
val t2_lt_zero = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type --> HOLogic.boolT) $ t2' $ zero
val zero_leq_j = Const (\<^const_name>‹Orderings.less_eq›,
split_type --> split_type --> HOLogic.boolT) $ zero $ j
val j_leq_zero = Const (\<^const_name>‹Orderings.less_eq›,
split_type --> split_type --> HOLogic.boolT) $ j $ zero
val j_lt_t2 = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type--> HOLogic.boolT) $ j $ t2'
val t2_lt_j = Const (\<^const_name>‹Orderings.less›,
split_type --> split_type--> HOLogic.boolT) $ t2' $ j
val t1_eq_t2_times_i_plus_j = Const (\<^const_name>‹HOL.eq›, split_type --> split_type --> HOLogic.boolT) $ t1' $
(Const (\<^const_name>‹Groups.plus›, split_type --> split_type --> split_type) $
(Const (\<^const_name>‹Groups.times›,
split_type --> split_type --> split_type) $ t2' $ i) $ j)
val not_false = HOLogic.mk_Trueprop (HOLogic.Not $ \<^term>‹False›)
val subgoal1 = (HOLogic.mk_Trueprop t2_eq_zero) :: terms1 @ [not_false]
val subgoal2 = (map HOLogic.mk_Trueprop [zero_lt_t2, zero_leq_j])
@ hd terms2_3
:: (if tl terms2_3 = [] then [not_false] else [])
@ (map HOLogic.mk_Trueprop [j_lt_t2, t1_eq_t2_times_i_plus_j])
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
val subgoal3 = (map HOLogic.mk_Trueprop [t2_lt_zero, t2_lt_j])
@ hd terms2_3
:: (if tl terms2_3 = [] then [not_false] else [])
@ (map HOLogic.mk_Trueprop [j_leq_zero, t1_eq_t2_times_i_plus_j])
@ (if tl terms2_3 = [] then [] else tl terms2_3 @ [not_false])
val Ts' = split_type :: split_type :: Ts
in
SOME [(Ts, subgoal1), (Ts', subgoal2), (Ts', subgoal3)]
end
| (t, ts) =>
(if Context_Position.is_visible ctxt then
warning ("Lin. Arith.: split rule for " ^ Syntax.string_of_term ctxt t ^
" (with " ^ string_of_int (length ts) ^
" argument(s)) not implemented; proof reconstruction is likely to fail")
else (); NONE))
end;
fun filter_prems_tac_items (p : term -> bool) (terms : term list) : term list =
let
fun filter_prems t (left, right) =
if p t then (left, right @ [t]) else (left @ right, [])
val (left, right) = fold filter_prems terms ([], [])
in
right @ left
end;
fun negated_term_occurs_positively (terms : term list) : bool =
exists
(fn (Trueprop $ (Const (\<^const_name>‹Not›, _) $ t)) =>
member Envir.aeconv terms (Trueprop $ t)
| _ => false)
terms;
fun pre_decomp ctxt (Ts : typ list, terms : term list) : (typ list * term list) list =
let
fun split_loop ([] : (typ list * term list) list) = ([] : (typ list * term list) list)
| split_loop (subgoal::subgoals) =
(case split_once_items ctxt subgoal of
SOME new_subgoals => split_loop (new_subgoals @ subgoals)
| NONE => subgoal :: split_loop subgoals)
fun is_relevant t = is_some (decomp ctxt t)
val relevant_terms = filter_prems_tac_items is_relevant terms
val split_goals = split_loop [(Ts, relevant_terms)]
val beta_eta_norm = map (apsnd (map (Envir.eta_contract o Envir.beta_norm)))
split_goals
val result = filter_out (negated_term_occurs_positively o snd) beta_eta_norm
in
result
end;
fun split_once_tac ctxt split_thms =
let
val thy = Proof_Context.theory_of ctxt
val cond_split_tac = SUBGOAL (fn (subgoal, i) =>
let
val Ts = rev (map snd (Logic.strip_params subgoal))
val concl = HOLogic.dest_Trueprop (Logic.strip_assums_concl subgoal)
val cmap = Splitter.cmap_of_split_thms split_thms
val splits = Splitter.split_posns cmap thy Ts concl
in
if null splits orelse length splits > Config.get ctxt split_limit then
no_tac
else if null (#2 (hd splits)) then
split_tac ctxt split_thms i
else
no_tac
end)
in
EVERY' [
REPEAT_DETERM o eresolve_tac ctxt [rev_mp],
cond_split_tac,
resolve_tac ctxt @{thms ccontr},
prem_nnf_tac ctxt,
TRY o REPEAT_ALL_NEW
(DETERM o (eresolve_tac ctxt [conjE, exE] ORELSE' eresolve_tac ctxt [disjE]))
]
end;
fun pre_tac ctxt i =
let
val split_thms = filter (is_split_thm ctxt) (get_splits ctxt)
fun is_relevant t = is_some (decomp ctxt t)
in
DETERM (
TRY (filter_prems_tac ctxt is_relevant i)
THEN (
(TRY o REPEAT_ALL_NEW (split_once_tac ctxt split_thms))
THEN_ALL_NEW
(CONVERSION Drule.beta_eta_conversion
THEN'
(TRY o (eresolve_tac ctxt [notE] THEN' eq_assume_tac)))
) i
)
end;
end;
val pre_tac = LA_Data.pre_tac;
structure Fast_Arith = Fast_Lin_Arith(structure LA_Logic = LA_Logic and LA_Data = LA_Data);
val add_inj_thms = Fast_Arith.add_inj_thms;
val add_lessD = Fast_Arith.add_lessD;
val add_simps = Fast_Arith.add_simps;
val add_simprocs = Fast_Arith.add_simprocs;
val set_number_of = Fast_Arith.set_number_of;
val simple_tac = Fast_Arith.lin_arith_tac;
val init_arith_data =
Fast_Arith.map_data (fn {add_mono_thms, mult_mono_thms, inj_thms, lessD, neqE, number_of, ...} =>
{add_mono_thms =
map Thm.trim_context @{thms add_mono_thms_linordered_semiring add_mono_thms_linordered_field}
@ add_mono_thms,
mult_mono_thms =
map Thm.trim_context
(@{thms mult_strict_left_mono mult_left_mono} @
[@{lemma "a = b ==> c * a = c * b" by (rule arg_cong)}]) @ mult_mono_thms,
inj_thms = inj_thms,
lessD = lessD,
neqE = map Thm.trim_context @{thms linorder_neqE_nat linorder_neqE_linordered_idom} @ neqE,
simpset =
put_simpset HOL_basic_ss \<^context> |> Simplifier.add_cong @{thm if_weak_cong} |> simpset_of,
number_of = number_of});
fun add_arith_facts ctxt =
Simplifier.add_prems (rev (Named_Theorems.get ctxt \<^named_theorems>‹arith›)) ctxt;
val simproc = add_arith_facts #> Fast_Arith.lin_arith_simproc;
fun refute_tac ctxt test prep_tac ref_tac =
let val refute_prems_tac =
REPEAT_DETERM
(eresolve_tac ctxt [@{thm conjE}, @{thm exE}] 1 ORELSE
filter_prems_tac ctxt test 1 ORELSE
eresolve_tac ctxt @{thms disjE} 1) THEN
(DETERM (eresolve_tac ctxt @{thms notE} 1 THEN eq_assume_tac 1) ORELSE
ref_tac 1);
in EVERY'[TRY o filter_prems_tac ctxt test,
REPEAT_DETERM o eresolve_tac ctxt @{thms rev_mp}, prep_tac,
resolve_tac ctxt @{thms ccontr}, prem_nnf_tac ctxt,
SELECT_GOAL (DEPTH_SOLVE refute_prems_tac)]
end;
local
fun raw_tac ctxt =
refute_tac ctxt (K true)
(REPEAT_DETERM o split_tac ctxt (get_splits ctxt))
(Fast_Arith.lin_arith_tac ctxt);
in
fun tac ctxt =
FIRST' [simple_tac ctxt,
Object_Logic.full_atomize_tac ctxt THEN'
(REPEAT_DETERM o resolve_tac ctxt [impI]) THEN' raw_tac ctxt];
end;
val global_setup =
map_theory_simpset (fn ctxt => ctxt
addSolver (mk_solver "lin_arith" (add_arith_facts #> Fast_Arith.prems_lin_arith_tac))) #>
Attrib.setup \<^binding>‹linarith_split› (Scan.succeed (Thm.declaration_attribute add_split))
"declaration of split rules for arithmetic procedure" #>
Method.setup \<^binding>‹linarith›
(Scan.succeed (fn ctxt =>
METHOD (fn facts =>
HEADGOAL
(Method.insert_tac ctxt
(rev (Named_Theorems.get ctxt \<^named_theorems>‹arith›) @ facts)
THEN' tac ctxt)))) "linear arithmetic" #>
Arith_Data.add_tactic "linear arithmetic" tac;
end;