src/Provers/Arith/cancel_numerals.ML
author skalberg
Thu Mar 03 12:43:01 2005 +0100 (2005-03-03)
changeset 15570 8d8c70b41bab
parent 15531 08c8dad8e399
child 15965 f422f8283491
permissions -rw-r--r--
Move towards standard functions.
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@14387
    28
  val mk_sum: typ -> 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@9546
    39
  val prove_conv: tactic list -> Sign.sg -> 
wenzelm@13484
    40
                  thm list -> string list -> term * term -> thm option
paulson@8799
    41
  val trans_tac: thm option -> tactic (*applies the initial lemma*)
paulson@8799
    42
  val norm_tac: tactic                (*proves the initial lemma*)
paulson@8799
    43
  val numeral_simp_tac: tactic        (*proves the final theorem*)
paulson@8799
    44
  val simplify_meta_eq: thm -> thm    (*simplifies the final theorem*)
paulson@8736
    45
end;
paulson@8736
    46
paulson@8736
    47
paulson@8760
    48
functor CancelNumeralsFun(Data: CANCEL_NUMERALS_DATA):
paulson@8760
    49
  sig
wenzelm@15027
    50
  val proc: Sign.sg -> simpset -> term -> thm option
paulson@8760
    51
  end 
paulson@8760
    52
=
paulson@8736
    53
struct
paulson@8736
    54
paulson@8779
    55
(*For t = #n*u then put u in the table*)
paulson@8760
    56
fun update_by_coeff (tab, t) =
paulson@8779
    57
  Termtab.update ((#2 (Data.dest_coeff t), ()), tab);
paulson@8760
    58
paulson@8760
    59
(*a left-to-right scan of terms1, seeking a term of the form #n*u, where
paulson@8760
    60
  #m*u is in terms2 for some m*)
paulson@8760
    61
fun find_common (terms1,terms2) =
skalberg@15570
    62
  let val tab2 = Library.foldl update_by_coeff (Termtab.empty, terms2)
paulson@8760
    63
      fun seek [] = raise TERM("find_common", []) 
paulson@8760
    64
	| seek (t::terms) =
paulson@8760
    65
	      let val (_,u) = Data.dest_coeff t 
skalberg@15570
    66
	      in  if isSome (Termtab.lookup (tab2, u)) then u
paulson@8760
    67
		  else seek terms
paulson@8760
    68
	      end
paulson@8760
    69
  in  seek terms1 end;
paulson@8736
    70
paulson@8736
    71
(*the simplification procedure*)
wenzelm@15027
    72
fun proc sg ss t =
wenzelm@15027
    73
  let
wenzelm@15027
    74
      val hyps = prems_of_ss ss;
wenzelm@15027
    75
      (*first freeze any Vars in the term to prevent flex-flex problems*)
wenzelm@13484
    76
      val (t', xs) = Term.adhoc_freeze_vars t;
paulson@9191
    77
      val (t1,t2) = Data.dest_bal t' 
paulson@8760
    78
      val terms1 = Data.dest_sum t1
paulson@8760
    79
      and terms2 = Data.dest_sum t2
paulson@8760
    80
      val u = find_common (terms1,terms2)
paulson@8760
    81
      val (n1, terms1') = Data.find_first_coeff u terms1
paulson@8760
    82
      and (n2, terms2') = Data.find_first_coeff u terms2
paulson@14387
    83
      and T = Term.fastype_of u
paulson@14387
    84
      fun newshape (i,terms) = Data.mk_sum T (Data.mk_coeff(i,u)::terms)
paulson@8779
    85
      val reshape =  (*Move i*u to the front and put j*u into standard form
paulson@8779
    86
		       i + #m + j + k == #m + i + (j + k) *)
paulson@8772
    87
	    if n1=0 orelse n2=0 then   (*trivial, so do nothing*)
paulson@8772
    88
		raise TERM("cancel_numerals", []) 
wenzelm@13484
    89
	    else Data.prove_conv [Data.norm_tac] sg hyps xs
paulson@9191
    90
			(t', 
paulson@8772
    91
			 Data.mk_bal (newshape(n1,terms1'), 
paulson@8779
    92
				      newshape(n2,terms2')))
paulson@8736
    93
  in
skalberg@15570
    94
      Option.map Data.simplify_meta_eq
paulson@8799
    95
       (if n2<=n1 then 
paulson@8799
    96
	    Data.prove_conv 
paulson@8799
    97
	       [Data.trans_tac reshape, rtac Data.bal_add1 1,
wenzelm@13484
    98
		Data.numeral_simp_tac] sg hyps xs
paulson@9191
    99
	       (t', Data.mk_bal (newshape(n1-n2,terms1'), 
paulson@14387
   100
				 Data.mk_sum T terms2'))
paulson@8799
   101
	else
paulson@8799
   102
	    Data.prove_conv 
paulson@8799
   103
	       [Data.trans_tac reshape, rtac Data.bal_add2 1,
wenzelm@13484
   104
		Data.numeral_simp_tac] sg hyps xs
paulson@14387
   105
	       (t', Data.mk_bal (Data.mk_sum T terms1', 
paulson@9191
   106
				 newshape(n2-n1,terms2'))))
paulson@8736
   107
  end
skalberg@15531
   108
  handle TERM _ => NONE
skalberg@15531
   109
       | TYPE _ => NONE;   (*Typically (if thy doesn't include Numeral)
paulson@8779
   110
			     Undeclared type constructor "Numeral.bin"*)
paulson@8736
   111
paulson@8736
   112
end;