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*)
|
|
20 |
val mk_sum: term list -> term
|
|
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 ->
|
|
27 |
thm list -> term * term -> thm option
|
|
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*)
|
|
55 |
val rand_s = gensym"_"
|
|
56 |
fun mk_inst (var as Var((a,i),T)) =
|
|
57 |
(var, Free((a ^ rand_s ^ string_of_int i), T))
|
|
58 |
val t' = subst_atomic (map mk_inst (term_vars t)) t
|
|
59 |
val (t1,t2) = Data.dest_bal t'
|
|
60 |
val terms1 = Data.dest_sum t1
|
|
61 |
and terms2 = Data.dest_sum t2
|
|
62 |
val u = find_common (terms1,terms2)
|
|
63 |
val terms1' = Data.find_first u terms1
|
|
64 |
and terms2' = Data.find_first u terms2
|
|
65 |
val reshape =
|
|
66 |
Data.prove_conv [Data.norm_tac] sg hyps
|
|
67 |
(t',
|
|
68 |
Data.mk_bal (Data.mk_sum (u::terms1'),
|
|
69 |
Data.mk_sum (u::terms2')))
|
|
70 |
in
|
|
71 |
apsome Data.simplify_meta_eq reshape
|
|
72 |
end
|
|
73 |
handle TERM _ => None
|
|
74 |
| TYPE _ => None; (*Typically (if thy doesn't include Numeral)
|
|
75 |
Undeclared type constructor "Numeral.bin"*)
|
|
76 |
|
|
77 |
end;
|