Open brianhuffman opened 2 years ago
The root of the problem is that this definition of termToPat
has a catch-all case that maps lots of terms to the wildcard pattern Var
. As new constructors have been added to the Term
datatype, this function definition was not kept up to date.
I will rewrite this function so that it does not use the underscore catch-all case, instead using a complete pattern match. This way we will get a compiler warning if we forget to update termToPat
after changing the Term
datatype again.
The other reason why we're returning way too many rewrite rules is that the rewriter queries the term net in "unify" mode instead of "match" mode: https://github.com/GaloisInc/saw-script/blob/81287fb74cbdac3bb3f75b984f1125deb3055e64/saw-core/src/Verifier/SAW/Rewriter.hs#L649
This change was introduced in 916040bbec1d46e3aad2901e179d76db85d63264 in order to allow rule matching modulo Nat
equations. To get reasonable performance with large simpsets, we'll have to figure out how to do this another way.
While working on some saw proofs that prove a lot of small lemmas using rewriting, I noticed that many of the calls to the simplifier were taking more time than I thought they should. Profiling showed that the
apply
function (defined locally insiderewriteSharedTerm
) was being called hundreds of times more often than the surroundingrewriteTop
function, suggesting thatTermNet.unify_term
is returning a lot more potential matches than it should, and all these bogus potential matches need to be filtered out by calling the much more expensive functionscMatch
.By adding some debug output to
rewriteSharedTerm
, I discovered that when rewriting withcryptol_ss
, terms likesort 0
,Prelude.Nat
, orPrelude.Vec 32 Prelude.Bool
yield a list of 161 potential rewrite rules. This is the smallest set of potential rewrites that arises from any term when simplified withcryptol_ss
. The potential rules all seem to be ones that have saw-core record projections appearing on the left-hand side.For other terms, including function types, product types, or natural numbers, the rewriter finds 430 potential rewrite rules. (I think this is actually the complete set of all the rules in
cryptol_ss
.)Basically this means that the term net data structure is doing nothing for us. I believe the issue is in the
toPat
function that turns a saw-coreTerm
into a term-netPat
: It looks like it's treating way too many term constructors as wildcards that could potentially match anything.