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

Formula simplifier using e-graphs #14

Open
dariusf opened this issue Aug 13, 2024 · 1 comment
Open

Formula simplifier using e-graphs #14

dariusf opened this issue Aug 13, 2024 · 1 comment

Comments

@dariusf
Copy link
Collaborator

dariusf commented Aug 13, 2024

Here's a first attempt using egglog to remove temporary existentials, e.g.:

exists x. a=b/\res=x/\x=1
==>
a=b/\res=1
(datatype Value (Num i64))
(declare True Value)
(declare False Value)
(datatype VarType)
(datatype Term
    (Val Value)
    (Var VarType)
    (And Term Term)
    (Eq Term Term)
    (Ex VarType Term))
(function V (String) VarType)
(function From (Term) VarType)


(rewrite (And ?a ?b) (And ?b ?a))
(rewrite (Eq ?a ?b) (Eq ?b ?a))

(rewrite
  (Ex ?v (And (Eq (Var ?v) ?x) (Eq (Var ?v) ?y)))
  (Eq ?x ?y))

(let e1 (Ex (V "x") (And (Eq (Var (V "x")) (Val (Num 1))) (Eq (Var (V "x")) (Var (V "res"))))))

(push)

(run 4)
(check (= e1 (Eq (Var (V "res")) (Val (Num 1)))))
(extract e1)
(pop)

Dealing with binders

A simple solution (used above), where variables are represented with strings and assumed to be fresh, may be sufficient. If not, here are a few pointers.

Next steps

@dariusf
Copy link
Collaborator Author

dariusf commented Aug 28, 2024

Here are some more simplifications which are currently implemented:

simplify_existential_locations:
  ex x; ens x->...; ex y; ens y->... ==> ex x; ens x->...; ex y; ens x->...
    if x=y appears somewhere

remove_temp_vars:
  ex x; ens x=c; S ==> ex x; S
    if x does not occur in S and c is a constant

optimize_existentials:
  ex x; S ==> S
    if x does not occur in S
  ex x; S; S1 ==> S; ex x; S1
    if x does not occur in S but occurs in S1

remove_vars_occurring_twice:
  ex x; ens x=0/\res=x ==> ex x; ens 0=0/\res=0

propagate_function_stage_equalities:
  ens b=(fun ...)/\a=b; a(...) ==> ens b=(fun ...); b(...)

simplify_pure:
  ens !(x=true) ==> ens x=false
  ens x=x ==> ens true
  ens c=c ==> ens true
  ens true/\P ==> ens P
  ens false/\P ==> ens false
  ens c+c ==> ens c1
    if c1=c+C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant