Skip to content

Commit 33b274b

Browse files
Merge pull request #603 from JuliaSymbolics/s/fix-depwarn
similarterm -> maketerm in Walk and PolyForm
2 parents 9e3b9de + 0520c78 commit 33b274b

File tree

9 files changed

+70
-48
lines changed

9 files changed

+70
-48
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymbolicUtils"
22
uuid = "d1185830-fcd6-423d-90d6-eec64667417b"
33
authors = ["Shashi Gowda"]
4-
version = "2.0.0"
4+
version = "2.0.1"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/interface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ with `head` as the head and `args` as the arguments, `type` as the symtype
7878
and `metadata` as the metadata. By default this will execute `head(args...)`.
7979
`x` parameter can also be a `Type`. The `exprhead` keyword argument is useful
8080
when manipulating `Expr`s.
81+
82+
`similarterm` is deprecated see help for `maketerm` instead.
8183
"""
8284
function similarterm end

src/polyform.jl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ function polyize(x, pvar2sym, sym2term, vtype, pow, Fs, recurse)
118118
# create a new symbol to store this
119119

120120
y = if recurse
121-
similarterm(x,
122-
op,
123-
map(a->PolyForm(a, pvar2sym, sym2term, vtype; Fs, recurse),
124-
args), symtype(x))
121+
maketerm(typeof(x),
122+
op,
123+
map(a->PolyForm(a, pvar2sym, sym2term, vtype; Fs, recurse), args),
124+
symtype(x),
125+
metadata(x))
125126
else
126127
x
127128
end
@@ -175,11 +176,11 @@ isexpr(x::PolyForm) = true
175176
iscall(x::Type{<:PolyForm}) = true
176177
iscall(x::PolyForm) = true
177178

178-
function similarterm(t::PolyForm, f, args, symtype; metadata=nothing)
179-
basic_similarterm(t, f, args, symtype; metadata=metadata)
179+
function maketerm(::Type{<:PolyForm}, f, args, symtype, metadata)
180+
basicsymbolic(t, f, args, symtype, metadata)
180181
end
181-
function similarterm(::PolyForm, f::Union{typeof(*), typeof(+), typeof(^)},
182-
args, symtype; metadata=nothing)
182+
function maketerm(::Type{<:PolyForm}, f::Union{typeof(*), typeof(+), typeof(^)},
183+
args, symtype, metadata)
183184
f(args...)
184185
end
185186

@@ -248,8 +249,10 @@ multivariate polynomials implementation.
248249
expand(expr) = unpolyize(PolyForm(expr, Fs=Union{typeof(+), typeof(*), typeof(^)}, recurse=true))
249250

250251
function unpolyize(x)
251-
simterm(x, f, args; kw...) = similarterm(x, f, args, symtype(x); kw...)
252-
Postwalk(identity, similarterm=simterm)(x)
252+
# we need a special makterm here because the default one used in Postwalk will call
253+
# promote_symtype to get the new type, but we just want to forward that in case
254+
# promote_symtype is not defined for some of the expressions here.
255+
Postwalk(identity, maketerm=(T,f,args,sT,m) -> maketerm(T, f, args, symtype(x), m))(x)
253256
end
254257

255258
function toterm(x::PolyForm)
@@ -301,7 +304,7 @@ function add_divs(x, y)
301304
end
302305
end
303306

304-
function frac_similarterm(x, f, args; kw...)
307+
function frac_maketerm(T, f, args, stype, metadata)
305308
if f in (*, /, \, +, -)
306309
f(args...)
307310
elseif f == (^)
@@ -311,7 +314,7 @@ function frac_similarterm(x, f, args; kw...)
311314
args[1]^args[2]
312315
end
313316
else
314-
similarterm(x, f, args; kw...)
317+
maketerm(T, f, args, stype, metadata)
315318
end
316319
end
317320

@@ -333,8 +336,8 @@ function simplify_fractions(x; polyform=false)
333336
sdiv(a) = isdiv(a) ? simplify_div(a) : a
334337

335338
expr = Postwalk(sdiv quick_cancel,
336-
similarterm=frac_similarterm)(Postwalk(add_with_div,
337-
similarterm=frac_similarterm)(x))
339+
maketerm=frac_maketerm)(Postwalk(add_with_div,
340+
maketerm=frac_maketerm)(x))
338341

339342
polyform ? expr : unpolyize(expr)
340343
end

src/rewriters.jl

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module Rewriters
3333
using SymbolicUtils: @timer
3434
using TermInterface
3535

36-
import SymbolicUtils: similarterm, istree, operation, arguments, unsorted_arguments, metadata, node_count
36+
import SymbolicUtils: iscall, operation, arguments, unsorted_arguments, metadata, node_count, _promote_symtype
3737
export Empty, IfElse, If, Chain, RestartedChain, Fixpoint, Postwalk, Prewalk, PassThrough
3838

3939
# Cache of printed rules to speed up @timer
@@ -167,24 +167,41 @@ end
167167
struct Walk{ord, C, F, threaded}
168168
rw::C
169169
thread_cutoff::Int
170-
similarterm::F
170+
maketerm::F # XXX: for the 2.0 deprecation cycle, we actually store a function
171+
# that behaves like `similarterm` here, we use `compatmaker` to wrap
172+
# maketerm-like input to do this, with a warning if similarterm provided
173+
# we need this workaround to deprecate because similarterm takes value
174+
# but maketerm only knows the type.
171175
end
172176

173177
function instrument(x::Walk{ord, C,F,threaded}, f) where {ord,C,F,threaded}
174178
irw = instrument(x.rw, f)
175-
Walk{ord, typeof(irw), typeof(x.similarterm), threaded}(irw,
179+
Walk{ord, typeof(irw), typeof(x.maketerm), threaded}(irw,
176180
x.thread_cutoff,
177-
x.similarterm)
181+
x.maketerm)
178182
end
179183

180184
using .Threads
181185

182-
function Postwalk(rw; threaded::Bool=false, thread_cutoff=100, similarterm=similarterm)
183-
Walk{:post, typeof(rw), typeof(similarterm), threaded}(rw, thread_cutoff, similarterm)
186+
function compatmaker(similarterm, maketerm)
187+
# XXX: delete this and only use maketerm in a future release.
188+
if similarterm isa Nothing
189+
function (x, f, args, type=_promote_symtype(f, args); metadata)
190+
maketerm(typeof(x), f, args, type, metadata)
191+
end
192+
else
193+
Base.depwarn("Prewalk and Postwalk now take maketerm instead of similarterm keyword argument. similarterm(x, f, args, type; metadata) is now maketerm(typeof(x), f, args, type, metadata)", :similarterm)
194+
similarterm
195+
end
196+
end
197+
function Postwalk(rw; threaded::Bool=false, thread_cutoff=100, maketerm=maketerm, similarterm=nothing)
198+
maker = compatmaker(similarterm, maketerm)
199+
Walk{:post, typeof(rw), typeof(maker), threaded}(rw, thread_cutoff, maker)
184200
end
185201

186-
function Prewalk(rw; threaded::Bool=false, thread_cutoff=100, similarterm=similarterm)
187-
Walk{:pre, typeof(rw), typeof(similarterm), threaded}(rw, thread_cutoff, similarterm)
202+
function Prewalk(rw; threaded::Bool=false, thread_cutoff=100, maketerm=maketerm, similarterm=nothing)
203+
maker = compatmaker(similarterm, maketerm)
204+
Walk{:pre, typeof(rw), typeof(maker), threaded}(rw, thread_cutoff, maker)
188205
end
189206

190207
struct PassThrough{C}
@@ -202,8 +219,8 @@ function (p::Walk{ord, C, F, false})(x) where {ord, C, F}
202219
x = p.rw(x)
203220
end
204221

205-
if istree(x)
206-
x = p.similarterm(x, operation(x), map(PassThrough(p),
222+
if iscall(x)
223+
x = p.maketerm(x, operation(x), map(PassThrough(p),
207224
unsorted_arguments(x)), metadata=metadata(x))
208225
end
209226

@@ -228,7 +245,7 @@ function (p::Walk{ord, C, F, true})(x) where {ord, C, F}
228245
end
229246
end
230247
args = map((t,a) -> passthrough(t isa Task ? fetch(t) : t, a), _args, arguments(x))
231-
t = p.similarterm(x, operation(x), args, metadata=metadata(x))
248+
t = p.maketerm(x, operation(x), args, metadata=metadata(x))
232249
end
233250
return ord === :post ? p.rw(t) : t
234251
else

src/rule.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ function (acr::ACRule)(term)
408408
if result !== nothing
409409
# Assumption: inds are unique
410410
length(args) == length(inds) && return result
411-
return similarterm(term, f, [result, (args[i] for i in eachindex(args) if i inds)...], symtype(term))
411+
return maketerm(typeof(term), f, [result, (args[i] for i in eachindex(args) if i inds)...], symtype(term), metadata(term))
412412
end
413413
end
414414
end

src/substitute.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ function substitute(expr, dict; fold=true)
3131
args = map(x->substitute(x, dict, fold=fold), unsorted_arguments(expr))
3232
end
3333

34-
similarterm(expr,
35-
op,
36-
args,
37-
symtype(expr);
38-
metadata=metadata(expr))
34+
maketerm(typeof(expr),
35+
op,
36+
args,
37+
symtype(expr),
38+
metadata(expr))
3939
else
4040
expr
4141
end

src/utils.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function fold(t)
5353
# evaluate it
5454
return operation(t)(tt...)
5555
else
56-
return similarterm(t, operation(t), tt)
56+
return maketerm(typeof(t), operation(t), tt, symtype(t), metadata(t))
5757
end
5858
else
5959
return t
@@ -147,19 +147,19 @@ function flatten_term(⋆, x)
147147
push!(flattened_args, t)
148148
end
149149
end
150-
similarterm(x, , flattened_args)
150+
maketerm(typeof(x), , flattened_args, symtype(x), metadata(x))
151151
end
152152

153153
function sort_args(f, t)
154154
args = arguments(t)
155155
if length(args) < 2
156-
return similarterm(t, f, args)
156+
return maketerm(typeof(t), f, args, symtype(t), metadata(t))
157157
elseif length(args) == 2
158158
x, y = args
159-
return similarterm(t, f, x <ₑ y ? [x,y] : [y,x])
159+
return maketerm(typeof(t), f, x <ₑ y ? [x,y] : [y,x], symtype(t), metadata(t))
160160
end
161161
args = args isa Tuple ? [args...] : args
162-
similarterm(t, f, sort(args, lt=<ₑ))
162+
maketerm(typeof(t), f, sort(args, lt=<), symtype(t), metadata(t))
163163
end
164164

165165
# Linked List interface
@@ -225,7 +225,7 @@ macro matchable(expr)
225225
SymbolicUtils.arguments(x::$name) = getfield.((x,), ($(QuoteNode.(fields)...),))
226226
SymbolicUtils.children(x::$name) = [SymbolicUtils.operation(x); SymbolicUtils.children(x)]
227227
Base.length(x::$name) = $(length(fields) + 1)
228-
SymbolicUtils.similarterm(x::$name, f, args, type; kw...) = f(args...)
228+
SymbolicUtils.maketerm(x::$name, f, args, type, metadata) = f(args...)
229229
end |> esc
230230
end
231231

test/basics.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,20 @@ end
214214
@test_reference "inspect_output/sub14.txt" sprint(io->SymbolicUtils.inspect(io, SymbolicUtils.pluck(ex, 14)))
215215
end
216216

217-
@testset "similarterm" begin
217+
@testset "maketerm" begin
218218
@syms a b c
219-
@test isequal(SymbolicUtils.similarterm((b + c), +, [a, (b+c)]).dict, Dict(a=>1,b=>1,c=>1))
220-
@test isequal(SymbolicUtils.similarterm(b^2, ^, [b^2, 1//2]), b)
219+
@test isequal(SymbolicUtils.maketerm(typeof(b + c), +, [a, (b+c)], Number, nothing).dict, Dict(a=>1,b=>1,c=>1))
220+
@test isequal(SymbolicUtils.maketerm(typeof(b^2), ^, [b^2, 1//2], Number, nothing), b)
221221

222-
# test that similarterm doesn't hard-code BasicSymbolic subtype
222+
# test that maketerm doesn't hard-code BasicSymbolic subtype
223223
# and is consistent with BasicSymbolic arithmetic operations
224-
@test isequal(SymbolicUtils.similarterm(a / b, *, [a / b, c]), (a / b) * c)
225-
@test isequal(SymbolicUtils.similarterm(a * b, *, [0, c]), 0)
226-
@test isequal(SymbolicUtils.similarterm(a^b, ^, [a * b, 3]), (a * b)^3)
224+
@test isequal(SymbolicUtils.maketerm(typeof(a / b), *, [a / b, c], Number, nothing), (a / b) * c)
225+
@test isequal(SymbolicUtils.maketerm(typeof(a * b), *, [0, c], Number, nothing), 0)
226+
@test isequal(SymbolicUtils.maketerm(typeof(a^b), ^, [a * b, 3], Number, nothing), (a * b)^3)
227227

228-
# test that similarterm sets metadata correctly
228+
# test that maketerm sets metadata correctly
229229
metadata = Base.ImmutableDict{DataType, Any}(Ctx1, "meta_1")
230-
s = SymbolicUtils.similarterm(a^b, ^, [a * b, 3]; metadata = metadata)
230+
s = SymbolicUtils.maketerm(typeof(a^b), ^, [a * b, 3], Number, metadata)
231231
@test hasmetadata(s, Ctx1)
232232
@test getmetadata(s, Ctx1) == "meta_1"
233233
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ else
3434
include("cse.jl")
3535
include("interface.jl")
3636
# Disabled until https://github.com/JuliaMath/SpecialFunctions.jl/issues/446 is fixed
37-
# include("fuzz.jl")
37+
include("fuzz.jl")
3838
include("adjoints.jl")
3939
end

0 commit comments

Comments
 (0)