Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewriting SAW core vector folds #1811

Merged
merged 5 commits into from
Feb 7, 2023
Merged

Conversation

eddywestbrook
Copy link
Contributor

The foldl and foldr operations in SAW core do not have any axioms to reason about them, which is a problem because they are primitives, and so don't compute. This PR adds axioms that define their behavior, in terms of a pair of rules for each operation, one for the 0 length case and one for the Succ length case. These rules are written in terms of the vector head and tail operations, which also needed rewrite rules to define their behavior when applied to a vector created by gen.

In testing these new rules, I also discovered that the SAW rewriter was not able to match a term of the form Succ x against a natural number literal. So this PR also includes a fix for that problem. I also added some comments to explain how the rewriter works in more detail, as I had to figure out that detail in order to modify the rewriter.

The intTests directory has been expanded with a new test, test_fold_rewrite_proof, that uses this new functionality to prove a property about a simple cryptol function.

fnc_lemma <- proveit {{ \x y z i -> (fnc' (fnc (x, y, z) i) i).0 == x }} z3;

proveit {{ foldFunctionInverse }} do {
unfolding [ "foldFunctionInverse"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is redundant, goal_normalize ["fnc", "fnc'"]; should do this as well.

, "foldFunction'"
];
goal_normalize ["fnc", "fnc'"];
simplify (add_prelude_eqs ["foldl_cons","foldl_nil",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to use fnc_lemma here with the use_lemmas function you created above?

Also, this script would be more convincing if the trivial prover was used (rather than z3).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that it would be a better example if we could figure out a way to use fnc_lemma, but unfortunately it doesn't match any term in the goal. I tried for a while to modify fnc_lemma to actually apply in this case, but couldn't figure out a way to make it work, so I just had Z3 unfold all the calls to fnc and fnc' to prove the result. After rewriting all the foldls away, the resulting term looks like

\x y z -> (fnc' (fnc' (... (fnc' ((fnc (fnc (... (fnc (x,y,z) 0)...) 14) 15).0, y, z) 15) ... 1) 0).0 == True

So fnc_lemma needs to somehow rewrite a term of that form, probably something of the form fnc' ((fnc tup i).0, y, z) i but I wasn't sure what to rewrite that to, since the final .0 doesn't apply until all 16 fnc' applications happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this example for now is to demonstrate rewriting folds, and it does that. So I'm going to merge this PR now. I'd love to figure out the "right" way to write this lemma, but let's not wait on that before merging this PR.

@eddywestbrook eddywestbrook added ready-to-merge If at least one person approves this PR, automatically merge it when CI finishes successfully. saw-core Related to the saw-core package labels Feb 7, 2023
@eddywestbrook eddywestbrook merged commit 4ac0219 into master Feb 7, 2023
@mergify mergify bot deleted the saw-core/vector-fold-axioms branch February 7, 2023 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge If at least one person approves this PR, automatically merge it when CI finishes successfully. saw-core Related to the saw-core package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants