11using Metatheory. Rewriters
22
3- function EGraphs. preprocess (t)
4- Chain ([ Postwalk ( toterm), Postwalk (unflatten)])(t )
3+ function EGraphs. preprocess (t:: Symbolic )
4+ toterm (unflatten (t) )
55end
66
77function symbolicegraph (ex)
@@ -76,12 +76,26 @@ function costfun(n::ENodeTerm, g::EGraph, an)
7676 cost
7777end
7878
79- costfun (n:: ENodeLiteral , g:: EGraph , an) = 1
79+ costfun (n:: ENodeLiteral , g:: EGraph , an) = 0
8080
81+ egraph_simterm (x, head, args, symtype= nothing ; metadata= nothing , exprhead= exprhead (x)) =
82+ TermInterface. similarterm (typeof (x), head, args, symtype; metadata= metadata, exprhead= exprhead)
83+
84+
85+ # Custom similarterm to use in EGraphs on <:Symbolic types that treats everything as a Term
86+ function egraph_simterm (x:: Type{<:Term} , f, args, symtype= nothing ; metadata= nothing , exprhead= :call )
87+ T = symtype
88+ if T === nothing
89+ T = _promote_symtype (f, args)
90+ end
91+ res = Term {T} (f isa Symbol ? eval (f) : f, args; metadata= metadata);
92+ return res
93+ end
8194
8295function optimize (ex; params= SaturationParams (timeout= 20 ))
8396 # @show ex
8497 g = symbolicegraph (ex)
98+ params. simterm = egraph_simterm
8599 saturate! (g, opt_theory, params)
86100 return extract! (g, costfun)
87101end
0 commit comments