src/HOL/Decision_Procs/mir_tac.ML
author nipkow
Wed Jun 24 09:41:14 2009 +0200 (2009-06-24)
changeset 31790 05c92381363c
parent 31240 2c20bcd70fbe
child 32740 9dd0a2f83429
permissions -rw-r--r--
corrected and unified thm names
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", 
nipkow@31790
    29
                 "Suc_eq_plus1"])@
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 imp_le_cong = @{thm "imp_le_cong"};
chaieb@23264
    54
val conj_le_cong = @{thm "conj_le_cong"};
nipkow@30224
    55
val mod_add_eq = @{thm "mod_add_eq"} RS sym;
nipkow@30034
    56
val mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
nipkow@30034
    57
val mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
chaieb@23264
    58
val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
chaieb@23264
    59
val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
chaieb@23264
    60
val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
chaieb@23264
    61
val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
chaieb@23264
    62
haftmann@27456
    63
fun prepare_for_mir thy q fm = 
chaieb@23264
    64
  let
chaieb@23264
    65
    val ps = Logic.strip_params fm
chaieb@23264
    66
    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
chaieb@23264
    67
    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
chaieb@23264
    68
    fun mk_all ((s, T), (P,n)) =
chaieb@23264
    69
      if 0 mem loose_bnos P then
chaieb@23264
    70
        (HOLogic.all_const T $ Abs (s, T, P), n)
chaieb@23264
    71
      else (incr_boundvars ~1 P, n-1)
chaieb@23264
    72
    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
chaieb@23264
    73
      val rhs = hs
chaieb@23264
    74
(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
chaieb@23264
    75
    val np = length ps
chaieb@23264
    76
    val (fm',np) =  foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
chaieb@23264
    77
      (foldr HOLogic.mk_imp c rhs, np) ps
chaieb@23264
    78
    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
wenzelm@29265
    79
      (OldTerm.term_frees fm' @ OldTerm.term_vars fm');
chaieb@23264
    80
    val fm2 = foldr mk_all2 fm' vs
chaieb@23264
    81
  in (fm2, np + length vs, length rhs) end;
chaieb@23264
    82
chaieb@23264
    83
(*Object quantifier to meta --*)
chaieb@23264
    84
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
chaieb@23264
    85
chaieb@23264
    86
(* object implication to meta---*)
chaieb@23264
    87
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
chaieb@23264
    88
chaieb@23264
    89
chaieb@23264
    90
fun mir_tac ctxt q i = 
wenzelm@31240
    91
    ObjectLogic.atomize_prems_tac i
wenzelm@31240
    92
        THEN simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps simp_thms) i
wenzelm@31240
    93
        THEN REPEAT_DETERM (split_tac [@{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}] i)
wenzelm@28290
    94
        THEN (fn st =>
chaieb@23264
    95
  let
chaieb@23264
    96
    val g = List.nth (prems_of st, i - 1)
haftmann@27456
    97
    val thy = ProofContext.theory_of ctxt
chaieb@23264
    98
    (* Transform the term*)
haftmann@27456
    99
    val (t,np,nh) = prepare_for_mir thy q g
chaieb@23264
   100
    (* Some simpsets for dealing with mod div abs and nat*)
chaieb@23264
   101
    val mod_div_simpset = HOL_basic_ss 
nipkow@30224
   102
                        addsimps [refl, mod_add_eq, 
wenzelm@28290
   103
                                  @{thm "mod_self"}, @{thm "zmod_self"},
wenzelm@28290
   104
                                  @{thm "zdiv_zero"},@{thm "zmod_zero"},@{thm "div_0"}, @{thm "mod_0"},
nipkow@30031
   105
                                  @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, @{thm "mod_1"},
nipkow@31790
   106
                                  @{thm "Suc_eq_plus1"}]
wenzelm@28290
   107
                        addsimps @{thms add_ac}
haftmann@30939
   108
                        addsimprocs [cancel_div_mod_nat_proc, cancel_div_mod_int_proc]
chaieb@23264
   109
    val simpset0 = HOL_basic_ss
nipkow@31790
   110
      addsimps [mod_div_equality', @{thm Suc_eq_plus1}]
chaieb@23318
   111
      addsimps comp_ths
chaieb@23264
   112
      addsplits [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"}, @{thm "split_min"}, @{thm "split_max"}]
chaieb@23264
   113
    (* Simp rules for changing (n::int) to int n *)
chaieb@23264
   114
    val simpset1 = HOL_basic_ss
chaieb@23381
   115
      addsimps [@{thm "nat_number_of_def"}, @{thm "zdvd_int"}] @ map (fn r => r RS sym)
chaieb@23381
   116
        [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
chaieb@23381
   117
         @{thm "zmult_int"}]
chaieb@23381
   118
      addsplits [@{thm "zdiff_int_split"}]
chaieb@23264
   119
    (*simp rules for elimination of int n*)
chaieb@23264
   120
chaieb@23264
   121
    val simpset2 = HOL_basic_ss
chaieb@23381
   122
      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm "number_of1"}, 
chaieb@23381
   123
                @{thm "number_of2"}, @{thm "int_0"}, @{thm "int_1"}]
chaieb@23381
   124
      addcongs [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
chaieb@23264
   125
    (* simp rules for elimination of abs *)
haftmann@27456
   126
    val ct = cterm_of thy (HOLogic.mk_Trueprop t)
chaieb@23264
   127
    (* Theorem for the nat --> int transformation *)
chaieb@23264
   128
    val pre_thm = Seq.hd (EVERY
chaieb@23264
   129
      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
chaieb@23264
   130
       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), TRY (simp_tac mir_ss 1)]
chaieb@23264
   131
      (trivial ct))
chaieb@23264
   132
    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
chaieb@23264
   133
    (* The result of the quantifier elimination *)
chaieb@23264
   134
    val (th, tac) = case (prop_of pre_thm) of
chaieb@23264
   135
        Const ("==>", _) $ (Const ("Trueprop", _) $ t1) $ _ =>
wenzelm@28290
   136
    let val pth =
chaieb@23264
   137
          (* If quick_and_dirty then run without proof generation as oracle*)
wenzelm@28290
   138
             if !quick_and_dirty
wenzelm@28290
   139
             then mirfr_oracle (false, cterm_of thy (Pattern.eta_long [] t1))
wenzelm@28290
   140
             else mirfr_oracle (true, cterm_of thy (Pattern.eta_long [] t1))
chaieb@23264
   141
    in 
chaieb@23264
   142
          (trace_msg ("calling procedure with term:\n" ^
wenzelm@26939
   143
             Syntax.string_of_term ctxt t1);
chaieb@23264
   144
           ((pth RS iffD2) RS pre_thm,
chaieb@23264
   145
            assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
chaieb@23264
   146
    end
chaieb@23264
   147
      | _ => (pre_thm, assm_tac i)
chaieb@23264
   148
  in (rtac (((mp_step nh) o (spec_step np)) th) i 
chaieb@23264
   149
      THEN tac) st
haftmann@27456
   150
  end handle Subscript => no_tac st);
chaieb@23264
   151
chaieb@23264
   152
val setup =
wenzelm@31240
   153
  Method.setup @{binding mir}
wenzelm@31240
   154
    let
wenzelm@31240
   155
      val parse_flag = Args.$$$ "no_quantify" >> K (K false)
wenzelm@31240
   156
    in
wenzelm@31240
   157
      Scan.lift (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
wenzelm@31240
   158
        curry (Library.foldl op |>) true) >>
wenzelm@31240
   159
      (fn q => fn ctxt => SIMPLE_METHOD' (mir_tac ctxt q))
wenzelm@31240
   160
    end
wenzelm@31240
   161
    "decision procedure for MIR arithmetic";
chaieb@23264
   162
wenzelm@23590
   163
end