Skip to content

Commit

Permalink
Merge pull request #342 from JuliaSymbolics/sm/rename
Browse files Browse the repository at this point in the history
Remove Namespace in favor of renaming
  • Loading branch information
YingboMa authored Aug 9, 2021
2 parents 2aeff8d + 7975269 commit 4187f0d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Symbolics"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
authors = ["Shashi Gowda <gowda@mit.edu>"]
version = "2.1.0"
version = "3.0.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
2 changes: 1 addition & 1 deletion src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ RuntimeGeneratedFunctions.init(@__MODULE__)
export simplify, substitute

using SciMLBase, IfElse
export Num, Namespace
export Num
using MacroTools
import MacroTools: splitdef, combinedef, postwalk, striplines
include("wrapper-types.jl")
Expand Down
107 changes: 63 additions & 44 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@ function map_subscripts(indices)
join(IndexMap[c] for c in str)
end

rename(x::Sym,name) = @set! x.name = name
function rename(x::Symbolic, name)
if operation(x) isa Sym
@assert x isa Term
@set! x.f = rename(operation(x), name)
@set! x.hash = Ref{UInt}(0)
return x
else
error("can't rename $x to $name")
end
end

function unwrap_runtime_var(v)
isruntime = Meta.isexpr(v, :$) && length(v.args) == 1
Expand Down Expand Up @@ -260,7 +249,7 @@ function _construct_array_vars(macroname, var_name, type, call_args, val, prop,
need_scalarize = true
ex = :($Sym{Array{$FnType{Tuple, $type}, $ndim}}($var_name))
ex = :($setmetadata($ex, $ArrayShapeCtx, ($(indices...),)))
:($map(f->$CallWithMetadata(f), $ex))
:($map($CallWithMetadata, $ex))
else
# [(R -> R)(R) ....]
need_scalarize = true
Expand Down Expand Up @@ -357,50 +346,18 @@ function TreeViews.treelabel(io::IO,x::Sym,
show(io,mime,Text(x.name))
end

struct Namespace{T} <: Symbolic{T}
parent::Any
named::Symbolic{T}
function Namespace(p, n)
n isa Namespace && error("Ill-formed namespacing. $n shouldn't be a namespace.")
new{symtype(n)}(p, n)
end
end

Base.hash(ns::Namespace, salt::UInt) = hash(ns.named, hash(getname(ns.parent), salt 0x906e89687f904e4a))
SymbolicUtils.metadata(ns::Namespace) = SymbolicUtils.metadata(ns.named)
SymbolicUtils.metadata(ns::Namespace, meta) = @set ns.named = SymbolicUtils.metadata(ns.named, meta)
SymbolicUtils.setmetadata(ns::Namespace, typ::DataType, data) = @set ns.named = SymbolicUtils.setmetadata(ns.named, typ, data)
Base.nameof(x::Namespace) = getname(x)
function SymbolicUtils.Code.toexpr(ns::Namespace, st)
if haskey(st.symbolify, ns)
st.symbolify[ns]
else
:($(getname(ns.parent)).$(SymbolicUtils.Code.toexpr(ns.named, st)))
end
end
Base.show(io::IO, x::Namespace) = print(io, getname(x))
function Base.isequal(x::Namespace, y::Namespace)
isequal(x.named, y.named) && (x.parent === y.parent || isequal(x.parent, y.parent))
end
# Namespace must be treated as a variable
SymbolicUtils.Code.get_symbolify(ns::Namespace) = (ns,)

const _fail = Dict()

_getname(x, _) = nameof(x)
_getname(x::Symbol, _) = x
function _getname(x::Symbolic, val)
if istree(x) && (op = operation(x)) isa Namespace
return getname(op)
end
ss = getsource(x, nothing)
if ss === nothing
ss = getsource(getparent(x), val)
end
ss === _fail && throw(ArgumentError("Variable $x doesn't have a source defined."))
ss[2]
end
_getname(x::Namespace, val) = Symbol(getname(x.parent), :(.), getname(x.named, val))

getsource(x, val=_fail) = getmetadata(unwrap(x), VariableSource, val)

Expand Down Expand Up @@ -474,6 +431,68 @@ function variable(name, idx...; T=Real)
end
end

##### Renaming #####
# getname
# rename
# getindex parent
# calls
# symbolic function x[1:3](..)
#
# x_t
# sys.x

function rename_getindex_source(x, parent=x)
getindex_posthook(x) do r,x,i...
hasmetadata(r, GetindexParent) ? setmetadata(r, GetindexParent, parent) : r
end
end

function rename_metadata(from, to, name)
if hasmetadata(from, VariableSource)
s = getmetadata(from, VariableSource)
to = setmetadata(to, VariableSource, (s[1], name))
end
if hasmetadata(from, GetindexParent)
s = getmetadata(from, GetindexParent)
to = setmetadata(to, GetindexParent, rename(s, name))
end
return to
end

function rename(x::Sym, name)
xx = @set! x.name = name
xx = rename_metadata(x, xx, name)
symtype(xx) <: AbstractArray ? rename_getindex_source(xx) : xx
end

rename(x::Union{Num, Arr}, name) = wrap(rename(unwrap(x), name))
function rename(x::ArrayOp, name)
t = x.term
args = arguments(t)
# Hack:
@assert operation(t) === (map) && (args[1] isa CallWith || args[1] == CallWithMetadata)
rn = rename(args[2], name)

xx = metadata(operation(t)(args[1], rn), metadata(x))
rename_getindex_source(rename_metadata(x, xx, name))
end

function rename(x::CallWithMetadata, name)
rename_metadata(x, CallWithMetadata(rename(x.f, name), x.metadata), name)
end

function rename(x::Symbolic, name)
if istree(x) && operation(x) === getindex
rename(arguments(x)[1], name)[arguments(x)[2:end]...]
elseif istree(x) && symtype(operation(x)) <: FnType || operation(x) isa CallWithMetadata
@assert x isa Term
xx = @set x.f = rename(operation(x), name)
@set! xx.hash = Ref{UInt}(0)
return rename_metadata(x, xx, name)
else
error("can't rename $x to $name")
end
end

# Deprecation below

Expand Down
21 changes: 16 additions & 5 deletions test/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ many_vars = @variables t=0 a=1 x[1:4]=2 y[1:4](t)=3 w[1:4] = 1:4 z[1:4](t) = 2:5
@test getdefaultval(w[4]) == 4
@test getdefaultval(z[3]) == 4

nxt = Namespace(unwrap(x), unwrap(t))
nxt_1 = Namespace(nxt, unwrap(x))
@test getname(nxt) == Symbol(:x, :(.), :t)
@test getname(nxt_1) == Symbol(:x, :(.), :t, :(.), :x)

@test p[1] isa Symbolics.CallWithMetadata
@test symtype(p[1]) <: FnType{Tuple, Real}
@test p[1](t) isa Symbolics.Num
Expand Down Expand Up @@ -65,3 +60,19 @@ end
@test foo(x, wrap(2)) isa FooWrap
@test foo(x, wrap(1)) isa Num
@test foo(x, wrap(6)) isa String


let
vars = @variables t a b(a) c(..) x[1:2] y[1:3](t) z[1:2](..)
vars2 = [Symbolics.rename(v, Symbol(Symbolics.getname(v), "_2")) for v in vars]

for (v, v2) in zip(vars, vars2)
@test typeof(v) == typeof(v2)
@test Symbol(Symbolics.getname(v), "_2") == Symbolics.getname(v2)
end

@test Symbolics.getname(getmetadata(z[2](t), Symbolics.GetindexParent)) === :z
@test Symbolics.getname(getmetadata(vars2[end][2](t), Symbolics.GetindexParent)) === :z_2

@test Symbolics.getname(Symbolics.rename(y[2], :u)) === :u
end

2 comments on commit 4187f0d

@YingboMa
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/42514

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.0.0 -m "<description of version>" 4187f0ddff22d08d45c479a2230b20cbca16c70e
git push origin v3.0.0

Please sign in to comment.