src/Provers/Arith/cancel_numeral_factor.ML
author wenzelm
Fri, 08 Mar 2002 16:24:06 +0100
changeset 13049 ce180e5b7fa0
parent 10656 68f3fddd6e24
child 13484 d8f5d3391766
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10537
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     1
(*  Title:      Provers/Arith/cancel_numeral_factor.ML
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     2
    ID:         $Id$
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     4
    Copyright   2000  University of Cambridge
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     5
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     6
Cancel common coefficients in balanced expressions:
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     7
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     8
     u*#m ~~ u'*#m'  ==  #n*u ~~ #n'*u'
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
     9
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    10
where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    11
and d = gcd(m,m') and n=m/d and n'=m'/d.
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    12
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    13
It works by (a) massaging both sides to bring gcd(m,m') to the front:
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    14
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    15
     u*#m ~~ u'*#m'  ==  #d*(#n*u) ~~ #d*(#n'*u')
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    16
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    17
(b) then using the rule "cancel" to reach #n*u ~~ #n'*u'.
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    18
*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    19
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    20
signature CANCEL_NUMERAL_FACTOR_DATA =
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    21
sig
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    22
  (*abstract syntax*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    23
  val mk_bal: term * term -> term
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    24
  val dest_bal: term -> term * term
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    25
  val mk_coeff: int * term -> term
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    26
  val dest_coeff: term -> int * term
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    27
  (*rules*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    28
  val cancel: thm
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    29
  val neg_exchanges: bool  (*true if a negative coeff swaps the two operands,
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    30
                             as with < and <= but not = and div*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    31
  (*proof tools*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    32
  val prove_conv: tactic list -> Sign.sg -> 
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    33
                  thm list -> term * term -> thm option
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    34
  val trans_tac: thm option -> tactic (*applies the initial lemma*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    35
  val norm_tac: tactic                (*proves the initial lemma*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    36
  val numeral_simp_tac: tactic        (*proves the final theorem*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    37
  val simplify_meta_eq: thm -> thm    (*simplifies the final theorem*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    38
end;
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    39
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    40
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    41
functor CancelNumeralFactorFun(Data: CANCEL_NUMERAL_FACTOR_DATA):
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    42
  sig
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    43
  val proc: Sign.sg -> thm list -> term -> thm option
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    44
  end 
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    45
=
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    46
struct
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    47
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    48
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    49
(* greatest common divisor *)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    50
fun gcd (0, n) = abs n
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    51
  | gcd (m, n) = gcd (n mod m, m);
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    52
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    53
(*the simplification procedure*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    54
fun proc sg hyps t =
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    55
  let (*first freeze any Vars in the term to prevent flex-flex problems*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    56
      val rand_s = gensym"_"
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    57
      fun mk_inst (var as Var((a,i),T))  = 
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    58
	    (var,  Free((a ^ rand_s ^ string_of_int i), T))
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    59
      val t' = subst_atomic (map mk_inst (term_vars t)) t
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    60
      val (t1,t2) = Data.dest_bal t' 
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    61
      val (m1, t1') = Data.dest_coeff t1
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    62
      and (m2, t2') = Data.dest_coeff t2
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    63
      val d = (*if both are negative, also divide through by ~1*)
10656
68f3fddd6e24 tries harder to remove negative literals, e.g.
paulson
parents: 10537
diff changeset
    64
          if (m1<0 andalso m2<=0) orelse
68f3fddd6e24 tries harder to remove negative literals, e.g.
paulson
parents: 10537
diff changeset
    65
             (m1<=0 andalso m2<0) then ~ (gcd(m1,m2)) else gcd(m1,m2)
10537
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    66
      val _ = if d=1 then   (*trivial, so do nothing*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    67
		      raise TERM("cancel_numeral_factor", []) 
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    68
              else ()
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    69
      fun newshape (i,t) = Data.mk_coeff(d, Data.mk_coeff(i,t))
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    70
      val n1 = m1 div d and n2 = m2 div d
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    71
      val rhs = if d<0 andalso Data.neg_exchanges
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    72
                then Data.mk_bal (Data.mk_coeff(n2,t2'), Data.mk_coeff(n1,t1'))
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    73
                else Data.mk_bal (Data.mk_coeff(n1,t1'), Data.mk_coeff(n2,t2'))
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    74
      val reshape =  (*Move d to the front and put the rest into standard form
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    75
		       i * #m * j == #d * (#n * (j * k)) *)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    76
	    Data.prove_conv [Data.norm_tac] sg hyps 
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    77
	      (t',   Data.mk_bal (newshape(n1,t1'), newshape(n2,t2')))
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    78
  in
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    79
      apsome Data.simplify_meta_eq
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    80
       (Data.prove_conv 
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    81
	       [Data.trans_tac reshape, rtac Data.cancel 1,
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    82
		Data.numeral_simp_tac] sg hyps (t', rhs))
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    83
  end
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    84
  handle TERM _ => None
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    85
       | TYPE _ => None;   (*Typically (if thy doesn't include Numeral)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    86
			     Undeclared type constructor "Numeral.bin"*)
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    87
1d2f15504d38 simproc for cancelling common factors around = < <= div /
paulson
parents:
diff changeset
    88
end;