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