src/HOL/Integ/simproc.ML
author paulson
Tue, 29 Sep 1998 15:57:42 +0200
changeset 5582 a356fb49e69e
parent 5553 ae42b36a50c2
child 5595 d283d6120548
permissions -rw-r--r--
many renamings and changes. Simproc for cancelling common terms in relations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     1
(*  Title:      HOL/Integ/simproc
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     2
    ID:         $Id$
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     4
    Copyright   1998  University of Cambridge
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     5
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
     6
Simplification procedures for abelian groups (e.g. integers, reals)
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     7
*)
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     8
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
     9
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    10
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    11
(*Deletion of other terms in the formula, seeking the -x at the front of z*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    12
Goal "((x::int) + (y + z) = y + u) = ((x + z) = u)";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    13
by (stac zadd_left_commute 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    14
by (rtac zadd_left_cancel 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    15
qed "zadd_cancel_21";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    16
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    17
(*A further rule to deal with the case that
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    18
  everything gets cancelled on the right.*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    19
Goal "((x::int) + (y + z) = y) = (x = -z)";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    20
by (stac zadd_left_commute 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    21
by (res_inst_tac [("t", "y")] (zadd_nat0_right RS subst) 1
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    22
    THEN stac zadd_left_cancel 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    23
by (simp_tac (simpset() addsimps [eq_zdiff_eq RS sym]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    24
qed "zadd_cancel_minus";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    25
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    26
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    27
val prepare_ss = HOL_ss addsimps [zadd_assoc, zdiff_def, 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    28
				  zminus_zadd_distrib, zminus_zminus,
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    29
				  zminus_nat0, zadd_nat0, zadd_nat0_right];
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    30
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    31
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    32
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    33
(*prove while suppressing timing information*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    34
fun prove ct = setmp Goals.proof_timing false (prove_goalw_cterm [] ct);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    35
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    36
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    37
(*This one cancels complementary terms in sums.  Examples:
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    38
   x-x = 0    x+(y-x) = y   -x+(y+(x+z)) = y+z
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    39
  It will unfold the definition of diff and associate to the right if 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    40
  necessary.  With big formulae, rewriting is faster if the formula is already
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    41
  in that form.
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    42
*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    43
structure Sum_Cancel =
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    44
struct
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    45
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    46
val thy = IntDef.thy;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    47
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    48
val intT = Type ("IntDef.int", []);
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    49
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    50
val zplus = Const ("op +", [intT,intT] ---> intT);
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    51
val zminus = Const ("uminus", intT --> intT);
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    52
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    53
val zero = Const ("IntDef.int", HOLogic.natT --> intT) $ HOLogic.zero;
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    54
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    55
(*These rules eliminate the first two terms, if they cancel*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    56
val cancel_laws = 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    57
    [zadd_zminus_inverse_nat, zadd_zminus_inverse_nat2,
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    58
     zadd_zminus_cancel, zminus_zadd_cancel, 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    59
     zadd_cancel_21, zadd_cancel_minus, zminus_zminus];
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    60
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    61
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    62
val cancel_ss = HOL_ss addsimps cancel_laws;
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    63
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    64
fun mk_sum []  = zero
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    65
  | mk_sum tms = foldr1 (fn (x,y) => zplus $ x $ y) tms;
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    66
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    67
(*We map -t to t and (in other cases) t to -t.  No need to check the type of
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    68
  uminus, since the simproc is only called on integer sums.*)
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    69
fun negate (Const("uminus",_) $ t) = t
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    70
  | negate t                       = zminus $ t;
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    71
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    72
(*Flatten a formula built from +, binary - and unary -.
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    73
  No need to check types PROVIDED they are checked upon entry!*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    74
fun add_terms neg (Const ("op +", _) $ x $ y, ts) =
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    75
	add_terms neg (x, add_terms neg (y, ts))
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    76
  | add_terms neg (Const ("op -", _) $ x $ y, ts) =
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    77
	add_terms neg (x, add_terms (not neg) (y, ts))
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    78
  | add_terms neg (Const ("uminus", _) $ x, ts) = 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    79
	add_terms (not neg) (x, ts)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    80
  | add_terms neg (x, ts) = 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    81
	(if neg then negate x else x) :: ts;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    82
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    83
fun terms fml = add_terms false (fml, []);
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    84
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    85
exception Cancel;
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    86
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    87
(*Cancels just the first occurrence of head', leaving the rest unchanged*)
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    88
fun cancelled head' tail =
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    89
    let fun find ([], _) = raise Cancel
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    90
	  | find (t::ts, heads) = if head' aconv t then rev heads @ ts
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    91
				  else find (ts, t::heads)
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    92
    in  mk_sum (find (tail, []))   end;
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    93
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    94
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    95
val trace = ref false;
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    96
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
    97
fun proc sg _ lhs =
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    98
  let val _ = if !trace then writeln ("cancel_sums: LHS = " ^ 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
    99
				      string_of_cterm (cterm_of sg lhs))
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   100
              else ()
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   101
      val (head::tail) = terms lhs
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   102
      val head' = negate head
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   103
      val rhs = cancelled head' tail
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   104
      and chead' = Thm.cterm_of sg head'
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   105
      val _ = if !trace then 
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   106
	        writeln ("RHS = " ^ string_of_cterm (Thm.cterm_of sg rhs))
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   107
              else ()
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   108
      val ct = Thm.cterm_of sg (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)))
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   109
      val thm = prove ct 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   110
	          (fn _ => [simp_tac prepare_ss 1,
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   111
			    IF_UNSOLVED (simp_tac cancel_ss 1)])
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   112
	handle ERROR =>
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   113
	error("cancel_sums simproc:\nThe error(s) above occurred while trying to prove " ^
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   114
	      string_of_cterm ct)
5553
ae42b36a50c2 renamed mk_meta_eq to mk_eq
oheimb
parents: 5501
diff changeset
   115
  in Some (mk_meta_eq thm) end
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   116
  handle Cancel => None;
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   117
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   118
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   119
val conv = 
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   120
    Simplifier.mk_simproc "cancel_sums"
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   121
      (map (Thm.read_cterm (sign_of thy)) 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   122
       [("x + y", intT), ("x - y", intT)])
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   123
      proc;
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   124
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   125
end;
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   126
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   127
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   128
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   129
Addsimprocs [Sum_Cancel.conv];
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   130
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   131
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   132
(** The idea is to cancel like terms on opposite sides via subtraction **)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   133
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   134
Goal "(x::int) - y = x' - y' ==> (x<y) = (x'<y')";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   135
by (asm_simp_tac (simpset() addsimps [zless_def]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   136
qed "zless_eqI";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   137
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   138
Goal "(x::int) - y = x' - y' ==> (y<=x) = (y'<=x')";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   139
bd zless_eqI 1;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   140
by (asm_simp_tac (simpset() addsimps [zle_def]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   141
qed "zle_eqI";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   142
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   143
Goal "(x::int) - y = x' - y' ==> (x=y) = (x'=y')";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   144
by Safe_tac;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   145
by (asm_full_simp_tac (simpset() addsimps [eq_zdiff_eq]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   146
by (asm_full_simp_tac (simpset() addsimps [zdiff_eq_eq]) 1);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   147
qed "zeq_eqI";
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   148
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   149
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   150
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   151
(** For simplifying relations **)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   152
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   153
structure Rel_Cancel =
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   154
struct
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   155
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   156
(*Cancel the FIRST occurrence of a term.  If it's repeated, then repeated
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   157
  calls to the simproc will be needed.*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   158
fun cancel1 ([], u)    = raise Match (*impossible: it's a common term*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   159
  | cancel1 (t::ts, u) = if t aconv u then ts
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   160
                         else t :: cancel1 (ts,u);
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   161
    
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   162
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   163
exception Relation;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   164
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   165
val trace = ref false;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   166
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   167
val sum_cancel_ss = HOL_ss addsimprocs [Sum_Cancel.conv]
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   168
                           addsimps [zadd_nat0, zadd_nat0_right];
5501
a63e0c326e6c new files in Integ
paulson
parents:
diff changeset
   169
5582
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   170
fun proc sg _ (lhs as (rel$lt$rt)) =
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   171
  let val _ = if !trace then writeln ("cancel_relations: LHS = " ^ 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   172
				      string_of_cterm (cterm_of sg lhs))
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   173
              else ()
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   174
      val ltms = Sum_Cancel.terms lt
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   175
      and rtms = Sum_Cancel.terms rt
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   176
      val common = gen_distinct (op aconv)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   177
	               (inter_term (ltms, rtms))
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   178
      val _ = if null common then raise Relation  (*nothing to do*)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   179
	                          else ()
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   180
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   181
      fun cancelled tms = Sum_Cancel.mk_sum (foldl cancel1 (tms, common))
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   182
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   183
      val lt' = cancelled ltms
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   184
      and rt' = cancelled rtms
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   185
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   186
      val rhs = rel$lt'$rt'
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   187
      val _ = if !trace then 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   188
	        writeln ("RHS = " ^ string_of_cterm (Thm.cterm_of sg rhs))
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   189
              else ()
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   190
      val ct = Thm.cterm_of sg (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)))
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   191
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   192
      val thm = prove ct 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   193
	          (fn _ => [resolve_tac [zless_eqI, zeq_eqI, zle_eqI] 1,
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   194
			    simp_tac prepare_ss 1,
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   195
			    simp_tac sum_cancel_ss 1,
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   196
			    IF_UNSOLVED
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   197
			      (simp_tac (HOL_ss addsimps zadd_ac) 1)])
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   198
	handle ERROR =>
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   199
	error("cancel_relations simproc:\nThe error(s) above occurred while trying to prove " ^
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   200
	      string_of_cterm ct)
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   201
  in Some (mk_meta_eq thm) end
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   202
  handle Relation => None;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   203
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   204
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   205
val conv = 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   206
    Simplifier.mk_simproc "cancel_relations"
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   207
      (map (Thm.read_cterm (sign_of thy)) 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   208
       [("x < (y::int)", HOLogic.boolT), 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   209
	("x = (y::int)", HOLogic.boolT), 
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   210
	("x <= (y::int)", HOLogic.boolT)])
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   211
      proc;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   212
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   213
end;
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   214
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   215
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   216
a356fb49e69e many renamings and changes. Simproc for cancelling common terms in relations
paulson
parents: 5553
diff changeset
   217
Addsimprocs [Rel_Cancel.conv];