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