src/HOL/Decision_Procs/mir_tac.ML
author hoelzl
Fri Mar 22 10:41:43 2013 +0100 (2013-03-22)
changeset 51474 1e9e68247ad1
parent 51369 960b0ca9ae5d
child 51717 9e7d1c139569
permissions -rw-r--r--
generalize Bfun and Bseq to metric spaces; Bseq is an abbreviation for Bfun
     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 Unsynchronized.ref
     8   val mir_tac: Proof.context -> bool -> int -> tactic
     9 end
    10 
    11 structure Mir_Tac =
    12 struct
    13 
    14 val trace = Unsynchronized.ref false;
    15 fun trace_msg s = if !trace then tracing s else ();
    16 
    17 val mir_ss = 
    18 let val ths = [@{thm "real_of_int_inject"}, @{thm "real_of_int_less_iff"}, @{thm "real_of_int_le_iff"}]
    19 in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
    20 end;
    21 
    22 val nT = HOLogic.natT;
    23   val nat_arith = [@{thm diff_nat_numeral}];
    24 
    25   val comp_arith = [@{thm "Let_def"}, @{thm "if_False"}, @{thm "if_True"}, @{thm "add_0"},
    26                  @{thm "add_Suc"}, @{thm add_numeral_left}, @{thm mult_numeral_left(1)},
    27                  @{thm "Suc_eq_plus1"}] @
    28                  (map (fn th => th RS sym) [@{thm "numeral_1_eq_1"}])
    29                  @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
    30   val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
    31              @{thm real_of_nat_numeral},
    32              @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
    33              @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
    34              @{thm "divide_zero"}, 
    35              @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
    36              @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
    37              @{thm "diff_minus"}, @{thm "minus_divide_left"}]
    38 val comp_ths = ths @ comp_arith @ @{thms simp_thms};
    39 
    40 
    41 val zdvd_int = @{thm "zdvd_int"};
    42 val zdiff_int_split = @{thm "zdiff_int_split"};
    43 val all_nat = @{thm "all_nat"};
    44 val ex_nat = @{thm "ex_nat"};
    45 val split_zdiv = @{thm "split_zdiv"};
    46 val split_zmod = @{thm "split_zmod"};
    47 val mod_div_equality' = @{thm "mod_div_equality'"};
    48 val split_div' = @{thm "split_div'"};
    49 val imp_le_cong = @{thm "imp_le_cong"};
    50 val conj_le_cong = @{thm "conj_le_cong"};
    51 val mod_add_eq = @{thm "mod_add_eq"} RS sym;
    52 val mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
    53 val mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
    54 val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
    55 val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
    56 
    57 fun prepare_for_mir q fm = 
    58   let
    59     val ps = Logic.strip_params fm
    60     val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    61     val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    62     fun mk_all ((s, T), (P,n)) =
    63       if Term.is_dependent P then
    64         (HOLogic.all_const T $ Abs (s, T, P), n)
    65       else (incr_boundvars ~1 P, n-1)
    66     fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    67       val rhs = hs
    68 (*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
    69     val np = length ps
    70     val (fm',np) = List.foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
    71       (List.foldr HOLogic.mk_imp c rhs, np) ps
    72     val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
    73       (Misc_Legacy.term_frees fm' @ Misc_Legacy.term_vars fm');
    74     val fm2 = List.foldr mk_all2 fm' vs
    75   in (fm2, np + length vs, length rhs) end;
    76 
    77 (*Object quantifier to meta --*)
    78 fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
    79 
    80 (* object implication to meta---*)
    81 fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
    82 
    83 
    84 fun mir_tac ctxt q = 
    85     Object_Logic.atomize_prems_tac
    86         THEN' simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps @{thms simp_thms})
    87         THEN' (REPEAT_DETERM o split_tac [@{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}])
    88         THEN' SUBGOAL (fn (g, i) =>
    89   let
    90     val thy = Proof_Context.theory_of ctxt
    91     (* Transform the term*)
    92     val (t,np,nh) = prepare_for_mir q g
    93     (* Some simpsets for dealing with mod div abs and nat*)
    94     val mod_div_simpset = HOL_basic_ss 
    95                         addsimps [refl, mod_add_eq, 
    96                                   @{thm mod_self},
    97                                   @{thm div_0}, @{thm mod_0},
    98                                   @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, @{thm "mod_1"},
    99                                   @{thm "Suc_eq_plus1"}]
   100                         addsimps @{thms add_ac}
   101                         addsimprocs [@{simproc cancel_div_mod_nat}, @{simproc cancel_div_mod_int}]
   102     val simpset0 = HOL_basic_ss
   103       addsimps [mod_div_equality', @{thm Suc_eq_plus1}]
   104       addsimps comp_ths
   105       |> fold Splitter.add_split
   106           [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"},
   107             @{thm "split_min"}, @{thm "split_max"}]
   108     (* Simp rules for changing (n::int) to int n *)
   109     val simpset1 = HOL_basic_ss
   110       addsimps [@{thm "zdvd_int"}] @ map (fn r => r RS sym)
   111         [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
   112          @{thm nat_numeral}, @{thm "zmult_int"}]
   113       |> Splitter.add_split @{thm "zdiff_int_split"}
   114     (*simp rules for elimination of int n*)
   115 
   116     val simpset2 = HOL_basic_ss
   117       addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm zero_le_numeral}, 
   118                 @{thm "int_0"}, @{thm "int_1"}]
   119       |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
   120     (* simp rules for elimination of abs *)
   121     val ct = cterm_of thy (HOLogic.mk_Trueprop t)
   122     (* Theorem for the nat --> int transformation *)
   123     val pre_thm = Seq.hd (EVERY
   124       [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
   125        TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), TRY (simp_tac mir_ss 1)]
   126       (Thm.trivial ct))
   127     fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
   128     (* The result of the quantifier elimination *)
   129     val (th, tac) = case (prop_of pre_thm) of
   130         Const ("==>", _) $ (Const (@{const_name Trueprop}, _) $ t1) $ _ =>
   131     let val pth =
   132           (* If quick_and_dirty then run without proof generation as oracle*)
   133              if !quick_and_dirty
   134              then mirfr_oracle (false, cterm_of thy (Pattern.eta_long [] t1))
   135              else mirfr_oracle (true, cterm_of thy (Pattern.eta_long [] t1))
   136     in 
   137           (trace_msg ("calling procedure with term:\n" ^
   138              Syntax.string_of_term ctxt t1);
   139            ((pth RS iffD2) RS pre_thm,
   140             assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
   141     end
   142       | _ => (pre_thm, assm_tac i)
   143   in rtac (((mp_step nh) o (spec_step np)) th) i THEN tac end);
   144 
   145 end