src/HOL/Real/ferrante_rackoff.ML
changeset 19640 40ec89317425
child 19825 bb5357536621
equal deleted inserted replaced
19639:d9079a9ccbfb 19640:40ec89317425
       
     1 (*
       
     2     ID:         $Id$
       
     3     Author:     Amine Chaieb, TU Muenchen
       
     4 
       
     5 Ferrante and Rackoff Algorithm.
       
     6 *)
       
     7 
       
     8 structure Ferrante_Rackoff:
       
     9 sig
       
    10   val trace : bool ref
       
    11   val ferrack_tac : bool -> int -> tactic
       
    12   val setup : theory -> theory
       
    13 end =
       
    14 struct
       
    15 
       
    16 val trace = ref false;
       
    17 fun trace_msg s = if !trace then tracing s else ();
       
    18 
       
    19 val context_ss = simpset_of (the_context ());
       
    20 
       
    21 val nT = HOLogic.natT;
       
    22 val binarith = map thm
       
    23   ["Pls_0_eq", "Min_1_eq",
       
    24  "bin_pred_Pls","bin_pred_Min","bin_pred_1","bin_pred_0",
       
    25   "bin_succ_Pls", "bin_succ_Min", "bin_succ_1", "bin_succ_0",
       
    26   "bin_add_Pls", "bin_add_Min", "bin_add_BIT_0", "bin_add_BIT_10",
       
    27   "bin_add_BIT_11", "bin_minus_Pls", "bin_minus_Min", "bin_minus_1", 
       
    28   "bin_minus_0", "bin_mult_Pls", "bin_mult_Min", "bin_mult_1", "bin_mult_0", 
       
    29   "bin_add_Pls_right", "bin_add_Min_right"];
       
    30  val intarithrel = 
       
    31      (map thm ["int_eq_number_of_eq","int_neg_number_of_BIT", 
       
    32 		"int_le_number_of_eq","int_iszero_number_of_0",
       
    33 		"int_less_number_of_eq_neg"]) @
       
    34      (map (fn s => thm s RS thm "lift_bool") 
       
    35 	  ["int_iszero_number_of_Pls","int_iszero_number_of_1",
       
    36 	   "int_neg_number_of_Min"])@
       
    37      (map (fn s => thm s RS thm "nlift_bool") 
       
    38 	  ["int_nonzero_number_of_Min","int_not_neg_number_of_Pls"]);
       
    39      
       
    40 val intarith = map thm ["int_number_of_add_sym", "int_number_of_minus_sym",
       
    41 			"int_number_of_diff_sym", "int_number_of_mult_sym"];
       
    42 val natarith = map thm ["add_nat_number_of", "diff_nat_number_of",
       
    43 			"mult_nat_number_of", "eq_nat_number_of",
       
    44 			"less_nat_number_of"]
       
    45 val powerarith = 
       
    46     (map thm ["nat_number_of", "zpower_number_of_even", 
       
    47 	      "zpower_Pls", "zpower_Min"]) @ 
       
    48     [(Tactic.simplify true [thm "zero_eq_Numeral0_nring", 
       
    49 			   thm "one_eq_Numeral1_nring"] 
       
    50   (thm "zpower_number_of_odd"))]
       
    51 
       
    52 val comp_arith = binarith @ intarith @ intarithrel @ natarith 
       
    53 	    @ powerarith @[thm"not_false_eq_true", thm "not_true_eq_false"];
       
    54 
       
    55 fun prepare_for_linr sg q fm = 
       
    56   let
       
    57     val ps = Logic.strip_params fm
       
    58     val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
       
    59     val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
       
    60     fun mk_all ((s, T), (P,n)) =
       
    61       if 0 mem loose_bnos P then
       
    62         (HOLogic.all_const T $ Abs (s, T, P), n)
       
    63       else (incr_boundvars ~1 P, n-1)
       
    64     fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
       
    65       val rhs = hs
       
    66 (*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
       
    67     val np = length ps
       
    68     val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
       
    69       (foldr HOLogic.mk_imp c rhs, np) ps
       
    70     val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
       
    71       (term_frees fm' @ term_vars fm');
       
    72     val fm2 = foldr mk_all2 fm' vs
       
    73   in (fm2, np + length vs, length rhs) end;
       
    74 
       
    75 (*Object quantifier to meta --*)
       
    76 fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
       
    77 
       
    78 (* object implication to meta---*)
       
    79 fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
       
    80 
       
    81 
       
    82 fun ferrack_tac q i = ObjectLogic.atomize_tac i THEN (fn st =>
       
    83   let
       
    84     val g = List.nth (prems_of st, i - 1)
       
    85     val sg = sign_of_thm st
       
    86     (* Transform the term*)
       
    87     val (t,np,nh) = prepare_for_linr sg q g
       
    88     (* Some simpsets for dealing with mod div abs and nat*)
       
    89     val simpset0 = HOL_basic_ss addsimps comp_arith addsplits [split_min, split_max]
       
    90     (* simp rules for elimination of abs *)
       
    91     val simpset3 = HOL_basic_ss addsplits [abs_split]
       
    92     val ct = cterm_of sg (HOLogic.mk_Trueprop t)
       
    93     (* Theorem for the nat --> int transformation *)
       
    94     val pre_thm = Seq.hd (EVERY
       
    95       [simp_tac simpset0 1,
       
    96        TRY (simp_tac simpset3 1), TRY (simp_tac context_ss 1)]
       
    97       (trivial ct))
       
    98     fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
       
    99     (* The result of the quantifier elimination *)
       
   100     val (th, tac) = case (prop_of pre_thm) of
       
   101         Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
       
   102     let val pth = Ferrante_Rackoff_Proof.qelim (cterm_of sg (Pattern.eta_long [] t1))
       
   103     in 
       
   104           (trace_msg ("calling procedure with term:\n" ^
       
   105              Sign.string_of_term sg t1);
       
   106            ((pth RS iffD2) RS pre_thm,
       
   107             assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
       
   108     end
       
   109       | _ => (pre_thm, assm_tac i)
       
   110   in (rtac (((mp_step nh) o (spec_step np)) th) i 
       
   111       THEN tac) st
       
   112   end handle Subscript => no_tac st | Ferrante_Rackoff_Proof.FAILURE _ => no_tac st);
       
   113 
       
   114 fun ferrack_args meth =
       
   115  let val parse_flag = 
       
   116          Args.$$$ "no_quantify" >> (K (K false));
       
   117  in
       
   118    Method.simple_args 
       
   119   (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
       
   120     curry (Library.foldl op |>) true)
       
   121     (fn q => fn _ => meth q 1)
       
   122   end;
       
   123 
       
   124 val setup =
       
   125   Method.add_method ("ferrack",
       
   126      ferrack_args (Method.SIMPLE_METHOD oo ferrack_tac),
       
   127      "LCF-proof-producing decision procedure for linear real arithmetic");
       
   128 
       
   129 end