src/ZF/arith_data.ML
author haftmann
Thu, 18 Nov 2010 17:01:15 +0100
changeset 40602 91e583511113
parent 38715 6513ea67d95d
child 40878 7695e4de4d86
permissions -rw-r--r--
map_fun combinator in theory Fun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     1
(*  Title:      ZF/arith_data.ML
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     3
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     4
Arithmetic simplification: cancellation of common terms
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     5
*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     6
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     7
signature ARITH_DATA =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     8
sig
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
     9
  (*the main outcome*)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    10
  val nat_cancel: simproc list
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    11
  (*tools for use in similar applications*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    12
  val gen_trans_tac: thm -> thm option -> tactic
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20044
diff changeset
    13
  val prove_conv: string -> tactic list -> Proof.context -> thm list -> term * term -> thm option
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
    14
  val simplify_meta_eq: thm list -> simpset -> thm -> thm
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    15
  (*debugging*)
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    16
  structure EqCancelNumeralsData   : CANCEL_NUMERALS_DATA
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    17
  structure LessCancelNumeralsData : CANCEL_NUMERALS_DATA
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    18
  structure DiffCancelNumeralsData : CANCEL_NUMERALS_DATA
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    19
end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    20
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    21
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    22
structure ArithData: ARITH_DATA =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    23
struct
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    24
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    25
val iT = Ind_Syntax.iT;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    26
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    27
val zero = Const(@{const_name 0}, iT);
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    28
val succ = Const(@{const_name succ}, iT --> iT);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    29
fun mk_succ t = succ $ t;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    30
val one = mk_succ zero;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    31
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    32
val mk_plus = FOLogic.mk_binop @{const_name Arith.add};
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    33
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    34
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    35
fun mk_sum []        = zero
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    36
  | mk_sum [t,u]     = mk_plus (t, u)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    37
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    38
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    39
(*this version ALWAYS includes a trailing zero*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    40
fun long_mk_sum []        = zero
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    41
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    42
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    43
val dest_plus = FOLogic.dest_bin @{const_name Arith.add} iT;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    44
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    45
(* dest_sum *)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    46
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    47
fun dest_sum (Const(@{const_name 0},_)) = []
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    48
  | dest_sum (Const(@{const_name succ},_) $ t) = one :: dest_sum t
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    49
  | dest_sum (Const(@{const_name Arith.add},_) $ t $ u) = dest_sum t @ dest_sum u
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    50
  | dest_sum tm = [tm];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    51
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    52
(*Apply the given rewrite (if present) just once*)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14387
diff changeset
    53
fun gen_trans_tac th2 NONE      = all_tac
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14387
diff changeset
    54
  | gen_trans_tac th2 (SOME th) = ALLGOALS (rtac (th RS th2));
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    55
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    56
(*Use <-> or = depending on the type of t*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    57
fun mk_eq_iff(t,u) =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    58
  if fastype_of t = iT then FOLogic.mk_eq(t,u)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    59
                       else FOLogic.mk_iff(t,u);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    60
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    61
(*We remove equality assumptions because they confuse the simplifier and
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    62
  because only type-checking assumptions are necessary.*)
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
    63
fun is_eq_thm th =
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    64
    can FOLogic.dest_eq (FOLogic.dest_Trueprop (#prop (rep_thm th)));
9649
89155e48fa53 simproc bug fix: only TYPING assumptions are given to the simplifier
paulson
parents: 9570
diff changeset
    65
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    66
fun add_chyps chyps ct = Drule.list_implies (map cprop_of chyps, ct);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    67
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20044
diff changeset
    68
fun prove_conv name tacs ctxt prems (t,u) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14387
diff changeset
    69
  if t aconv u then NONE
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    70
  else
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 32155
diff changeset
    71
  let val prems' = filter_out is_eq_thm prems
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20044
diff changeset
    72
      val goal = Logic.list_implies (map (#prop o Thm.rep_thm) prems',
12134
7049eead7a50 use Tactic.prove;
wenzelm
parents: 9874
diff changeset
    73
        FOLogic.mk_Trueprop (mk_eq_iff (t, u)));
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20044
diff changeset
    74
  in SOME (prems' MRS Goal.prove ctxt [] [] goal (K (EVERY tacs)))
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18328
diff changeset
    75
      handle ERROR msg =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14387
diff changeset
    76
        (warning (msg ^ "\nCancellation failed: no typing information? (" ^ name ^ ")"); NONE)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    77
  end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    78
32155
e2bf2f73b0c8 more @{theory} antiquotations;
wenzelm
parents: 29269
diff changeset
    79
fun prep_simproc thy (name, pats, proc) =
38715
6513ea67d95d renamed Simplifier.simproc(_i) to Simplifier.simproc_global(_i) to emphasize that this is not the real thing;
wenzelm
parents: 38513
diff changeset
    80
  Simplifier.simproc_global thy name pats proc;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    81
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    82
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
    83
(*** Use CancelNumerals simproc without binary numerals,
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    84
     just for cancellation ***)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    85
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    86
val mk_times = FOLogic.mk_binop @{const_name Arith.mult};
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    87
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    88
fun mk_prod [] = one
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    89
  | mk_prod [t] = t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    90
  | mk_prod (t :: ts) = if t = one then mk_prod ts
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    91
                        else mk_times (t, mk_prod ts);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    92
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
    93
val dest_times = FOLogic.dest_bin @{const_name Arith.mult} iT;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    94
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    95
fun dest_prod t =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    96
      let val (t,u) = dest_times t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    97
      in  dest_prod t @ dest_prod u  end
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    98
      handle TERM _ => [t];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    99
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   100
(*Dummy version: the only arguments are 0 and 1*)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 20342
diff changeset
   101
fun mk_coeff (0, t) = zero
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   102
  | mk_coeff (1, t) = t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   103
  | mk_coeff _       = raise TERM("mk_coeff", []);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   104
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   105
(*Dummy version: the "coefficient" is always 1.
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   106
  In the result, the factors are sorted terms*)
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 33317
diff changeset
   107
fun dest_coeff t = (1, mk_prod (sort Term_Ord.term_ord (dest_prod t)));
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   108
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   109
(*Find first coefficient-term THAT MATCHES u*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   110
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   111
  | find_first_coeff past u (t::terms) =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   112
        let val (n,u') = dest_coeff t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   113
        in  if u aconv u' then (n, rev past @ terms)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   114
                          else find_first_coeff (t::past) u terms
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   115
        end
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   116
        handle TERM _ => find_first_coeff (t::past) u terms;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   117
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   118
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   119
(*Simplify #1*n and n*#1 to n*)
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   120
val add_0s = [@{thm add_0_natify}, @{thm add_0_right_natify}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   121
val add_succs = [@{thm add_succ}, @{thm add_succ_right}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   122
val mult_1s = [@{thm mult_1_natify}, @{thm mult_1_right_natify}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   123
val tc_rules = [@{thm natify_in_nat}, @{thm add_type}, @{thm diff_type}, @{thm mult_type}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   124
val natifys = [@{thm natify_0}, @{thm natify_ident}, @{thm add_natify1}, @{thm add_natify2},
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   125
               @{thm diff_natify1}, @{thm diff_natify2}];
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   126
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   127
(*Final simplification: cancel + and **)
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   128
fun simplify_meta_eq rules =
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   129
  let val ss0 =
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   130
    FOL_ss addeqcongs [@{thm eq_cong2}, @{thm iff_cong2}]
26287
df8e5362cff9 proper antiquotations;
wenzelm
parents: 24893
diff changeset
   131
      delsimps @{thms iff_simps} (*these could erase the whole rule!*)
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   132
      addsimps rules
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   133
  in fn ss => mk_meta_eq o simplify (Simplifier.inherit_context ss ss0) end;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   134
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   135
val final_rules = add_0s @ mult_1s @ [@{thm mult_0}, @{thm mult_0_right}];
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   136
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   137
structure CancelNumeralsCommon =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   138
  struct
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 13487
diff changeset
   139
  val mk_sum            = (fn T:typ => mk_sum)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   140
  val dest_sum          = dest_sum
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   141
  val mk_coeff          = mk_coeff
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   142
  val dest_coeff        = dest_coeff
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   143
  val find_first_coeff  = find_first_coeff []
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   144
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   145
  val norm_ss1 = ZF_ss addsimps add_0s @ add_succs @ mult_1s @ @{thms add_ac}
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   146
  val norm_ss2 = ZF_ss addsimps add_0s @ mult_1s @ @{thms add_ac} @
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   147
    @{thms mult_ac} @ tc_rules @ natifys
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   148
  fun norm_tac ss =
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   149
    ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss1))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   150
    THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss2))
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   151
  val numeral_simp_ss = ZF_ss addsimps add_0s @ tc_rules @ natifys
16973
b2a894562b8f simprocs: Simplifier.inherit_bounds;
wenzelm
parents: 15965
diff changeset
   152
  fun numeral_simp_tac ss =
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   153
    ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   154
  val simplify_meta_eq  = simplify_meta_eq final_rules
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   155
  end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   156
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   157
(** The functor argumnets are declared as separate structures
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   158
    so that they can be exported to ease debugging. **)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   159
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   160
structure EqCancelNumeralsData =
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   161
  struct
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   162
  open CancelNumeralsCommon
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   163
  val prove_conv = prove_conv "nateq_cancel_numerals"
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   164
  val mk_bal   = FOLogic.mk_eq
9649
89155e48fa53 simproc bug fix: only TYPING assumptions are given to the simplifier
paulson
parents: 9570
diff changeset
   165
  val dest_bal = FOLogic.dest_eq
35409
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   166
  val bal_add1 = @{thm eq_add_iff} RS @{thm iff_trans}
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   167
  val bal_add2 = @{thm eq_add_iff} RS @{thm iff_trans}
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   168
  fun trans_tac _ = gen_trans_tac @{thm iff_trans}
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   169
  end;
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   170
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   171
structure EqCancelNumerals = CancelNumeralsFun(EqCancelNumeralsData);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   172
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   173
structure LessCancelNumeralsData =
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   174
  struct
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   175
  open CancelNumeralsCommon
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   176
  val prove_conv = prove_conv "natless_cancel_numerals"
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
   177
  val mk_bal   = FOLogic.mk_binrel @{const_name Ordinal.lt}
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
   178
  val dest_bal = FOLogic.dest_bin @{const_name Ordinal.lt} iT
35409
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   179
  val bal_add1 = @{thm less_add_iff} RS @{thm iff_trans}
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   180
  val bal_add2 = @{thm less_add_iff} RS @{thm iff_trans}
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   181
  fun trans_tac _ = gen_trans_tac @{thm iff_trans}
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   182
  end;
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   183
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   184
structure LessCancelNumerals = CancelNumeralsFun(LessCancelNumeralsData);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   185
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   186
structure DiffCancelNumeralsData =
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   187
  struct
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   188
  open CancelNumeralsCommon
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   189
  val prove_conv = prove_conv "natdiff_cancel_numerals"
38513
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
   190
  val mk_bal   = FOLogic.mk_binop @{const_name Arith.diff}
33ab01218ae1 more antiquotations
haftmann
parents: 35409
diff changeset
   191
  val dest_bal = FOLogic.dest_bin @{const_name Arith.diff} iT
35409
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   192
  val bal_add1 = @{thm diff_add_eq} RS @{thm trans}
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   193
  val bal_add2 = @{thm diff_add_eq} RS @{thm trans}
5c5bb83f2bae more antiquotations;
wenzelm
parents: 35408
diff changeset
   194
  fun trans_tac _ = gen_trans_tac @{thm trans}
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   195
  end;
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   196
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   197
structure DiffCancelNumerals = CancelNumeralsFun(DiffCancelNumeralsData);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   198
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   199
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   200
val nat_cancel =
32155
e2bf2f73b0c8 more @{theory} antiquotations;
wenzelm
parents: 29269
diff changeset
   201
  map (prep_simproc @{theory})
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   202
   [("nateq_cancel_numerals",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   203
     ["l #+ m = n", "l = m #+ n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   204
      "l #* m = n", "l = m #* n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   205
      "succ(m) = n", "m = succ(n)"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19250
diff changeset
   206
     (K EqCancelNumerals.proc)),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   207
    ("natless_cancel_numerals",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   208
     ["l #+ m < n", "l < m #+ n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   209
      "l #* m < n", "l < m #* n",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   210
      "succ(m) < n", "m < succ(n)"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19250
diff changeset
   211
     (K LessCancelNumerals.proc)),
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   212
    ("natdiff_cancel_numerals",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   213
     ["(l #+ m) #- n", "l #- (m #+ n)",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   214
      "(l #* m) #- n", "l #- (m #* n)",
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   215
      "succ(m) #- n", "m #- succ(n)"],
20044
92cc2f4c7335 simprocs: no theory argument -- use simpset context instead;
wenzelm
parents: 19250
diff changeset
   216
     (K DiffCancelNumerals.proc))];
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   217
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   218
end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   219
13259
01fa0c8dbc92 conversion of many files to Isar format
paulson
parents: 13155
diff changeset
   220
Addsimprocs ArithData.nat_cancel;
01fa0c8dbc92 conversion of many files to Isar format
paulson
parents: 13155
diff changeset
   221
01fa0c8dbc92 conversion of many files to Isar format
paulson
parents: 13155
diff changeset
   222
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   223
(*examples:
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   224
print_depth 22;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   225
set timing;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   226
set trace_simp;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   227
fun test s = (Goal s; by (Asm_simp_tac 1));
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   228
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   229
test "x #+ y = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   230
test "y #+ x = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   231
test "x #+ y #+ z = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   232
test "y #+ (z #+ x) = z #+ x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   233
test "x #+ y #+ z = (z #+ y) #+ (x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   234
test "x#*y #+ z = (z #+ y) #+ (y#*x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   235
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   236
test "x #+ succ(y) = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   237
test "x #+ succ(y) = succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   238
test "succ(x) #+ succ(y) #+ z = succ(z #+ y) #+ succ(x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   239
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   240
test "(x #+ y) #- (x #+ z) = w";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   241
test "(y #+ x) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   242
test "(x #+ y #+ z) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   243
test "(y #+ (z #+ x)) #- (z #+ x) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   244
test "(x #+ y #+ z) #- ((z #+ y) #+ (x #+ w)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   245
test "(x#*y #+ z) #- ((z #+ y) #+ (y#*x #+ w)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   246
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   247
(*BAD occurrence of natify*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   248
test "(x #+ succ(y)) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   249
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   250
test "x #* y2 #+ y #* x2 = y #* x2 #+ x #* y2";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   251
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   252
test "(x #+ succ(y)) #- (succ(z #+ x)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   253
test "(succ(x) #+ succ(y) #+ z) #- (succ(z #+ y) #+ succ(x #+ w)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   254
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   255
(*use of typing information*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   256
test "x : nat ==> x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   257
test "x : nat --> x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   258
test "x : nat ==> x #+ y < x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   259
test "x : nat ==> x < y#+x";
13126
97e83120d6c8 fixed simproc bug
paulson
parents: 12206
diff changeset
   260
test "x : nat ==> x le succ(x)";
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   261
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   262
(*fails: no typing information isn't visible*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   263
test "x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   264
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   265
test "x #+ y < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   266
test "y #+ x < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   267
test "x #+ y #+ z < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   268
test "y #+ z #+ x < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   269
test "y #+ (z #+ x) < z #+ x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   270
test "x #+ y #+ z < (z #+ y) #+ (x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   271
test "x#*y #+ z < (z #+ y) #+ (y#*x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   272
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   273
test "x #+ succ(y) < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   274
test "x #+ succ(y) < succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   275
test "succ(x) #+ succ(y) #+ z < succ(z #+ y) #+ succ(x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   276
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   277
test "x #+ succ(y) le succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   278
*)