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