| author | wenzelm | 
| Mon, 20 Jun 2005 22:13:59 +0200 | |
| changeset 16486 | 1a12cdb6ee6b | 
| parent 15570 | 8d8c70b41bab | 
| child 16973 | b2a894562b8f | 
| 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: 
13484diff
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 | |
| 15027 | 35 | val proc: Sign.sg -> simpset -> term -> thm option | 
| 10694 | 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) = | |
| 15570 | 45 | let val tab2 = Library.foldl update_by_coeff (Termtab.empty, terms2) | 
| 10694 | 46 |       fun seek [] = raise TERM("find_common", []) 
 | 
| 47 | | seek (u::terms) = | |
| 15570 | 48 | if isSome (Termtab.lookup (tab2, u)) then u | 
| 10694 | 49 | else seek terms | 
| 50 | in seek terms1 end; | |
| 51 | ||
| 52 | (*the simplification procedure*) | |
| 15027 | 53 | fun proc sg ss t = | 
| 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: 
13484diff
changeset | 64 | and T = Term.fastype_of u | 
| 10694 | 65 | val reshape = | 
| 13484 | 66 | Data.prove_conv [Data.norm_tac] sg hyps xs | 
| 10694 | 67 | (t', | 
| 14387 
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
 paulson parents: 
13484diff
changeset | 68 | Data.mk_bal (Data.mk_sum T (u::terms1'), | 
| 
e96d5c42c4b0
Polymorphic treatment of binary arithmetic using axclasses
 paulson parents: 
13484diff
changeset | 69 | Data.mk_sum T (u::terms2'))) | 
| 10694 | 70 | in | 
| 15570 | 71 | Option.map Data.simplify_meta_eq 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; |