author | wenzelm |
Mon, 01 Aug 2005 19:20:26 +0200 | |
changeset 16973 | b2a894562b8f |
parent 15570 | 8d8c70b41bab |
child 17223 | 430edc6b7826 |
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*) |
|
16973 | 26 |
val prove_conv: tactic list -> theory -> |
13484 | 27 |
thm list -> string list -> term * term -> thm option |
16973 | 28 |
val norm_tac: simpset -> tactic (*proves the result*) |
29 |
val simplify_meta_eq: simpset -> thm -> thm (*simplifies the result*) |
|
10694 | 30 |
end; |
31 |
||
32 |
||
33 |
functor ExtractCommonTermFun(Data: EXTRACT_COMMON_TERM_DATA): |
|
34 |
sig |
|
16973 | 35 |
val proc: theory -> simpset -> term -> thm option |
10694 | 36 |
end |
37 |
= |
|
38 |
struct |
|
39 |
||
40 |
(*Store the term t in the table*) |
|
16973 | 41 |
fun update_by_coeff t tab = Termtab.update ((t, ()), tab); |
10694 | 42 |
|
43 |
(*a left-to-right scan of terms1, seeking a term u that is also in terms2*) |
|
44 |
fun find_common (terms1,terms2) = |
|
16973 | 45 |
let val tab2 = fold update_by_coeff terms2 Termtab.empty |
10694 | 46 |
fun seek [] = raise TERM("find_common", []) |
47 |
| seek (u::terms) = |
|
16973 | 48 |
if Termtab.defined tab2 u then u |
10694 | 49 |
else seek terms |
16973 | 50 |
in seek terms1 end; |
10694 | 51 |
|
52 |
(*the simplification procedure*) |
|
16973 | 53 |
fun proc thy ss t = |
15027 | 54 |
let |
55 |
val hyps = prems_of_ss ss; |
|
56 |
(*first freeze any Vars in the term to prevent flex-flex problems*) |
|
13484 | 57 |
val (t', xs) = Term.adhoc_freeze_vars t; |
10694 | 58 |
val (t1,t2) = Data.dest_bal t' |
59 |
val terms1 = Data.dest_sum t1 |
|
60 |
and terms2 = Data.dest_sum t2 |
|
61 |
val u = find_common (terms1,terms2) |
|
62 |
val terms1' = Data.find_first u terms1 |
|
63 |
and terms2' = Data.find_first u terms2 |
|
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13484
diff
changeset
|
64 |
and T = Term.fastype_of u |
10694 | 65 |
val reshape = |
16973 | 66 |
Data.prove_conv [Data.norm_tac ss] thy hyps xs |
10694 | 67 |
(t', |
14387
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13484
diff
changeset
|
68 |
Data.mk_bal (Data.mk_sum T (u::terms1'), |
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
paulson
parents:
13484
diff
changeset
|
69 |
Data.mk_sum T (u::terms2'))) |
10694 | 70 |
in |
16973 | 71 |
Option.map (Data.simplify_meta_eq ss) reshape |
10694 | 72 |
end |
15531 | 73 |
handle TERM _ => NONE |
74 |
| TYPE _ => NONE; (*Typically (if thy doesn't include Numeral) |
|
10694 | 75 |
Undeclared type constructor "Numeral.bin"*) |
76 |
||
77 |
end; |