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