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