src/ZF/arith_data.ML
author oheimb
Wed, 03 Apr 2002 10:21:13 +0200
changeset 13076 70704dd48bd5
parent 12206 60d52181840c
child 13126 97e83120d6c8
permissions -rw-r--r--
bugfix concerning claset(), added limited support for ALLGOALS + fast_tac etc.
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
    ID:         $Id$
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     4
    Copyright   2000  University of Cambridge
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
Arithmetic simplification: cancellation of common terms
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     7
*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     8
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
     9
signature ARITH_DATA =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    10
sig
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    11
  (*the main outcome*)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    12
  val nat_cancel: simproc list
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    13
  (*tools for use in similar applications*)
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    14
  val gen_trans_tac: thm -> thm option -> tactic
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    15
  val prove_conv: string -> tactic list -> Sign.sg -> 
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    16
                  thm list -> term * term -> thm option
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    17
  val simplify_meta_eq: thm list -> thm -> thm
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    18
  (*debugging*)
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    19
  structure EqCancelNumeralsData   : CANCEL_NUMERALS_DATA
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    20
  structure LessCancelNumeralsData : CANCEL_NUMERALS_DATA
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    21
  structure DiffCancelNumeralsData : CANCEL_NUMERALS_DATA
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    22
end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    23
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    24
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    25
structure ArithData: ARITH_DATA =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    26
struct
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    27
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    28
val iT = Ind_Syntax.iT;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    29
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    30
val zero = Const("0", iT);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    31
val succ = Const("succ", iT --> iT);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    32
fun mk_succ t = succ $ t;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    33
val one = mk_succ zero;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    34
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    35
val mk_plus = FOLogic.mk_binop "Arith.add";
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    36
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    37
(*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
    38
fun mk_sum []        = zero
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    39
  | mk_sum [t,u]     = mk_plus (t, u)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    40
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    41
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    42
(*this version ALWAYS includes a trailing zero*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    43
fun long_mk_sum []        = zero
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    44
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    45
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    46
val dest_plus = FOLogic.dest_bin "Arith.add" iT;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    47
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    48
(* dest_sum *)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    49
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    50
fun dest_sum (Const("0",_)) = []
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    51
  | dest_sum (Const("succ",_) $ t) = one :: dest_sum t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    52
  | dest_sum (Const("Arith.add",_) $ t $ u) = dest_sum t @ dest_sum u
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    53
  | dest_sum tm = [tm];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    54
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    55
(*Apply the given rewrite (if present) just once*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    56
fun gen_trans_tac th2 None      = all_tac
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    57
  | gen_trans_tac th2 (Some th) = ALLGOALS (rtac (th RS th2));
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    58
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    59
(*Use <-> or = depending on the type of t*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    60
fun mk_eq_iff(t,u) =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    61
  if fastype_of t = iT then FOLogic.mk_eq(t,u)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    62
                       else FOLogic.mk_iff(t,u);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    63
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    64
(*We remove equality assumptions because they confuse the simplifier and
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    65
  because only type-checking assumptions are necessary.*)
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    66
fun is_eq_thm th = 
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    67
    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
    68
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    69
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
    70
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    71
fun prove_conv name tacs sg hyps (t,u) =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    72
  if t aconv u then None
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    73
  else
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    74
  let val hyps' = filter (not o is_eq_thm) hyps
12134
7049eead7a50 use Tactic.prove;
wenzelm
parents: 9874
diff changeset
    75
      val goal = Logic.list_implies (map (#prop o Thm.rep_thm) hyps',
7049eead7a50 use Tactic.prove;
wenzelm
parents: 9874
diff changeset
    76
        FOLogic.mk_Trueprop (mk_eq_iff (t, u)));
7049eead7a50 use Tactic.prove;
wenzelm
parents: 9874
diff changeset
    77
  in Some (hyps' MRS Tactic.prove sg [] [] goal (K (EVERY tacs)))
12206
60d52181840c no handle ERROR;
wenzelm
parents: 12134
diff changeset
    78
      handle ERROR_MESSAGE msg => 
60d52181840c no handle ERROR;
wenzelm
parents: 12134
diff changeset
    79
	(warning (msg ^ "\nCancellation failed: no typing information? (" ^ name ^ ")"); None)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    80
  end;
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
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    83
fun prep_pat s = Thm.read_cterm (Theory.sign_of (the_context ()))
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    84
                      (s, TypeInfer.anyT ["logic"]);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    85
val prep_pats = map prep_pat;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    86
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
(*** Use CancelNumerals simproc without binary numerals, 
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    89
     just for cancellation ***)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    90
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
    91
val mk_times = FOLogic.mk_binop "Arith.mult";
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    92
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    93
fun mk_prod [] = one
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    94
  | mk_prod [t] = t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    95
  | mk_prod (t :: ts) = if t = one then mk_prod ts
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    96
                        else mk_times (t, mk_prod ts);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    97
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    98
val dest_times = FOLogic.dest_bin "Arith.mult" iT;
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
fun dest_prod t =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   101
      let val (t,u) = dest_times t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   102
      in  dest_prod t @ dest_prod u  end
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   103
      handle TERM _ => [t];
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 only arguments are 0 and 1*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   106
fun mk_coeff (0, t) = zero
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   107
  | mk_coeff (1, t) = t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   108
  | mk_coeff _       = raise TERM("mk_coeff", []);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   109
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   110
(*Dummy version: the "coefficient" is always 1.
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   111
  In the result, the factors are sorted terms*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   112
fun dest_coeff t = (1, mk_prod (sort Term.term_ord (dest_prod t)));
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   113
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   114
(*Find first coefficient-term THAT MATCHES u*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   115
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   116
  | find_first_coeff past u (t::terms) =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   117
        let val (n,u') = dest_coeff t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   118
        in  if u aconv u' then (n, rev past @ terms)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   119
                          else find_first_coeff (t::past) u terms
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   120
        end
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   121
        handle TERM _ => find_first_coeff (t::past) u terms;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   122
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   123
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   124
(*Simplify #1*n and n*#1 to n*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   125
val add_0s = [add_0_natify, add_0_right_natify];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   126
val add_succs = [add_succ, add_succ_right];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   127
val mult_1s = [mult_1_natify, mult_1_right_natify];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   128
val tc_rules = [natify_in_nat, add_type, diff_type, mult_type];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   129
val natifys = [natify_0, natify_ident, add_natify1, add_natify2,
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
   130
               diff_natify1, diff_natify2];
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   131
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   132
(*Final simplification: cancel + and **)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   133
fun simplify_meta_eq rules =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   134
    mk_meta_eq o
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   135
    simplify (FOL_ss addeqcongs[eq_cong2,iff_cong2] 
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   136
                     delsimps iff_simps (*these could erase the whole rule!*)
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
   137
		     addsimps rules);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   138
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   139
val final_rules = add_0s @ mult_1s @ [mult_0, mult_0_right];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   140
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   141
structure CancelNumeralsCommon =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   142
  struct
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   143
  val mk_sum            = mk_sum
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   144
  val dest_sum          = dest_sum
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   145
  val mk_coeff          = mk_coeff
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   146
  val dest_coeff        = dest_coeff
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   147
  val find_first_coeff  = find_first_coeff []
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   148
  val norm_tac_ss1 = ZF_ss addsimps add_0s@add_succs@mult_1s@add_ac
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   149
  val norm_tac_ss2 = ZF_ss addsimps add_ac@mult_ac@tc_rules@natifys
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   150
  val norm_tac = ALLGOALS (asm_simp_tac norm_tac_ss1)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   151
                 THEN ALLGOALS (asm_simp_tac norm_tac_ss2)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   152
  val numeral_simp_tac_ss = ZF_ss addsimps add_0s@tc_rules@natifys
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   153
  val numeral_simp_tac  = ALLGOALS (asm_simp_tac numeral_simp_tac_ss)
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
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   160
structure EqCancelNumeralsData = 
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
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   166
  val bal_add1 = eq_add_iff RS iff_trans
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   167
  val bal_add2 = eq_add_iff RS iff_trans
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   168
  val trans_tac = gen_trans_tac 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
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   173
structure LessCancelNumeralsData = 
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"
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
   177
  val mk_bal   = FOLogic.mk_binrel "Ordinal.op <"
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   178
  val dest_bal = FOLogic.dest_bin "Ordinal.op <" iT
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   179
  val bal_add1 = less_add_iff RS iff_trans
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   180
  val bal_add2 = less_add_iff RS iff_trans
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   181
  val trans_tac = gen_trans_tac 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
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   186
structure DiffCancelNumeralsData = 
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"
9570
e16e168984e1 installation of cancellation simprocs for the integers
paulson
parents: 9548
diff changeset
   190
  val mk_bal   = FOLogic.mk_binop "Arith.diff"
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   191
  val dest_bal = FOLogic.dest_bin "Arith.diff" iT
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   192
  val bal_add1 = diff_add_eq RS trans
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   193
  val bal_add2 = diff_add_eq RS trans
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   194
  val trans_tac = gen_trans_tac 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 =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   201
      map prep_simproc
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   202
       [("nateq_cancel_numerals",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   203
	 prep_pats ["l #+ m = n", "l = m #+ n",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   204
		    "l #* m = n", "l = m #* n",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   205
		    "succ(m) = n", "m = succ(n)"],
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   206
	 EqCancelNumerals.proc),
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   207
	("natless_cancel_numerals",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   208
	 prep_pats ["l #+ m < n", "l < m #+ n",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   209
		    "l #* m < n", "l < m #* n",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   210
		    "succ(m) < n", "m < succ(n)"],
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   211
	 LessCancelNumerals.proc),
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   212
	("natdiff_cancel_numerals",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   213
	 prep_pats ["(l #+ m) #- n", "l #- (m #+ n)",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   214
		    "(l #* m) #- n", "l #- (m #* n)",
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   215
		    "succ(m) #- n", "m #- succ(n)"],
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   216
	 DiffCancelNumerals.proc)];
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
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   220
(*examples:
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   221
print_depth 22;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   222
set timing;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   223
set trace_simp;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   224
fun test s = (Goal s; by (Asm_simp_tac 1));
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   225
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   226
test "x #+ y = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   227
test "y #+ x = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   228
test "x #+ y #+ z = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   229
test "y #+ (z #+ x) = z #+ x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   230
test "x #+ y #+ z = (z #+ y) #+ (x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   231
test "x#*y #+ z = (z #+ y) #+ (y#*x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   232
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   233
test "x #+ succ(y) = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   234
test "x #+ succ(y) = succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   235
test "succ(x) #+ succ(y) #+ z = succ(z #+ y) #+ succ(x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   236
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   237
test "(x #+ y) #- (x #+ z) = w";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   238
test "(y #+ x) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   239
test "(x #+ y #+ z) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   240
test "(y #+ (z #+ x)) #- (z #+ x) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   241
test "(x #+ y #+ z) #- ((z #+ y) #+ (x #+ w)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   242
test "(x#*y #+ z) #- ((z #+ y) #+ (y#*x #+ w)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   243
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   244
(*BAD occurrence of natify*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   245
test "(x #+ succ(y)) #- (x #+ z) = 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
test "x #* y2 #+ y #* x2 = y #* x2 #+ x #* y2";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   248
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   249
test "(x #+ succ(y)) #- (succ(z #+ x)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   250
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
   251
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   252
(*use of typing information*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   253
test "x : nat ==> x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   254
test "x : nat --> x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   255
test "x : nat ==> x #+ y < x";
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
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   258
(*fails: no typing information isn't visible*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   259
test "x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   260
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   261
test "x #+ y < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   262
test "y #+ x < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   263
test "x #+ y #+ z < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   264
test "y #+ z #+ x < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   265
test "y #+ (z #+ x) < z #+ x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   266
test "x #+ y #+ z < (z #+ y) #+ (x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   267
test "x#*y #+ z < (z #+ y) #+ (y#*x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   268
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   269
test "x #+ succ(y) < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   270
test "x #+ succ(y) < succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   271
test "succ(x) #+ succ(y) #+ z < succ(z #+ y) #+ succ(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) le succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   274
*)