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