src/HOL/Reflection/cooper_tac.ML
author haftmann
Tue Feb 03 16:50:41 2009 +0100 (2009-02-03)
changeset 29788 1b80ebe713a4
parent 29265 src/HOL/ex/coopertac.ML@5b4247055bd7
permissions -rw-r--r--
established session HOL-Reflection
haftmann@29788
     1
(*  Title:      HOL/Reflection/cooper_tac.ML
haftmann@29788
     2
    Author:     Amine Chaieb, TU Muenchen
haftmann@29788
     3
*)
haftmann@29788
     4
haftmann@29788
     5
structure Cooper_Tac =
chaieb@23274
     6
struct
chaieb@23274
     7
chaieb@23274
     8
val trace = ref false;
chaieb@23274
     9
fun trace_msg s = if !trace then tracing s else ();
chaieb@23274
    10
chaieb@23274
    11
val cooper_ss = @{simpset};
chaieb@23274
    12
chaieb@23274
    13
val nT = HOLogic.natT;
huffman@26075
    14
val binarith = @{thms normalize_bin_simps};
chaieb@23318
    15
val comp_arith = binarith @ simp_thms
chaieb@23274
    16
haftmann@27651
    17
val zdvd_int = @{thm zdvd_int};
haftmann@27651
    18
val zdiff_int_split = @{thm zdiff_int_split};
haftmann@27651
    19
val all_nat = @{thm all_nat};
haftmann@27651
    20
val ex_nat = @{thm ex_nat};
haftmann@27651
    21
val number_of1 = @{thm number_of1};
haftmann@27651
    22
val number_of2 = @{thm number_of2};
haftmann@27651
    23
val split_zdiv = @{thm split_zdiv};
haftmann@27651
    24
val split_zmod = @{thm split_zmod};
haftmann@27651
    25
val mod_div_equality' = @{thm mod_div_equality'};
haftmann@27651
    26
val split_div' = @{thm split_div'};
haftmann@27651
    27
val Suc_plus1 = @{thm Suc_plus1};
haftmann@27651
    28
val imp_le_cong = @{thm imp_le_cong};
haftmann@27651
    29
val conj_le_cong = @{thm conj_le_cong};
huffman@23469
    30
val nat_mod_add_eq = @{thm mod_add1_eq} RS sym;
huffman@23469
    31
val nat_mod_add_left_eq = @{thm mod_add_left_eq} RS sym;
huffman@23469
    32
val nat_mod_add_right_eq = @{thm mod_add_right_eq} RS sym;
haftmann@27651
    33
val int_mod_add_eq = @{thm zmod_zadd1_eq} RS sym;
haftmann@27651
    34
val int_mod_add_left_eq = @{thm zmod_zadd_left_eq} RS sym;
haftmann@27651
    35
val int_mod_add_right_eq = @{thm zmod_zadd_right_eq} RS sym;
haftmann@27651
    36
val nat_div_add_eq = @{thm div_add1_eq} RS sym;
haftmann@27651
    37
val int_div_add_eq = @{thm zdiv_zadd1_eq} RS sym;
chaieb@23274
    38
chaieb@23274
    39
fun prepare_for_linz q fm = 
chaieb@23274
    40
  let
chaieb@23274
    41
    val ps = Logic.strip_params fm
chaieb@23274
    42
    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
chaieb@23274
    43
    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
chaieb@23274
    44
    fun mk_all ((s, T), (P,n)) =
chaieb@23274
    45
      if 0 mem loose_bnos P then
chaieb@23274
    46
        (HOLogic.all_const T $ Abs (s, T, P), n)
chaieb@23274
    47
      else (incr_boundvars ~1 P, n-1)
chaieb@23274
    48
    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
haftmann@27651
    49
    val rhs = hs
chaieb@23274
    50
    val np = length ps
chaieb@23274
    51
    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
chaieb@23274
    52
      (foldr HOLogic.mk_imp c rhs, np) ps
chaieb@23274
    53
    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
wenzelm@29265
    54
      (OldTerm.term_frees fm' @ OldTerm.term_vars fm');
chaieb@23274
    55
    val fm2 = foldr mk_all2 fm' vs
chaieb@23274
    56
  in (fm2, np + length vs, length rhs) end;
chaieb@23274
    57
chaieb@23274
    58
(*Object quantifier to meta --*)
chaieb@23274
    59
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
chaieb@23274
    60
chaieb@23274
    61
(* object implication to meta---*)
chaieb@23274
    62
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
chaieb@23274
    63
chaieb@23274
    64
wenzelm@23590
    65
fun linz_tac ctxt q i = ObjectLogic.atomize_prems_tac i THEN (fn st =>
chaieb@23274
    66
  let
chaieb@23274
    67
    val g = List.nth (prems_of st, i - 1)
chaieb@23274
    68
    val thy = ProofContext.theory_of ctxt
chaieb@23274
    69
    (* Transform the term*)
chaieb@23274
    70
    val (t,np,nh) = prepare_for_linz q g
chaieb@23274
    71
    (* Some simpsets for dealing with mod div abs and nat*)
chaieb@23274
    72
    val mod_div_simpset = HOL_basic_ss 
chaieb@23274
    73
			addsimps [refl,nat_mod_add_eq, nat_mod_add_left_eq, 
chaieb@23274
    74
				  nat_mod_add_right_eq, int_mod_add_eq, 
chaieb@23274
    75
				  int_mod_add_right_eq, int_mod_add_left_eq,
chaieb@23274
    76
				  nat_div_add_eq, int_div_add_eq,
huffman@23469
    77
				  @{thm mod_self}, @{thm "zmod_self"},
haftmann@27651
    78
				  @{thm mod_by_0}, @{thm div_by_0},
chaieb@23274
    79
				  @{thm "zdiv_zero"}, @{thm "zmod_zero"}, @{thm "div_0"}, @{thm "mod_0"},
chaieb@23274
    80
				  @{thm "zdiv_1"}, @{thm "zmod_1"}, @{thm "div_1"}, @{thm "mod_1"},
chaieb@23274
    81
				  Suc_plus1]
haftmann@23880
    82
			addsimps @{thms add_ac}
chaieb@23274
    83
			addsimprocs [cancel_div_mod_proc]
chaieb@23274
    84
    val simpset0 = HOL_basic_ss
chaieb@23274
    85
      addsimps [mod_div_equality', Suc_plus1]
chaieb@23274
    86
      addsimps comp_arith
chaieb@23274
    87
      addsplits [split_zdiv, split_zmod, split_div', @{thm "split_min"}, @{thm "split_max"}]
chaieb@23274
    88
    (* Simp rules for changing (n::int) to int n *)
chaieb@23274
    89
    val simpset1 = HOL_basic_ss
chaieb@23274
    90
      addsimps [nat_number_of_def, zdvd_int] @ map (fn r => r RS sym)
huffman@23364
    91
        [@{thm int_int_eq}, @{thm zle_int}, @{thm zless_int}, @{thm zadd_int}, @{thm zmult_int}]
chaieb@23274
    92
      addsplits [zdiff_int_split]
chaieb@23274
    93
    (*simp rules for elimination of int n*)
chaieb@23274
    94
chaieb@23274
    95
    val simpset2 = HOL_basic_ss
huffman@23364
    96
      addsimps [@{thm nat_0_le}, @{thm all_nat}, @{thm ex_nat}, @{thm number_of1}, @{thm number_of2}, @{thm int_0}, @{thm int_1}]
huffman@23364
    97
      addcongs [@{thm conj_le_cong}, @{thm imp_le_cong}]
chaieb@23274
    98
    (* simp rules for elimination of abs *)
huffman@23364
    99
    val simpset3 = HOL_basic_ss addsplits [@{thm abs_split}]
chaieb@23274
   100
    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
chaieb@23274
   101
    (* Theorem for the nat --> int transformation *)
chaieb@23274
   102
    val pre_thm = Seq.hd (EVERY
chaieb@23274
   103
      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
chaieb@23274
   104
       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1),
chaieb@23274
   105
       TRY (simp_tac simpset3 1), TRY (simp_tac cooper_ss 1)]
chaieb@23274
   106
      (trivial ct))
chaieb@23274
   107
    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
chaieb@23274
   108
    (* The result of the quantifier elimination *)
chaieb@23274
   109
    val (th, tac) = case (prop_of pre_thm) of
chaieb@23274
   110
        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
wenzelm@28290
   111
    let val pth = linzqe_oracle (cterm_of thy (Pattern.eta_long [] t1))
chaieb@23274
   112
    in 
chaieb@23274
   113
          ((pth RS iffD2) RS pre_thm,
chaieb@23274
   114
            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i))
chaieb@23274
   115
    end
chaieb@23274
   116
      | _ => (pre_thm, assm_tac i)
chaieb@23274
   117
  in (rtac (((mp_step nh) o (spec_step np)) th) i 
chaieb@23274
   118
      THEN tac) st
chaieb@23274
   119
  end handle Subscript => no_tac st);
chaieb@23274
   120
chaieb@23274
   121
fun linz_args meth =
chaieb@23274
   122
 let val parse_flag = 
chaieb@23274
   123
         Args.$$$ "no_quantify" >> (K (K false));
chaieb@23274
   124
 in
chaieb@23274
   125
   Method.simple_args 
chaieb@23274
   126
  (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
chaieb@23274
   127
    curry (Library.foldl op |>) true)
chaieb@23274
   128
    (fn q => fn ctxt => meth ctxt q 1)
chaieb@23274
   129
  end;
chaieb@23274
   130
chaieb@23274
   131
fun linz_method ctxt q i = Method.METHOD (fn facts =>
chaieb@23274
   132
  Method.insert_tac facts 1 THEN linz_tac ctxt q i);
chaieb@23274
   133
chaieb@23274
   134
val setup =
chaieb@23274
   135
  Method.add_method ("cooper",
chaieb@23274
   136
     linz_args linz_method,
chaieb@23274
   137
     "decision procedure for linear integer arithmetic");
chaieb@23274
   138
wenzelm@23590
   139
end