src/Provers/Arith/cancel_numerals.ML
author paulson
Fri, 28 Apr 2000 10:44:03 +0200
changeset 8772 ebb07113c4f7
parent 8760 9139453d7033
child 8779 2d4afbc46801
permissions -rw-r--r--
inserted triviality check
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
     1
(*  Title:      Provers/Arith/cancel_numerals.ML
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
     2
    ID:         $Id$
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
     4
    Copyright   2000  University of Cambridge
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
     5
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
     6
Cancel common coefficients in balanced expressions:
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
     7
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
     8
     i + #m*u + j ~~ i' + #m'*u + j'  ==  #(m-m')*u + i + j ~~ i' + j'
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
     9
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    10
where ~~ is an appropriate balancing operation (e.g. =, <=, <, -).
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    11
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    12
It works by (a) massaging both sides to bring the selected term to the front:
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    13
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    14
     #m*u + (i + j) ~~ #m'*u + (i' + j') 
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    15
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    16
(b) then using bal_add1 or bal_add2 to reach
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    17
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    18
     #(m-m')*u + i + j ~~ i' + j'       (if m'<=m)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    19
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    20
or
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    21
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    22
     i + j ~~ #(m'-m)*u + i' + j'       (otherwise)
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    23
*)
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    24
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    25
signature CANCEL_NUMERALS_DATA =
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    26
sig
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    27
  (*abstract syntax*)
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    28
  val mk_sum: term list -> term
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    29
  val dest_sum: term -> term list
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    30
  val mk_bal: term * term -> term
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    31
  val dest_bal: term -> term * term
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    32
  val mk_coeff: int * term -> term
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    33
  val dest_coeff: term -> int * term
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    34
  val find_first_coeff: term -> term list -> int * term list
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    35
  (*rules*)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    36
  val bal_add1: thm
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    37
  val bal_add2: thm
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    38
  (*proof tools*)
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    39
  val prove_conv: tactic list -> Sign.sg -> term * term -> thm option
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    40
  val norm_tac: tactic
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    41
  val numeral_simp_tac: tactic
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    42
end;
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    43
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    44
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    45
functor CancelNumeralsFun(Data: CANCEL_NUMERALS_DATA):
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    46
  sig
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    47
  val proc: Sign.sg -> thm list -> term -> thm option
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    48
  end 
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    49
=
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    50
struct
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    51
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    52
fun listof None = []
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    53
  | listof (Some x) = [x];
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    54
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    55
(*If t = #n*u then put u in the table*)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    56
fun update_by_coeff (tab, t) =
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    57
  Termtab.update ((#2 (Data.dest_coeff t), ()), tab)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    58
  handle TERM _ => tab;
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    59
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    60
(*a left-to-right scan of terms1, seeking a term of the form #n*u, where
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    61
  #m*u is in terms2 for some m*)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    62
fun find_common (terms1,terms2) =
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    63
  let val tab2 = foldl update_by_coeff (Termtab.empty, terms2)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    64
      fun seek [] = raise TERM("find_common", []) 
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    65
	| seek (t::terms) =
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    66
	      let val (_,u) = Data.dest_coeff t 
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    67
	      in  if is_some (Termtab.lookup (tab2, u)) then u
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    68
		  else seek terms
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    69
	      end
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    70
	      handle TERM _ => seek terms
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    71
  in  seek terms1 end;
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    72
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    73
(*the simplification procedure*)
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    74
fun proc sg _ t =
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    75
  let val (t1,t2) = Data.dest_bal t 
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    76
      val terms1 = Data.dest_sum t1
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    77
      and terms2 = Data.dest_sum t2
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    78
      val u = find_common (terms1,terms2)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    79
      val (n1, terms1') = Data.find_first_coeff u terms1
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    80
      and (n2, terms2') = Data.find_first_coeff u terms2
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    81
      fun newshape (i,terms) = Data.mk_sum (Data.mk_coeff(i,u)::terms)
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    82
      val reshapes =  (*Move i*u to the front and put j*u into standard form
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    83
		      i + #m + j + k == #m + i + (j + k) *)
8772
ebb07113c4f7 inserted triviality check
paulson
parents: 8760
diff changeset
    84
	    if n1=0 orelse n2=0 then   (*trivial, so do nothing*)
ebb07113c4f7 inserted triviality check
paulson
parents: 8760
diff changeset
    85
		raise TERM("cancel_numerals", []) 
ebb07113c4f7 inserted triviality check
paulson
parents: 8760
diff changeset
    86
	    else
ebb07113c4f7 inserted triviality check
paulson
parents: 8760
diff changeset
    87
		listof (Data.prove_conv [Data.norm_tac] sg 
ebb07113c4f7 inserted triviality check
paulson
parents: 8760
diff changeset
    88
			(t, 
ebb07113c4f7 inserted triviality check
paulson
parents: 8760
diff changeset
    89
			 Data.mk_bal (newshape(n1,terms1'), 
ebb07113c4f7 inserted triviality check
paulson
parents: 8760
diff changeset
    90
				      newshape(n2,terms2'))))
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
    91
  in
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    92
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    93
      if n2<=n1 then 
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    94
	  Data.prove_conv 
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    95
	     [rewrite_goals_tac reshapes, rtac Data.bal_add1 1,
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    96
	      Data.numeral_simp_tac] sg
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    97
	     (t, Data.mk_bal (newshape(n1-n2,terms1'), Data.mk_sum terms2'))
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    98
      else
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
    99
	  Data.prove_conv 
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
   100
	     [rewrite_goals_tac reshapes, rtac Data.bal_add2 1,
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
   101
	      Data.numeral_simp_tac] sg
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
   102
	     (t, Data.mk_bal (Data.mk_sum terms1', newshape(n2-n1,terms2')))
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
   103
  end
8760
9139453d7033 now works for coefficients, not just for numerals
paulson
parents: 8736
diff changeset
   104
  handle TERM _ => None;
8736
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
   105
0bfd6678a5fa new simprocs for numerals of type "nat"
paulson
parents:
diff changeset
   106
end;