Skip to content

Commit c588a6e

Browse files
author
Shashi Gowda
committed
rename istree to iscall
1 parent d68e092 commit c588a6e

16 files changed

+78
-75
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2222
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2323
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2424
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
25+
TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c"
2526
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
2627
Unityper = "a7c27f48-0311-42f6-a7f8-2c11e75eb415"
2728

src/SymbolicUtils.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ $(DocStringExtensions.README)
44
module SymbolicUtils
55

66
using DocStringExtensions
7+
78
export @syms, term, showraw, hasmetadata, getmetadata, setmetadata
89

910
using Unityper
10-
11-
# Sym, Term,
12-
# Add, Mul and Pow
11+
using TermInterface
1312
using DataStructures
1413
using Setfield
1514
import Setfield: PropertyLens
1615
using SymbolicIndexingInterface
1716
import Base: +, -, *, /, //, \, ^, ImmutableDict
1817
using ConstructionBase
1918
include("interface.jl")
19+
20+
# Sym, Term,
21+
# Add, Mul and Pow
2022
include("types.jl")
21-
export istree, operation, arguments, similarterm
23+
export iscall, operation, arguments, similarterm
2224

2325
# Methods on symbolic objects
2426
using SpecialFunctions, NaNMath

src/code.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export toexpr, Assignment, (←), Let, Func, DestructuredArgs, LiteralExpr,
88

99
import ..SymbolicUtils
1010
import ..SymbolicUtils.Rewriters
11-
import SymbolicUtils: @matchable, BasicSymbolic, Sym, Term, istree, operation, arguments, issym,
11+
import SymbolicUtils: @matchable, BasicSymbolic, Sym, Term, iscall, operation, arguments, issym,
1212
symtype, similarterm, unsorted_arguments, metadata, isterm, term
1313

1414
##== state management ==##
@@ -162,7 +162,7 @@ end
162162
toexpr(O::Expr, st) = O
163163

164164
function substitute_name(O, st)
165-
if (issym(O) || istree(O)) && haskey(st.rewrites, O)
165+
if (issym(O) || iscall(O)) && haskey(st.rewrites, O)
166166
st.rewrites[O]
167167
else
168168
O
@@ -176,13 +176,13 @@ function toexpr(O, st)
176176
end
177177
O = substitute_name(O, st)
178178

179-
!istree(O) && return O
179+
!iscall(O) && return O
180180
op = operation(O)
181181
expr′ = function_to_expr(op, O, st)
182182
if expr′ !== nothing
183183
return expr′
184184
else
185-
!istree(O) && return O
185+
!iscall(O) && return O
186186
args = arguments(O)
187187
return Expr(:call, toexpr(op, st), map(x->toexpr(x, st), args)...)
188188
end
@@ -221,7 +221,7 @@ get_rewrites(args::DestructuredArgs) = ()
221221
function get_rewrites(args::Union{AbstractArray, Tuple})
222222
cflatten(map(get_rewrites, args))
223223
end
224-
get_rewrites(x) = istree(x) ? (x,) : ()
224+
get_rewrites(x) = iscall(x) ? (x,) : ()
225225
cflatten(x) = Iterators.flatten(x) |> collect
226226

227227
# Used in Symbolics
@@ -691,7 +691,7 @@ end
691691
@inline newsym(::Type{T}) where T = Sym{T}(gensym("cse"))
692692

693693
function _cse!(mem, expr)
694-
istree(expr) || return expr
694+
iscall(expr) || return expr
695695
op = _cse!(mem, operation(expr))
696696
args = map(Base.Fix1(_cse!, mem), arguments(expr))
697697
t = similarterm(expr, op, args)
@@ -742,7 +742,7 @@ end
742742

743743

744744
function cse_state!(state, t)
745-
!istree(t) && return t
745+
!iscall(t) && return t
746746
state[t] = Base.get!(state, t, 0) + 1
747747
foreach(x->cse_state!(state, x), unsorted_arguments(t))
748748
end
@@ -758,7 +758,7 @@ function cse_block!(assignments, counter, names, name, state, x)
758758
counter[] += 1
759759
return sym
760760
end
761-
elseif istree(x)
761+
elseif iscall(x)
762762
args = map(a->cse_block!(assignments, counter, names, name, state,a), unsorted_arguments(x))
763763
if isterm(x)
764764
return term(operation(x), args...)

src/inspect.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import AbstractTrees
22

33
const inspect_metadata = Ref{Bool}(false)
44
function AbstractTrees.nodevalue(x::Symbolic)
5-
istree(x) ? operation(x) : x
5+
iscall(x) ? operation(x) : x
66
end
77

88
function AbstractTrees.nodevalue(x::BasicSymbolic)
9-
str = if !istree(x)
9+
str = if !iscall(x)
1010
string(exprtype(x), "(", x, ")")
1111
elseif isadd(x)
1212
string(exprtype(x),
@@ -27,7 +27,7 @@ function AbstractTrees.nodevalue(x::BasicSymbolic)
2727
end
2828

2929
function AbstractTrees.children(x::Symbolic)
30-
istree(x) ? arguments(x) : ()
30+
iscall(x) ? arguments(x) : ()
3131
end
3232

3333
"""

src/interface.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
2-
istree(x)
2+
iscall(x)
33
44
Returns `true` if `x` is a term. If true, `operation`, `arguments`
55
must also be defined for `x` appropriately.
66
"""
7-
istree(x) = false
7+
iscall(x) = false
88

99
"""
1010
symtype(x)
@@ -29,7 +29,7 @@ issym(x) = false
2929
"""
3030
operation(x)
3131
32-
If `x` is a term as defined by `istree(x)`, `operation(x)` returns the
32+
If `x` is a term as defined by `iscall(x)`, `operation(x)` returns the
3333
head of the term if `x` represents a function call, for example, the head
3434
is the function being called.
3535
"""
@@ -38,14 +38,14 @@ function operation end
3838
"""
3939
arguments(x)
4040
41-
Get the arguments of `x`, must be defined if `istree(x)` is `true`.
41+
Get the arguments of `x`, must be defined if `iscall(x)` is `true`.
4242
"""
4343
function arguments end
4444

4545
"""
4646
unsorted_arguments(x::T)
4747
48-
If x is a term satisfying `istree(x)` and your term type `T` provides
48+
If x is a term satisfying `iscall(x)` and your term type `T` provides
4949
an optimized implementation for storing the arguments, this function can
5050
be used to retrieve the arguments when the order of arguments does not matter
5151
but the speed of the operation does.

src/matchers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# 3. Callback: takes arguments Dictionary × Number of elements matched
77
#
88
function matcher(val::Any)
9-
istree(val) && return term_matcher(val)
9+
iscall(val) && return term_matcher(val)
1010
function literal_matcher(next, data, bindings)
1111
islist(data) && isequal(car(data), val) ? next(bindings, 1) : nothing
1212
end
@@ -89,7 +89,7 @@ function term_matcher(term)
8989
function term_matcher(success, data, bindings)
9090

9191
!islist(data) && return nothing
92-
!istree(car(data)) && return nothing
92+
!iscall(car(data)) && return nothing
9393

9494
function loop(term, bindings′, matchers′) # Get it to compile faster
9595
if !islist(matchers′)

src/ordering.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
function get_degrees(expr)
2121
if issym(expr)
2222
((Symbol(expr),) => 1,)
23-
elseif istree(expr)
23+
elseif iscall(expr)
2424
op = operation(expr)
2525
args = arguments(expr)
2626
if operation(expr) == (^) && args[2] isa Number
@@ -62,7 +62,7 @@ function lexlt(degs1, degs2)
6262
return false # they are equal
6363
end
6464

65-
_arglen(a) = istree(a) ? length(unsorted_arguments(a)) : 0
65+
_arglen(a) = iscall(a) ? length(unsorted_arguments(a)) : 0
6666

6767
function <(a::Tuple, b::Tuple)
6868
for (x, y) in zip(a, b)

src/polyform.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Bijections
66
77
Abstracts a [MultivariatePolynomials.jl](https://juliaalgebra.github.io/MultivariatePolynomials.jl/stable/) as a SymbolicUtils expression and vice-versa.
88
9-
The SymbolicUtils term interface (`istree`, `operation, and `arguments`) works on PolyForm lazily:
9+
The SymbolicUtils term interface (`iscall`, `operation, and `arguments`) works on PolyForm lazily:
1010
the `operation` and `arguments` are created by converting one level of arguments into SymbolicUtils expressions. They may further contain PolyForm within them.
1111
We use this to hold polynomials in memory while doing `simplify_fractions`.
1212
@@ -97,7 +97,7 @@ _isone(p::PolyForm) = isone(p.p)
9797
function polyize(x, pvar2sym, sym2term, vtype, pow, Fs, recurse)
9898
if x isa Number
9999
return x
100-
elseif istree(x)
100+
elseif iscall(x)
101101
if !(symtype(x) <: Number)
102102
error("Cannot convert $x of symtype $(symtype(x)) into a PolyForm")
103103
end
@@ -170,8 +170,8 @@ function PolyForm(x,
170170
PolyForm{symtype(x)}(p, pvar2sym, sym2term, metadata)
171171
end
172172

173-
istree(x::Type{<:PolyForm}) = true
174-
istree(x::PolyForm) = true
173+
iscall(x::Type{<:PolyForm}) = true
174+
iscall(x::PolyForm) = true
175175

176176
function similarterm(t::PolyForm, f, args, symtype; metadata=nothing)
177177
basic_similarterm(t, f, args, symtype; metadata=metadata)
@@ -336,7 +336,7 @@ function simplify_fractions(x; polyform=false)
336336
end
337337

338338
function add_with_div(x, flatten=true)
339-
(!istree(x) || operation(x) != (+)) && return x
339+
(!iscall(x) || operation(x) != (+)) && return x
340340
aa = unsorted_arguments(x)
341341
!any(a->isdiv(a), aa) && return x # no rewrite necessary
342342

@@ -361,26 +361,26 @@ function flatten_fractions(x)
361361
end
362362

363363
function fraction_iszero(x)
364-
!istree(x) && return _iszero(x)
364+
!iscall(x) && return _iszero(x)
365365
ff = flatten_fractions(x)
366366
# fast path and then slow path
367367
any(_iszero, numerators(ff)) ||
368368
any(_iszeroexpand, numerators(ff))
369369
end
370370

371371
function fraction_isone(x)
372-
!istree(x) && return _isone(x)
372+
!iscall(x) && return _isone(x)
373373
_isone(simplify_fractions(flatten_fractions(x)))
374374
end
375375

376376
function needs_div_rules(x)
377377
(isdiv(x) && !(x.num isa Number) && !(x.den isa Number)) ||
378-
(istree(x) && operation(x) === (+) && count(has_div, unsorted_arguments(x)) > 1) ||
379-
(istree(x) && any(needs_div_rules, unsorted_arguments(x)))
378+
(iscall(x) && operation(x) === (+) && count(has_div, unsorted_arguments(x)) > 1) ||
379+
(iscall(x) && any(needs_div_rules, unsorted_arguments(x)))
380380
end
381381

382382
function has_div(x)
383-
return isdiv(x) || (istree(x) && any(has_div, unsorted_arguments(x)))
383+
return isdiv(x) || (iscall(x) && any(has_div, unsorted_arguments(x)))
384384
end
385385

386386
flatten_pows(xs) = map(xs) do x

src/rewriters.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ rewriters.
3232
module Rewriters
3333
using SymbolicUtils: @timer
3434

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

3838
# Cache of printed rules to speed up @timer
@@ -190,11 +190,11 @@ instrument(x::PassThrough, f) = PassThrough(instrument(x.rw, f))
190190
passthrough(x, default) = x === nothing ? default : x
191191
function (p::Walk{ord, C, F, false})(x) where {ord, C, F}
192192
@assert ord === :pre || ord === :post
193-
if istree(x)
193+
if iscall(x)
194194
if ord === :pre
195195
x = p.rw(x)
196196
end
197-
if istree(x)
197+
if iscall(x)
198198
x = p.similarterm(x, operation(x), map(PassThrough(p), unsorted_arguments(x)))
199199
end
200200
return ord === :post ? p.rw(x) : x
@@ -205,11 +205,11 @@ end
205205

206206
function (p::Walk{ord, C, F, true})(x) where {ord, C, F}
207207
@assert ord === :pre || ord === :post
208-
if istree(x)
208+
if iscall(x)
209209
if ord === :pre
210210
x = p.rw(x)
211211
end
212-
if istree(x)
212+
if iscall(x)
213213
_args = map(arguments(x)) do arg
214214
if node_count(arg) > p.thread_cutoff
215215
Threads.@spawn p(arg)

src/rule.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ end
120120
getdepth(r::Rule) = r.depth
121121

122122
function rule_depth(rule, d=0, maxdepth=0)
123-
if istree(rule)
123+
if iscall(rule)
124124
maxdepth = reduce(max, (rule_depth(r, d+1, maxdepth) for r in arguments(rule)), init=1)
125125
elseif rule isa Slot || rule isa Segment
126126
maxdepth = max(d, maxdepth)
@@ -389,7 +389,7 @@ Base.show(io::IO, acr::ACRule) = print(io, "ACRule(", acr.rule, ")")
389389

390390
function (acr::ACRule)(term)
391391
r = Rule(acr)
392-
if !istree(term)
392+
if !iscall(term)
393393
r(term)
394394
else
395395
f = operation(term)

0 commit comments

Comments
 (0)