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