src/HOL/Decision_Procs/mir_tac.ML
author wenzelm
Mon May 25 12:46:14 2009 +0200 (2009-05-25)
changeset 31240 2c20bcd70fbe
parent 30939 207ec81543f6
child 31790 05c92381363c
permissions -rw-r--r--
proper signature constraints;
modernized method setup;
hoelzl@30439
     1
(*  Title:      HOL/Decision_Procs/mir_tac.ML
haftmann@23858
     2
    Author:     Amine Chaieb, TU Muenchen
haftmann@23858
     3
*)
haftmann@23858
     4
wenzelm@31240
     5
signature MIR_TAC =
wenzelm@31240
     6
sig
wenzelm@31240
     7
  val trace: bool ref
wenzelm@31240
     8
  val mir_tac: Proof.context -> bool -> int -> tactic
wenzelm@31240
     9
  val setup: theory -> theory
wenzelm@31240
    10
end
wenzelm@31240
    11
haftmann@29788
    12
structure Mir_Tac =
chaieb@23264
    13
struct
chaieb@23264
    14
chaieb@23264
    15
val trace = ref false;
chaieb@23264
    16
fun trace_msg s = if !trace then tracing s else ();
chaieb@23264
    17
chaieb@23264
    18
val mir_ss = 
chaieb@23264
    19
let val ths = map thm ["real_of_int_inject", "real_of_int_less_iff", "real_of_int_le_iff"]
chaieb@23264
    20
in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
chaieb@23264
    21
end;
chaieb@23264
    22
chaieb@23264
    23
val nT = HOLogic.natT;
chaieb@23264
    24
  val nat_arith = map thm ["add_nat_number_of", "diff_nat_number_of", 
chaieb@23264
    25
                       "mult_nat_number_of", "eq_nat_number_of", "less_nat_number_of"];
chaieb@23264
    26
chaieb@23264
    27
  val comp_arith = (map thm ["Let_def", "if_False", "if_True", "add_0", 
chaieb@23264
    28
                 "add_Suc", "add_number_of_left", "mult_number_of_left", 
chaieb@23264
    29
                 "Suc_eq_add_numeral_1"])@
chaieb@23264
    30
                 (map (fn s => thm s RS sym) ["numeral_1_eq_1", "numeral_0_eq_0"])
haftmann@25481
    31
                 @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
chaieb@23264
    32
  val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
chaieb@23264
    33
             @{thm "real_of_nat_number_of"},
chaieb@23264
    34
             @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
chaieb@23264
    35
             @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
chaieb@23264
    36
             @{thm "Ring_and_Field.divide_zero"}, 
chaieb@23264
    37
             @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
chaieb@23264
    38
             @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
chaieb@23264
    39
             @{thm "diff_def"}, @{thm "minus_divide_left"}]
chaieb@23264
    40
val comp_ths = ths @ comp_arith @ simp_thms 
chaieb@23264
    41
chaieb@23264
    42
chaieb@23264
    43
val zdvd_int = @{thm "zdvd_int"};
chaieb@23264
    44
val zdiff_int_split = @{thm "zdiff_int_split"};
chaieb@23264
    45
val all_nat = @{thm "all_nat"};
chaieb@23264
    46
val ex_nat = @{thm "ex_nat"};
chaieb@23264
    47
val number_of1 = @{thm "number_of1"};
chaieb@23264
    48
val number_of2 = @{thm "number_of2"};
chaieb@23264
    49
val split_zdiv = @{thm "split_zdiv"};
chaieb@23264
    50
val split_zmod = @{thm "split_zmod"};
chaieb@23264
    51
val mod_div_equality' = @{thm "mod_div_equality'"};
chaieb@23264
    52
val split_div' = @{thm "split_div'"};
chaieb@23264
    53
val Suc_plus1 = @{thm "Suc_plus1"};
chaieb@23264
    54
val imp_le_cong = @{thm "imp_le_cong"};
chaieb@23264
    55
val conj_le_cong = @{thm "conj_le_cong"};
nipkow@30224
    56
val mod_add_eq = @{thm "mod_add_eq"} RS sym;
nipkow@30034
    57
val mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
nipkow@30034
    58
val mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
chaieb@23264
    59
val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
chaieb@23264
    60
val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
chaieb@23264
    61
val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
chaieb@23264
    62
val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
chaieb@23264
    63
haftmann@27456
    64
fun prepare_for_mir thy q fm = 
chaieb@23264
    65
  let
chaieb@23264
    66
    val ps = Logic.strip_params fm
chaieb@23264
    67
    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
chaieb@23264
    68
    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
chaieb@23264
    69
    fun mk_all ((s, T), (P,n)) =
chaieb@23264
    70
      if 0 mem loose_bnos P then
chaieb@23264
    71
        (HOLogic.all_const T $ Abs (s, T, P), n)
chaieb@23264
    72
      else (incr_boundvars ~1 P, n-1)
chaieb@23264
    73
    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
chaieb@23264
    74
      val rhs = hs
chaieb@23264
    75
(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
chaieb@23264
    76
    val np = length ps
chaieb@23264
    77
    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
chaieb@23264
    78
      (foldr HOLogic.mk_imp c rhs, np) ps
chaieb@23264
    79
    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
wenzelm@29265
    80
      (OldTerm.term_frees fm' @ OldTerm.term_vars fm');
chaieb@23264
    81
    val fm2 = foldr mk_all2 fm' vs
chaieb@23264
    82
  in (fm2, np + length vs, length rhs) end;
chaieb@23264
    83
chaieb@23264
    84
(*Object quantifier to meta --*)
chaieb@23264
    85
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
chaieb@23264
    86
chaieb@23264
    87
(* object implication to meta---*)
chaieb@23264
    88
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
chaieb@23264
    89
chaieb@23264
    90
chaieb@23264
    91
fun mir_tac ctxt q i = 
wenzelm@31240
    92
    ObjectLogic.atomize_prems_tac i
wenzelm@31240
    93
        THEN simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps simp_thms) i
wenzelm@31240
    94
        THEN REPEAT_DETERM (split_tac [@{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}] i)
wenzelm@28290
    95
        THEN (fn st =>
chaieb@23264
    96
  let
chaieb@23264
    97
    val g = List.nth (prems_of st, i - 1)
haftmann@27456
    98
    val thy = ProofContext.theory_of ctxt
chaieb@23264
    99
    (* Transform the term*)
haftmann@27456
   100
    val (t,np,nh) = prepare_for_mir thy q g
chaieb@23264
   101
    (* Some simpsets for dealing with mod div abs and nat*)
chaieb@23264
   102
    val mod_div_simpset = HOL_basic_ss 
nipkow@30224
   103
                        addsimps [refl, mod_add_eq, 
wenzelm@28290
   104
                                  @{thm "mod_self"}, @{thm "zmod_self"},
wenzelm@28290
   105
                                  @{thm "zdiv_zero"},@{thm "zmod_zero"},@{thm "div_0"}, @{thm "mod_0"},
nipkow@30031
   106
                                  @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, @{thm "mod_1"},
wenzelm@28290
   107
                                  @{thm "Suc_plus1"}]
wenzelm@28290
   108
                        addsimps @{thms add_ac}
haftmann@30939
   109
                        addsimprocs [cancel_div_mod_nat_proc, cancel_div_mod_int_proc]
chaieb@23264
   110
    val simpset0 = HOL_basic_ss
chaieb@23264
   111
      addsimps [mod_div_equality', Suc_plus1]
chaieb@23318
   112
      addsimps comp_ths
chaieb@23264
   113
      addsplits [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"}, @{thm "split_min"}, @{thm "split_max"}]
chaieb@23264
   114
    (* Simp rules for changing (n::int) to int n *)
chaieb@23264
   115
    val simpset1 = HOL_basic_ss
chaieb@23381
   116
      addsimps [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}] @ map (fn r => r RS sym)
chaieb@23381
   117
        [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
chaieb@23381
   118
         @{thm "zmult_int"}]
chaieb@23381
   119
      addsplits [@{thm "zdiff_int_split"}]
chaieb@23264
   120
    (*simp rules for elimination of int n*)
chaieb@23264
   121
chaieb@23264
   122
    val simpset2 = HOL_basic_ss
chaieb@23381
   123
      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"}, 
chaieb@23381
   124
                @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}]
chaieb@23381
   125
      addcongs [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
chaieb@23264
   126
    (* simp rules for elimination of abs *)
haftmann@27456
   127
    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
chaieb@23264
   128
    (* Theorem for the nat --> int transformation *)
chaieb@23264
   129
    val pre_thm = Seq.hd (EVERY
chaieb@23264
   130
      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
chaieb@23264
   131
       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), TRY (simp_tac mir_ss 1)]
chaieb@23264
   132
      (trivial ct))
chaieb@23264
   133
    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
chaieb@23264
   134
    (* The result of the quantifier elimination *)
chaieb@23264
   135
    val (th, tac) = case (prop_of pre_thm) of
chaieb@23264
   136
        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
wenzelm@28290
   137
    let val pth =
chaieb@23264
   138
          (* If quick_and_dirty then run without proof generation as oracle*)
wenzelm@28290
   139
             if !quick_and_dirty
wenzelm@28290
   140
             then mirfr_oracle (false, cterm_of thy (Pattern.eta_long [] t1))
wenzelm@28290
   141
             else mirfr_oracle (true, cterm_of thy (Pattern.eta_long [] t1))
chaieb@23264
   142
    in 
chaieb@23264
   143
          (trace_msg ("calling procedure with term:\n" ^
wenzelm@26939
   144
             Syntax.string_of_term ctxt t1);
chaieb@23264
   145
           ((pth RS iffD2) RS pre_thm,
chaieb@23264
   146
            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
chaieb@23264
   147
    end
chaieb@23264
   148
      | _ => (pre_thm, assm_tac i)
chaieb@23264
   149
  in (rtac (((mp_step nh) o (spec_step np)) th) i 
chaieb@23264
   150
      THEN tac) st
haftmann@27456
   151
  end handle Subscript => no_tac st);
chaieb@23264
   152
chaieb@23264
   153
val setup =
wenzelm@31240
   154
  Method.setup @{binding mir}
wenzelm@31240
   155
    let
wenzelm@31240
   156
      val parse_flag = Args.$$$ "no_quantify" >> K (K false)
wenzelm@31240
   157
    in
wenzelm@31240
   158
      Scan.lift (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
wenzelm@31240
   159
        curry (Library.foldl op |>) true) >>
wenzelm@31240
   160
      (fn q => fn ctxt => SIMPLE_METHOD' (mir_tac ctxt q))
wenzelm@31240
   161
    end
wenzelm@31240
   162
    "decision procedure for MIR arithmetic";
chaieb@23264
   163
wenzelm@23590
   164
end