src/HOL/Real/ferrante_rackoff.ML
author chaieb
Mon, 11 Jun 2007 11:06:15 +0200
changeset 23318 6d68b07ab5cf
parent 22578 b0eb5652f210
child 23346 1517207ec8b9
permissions -rw-r--r--
tuned tactic

(*
    ID:         $Id$
    Author:     Amine Chaieb, TU Muenchen

Ferrante and Rackoff Algorithm.
*)

signature FERRANTE_RACKOFF =
sig
  val ferrack_tac: bool -> int -> tactic
  val setup: theory -> theory
  val trace: bool ref
end;

structure Ferrante_Rackoff : FERRANTE_RACKOFF =
struct

val trace = ref false;
fun trace_msg s = if !trace then tracing s else ();

val binarith = map thm ["Pls_0_eq", "Min_1_eq", "pred_Pls", "pred_Min","pred_1","pred_0",
  "succ_Pls", "succ_Min", "succ_1", "succ_0",
  "add_Pls", "add_Min", "add_BIT_0", "add_BIT_10", "add_BIT_11",
  "minus_Pls", "minus_Min", "minus_1", "minus_0",
  "mult_Pls", "mult_Min", "mult_1", "mult_0", 
  "add_Pls_right", "add_Min_right"];

val comp_arith = binarith @ arith_simps @ simp_thms

fun prepare_for_linr q fm = 
  let
    val ps = Logic.strip_params fm
    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    fun mk_all ((s, T), (P, n)) =
      if 0 mem loose_bnos P then
        (HOLogic.all_const T $ Abs (s, T, P), n)
      else (incr_boundvars ~1 P, n-1)
    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    val rhs = hs;
    val np = length ps;
    val (fm', np) = Library.foldr mk_all (ps, (Library.foldr HOLogic.mk_imp (rhs, c), np));
    val (vs, _) = List.partition (fn t => q orelse (type_of t) = HOLogic.natT)
      (term_frees fm' @ term_vars fm');
    val fm2 = Library.foldr mk_all2 (vs, fm');
  in (fm2, np + length vs, length rhs) end;

fun spec_step n th = if n = 0 then th else spec_step (n - 1) th RS spec ;
fun mp_step n th = if n = 0 then th else mp_step (n - 1) th RS mp;

val context_ss = simpset_of (the_context ());

fun ferrack_tac q i =  ObjectLogic.atomize_tac i
  THEN REPEAT_DETERM (split_tac [@{thm split_min}, @{thm split_max}, abs_split] i)
  THEN (fn st =>
  let
    val g = nth (prems_of st) (i - 1)
    val thy = Thm.theory_of_thm st
    (* Transform the term*)
    val (t,np,nh) = prepare_for_linr q g
    (* Some simpsets for dealing with mod div abs and nat*)
    val simpset0 = HOL_basic_ss addsimps comp_arith addsplits [@{thm split_min}, @{thm split_max}]
    (* simp rules for elimination of abs *)
    val simpset3 = HOL_basic_ss addsplits [abs_split]
    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
    (* Theorem for the nat --> int transformation *)
    val pre_thm = Seq.hd (EVERY
      [simp_tac simpset0 1, TRY (simp_tac context_ss 1)]
      (trivial ct))
    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
    (* The result of the quantifier elimination *)
    val (th, tac) = case (prop_of pre_thm) of
        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
    let val pth = Ferrante_Rackoff_Proof.qelim (cterm_of thy (Pattern.eta_long [] t1))
    in 
          (trace_msg ("calling procedure with term:\n" ^
             Sign.string_of_term thy t1);
           ((pth RS iffD2) RS pre_thm,
            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
    end
      | _ => (pre_thm, assm_tac i)
  in (rtac (((mp_step nh) o (spec_step np)) th) i 
      THEN tac) st
  end handle Subscript => no_tac st | Ferrante_Rackoff_Proof.FAILURE _ => no_tac st);

val ferrack_meth =
  let
    val parse_flag =  Args.$$$ "no_quantify" >> (K (K false));
  in
    Method.simple_args 
      (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
        curry (Library.foldl op |>) true)
      (fn q => K (Method.SIMPLE_METHOD' (ferrack_tac q)))
  end;

val setup =
  Method.add_method ("ferrack", ferrack_meth,
    "LCF-proof-producing decision procedure for linear real arithmetic");

end;