Skip to content

Commit

Permalink
Define name function
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenszhu committed Sep 16, 2024
1 parent 1660338 commit b88fb66
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/SymbolicUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module SymbolicUtils

using DocStringExtensions

export @syms, term, showraw, hasmetadata, getmetadata, setmetadata
export @syms, term, showraw, hasmetadata, getmetadata, setmetadata, name

using TermInterface
using DataStructures
Expand Down
14 changes: 12 additions & 2 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ function exprtype(x::BasicSymbolic)
end
end

function name(x::BasicSymbolic)
x.impl.name
end

# Same but different error messages
@noinline error_on_type() = error("Internal error: unreachable reached!")
@noinline error_sym() = error("Sym doesn't have a operation or arguments!")
Expand Down Expand Up @@ -308,7 +312,13 @@ end
Base.one( s::Symbolic) = one( symtype(s))
Base.zero(s::Symbolic) = zero(symtype(s))

Base.nameof(s::BasicSymbolic) = issym(s) ? s.impl.name : error("None Sym BasicSymbolic doesn't have a name")
function Base.nameof(s::BasicSymbolic)
if issym(s)
name(s)
else
error("None Sym BasicSymbolic doesn't have a name")
end
end

## This is much faster than hash of an array of Any
hashvec(xs, z) = foldr(hash, xs, init=z)
Expand Down Expand Up @@ -986,7 +996,7 @@ showraw(t) = showraw(stdout, t)

function Base.show(io::IO, v::BasicSymbolic)
@match v.impl begin
Sym(_...) => Base.show_unquoted(io, v.impl.name)
Sym(_...) => Base.show_unquoted(io, name(v))
Const(_...) => print(io, v.impl.val)
_ => show_term(io, v)
end
Expand Down
10 changes: 5 additions & 5 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ using Test
@syms a b::Float64 f(::Real) g(p, h(q::Real))::Int

@test issym(a) && symtype(a) == Number
@test a.impl.name === :a
@test name(a) === :a

@test issym(b) && symtype(b) == Float64
@test nameof(b) === :b

@test issym(f)
@test f.impl.name === :f
@test name(f) === :f
@test symtype(f) == FnType{Tuple{Real}, Number, Nothing}

@test issym(g)
@test g.impl.name === :g
@test name(g) === :g
@test symtype(g) == FnType{Tuple{Number, FnType{Tuple{Real}, Number, Nothing}}, Int, Nothing}

@test isterm(f(b))
Expand All @@ -37,14 +37,14 @@ using Test

@syms (f::typeof(max))(::Real, ::AbstractFloat)::Number a::Real
@test issym(f)
@test f.impl.name == :f
@test name(f) == :f
@test symtype(f) == FnType{Tuple{Real, AbstractFloat}, Number, typeof(max)}
@test isterm(f(a, b))
@test symtype(f(a, b)) == Number

@syms g(p, (h::typeof(identity))(q::Real)::Number)::Number
@test issym(g)
@test g.impl.name == :g
@test name(g) == :g
@test symtype(g) == FnType{Tuple{Number, FnType{Tuple{Real}, Number, typeof(identity)}}, Number, Nothing}
@test_throws "not a subtype of" g(a, f)
@syms (f::typeof(identity))(::Real)::Number
Expand Down
4 changes: 2 additions & 2 deletions test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ end
@test typeof(s1) == BasicSymbolic{Int64}
@test s1.metadata == SymbolicUtils.NO_METADATA
@test s1.hash[] == SymbolicUtils.EMPTY_HASH
@test s1.impl.name == :x
@test name(s1) == :x
@test typeof(s2) == BasicSymbolic{Float64}
@test s2.metadata == SymbolicUtils.NO_METADATA
@test s2.hash[] == SymbolicUtils.EMPTY_HASH
@test s2.impl.name == :y
@test name(s2) == :y
end
@testset "Term" begin
s1 = _Sym(Float64, :x)
Expand Down

0 comments on commit b88fb66

Please sign in to comment.