src/HOL/Decision_Procs/mir_tac.ML
author nipkow
Tue Feb 23 16:25:08 2016 +0100 (2016-02-23)
changeset 62390 842917225d56
parent 62348 9a5f43dac883
child 64240 eabf80376aab
permissions -rw-r--r--
more canonical names
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@36308
    28
             @{thm "divide_zero"}, 
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
chaieb@23264
    34
val mod_div_equality' = @{thm "mod_div_equality'"};
nipkow@30224
    35
val mod_add_eq = @{thm "mod_add_eq"} RS sym;
chaieb@23264
    36
wenzelm@51369
    37
fun prepare_for_mir q fm = 
chaieb@23264
    38
  let
chaieb@23264
    39
    val ps = Logic.strip_params fm
chaieb@23264
    40
    val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
chaieb@23264
    41
    val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
chaieb@23264
    42
    fun mk_all ((s, T), (P,n)) =
wenzelm@42083
    43
      if Term.is_dependent P then
chaieb@23264
    44
        (HOLogic.all_const T $ Abs (s, T, P), n)
chaieb@23264
    45
      else (incr_boundvars ~1 P, n-1)
chaieb@23264
    46
    fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
chaieb@23264
    47
      val rhs = hs
chaieb@23264
    48
(*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
chaieb@23264
    49
    val np = length ps
wenzelm@33004
    50
    val (fm',np) = List.foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
wenzelm@33004
    51
      (List.foldr HOLogic.mk_imp c rhs, np) ps
chaieb@23264
    52
    val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
wenzelm@44121
    53
      (Misc_Legacy.term_frees fm' @ Misc_Legacy.term_vars fm');
wenzelm@33004
    54
    val fm2 = List.foldr mk_all2 fm' vs
chaieb@23264
    55
  in (fm2, np + length vs, length rhs) end;
chaieb@23264
    56
chaieb@23264
    57
(*Object quantifier to meta --*)
chaieb@23264
    58
fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
chaieb@23264
    59
chaieb@23264
    60
(* object implication to meta---*)
chaieb@23264
    61
fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
chaieb@23264
    62
chaieb@23264
    63
wenzelm@42368
    64
fun mir_tac ctxt q = 
wenzelm@54742
    65
    Object_Logic.atomize_prems_tac ctxt
wenzelm@51717
    66
        THEN' simp_tac (put_simpset HOL_basic_ss ctxt
wenzelm@51717
    67
          addsimps [@{thm "abs_ge_zero"}] addsimps @{thms simp_thms})
wenzelm@58956
    68
        THEN' (REPEAT_DETERM o split_tac ctxt [@{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}])
wenzelm@42368
    69
        THEN' SUBGOAL (fn (g, i) =>
chaieb@23264
    70
  let
chaieb@23264
    71
    (* Transform the term*)
wenzelm@51369
    72
    val (t,np,nh) = prepare_for_mir q g
chaieb@23264
    73
    (* Some simpsets for dealing with mod div abs and nat*)
wenzelm@51717
    74
    val mod_div_simpset = put_simpset HOL_basic_ss ctxt
nipkow@30224
    75
                        addsimps [refl, mod_add_eq, 
huffman@47142
    76
                                  @{thm mod_self},
huffman@47142
    77
                                  @{thm div_0}, @{thm mod_0},
nipkow@30031
    78
                                  @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, @{thm "mod_1"},
nipkow@31790
    79
                                  @{thm "Suc_eq_plus1"}]
haftmann@57514
    80
                        addsimps @{thms add.assoc add.commute add.left_commute}
wenzelm@43594
    81
                        addsimprocs [@{simproc cancel_div_mod_nat}, @{simproc cancel_div_mod_int}]
wenzelm@51717
    82
    val simpset0 = put_simpset HOL_basic_ss ctxt
nipkow@31790
    83
      addsimps [mod_div_equality', @{thm Suc_eq_plus1}]
chaieb@23318
    84
      addsimps comp_ths
wenzelm@45620
    85
      |> fold Splitter.add_split
wenzelm@45620
    86
          [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"},
wenzelm@45620
    87
            @{thm "split_min"}, @{thm "split_max"}]
chaieb@23264
    88
    (* Simp rules for changing (n::int) to int n *)
wenzelm@51717
    89
    val simpset1 = put_simpset HOL_basic_ss ctxt
lp15@61694
    90
      addsimps [@{thm "zdvd_int"}, @{thm "of_nat_add"}, @{thm "of_nat_mult"}] @ 
haftmann@62348
    91
          map (fn r => r RS sym) [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "of_nat_less_iff"}, @{thm nat_numeral}]
wenzelm@45620
    92
      |> Splitter.add_split @{thm "zdiff_int_split"}
chaieb@23264
    93
    (*simp rules for elimination of int n*)
chaieb@23264
    94
wenzelm@51717
    95
    val simpset2 = put_simpset HOL_basic_ss ctxt
huffman@47108
    96
      addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm zero_le_numeral}, 
haftmann@62348
    97
                @{thm "of_nat_0"}, @{thm "of_nat_1"}]
wenzelm@45620
    98
      |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
chaieb@23264
    99
    (* simp rules for elimination of abs *)
wenzelm@59629
   100
    val ct = Thm.cterm_of ctxt (HOLogic.mk_Trueprop t)
chaieb@23264
   101
    (* Theorem for the nat --> int transformation *)
chaieb@23264
   102
    val pre_thm = Seq.hd (EVERY
chaieb@23264
   103
      [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
wenzelm@51717
   104
       TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1),
wenzelm@51717
   105
       TRY (simp_tac (put_simpset mir_ss ctxt) 1)]
wenzelm@36945
   106
      (Thm.trivial ct))
wenzelm@58963
   107
    fun assm_tac i = REPEAT_DETERM_N nh (assume_tac ctxt i)
chaieb@23264
   108
    (* The result of the quantifier elimination *)
wenzelm@59582
   109
    val (th, tac) =
wenzelm@59582
   110
      case Thm.prop_of pre_thm of
wenzelm@56245
   111
        Const (@{const_name Pure.imp}, _) $ (Const (@{const_name Trueprop}, _) $ t1) $ _ =>
wenzelm@60325
   112
    let
wenzelm@60325
   113
      val pth = mirfr_oracle (ctxt, Envir.eta_long [] t1)
chaieb@23264
   114
    in 
wenzelm@55506
   115
       ((pth RS iffD2) RS pre_thm,
wenzelm@60754
   116
        assm_tac (i + 1) THEN (if q then I else TRY) (resolve_tac ctxt [TrueI] i))
chaieb@23264
   117
    end
chaieb@23264
   118
      | _ => (pre_thm, assm_tac i)
wenzelm@60754
   119
  in resolve_tac ctxt [((mp_step nh) o (spec_step np)) th] i THEN tac end);
chaieb@23264
   120
wenzelm@23590
   121
end