src/HOL/Decision_Procs/mir_tac.ML
author huffman
Sun Mar 25 20:15:39 2012 +0200 (2012-03-25 ago)
changeset 47108 2a1953f0d20d
parent 45654 cf10bde35973
child 47142 d64fa2ca54b8
permissions -rw-r--r--
merged fork with new numeral representation (see NEWS)
     1 (*  Title:      HOL/Decision_Procs/mir_tac.ML
     2     Author:     Amine Chaieb, TU Muenchen
     3 *)
     4 
     5 signature MIR_TAC =
     6 sig
     7   val trace: bool Unsynchronized.ref
     8   val mir_tac: Proof.context -> bool -> int -> tactic
     9   val setup: theory -> theory
    10 end
    11 
    12 structure Mir_Tac =
    13 struct
    14 
    15 val trace = Unsynchronized.ref false;
    16 fun trace_msg s = if !trace then tracing s else ();
    17 
    18 val mir_ss = 
    19 let val ths = [@{thm "real_of_int_inject"}, @{thm "real_of_int_less_iff"}, @{thm "real_of_int_le_iff"}]
    20 in @{simpset} delsimps ths addsimps (map (fn th => th RS sym) ths)
    21 end;
    22 
    23 val nT = HOLogic.natT;
    24   val nat_arith = [@{thm diff_nat_numeral}];
    25 
    26   val comp_arith = [@{thm "Let_def"}, @{thm "if_False"}, @{thm "if_True"}, @{thm "add_0"},
    27                  @{thm "add_Suc"}, @{thm add_numeral_left}, @{thm mult_numeral_left(1)},
    28                  @{thm "Suc_eq_plus1"}] @
    29                  (map (fn th => th RS sym) [@{thm "numeral_1_eq_1"}])
    30                  @ @{thms arith_simps} @ nat_arith @ @{thms rel_simps} 
    31   val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"}, 
    32              @{thm real_of_nat_numeral},
    33              @{thm "real_of_nat_Suc"}, @{thm "real_of_nat_one"}, @{thm "real_of_one"},
    34              @{thm "real_of_int_zero"}, @{thm "real_of_nat_zero"},
    35              @{thm "divide_zero"}, 
    36              @{thm "divide_divide_eq_left"}, @{thm "times_divide_eq_right"}, 
    37              @{thm "times_divide_eq_left"}, @{thm "divide_divide_eq_right"},
    38              @{thm "diff_minus"}, @{thm "minus_divide_left"}]
    39 val comp_ths = ths @ comp_arith @ @{thms simp_thms};
    40 
    41 
    42 val zdvd_int = @{thm "zdvd_int"};
    43 val zdiff_int_split = @{thm "zdiff_int_split"};
    44 val all_nat = @{thm "all_nat"};
    45 val ex_nat = @{thm "ex_nat"};
    46 val split_zdiv = @{thm "split_zdiv"};
    47 val split_zmod = @{thm "split_zmod"};
    48 val mod_div_equality' = @{thm "mod_div_equality'"};
    49 val split_div' = @{thm "split_div'"};
    50 val imp_le_cong = @{thm "imp_le_cong"};
    51 val conj_le_cong = @{thm "conj_le_cong"};
    52 val mod_add_eq = @{thm "mod_add_eq"} RS sym;
    53 val mod_add_left_eq = @{thm "mod_add_left_eq"} RS sym;
    54 val mod_add_right_eq = @{thm "mod_add_right_eq"} RS sym;
    55 val nat_div_add_eq = @{thm "div_add1_eq"} RS sym;
    56 val int_div_add_eq = @{thm "zdiv_zadd1_eq"} RS sym;
    57 val ZDIVISION_BY_ZERO_MOD = @{thm "DIVISION_BY_ZERO"} RS conjunct2;
    58 val ZDIVISION_BY_ZERO_DIV = @{thm "DIVISION_BY_ZERO"} RS conjunct1;
    59 
    60 fun prepare_for_mir thy q fm = 
    61   let
    62     val ps = Logic.strip_params fm
    63     val hs = map HOLogic.dest_Trueprop (Logic.strip_assums_hyp fm)
    64     val c = HOLogic.dest_Trueprop (Logic.strip_assums_concl fm)
    65     fun mk_all ((s, T), (P,n)) =
    66       if Term.is_dependent P then
    67         (HOLogic.all_const T $ Abs (s, T, P), n)
    68       else (incr_boundvars ~1 P, n-1)
    69     fun mk_all2 (v, t) = HOLogic.all_const (fastype_of v) $ lambda v t;
    70       val rhs = hs
    71 (*    val (rhs,irhs) = List.partition (relevant (rev ps)) hs *)
    72     val np = length ps
    73     val (fm',np) = List.foldr (fn ((x, T), (fm,n)) => mk_all ((x, T), (fm,n)))
    74       (List.foldr HOLogic.mk_imp c rhs, np) ps
    75     val (vs, _) = List.partition (fn t => q orelse (type_of t) = nT)
    76       (Misc_Legacy.term_frees fm' @ Misc_Legacy.term_vars fm');
    77     val fm2 = List.foldr mk_all2 fm' vs
    78   in (fm2, np + length vs, length rhs) end;
    79 
    80 (*Object quantifier to meta --*)
    81 fun spec_step n th = if (n=0) then th else (spec_step (n-1) th) RS spec ;
    82 
    83 (* object implication to meta---*)
    84 fun mp_step n th = if (n=0) then th else (mp_step (n-1) th) RS mp;
    85 
    86 
    87 fun mir_tac ctxt q = 
    88     Object_Logic.atomize_prems_tac
    89         THEN' simp_tac (HOL_basic_ss addsimps [@{thm "abs_ge_zero"}] addsimps @{thms simp_thms})
    90         THEN' (REPEAT_DETERM o split_tac [@{thm "split_min"}, @{thm "split_max"}, @{thm "abs_split"}])
    91         THEN' SUBGOAL (fn (g, i) =>
    92   let
    93     val thy = Proof_Context.theory_of ctxt
    94     (* Transform the term*)
    95     val (t,np,nh) = prepare_for_mir thy q g
    96     (* Some simpsets for dealing with mod div abs and nat*)
    97     val mod_div_simpset = HOL_basic_ss 
    98                         addsimps [refl, mod_add_eq, 
    99                                   @{thm "mod_self"}, @{thm "zmod_self"},
   100                                   @{thm "zdiv_zero"},@{thm "zmod_zero"},@{thm "div_0"}, @{thm "mod_0"},
   101                                   @{thm "div_by_1"}, @{thm "mod_by_1"}, @{thm "div_1"}, @{thm "mod_1"},
   102                                   @{thm "Suc_eq_plus1"}]
   103                         addsimps @{thms add_ac}
   104                         addsimprocs [@{simproc cancel_div_mod_nat}, @{simproc cancel_div_mod_int}]
   105     val simpset0 = HOL_basic_ss
   106       addsimps [mod_div_equality', @{thm Suc_eq_plus1}]
   107       addsimps comp_ths
   108       |> fold Splitter.add_split
   109           [@{thm "split_zdiv"}, @{thm "split_zmod"}, @{thm "split_div'"},
   110             @{thm "split_min"}, @{thm "split_max"}]
   111     (* Simp rules for changing (n::int) to int n *)
   112     val simpset1 = HOL_basic_ss
   113       addsimps [@{thm "zdvd_int"}] @ map (fn r => r RS sym)
   114         [@{thm "int_int_eq"}, @{thm "zle_int"}, @{thm "zless_int"}, @{thm "zadd_int"}, 
   115          @{thm nat_numeral}, @{thm "zmult_int"}]
   116       |> Splitter.add_split @{thm "zdiff_int_split"}
   117     (*simp rules for elimination of int n*)
   118 
   119     val simpset2 = HOL_basic_ss
   120       addsimps [@{thm "nat_0_le"}, @{thm "all_nat"}, @{thm "ex_nat"}, @{thm zero_le_numeral}, 
   121                 @{thm "int_0"}, @{thm "int_1"}]
   122       |> fold Simplifier.add_cong [@{thm "conj_le_cong"}, @{thm "imp_le_cong"}]
   123     (* simp rules for elimination of abs *)
   124     val ct = cterm_of thy (HOLogic.mk_Trueprop t)
   125     (* Theorem for the nat --> int transformation *)
   126     val pre_thm = Seq.hd (EVERY
   127       [simp_tac mod_div_simpset 1, simp_tac simpset0 1,
   128        TRY (simp_tac simpset1 1), TRY (simp_tac simpset2 1), TRY (simp_tac mir_ss 1)]
   129       (Thm.trivial ct))
   130     fun assm_tac i = REPEAT_DETERM_N nh (assume_tac i)
   131     (* The result of the quantifier elimination *)
   132     val (th, tac) = case (prop_of pre_thm) of
   133         Const ("==>", _) $ (Const (@{const_name Trueprop}, _) $ t1) $ _ =>
   134     let val pth =
   135           (* If quick_and_dirty then run without proof generation as oracle*)
   136              if !quick_and_dirty
   137              then mirfr_oracle (false, cterm_of thy (Pattern.eta_long [] t1))
   138              else mirfr_oracle (true, cterm_of thy (Pattern.eta_long [] t1))
   139     in 
   140           (trace_msg ("calling procedure with term:\n" ^
   141              Syntax.string_of_term ctxt t1);
   142            ((pth RS iffD2) RS pre_thm,
   143             assm_tac (i + 1) THEN (if q then I else TRY) (rtac TrueI i)))
   144     end
   145       | _ => (pre_thm, assm_tac i)
   146   in rtac (((mp_step nh) o (spec_step np)) th) i THEN tac end);
   147 
   148 val setup =
   149   Method.setup @{binding mir}
   150     let
   151       val parse_flag = Args.$$$ "no_quantify" >> K (K false)
   152     in
   153       Scan.lift (Scan.optional (Args.$$$ "(" |-- Scan.repeat1 parse_flag --| Args.$$$ ")") [] >>
   154         curry (Library.foldl op |>) true) >>
   155       (fn q => fn ctxt => SIMPLE_METHOD' (mir_tac ctxt q))
   156     end
   157     "decision procedure for MIR arithmetic";
   158 
   159 end