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