8736
|
1 |
(* Title: Provers/Arith/cancel_sums.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
Copyright 2000 University of Cambridge
|
|
5 |
|
|
6 |
Cancel common literals in balanced expressions:
|
|
7 |
|
|
8 |
i + #m + j ~~ i' + #m' + j' == #(m-m') + i + j ~~ i' + j'
|
|
9 |
|
|
10 |
where ~~ is an appropriate balancing operation (e.g. =, <=, <, -).
|
|
11 |
*)
|
|
12 |
|
|
13 |
signature CANCEL_NUMERALS_DATA =
|
|
14 |
sig
|
|
15 |
(*abstract syntax*)
|
|
16 |
val mk_numeral: int -> term
|
|
17 |
val find_first_numeral: term list -> int * term * term list
|
|
18 |
val mk_sum: term list -> term
|
|
19 |
val dest_sum: term -> term list
|
|
20 |
val mk_bal: term * term -> term
|
|
21 |
val dest_bal: term -> term * term
|
|
22 |
(*proof tools*)
|
|
23 |
val prove_conv: tactic list -> Sign.sg -> term * term -> thm option
|
|
24 |
val subst_tac: term -> tactic
|
|
25 |
val all_simp_tac: tactic
|
|
26 |
end;
|
|
27 |
|
|
28 |
signature CANCEL_NUMERALS =
|
|
29 |
sig
|
|
30 |
val proc: Sign.sg -> thm list -> term -> thm option
|
|
31 |
end;
|
|
32 |
|
|
33 |
|
|
34 |
functor CancelNumeralsFun(Data: CANCEL_NUMERALS_DATA): CANCEL_NUMERALS =
|
|
35 |
struct
|
|
36 |
|
|
37 |
(*predicting the outputs of other simprocs given a term of the form
|
|
38 |
(i + ... #m + ... j) - #n *)
|
|
39 |
fun cancelled m n terms =
|
|
40 |
if m = n then (*cancel_sums: sort the terms*)
|
|
41 |
sort Term.term_ord terms
|
|
42 |
else (*inverse_fold: subtract, keeping original term order*)
|
|
43 |
Data.mk_numeral (m - n) :: terms;
|
|
44 |
|
|
45 |
(*the simplification procedure*)
|
|
46 |
fun proc sg _ t =
|
|
47 |
let val (t1,t2) = Data.dest_bal t
|
|
48 |
val (n1, lit1, terms1) = Data.find_first_numeral (Data.dest_sum t1)
|
|
49 |
and (n2, lit2, terms2) = Data.find_first_numeral (Data.dest_sum t2)
|
|
50 |
val lit_n = if n1<n2 then lit1 else lit2
|
|
51 |
and n = BasisLibrary.Int.min (n1,n2)
|
|
52 |
(*having both the literals and their integer values makes it
|
|
53 |
more robust against negative natural number literals*)
|
|
54 |
in
|
|
55 |
Data.prove_conv [Data.subst_tac lit_n, Data.all_simp_tac] sg
|
|
56 |
(t, Data.mk_bal (Data.mk_sum (cancelled n1 n terms1),
|
|
57 |
Data.mk_sum (cancelled n2 n terms2)))
|
|
58 |
end
|
|
59 |
handle _ => None;
|
|
60 |
|
|
61 |
end;
|