src/HOL/Decision_Procs/mir_tac.ML
author haftmann
Mon Jun 05 15:59:41 2017 +0200 (2017-06-05)
changeset 66010 2f7d39285a1a
parent 64593 50c715579715
child 67118 ccab07d1196c
permissions -rw-r--r--
executable domain membership checks
     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   simpset_of (@{context} delsimps [@{thm "of_int_eq_iff"}, @{thm "of_int_less_iff"}, @{thm "of_int_le_iff"}] 
    15                addsimps @{thms "iff_real_of_int"});
    16 
    17 val nT = HOLogic.natT;
    18   val nat_arith = [@{thm diff_nat_numeral}];
    19 
    20   val comp_arith = [@{thm "Let_def"}, @{thm "if_False"}, @{thm "if_True"}, @{thm "add_0"},
    21                  @{thm "add_Suc"}, @{thm add_numeral_left}, @{thm mult_numeral_left(1)}] @
    22                  (map (fn th => th RS sym) [@{thm "numeral_One"}])
    23                  @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
    24   val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
    25              @{thm of_nat_numeral},
    26              @{thm "of_nat_Suc"}, @{thm "of_nat_1"},
    27              @{thm "of_int_0"}, @{thm "of_nat_0"},
    28              @{thm "div_by_0"}, 
    29              @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
    30              @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
    31              @{thm uminus_add_conv_diff [symmetric]}, @{thm "minus_divide_left"}]
    32 val comp_ths = distinct Thm.eq_thm (ths @ comp_arith @ @{thms simp_thms});
    33 
    34 fun prepare_for_mir q fm = 
    35   let
    36     val ps = Logic.strip_params fm
    37     val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    38     val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    39     fun mk_all ((s, T), (P,n)) =
    40       if Term.is_dependent P then
    41         (HOLogic.all_const T $ Abs (s, T, P), n)
    42       else (incr_boundvars ~1 P, n-1)
    43     fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    44       val rhs = hs
    45 (*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
    46     val np = length ps
    47     val (fm',np) = List.foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
    48       (List.foldr HOLogic.mk_imp c rhs, np) ps
    49     val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
    50       (Misc_Legacy.term_frees fm' @ Misc_Legacy.term_vars fm');
    51     val fm2 = List.foldr mk_all2 fm' vs
    52   in (fm2, np + length vs, length rhs) end;
    53 
    54 (*Object quantifier to meta --*)
    55 fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
    56 
    57 (* object implication to meta---*)
    58 fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
    59 
    60 
    61 fun mir_tac ctxt q = 
    62     Object_Logic.atomize_prems_tac ctxt
    63         THEN' simp_tac (put_simpset HOL_basic_ss ctxt
    64           addsimps [@{thm "abs_ge_zero"}] addsimps @{thms simp_thms})
    65         THEN' (REPEAT_DETERM o split_tac ctxt [@{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}])
    66         THEN' SUBGOAL (fn (g, i) =>
    67   let
    68     (* Transform the term*)
    69     val (t,np,nh) = prepare_for_mir q g
    70     (* Some simpsets for dealing with mod div abs and nat*)
    71     val mod_div_simpset = put_simpset HOL_basic_ss ctxt
    72                         addsimps [refl, @{thm mod_add_eq}, 
    73                                   @{thm mod_self},
    74                                   @{thm div_0}, @{thm mod_0},
    75                                   @{thm div_by_1}, @{thm mod_by_1}, @{thm div_by_Suc_0}, @{thm mod_by_Suc_0},
    76                                   @{thm "Suc_eq_plus1"}]
    77                         addsimps @{thms add.assoc add.commute add.left_commute}
    78                         addsimprocs [@{simproc cancel_div_mod_nat}, @{simproc cancel_div_mod_int}]
    79     val simpset0 = put_simpset HOL_basic_ss ctxt
    80       addsimps @{thms minus_div_mult_eq_mod [symmetric] Suc_eq_plus1}
    81       addsimps comp_ths
    82       |> fold Splitter.add_split
    83           [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"},
    84             @{thm "split_min"}, @{thm "split_max"}]
    85     (* Simp rules for changing (n::int) to int n *)
    86     val simpset1 = put_simpset HOL_basic_ss ctxt
    87       addsimps [@{thm "zdvd_int"}, @{thm "of_nat_add"}, @{thm "of_nat_mult"}] @ 
    88           map (fn r => r RS sym) [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "of_nat_less_iff"}, @{thm nat_numeral}]
    89       |> Splitter.add_split @{thm "zdiff_int_split"}
    90     (*simp rules for elimination of int n*)
    91 
    92     val simpset2 = put_simpset HOL_basic_ss ctxt
    93       addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm zero_le_numeral}, 
    94                 @{thm "of_nat_0"}, @{thm "of_nat_1"}]
    95       |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
    96     (* simp rules for elimination of abs *)
    97     val ct = Thm.cterm_of ctxt (HOLogic.mk_Trueprop t)
    98     (* Theorem for the nat --> int transformation *)
    99     val pre_thm = Seq.hd (EVERY
   100       [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
   101        TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1),
   102        TRY (simp_tac (put_simpset mir_ss ctxt) 1)]
   103       (Thm.trivial ct))
   104     fun assm_tac i = REPEAT_DETERM_N nh (assume_tac ctxt i)
   105     (* The result of the quantifier elimination *)
   106     val (th, tac) =
   107       case Thm.prop_of pre_thm of
   108         Const (@{const_name Pure.imp}, _) $ (Const (@{const_name Trueprop}, _) $ t1) $ _ =>
   109     let
   110       val pth = mirfr_oracle (ctxt, Envir.eta_long [] t1)
   111     in 
   112        ((pth RS iffD2) RS pre_thm,
   113         assm_tac (i + 1) THEN (if q then I else TRY) (resolve_tac ctxt [TrueI] i))
   114     end
   115       | _ => (pre_thm, assm_tac i)
   116   in resolve_tac ctxt [((mp_step nh) o (spec_step np)) th] i THEN tac end);
   117 
   118 end