src/Provers/Arith/cancel_numerals.ML
author paulson
Fri Apr 28 10:44:03 2000 +0200 (2000-04-28)
changeset 8772 ebb07113c4f7
parent 8760 9139453d7033
child 8779 2d4afbc46801
permissions -rw-r--r--
inserted triviality check
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@8760
    40
  val norm_tac: tactic
paulson@8760
    41
  val numeral_simp_tac: tactic
paulson@8736
    42
end;
paulson@8736
    43
paulson@8736
    44
paulson@8760
    45
functor CancelNumeralsFun(Data: CANCEL_NUMERALS_DATA):
paulson@8760
    46
  sig
paulson@8760
    47
  val proc: Sign.sg -> thm list -> term -> thm option
paulson@8760
    48
  end 
paulson@8760
    49
=
paulson@8736
    50
struct
paulson@8736
    51
paulson@8760
    52
fun listof None = []
paulson@8760
    53
  | listof (Some x) = [x];
paulson@8760
    54
paulson@8760
    55
(*If t = #n*u then put u in the table*)
paulson@8760
    56
fun update_by_coeff (tab, t) =
paulson@8760
    57
  Termtab.update ((#2 (Data.dest_coeff t), ()), tab)
paulson@8760
    58
  handle TERM _ => tab;
paulson@8760
    59
paulson@8760
    60
(*a left-to-right scan of terms1, seeking a term of the form #n*u, where
paulson@8760
    61
  #m*u is in terms2 for some m*)
paulson@8760
    62
fun find_common (terms1,terms2) =
paulson@8760
    63
  let val tab2 = foldl update_by_coeff (Termtab.empty, terms2)
paulson@8760
    64
      fun seek [] = raise TERM("find_common", []) 
paulson@8760
    65
	| seek (t::terms) =
paulson@8760
    66
	      let val (_,u) = Data.dest_coeff t 
paulson@8760
    67
	      in  if is_some (Termtab.lookup (tab2, u)) then u
paulson@8760
    68
		  else seek terms
paulson@8760
    69
	      end
paulson@8760
    70
	      handle TERM _ => seek terms
paulson@8760
    71
  in  seek terms1 end;
paulson@8736
    72
paulson@8736
    73
(*the simplification procedure*)
paulson@8736
    74
fun proc sg _ t =
paulson@8736
    75
  let val (t1,t2) = Data.dest_bal t 
paulson@8760
    76
      val terms1 = Data.dest_sum t1
paulson@8760
    77
      and terms2 = Data.dest_sum t2
paulson@8760
    78
      val u = find_common (terms1,terms2)
paulson@8760
    79
      val (n1, terms1') = Data.find_first_coeff u terms1
paulson@8760
    80
      and (n2, terms2') = Data.find_first_coeff u terms2
paulson@8760
    81
      fun newshape (i,terms) = Data.mk_sum (Data.mk_coeff(i,u)::terms)
paulson@8760
    82
      val reshapes =  (*Move i*u to the front and put j*u into standard form
paulson@8760
    83
		      i + #m + j + k == #m + i + (j + k) *)
paulson@8772
    84
	    if n1=0 orelse n2=0 then   (*trivial, so do nothing*)
paulson@8772
    85
		raise TERM("cancel_numerals", []) 
paulson@8772
    86
	    else
paulson@8772
    87
		listof (Data.prove_conv [Data.norm_tac] sg 
paulson@8772
    88
			(t, 
paulson@8772
    89
			 Data.mk_bal (newshape(n1,terms1'), 
paulson@8772
    90
				      newshape(n2,terms2'))))
paulson@8736
    91
  in
paulson@8760
    92
paulson@8760
    93
      if n2<=n1 then 
paulson@8760
    94
	  Data.prove_conv 
paulson@8760
    95
	     [rewrite_goals_tac reshapes, rtac Data.bal_add1 1,
paulson@8760
    96
	      Data.numeral_simp_tac] sg
paulson@8760
    97
	     (t, Data.mk_bal (newshape(n1-n2,terms1'), Data.mk_sum terms2'))
paulson@8760
    98
      else
paulson@8760
    99
	  Data.prove_conv 
paulson@8760
   100
	     [rewrite_goals_tac reshapes, rtac Data.bal_add2 1,
paulson@8760
   101
	      Data.numeral_simp_tac] sg
paulson@8760
   102
	     (t, Data.mk_bal (Data.mk_sum terms1', newshape(n2-n1,terms2')))
paulson@8736
   103
  end
paulson@8760
   104
  handle TERM _ => None;
paulson@8736
   105
paulson@8736
   106
end;