Skip to content

Commit 7ce17ed

Browse files
Merge pull request #712 from AayushSabharwal/as/isequal-meta-custom-symbolic
fix: implement `isequal_with_metadata` for custom symbolics
2 parents 69283c0 + ce95557 commit 7ce17ed

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/types.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ function isequal_with_metadata(a::BasicSymbolic{T}, b::BasicSymbolic{S})::Bool w
314314
_isequal(a, b, E; comparator = isequal_with_metadata)::Bool && isequal_with_metadata(metadata(a), metadata(b)) || return false
315315
end
316316

317+
function isequal_with_metadata(a::Symbolic, b::Symbolic)::Bool
318+
a === b && return true
319+
typeof(a) == typeof(b) || return false
320+
321+
ma = metadata(a)
322+
mb = metadata(b)
323+
if iscall(a) && iscall(b)
324+
return isequal_with_metadata(operation(a), operation(b)) && isequal_with_metadata(arguments(a), arguments(b)) && isequal_with_metadata(ma, mb)
325+
elseif iscall(a) || iscall(b)
326+
return false
327+
else
328+
return isequal_with_metadata(ma, mb)
329+
end
330+
end
331+
317332
"""
318333
$(TYPEDSIGNATURES)
319334

test/hash_consing.jl

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using SymbolicUtils, Test
2-
using SymbolicUtils: Term, Add, Mul, Div, Pow, hash2, metadata
2+
using SymbolicUtils: Term, Add, Mul, Div, Pow, hash2, metadata, BasicSymbolic, Symbolic,
3+
isequal_with_metadata
4+
import TermInterface
35

46
struct Ctx1 end
57
struct Ctx2 end
@@ -147,3 +149,42 @@ end
147149
@test ex2.hash2[] != h
148150
end
149151
end
152+
153+
struct MySymbolic <: Symbolic{Real}
154+
sym::BasicSymbolic{Real}
155+
end
156+
157+
TermInterface.iscall(x::MySymbolic) = iscall(x.sym)
158+
TermInterface.operation(x::MySymbolic) = operation(x.sym)
159+
TermInterface.arguments(x::MySymbolic) = arguments(x.sym)
160+
SymbolicUtils.metadata(x::MySymbolic) = metadata(x.sym)
161+
Base.isequal(a::MySymbolic, b::MySymbolic) = isequal(a.sym, b.sym)
162+
163+
@testset "`isequal_with_metadata` on custom symbolics" begin
164+
@syms x::Real
165+
xx = setmetadata(x, Int, 3)
166+
@test isequal(x, xx)
167+
@test !isequal_with_metadata(x, xx)
168+
myx = MySymbolic(x)
169+
myxx = MySymbolic(xx)
170+
@test isequal(myx, myxx)
171+
@test !isequal_with_metadata(myx, myxx)
172+
173+
ex = 2x
174+
exx = 2xx
175+
myex = MySymbolic(ex)
176+
myexx = MySymbolic(exx)
177+
@test isequal(ex, exx)
178+
@test !isequal_with_metadata(ex, exx)
179+
@test isequal(myex, myexx)
180+
@test !isequal_with_metadata(myex, myexx)
181+
182+
t = Term{Real}(max, Any[x, myex])
183+
tt = Term{Real}(max, Any[xx, myexx])
184+
@test isequal(t, tt)
185+
@test !isequal_with_metadata(t, tt)
186+
myt = MySymbolic(t)
187+
mytt = MySymbolic(tt)
188+
@test isequal(myt, mytt)
189+
@test !isequal_with_metadata(myt, mytt)
190+
end

0 commit comments

Comments
 (0)