src/HOL/Decision_Procs/ferrack_tac.ML
author huffman
Sun Apr 01 16:09:58 2012 +0200 (2012-04-01)
changeset 47255 30a1692557b0
parent 47142 d64fa2ca54b8
child 47432 e1576d13e933
permissions -rw-r--r--
removed Nat_Numeral.thy, moving all theorems elsewhere
     1 (*  Title:      HOL/Decision_Procs/ferrack_tac.ML
     2     Author:     Amine Chaieb, TU Muenchen
     3 *)
     4 
     5 signature FERRACK_TAC =
     6 sig
     7   val trace: bool Unsynchronized.ref
     8   val linr_tac: Proof.context -> bool -> int -> tactic
     9   val setup: theory -> theory
    10 end
    11 
    12 structure Ferrack_Tac =
    13 struct
    14 
    15 val trace = Unsynchronized.ref false;
    16 fun trace_msg s = if !trace then tracing s else ();
    17 
    18 val ferrack_ss = let val ths = [@{thm real_of_int_inject}, @{thm real_of_int_less_iff}, 
    19                                 @{thm real_of_int_le_iff}]
    20              in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
    21              end;
    22 
    23 val binarith = @{thms arith_simps}
    24 val comp_arith = binarith @ @{thms simp_thms}
    25 
    26 val zdvd_int = @{thm zdvd_int};
    27 val zdiff_int_split = @{thm zdiff_int_split};
    28 val all_nat = @{thm all_nat};
    29 val ex_nat = @{thm ex_nat};
    30 val split_zdiv = @{thm split_zdiv};
    31 val split_zmod = @{thm split_zmod};
    32 val mod_div_equality' = @{thm mod_div_equality'};
    33 val split_div' = @{thm split_div'};
    34 val Suc_eq_plus1 = @{thm Suc_eq_plus1};
    35 val imp_le_cong = @{thm imp_le_cong};
    36 val conj_le_cong = @{thm conj_le_cong};
    37 val mod_add_left_eq = @{thm mod_add_left_eq} RS sym;
    38 val mod_add_right_eq = @{thm mod_add_right_eq} RS sym;
    39 val nat_div_add_eq = @{thm div_add1_eq} RS sym;
    40 val int_div_add_eq = @{thm zdiv_zadd1_eq} RS sym;
    41 
    42 fun prepare_for_linr sg q fm = 
    43   let
    44     val ps = Logic.strip_params fm
    45     val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    46     val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    47     fun mk_all ((s, T), (P,n)) =
    48       if Term.is_dependent P then
    49         (HOLogic.all_const T $ Abs (s, T, P), n)
    50       else (incr_boundvars ~1 P, n-1)
    51     fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    52       val rhs = hs
    53 (*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
    54     val np = length ps
    55     val (fm',np) =  List.foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
    56       (List.foldr HOLogic.mk_imp c rhs, np) ps
    57     val (vs, _) = List.partition (fn t => q orelse (type_of t) = HOLogic.natT)
    58       (Misc_Legacy.term_frees fm' @ Misc_Legacy.term_vars fm');
    59     val fm2 = List.foldr mk_all2 fm' vs
    60   in (fm2, np + length vs, length rhs) end;
    61 
    62 (*Object quantifier to meta --*)
    63 fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
    64 
    65 (* object implication to meta---*)
    66 fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
    67 
    68 
    69 fun linr_tac ctxt q =
    70     Object_Logic.atomize_prems_tac
    71         THEN' (REPEAT_DETERM o split_tac [@{thm split_min}, @{thm split_max}, @{thm abs_split}])
    72         THEN' SUBGOAL (fn (g, i) =>
    73   let
    74     val thy = Proof_Context.theory_of ctxt
    75     (* Transform the term*)
    76     val (t,np,nh) = prepare_for_linr thy q g
    77     (* Some simpsets for dealing with mod div abs and nat*)
    78     val simpset0 = Simplifier.context ctxt HOL_basic_ss addsimps comp_arith
    79     val ct = cterm_of thy (HOLogic.mk_Trueprop t)
    80     (* Theorem for the nat --> int transformation *)
    81    val pre_thm = Seq.hd (EVERY
    82       [simp_tac simpset0 1,
    83        TRY (simp_tac (Simplifier.context ctxt ferrack_ss) 1)]
    84       (Thm.trivial ct))
    85     fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
    86     (* The result of the quantifier elimination *)
    87     val (th, tac) = case prop_of pre_thm of
    88         Const ("==>", _) $ (Const (@{const_name Trueprop}, _) $ t1) $ _ =>
    89     let val pth = linr_oracle (ctxt, Pattern.eta_long [] t1)
    90     in 
    91           (trace_msg ("calling procedure with term:\n" ^
    92              Syntax.string_of_term ctxt t1);
    93            ((pth RS iffD2) RS pre_thm,
    94             assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
    95     end
    96       | _ => (pre_thm, assm_tac i)
    97   in rtac ((mp_step nh o spec_step np) th) i THEN tac end);
    98 
    99 val setup =
   100   Method.setup @{binding rferrack}
   101     (Args.mode "no_quantify" >> (fn q => fn ctxt =>
   102       SIMPLE_METHOD' (linr_tac ctxt (not q))))
   103     "decision procedure for linear real arithmetic";
   104 
   105 end