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