author | paulson |
Sun, 15 Feb 2004 10:46:37 +0100 | |
changeset 14387 | e96d5c42c4b0 |
parent 13484 | d8f5d3391766 |
child 15027 | d23887300b96 |
permissions | -rw-r--r-- |
10694 | 1 |
(* Title: Provers/Arith/extract_common_term.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 2000 University of Cambridge |
|
5 |
||
6 |
Extract common terms in balanced expressions: |
|
7 |
||
8 |
i + u + j ~~ i' + u + j' == u + (i + j) ~~ u + (i' + j') |
|
9 |
i + u ~~ u == u + i ~~ u + 0 |
|
10 |
||
11 |
where ~~ is an appropriate balancing operation (e.g. =, <=, <, -) and 0 is a |
|
12 |
suitable identity for +. |
|
13 |
||
14 |
This massaged formula is then simplified in a user-specified way. |
|
15 |
*) |
|
16 |
||
17 |
signature EXTRACT_COMMON_TERM_DATA = |
|
18 |
sig |
|
19 |
(*abstract syntax*) |
|
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13484
diff
changeset
|
20 |
val mk_sum: typ -> term list -> term |
10694 | 21 |
val dest_sum: term -> term list |
22 |
val mk_bal: term * term -> term |
|
23 |
val dest_bal: term -> term * term |
|
24 |
val find_first: term -> term list -> term list |
|
25 |
(*proof tools*) |
|
26 |
val prove_conv: tactic list -> Sign.sg -> |
|
13484 | 27 |
thm list -> string list -> term * term -> thm option |
10694 | 28 |
val norm_tac: tactic (*proves the result*) |
29 |
val simplify_meta_eq: thm -> thm (*simplifies the result*) |
|
30 |
end; |
|
31 |
||
32 |
||
33 |
functor ExtractCommonTermFun(Data: EXTRACT_COMMON_TERM_DATA): |
|
34 |
sig |
|
35 |
val proc: Sign.sg -> thm list -> term -> thm option |
|
36 |
end |
|
37 |
= |
|
38 |
struct |
|
39 |
||
40 |
(*Store the term t in the table*) |
|
41 |
fun update_by_coeff (tab, t) = Termtab.update ((t, ()), tab); |
|
42 |
||
43 |
(*a left-to-right scan of terms1, seeking a term u that is also in terms2*) |
|
44 |
fun find_common (terms1,terms2) = |
|
45 |
let val tab2 = foldl update_by_coeff (Termtab.empty, terms2) |
|
46 |
fun seek [] = raise TERM("find_common", []) |
|
47 |
| seek (u::terms) = |
|
48 |
if is_some (Termtab.lookup (tab2, u)) then u |
|
49 |
else seek terms |
|
50 |
in seek terms1 end; |
|
51 |
||
52 |
(*the simplification procedure*) |
|
53 |
fun proc sg hyps t = |
|
54 |
let (*first freeze any Vars in the term to prevent flex-flex problems*) |
|
13484 | 55 |
val (t', xs) = Term.adhoc_freeze_vars t; |
10694 | 56 |
val (t1,t2) = Data.dest_bal t' |
57 |
val terms1 = Data.dest_sum t1 |
|
58 |
and terms2 = Data.dest_sum t2 |
|
59 |
val u = find_common (terms1,terms2) |
|
60 |
val terms1' = Data.find_first u terms1 |
|
61 |
and terms2' = Data.find_first u terms2 |
|
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13484
diff
changeset
|
62 |
and T = Term.fastype_of u |
10694 | 63 |
val reshape = |
13484 | 64 |
Data.prove_conv [Data.norm_tac] sg hyps xs |
10694 | 65 |
(t', |
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13484
diff
changeset
|
66 |
Data.mk_bal (Data.mk_sum T (u::terms1'), |
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13484
diff
changeset
|
67 |
Data.mk_sum T (u::terms2'))) |
10694 | 68 |
in |
69 |
apsome Data.simplify_meta_eq reshape |
|
70 |
end |
|
71 |
handle TERM _ => None |
|
72 |
| TYPE _ => None; (*Typically (if thy doesn't include Numeral) |
|
73 |
Undeclared type constructor "Numeral.bin"*) |
|
74 |
||
75 |
end; |