src/ZF/arith_data.ML
author haftmann
Fri, 24 Sep 2021 11:04:18 +0000
changeset 74361 690928dd6f8f
parent 74342 5d411d85da8c
child 74375 ba880f3a4e52
permissions -rw-r--r--
apply declarations from interpretations in eigen context also
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*)
59530
2a20354c0877 proper context;
wenzelm
parents: 59498
diff changeset
    12
  val gen_trans_tac: Proof.context -> 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
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
    14
  val simplify_meta_eq: thm list -> Proof.context -> 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
74294
ee04dc00bf0a more antiquotations;
wenzelm
parents: 70470
diff changeset
    25
val zero = \<^Const>\<open>zero\<close>;
ee04dc00bf0a more antiquotations;
wenzelm
parents: 70470
diff changeset
    26
val succ = \<^Const>\<open>succ\<close>;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    27
fun mk_succ t = succ $ t;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    28
val one = mk_succ zero;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    29
74297
ac130a6bd6b2 more antiquotations;
wenzelm
parents: 74294
diff changeset
    30
fun mk_plus (t, u) = \<^Const>\<open>Arith.add for t u\<close>;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    31
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    32
(*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
    33
fun mk_sum []        = zero
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    34
  | mk_sum [t,u]     = mk_plus (t, u)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    35
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
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
(* dest_sum *)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    38
74294
ee04dc00bf0a more antiquotations;
wenzelm
parents: 70470
diff changeset
    39
fun dest_sum \<^Const_>\<open>zero\<close> = []
ee04dc00bf0a more antiquotations;
wenzelm
parents: 70470
diff changeset
    40
  | dest_sum \<^Const_>\<open>succ for t\<close> = one :: dest_sum t
ee04dc00bf0a more antiquotations;
wenzelm
parents: 70470
diff changeset
    41
  | dest_sum \<^Const_>\<open>Arith.add for t u\<close> = dest_sum t @ dest_sum u
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    42
  | dest_sum tm = [tm];
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    43
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    44
(*Apply the given rewrite (if present) just once*)
59530
2a20354c0877 proper context;
wenzelm
parents: 59498
diff changeset
    45
fun gen_trans_tac _ _ NONE = all_tac
2a20354c0877 proper context;
wenzelm
parents: 59498
diff changeset
    46
  | gen_trans_tac ctxt th2 (SOME th) = ALLGOALS (resolve_tac ctxt [th RS th2]);
9548
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
(*Use <-> or = depending on the type of t*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    49
fun mk_eq_iff(t,u) =
74319
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
    50
  if fastype_of t = \<^Type>\<open>i\<close>
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
    51
  then \<^Const>\<open>IFOL.eq \<open>\<^Type>\<open>i\<close>\<close> for t u\<close>
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
    52
  else \<^Const>\<open>IFOL.iff for t u\<close>;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    53
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    54
(*We remove equality assumptions because they confuse the simplifier and
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
    55
  because only type-checking assumptions are necessary.*)
74342
5d411d85da8c clarified antiquotations;
wenzelm
parents: 74319
diff changeset
    56
fun is_eq_thm th = can FOLogic.dest_eq (\<^dest_judgment> (Thm.prop_of th));
9649
89155e48fa53 simproc bug fix: only TYPING assumptions are given to the simplifier
paulson
parents: 9570
diff changeset
    57
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20044
diff changeset
    58
fun prove_conv name tacs ctxt prems (t,u) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14387
diff changeset
    59
  if t aconv u then NONE
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    60
  else
33317
b4534348b8fd standardized filter/filter_out;
wenzelm
parents: 32155
diff changeset
    61
  let val prems' = filter_out is_eq_thm prems
74342
5d411d85da8c clarified antiquotations;
wenzelm
parents: 74319
diff changeset
    62
      val goal = Logic.list_implies (map Thm.prop_of prems', \<^make_judgment> (mk_eq_iff (t, u)));
20113
90a8d14f3610 simplified prove_conv;
wenzelm
parents: 20044
diff changeset
    63
  in SOME (prems' MRS Goal.prove ctxt [] [] goal (K (EVERY tacs)))
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18328
diff changeset
    64
      handle ERROR msg =>
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14387
diff changeset
    65
        (warning (msg ^ "\nCancellation failed: no typing information? (" ^ name ^ ")"); NONE)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    66
  end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    67
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    68
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
    69
(*** Use CancelNumerals simproc without binary numerals,
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    70
     just for cancellation ***)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    71
74297
ac130a6bd6b2 more antiquotations;
wenzelm
parents: 74294
diff changeset
    72
fun mk_times (t, u) = \<^Const>\<open>Arith.mult for t u\<close>;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    73
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    74
fun mk_prod [] = one
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    75
  | mk_prod [t] = t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    76
  | mk_prod (t :: ts) = if t = one then mk_prod ts
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    77
                        else mk_times (t, mk_prod ts);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    78
74319
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
    79
fun dest_prod tm =
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
    80
  let val (t,u) = \<^Const_fn>\<open>Arith.mult for t u => \<open>(t, u)\<close>\<close> tm
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
    81
  in  dest_prod t @ dest_prod u  end
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
    82
  handle TERM _ => [tm];
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    83
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    84
(*Dummy version: the only arguments are 0 and 1*)
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 20342
diff changeset
    85
fun mk_coeff (0, t) = zero
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    86
  | mk_coeff (1, t) = t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    87
  | mk_coeff _       = raise TERM("mk_coeff", []);
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    88
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    89
(*Dummy version: the "coefficient" is always 1.
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    90
  In the result, the factors are sorted terms*)
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 33317
diff changeset
    91
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
    92
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    93
(*Find first coefficient-term THAT MATCHES u*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    94
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    95
  | find_first_coeff past u (t::terms) =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    96
        let val (n,u') = dest_coeff t
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    97
        in  if u aconv u' then (n, rev past @ terms)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    98
                          else find_first_coeff (t::past) u terms
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
    99
        end
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   100
        handle TERM _ => find_first_coeff (t::past) u terms;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   101
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   102
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   103
(*Simplify #1*n and n*#1 to n*)
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   104
val add_0s = [@{thm add_0_natify}, @{thm add_0_right_natify}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   105
val add_succs = [@{thm add_succ}, @{thm add_succ_right}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   106
val mult_1s = [@{thm mult_1_natify}, @{thm mult_1_right_natify}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   107
val tc_rules = [@{thm natify_in_nat}, @{thm add_type}, @{thm diff_type}, @{thm mult_type}];
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   108
val natifys = [@{thm natify_0}, @{thm natify_ident}, @{thm add_natify1}, @{thm add_natify2},
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   109
               @{thm diff_natify1}, @{thm diff_natify2}];
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   110
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   111
(*Final simplification: cancel + and **)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   112
fun simplify_meta_eq rules ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   113
  let val ctxt' =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   114
    put_simpset FOL_ss ctxt
26287
df8e5362cff9 proper antiquotations;
wenzelm
parents: 24893
diff changeset
   115
      delsimps @{thms iff_simps} (*these could erase the whole rule!*)
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   116
      addsimps rules
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 44947
diff changeset
   117
      |> fold Simplifier.add_eqcong [@{thm eq_cong2}, @{thm iff_cong2}]
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   118
  in mk_meta_eq o simplify ctxt' end;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   119
24893
b8ef7afe3a6b modernized specifications;
wenzelm
parents: 24630
diff changeset
   120
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
   121
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   122
structure CancelNumeralsCommon =
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   123
  struct
14387
e96d5c42c4b0 Polymorphic treatment of binary arithmetic using axclasses
paulson
parents: 13487
diff changeset
   124
  val mk_sum            = (fn T:typ => mk_sum)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   125
  val dest_sum          = dest_sum
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   126
  val mk_coeff          = mk_coeff
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   127
  val dest_coeff        = dest_coeff
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   128
  val find_first_coeff  = find_first_coeff []
18328
841261f303a1 simprocs: static evaluation of simpset;
wenzelm
parents: 17956
diff changeset
   129
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   130
  val norm_ss1 =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   131
    simpset_of (put_simpset ZF_ss \<^context> addsimps add_0s @ add_succs @ mult_1s @ @{thms add_ac})
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   132
  val norm_ss2 =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   133
    simpset_of (put_simpset ZF_ss \<^context> addsimps add_0s @ mult_1s @ @{thms add_ac} @
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   134
      @{thms mult_ac} @ tc_rules @ natifys)
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   135
  fun norm_tac ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   136
    ALLGOALS (asm_simp_tac (put_simpset norm_ss1 ctxt))
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   137
    THEN ALLGOALS (asm_simp_tac (put_simpset norm_ss2 ctxt))
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   138
  val numeral_simp_ss =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   139
    simpset_of (put_simpset ZF_ss \<^context> addsimps add_0s @ tc_rules @ natifys)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   140
  fun numeral_simp_tac ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 45620
diff changeset
   141
    ALLGOALS (asm_simp_tac (put_simpset numeral_simp_ss ctxt))
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   142
  val simplify_meta_eq  = simplify_meta_eq final_rules
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   143
  end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   144
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   145
(** The functor argumnets are declared as separate structures
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   146
    so that they can be exported to ease debugging. **)
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   147
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   148
structure EqCancelNumeralsData =
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   149
  struct
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   150
  open CancelNumeralsCommon
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   151
  val prove_conv = prove_conv "nateq_cancel_numerals"
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   152
  val mk_bal   = FOLogic.mk_eq
9649
89155e48fa53 simproc bug fix: only TYPING assumptions are given to the simplifier
paulson
parents: 9570
diff changeset
   153
  val dest_bal = FOLogic.dest_eq
70470
wenzelm
parents: 69593
diff changeset
   154
  val bal_add1 = @{thm eq_add_iff [THEN iff_trans]}
wenzelm
parents: 69593
diff changeset
   155
  val bal_add2 = @{thm eq_add_iff [THEN iff_trans]}
59530
2a20354c0877 proper context;
wenzelm
parents: 59498
diff changeset
   156
  fun trans_tac ctxt = gen_trans_tac ctxt @{thm iff_trans}
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   157
  end;
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   158
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   159
structure EqCancelNumerals = CancelNumeralsFun(EqCancelNumeralsData);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   160
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   161
structure LessCancelNumeralsData =
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   162
  struct
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   163
  open CancelNumeralsCommon
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   164
  val prove_conv = prove_conv "natless_cancel_numerals"
74297
ac130a6bd6b2 more antiquotations;
wenzelm
parents: 74294
diff changeset
   165
  fun mk_bal (t, u) = \<^Const>\<open>Ordinal.lt for t u\<close>
74319
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
   166
  val dest_bal = \<^Const_fn>\<open>Ordinal.lt for t u => \<open>(t, u)\<close>\<close>
70470
wenzelm
parents: 69593
diff changeset
   167
  val bal_add1 = @{thm less_add_iff [THEN iff_trans]}
wenzelm
parents: 69593
diff changeset
   168
  val bal_add2 = @{thm less_add_iff [THEN iff_trans]}
59530
2a20354c0877 proper context;
wenzelm
parents: 59498
diff changeset
   169
  fun trans_tac ctxt = gen_trans_tac ctxt @{thm iff_trans}
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   170
  end;
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   171
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   172
structure LessCancelNumerals = CancelNumeralsFun(LessCancelNumeralsData);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   173
13462
56610e2ba220 sane interface for simprocs;
wenzelm
parents: 13259
diff changeset
   174
structure DiffCancelNumeralsData =
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   175
  struct
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   176
  open CancelNumeralsCommon
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   177
  val prove_conv = prove_conv "natdiff_cancel_numerals"
74297
ac130a6bd6b2 more antiquotations;
wenzelm
parents: 74294
diff changeset
   178
  fun mk_bal (t, u) = \<^Const>\<open>Arith.diff for t u\<close>
74319
54b2e5f771da clarified signature -- prefer antiquotations (with subtle change of exception content);
wenzelm
parents: 74316
diff changeset
   179
  val dest_bal = \<^Const_fn>\<open>Arith.diff for t u => \<open>(t, u)\<close>\<close>
70470
wenzelm
parents: 69593
diff changeset
   180
  val bal_add1 = @{thm diff_add_eq [THEN trans]}
wenzelm
parents: 69593
diff changeset
   181
  val bal_add2 = @{thm diff_add_eq [THEN trans]}
59530
2a20354c0877 proper context;
wenzelm
parents: 59498
diff changeset
   182
  fun trans_tac ctxt = gen_trans_tac ctxt @{thm trans}
9874
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   183
  end;
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   184
0aa0874ab66b bug fix for arithmetic simprocs (nat & int)
paulson
parents: 9649
diff changeset
   185
structure DiffCancelNumerals = CancelNumeralsFun(DiffCancelNumeralsData);
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   186
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   187
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   188
val nat_cancel =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   189
 [Simplifier.make_simproc \<^context> "nateq_cancel_numerals"
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 59582
diff changeset
   190
   {lhss =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   191
     [\<^term>\<open>l #+ m = n\<close>, \<^term>\<open>l = m #+ n\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   192
      \<^term>\<open>l #* m = n\<close>, \<^term>\<open>l = m #* n\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   193
      \<^term>\<open>succ(m) = n\<close>, \<^term>\<open>m = succ(n)\<close>],
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 61144
diff changeset
   194
    proc = K EqCancelNumerals.proc},
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   195
  Simplifier.make_simproc \<^context> "natless_cancel_numerals"
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 59582
diff changeset
   196
   {lhss =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   197
     [\<^term>\<open>l #+ m < n\<close>, \<^term>\<open>l < m #+ n\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   198
      \<^term>\<open>l #* m < n\<close>, \<^term>\<open>l < m #* n\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   199
      \<^term>\<open>succ(m) < n\<close>, \<^term>\<open>m < succ(n)\<close>],
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 61144
diff changeset
   200
    proc = K LessCancelNumerals.proc},
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   201
  Simplifier.make_simproc \<^context> "natdiff_cancel_numerals"
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 59582
diff changeset
   202
   {lhss =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   203
     [\<^term>\<open>(l #+ m) #- n\<close>, \<^term>\<open>l #- (m #+ n)\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   204
      \<^term>\<open>(l #* m) #- n\<close>, \<^term>\<open>l #- (m #* n)\<close>,
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 62913
diff changeset
   205
      \<^term>\<open>succ(m) #- n\<close>, \<^term>\<open>m #- succ(n)\<close>],
62913
13252110a6fe eliminated unused simproc identifier;
wenzelm
parents: 61144
diff changeset
   206
    proc = K DiffCancelNumerals.proc}];
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   207
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   208
end;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   209
54388
8b165615ffe3 tuned signature -- removed obsolete Addsimprocs, Delsimprocs;
wenzelm
parents: 51717
diff changeset
   210
val _ =
8b165615ffe3 tuned signature -- removed obsolete Addsimprocs, Delsimprocs;
wenzelm
parents: 51717
diff changeset
   211
  Theory.setup (Simplifier.map_theory_simpset (fn ctxt =>
8b165615ffe3 tuned signature -- removed obsolete Addsimprocs, Delsimprocs;
wenzelm
parents: 51717
diff changeset
   212
    ctxt addsimprocs ArithData.nat_cancel));
13259
01fa0c8dbc92 conversion of many files to Isar format
paulson
parents: 13155
diff changeset
   213
01fa0c8dbc92 conversion of many files to Isar format
paulson
parents: 13155
diff changeset
   214
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   215
(*examples:
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   216
print_depth 22;
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   217
set timing;
40878
7695e4de4d86 renamed trace_simp to simp_trace, and debug_simp to simp_debug;
wenzelm
parents: 38715
diff changeset
   218
set simp_trace;
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   219
fun test s = (Goal s; by (Asm_simp_tac 1));
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   220
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   221
test "x #+ y = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   222
test "y #+ x = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   223
test "x #+ y #+ z = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   224
test "y #+ (z #+ x) = z #+ x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   225
test "x #+ y #+ z = (z #+ y) #+ (x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   226
test "x#*y #+ z = (z #+ y) #+ (y#*x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   227
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   228
test "x #+ succ(y) = x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   229
test "x #+ succ(y) = succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   230
test "succ(x) #+ succ(y) #+ z = succ(z #+ y) #+ succ(x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   231
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   232
test "(x #+ y) #- (x #+ z) = w";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   233
test "(y #+ x) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   234
test "(x #+ y #+ z) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   235
test "(y #+ (z #+ x)) #- (z #+ x) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   236
test "(x #+ y #+ z) #- ((z #+ y) #+ (x #+ w)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   237
test "(x#*y #+ z) #- ((z #+ y) #+ (y#*x #+ w)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   238
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   239
(*BAD occurrence of natify*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   240
test "(x #+ succ(y)) #- (x #+ z) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   241
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   242
test "x #* y2 #+ y #* x2 = y #* x2 #+ x #* y2";
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
test "(x #+ succ(y)) #- (succ(z #+ x)) = dd";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   245
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
   246
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   247
(*use of typing information*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   248
test "x : nat ==> x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   249
test "x : nat --> x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   250
test "x : nat ==> x #+ y < x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   251
test "x : nat ==> x < y#+x";
13126
97e83120d6c8 fixed simproc bug
paulson
parents: 12206
diff changeset
   252
test "x : nat ==> x le succ(x)";
9548
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   253
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   254
(*fails: no typing information isn't visible*)
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   255
test "x #+ y = x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   256
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   257
test "x #+ y < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   258
test "y #+ x < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   259
test "x #+ y #+ z < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   260
test "y #+ z #+ x < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   261
test "y #+ (z #+ x) < z #+ x";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   262
test "x #+ y #+ z < (z #+ y) #+ (x #+ w)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   263
test "x#*y #+ z < (z #+ y) #+ (y#*x #+ w)";
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 #+ succ(y) < x #+ z";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   266
test "x #+ succ(y) < succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   267
test "succ(x) #+ succ(y) #+ z < succ(z #+ y) #+ succ(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) le succ(z #+ x)";
15bee2731e43 instantiated Cancel_Numerals for "nat" in ZF
paulson
parents:
diff changeset
   270
*)