Skip to content

Commit dec6766

Browse files
author
Shashi Gowda
committed
stash
1 parent c588a6e commit dec6766

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/SymbolicUtils.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import Setfield: PropertyLens
1515
using SymbolicIndexingInterface
1616
import Base: +, -, *, /, //, \, ^, ImmutableDict
1717
using ConstructionBase
18-
include("interface.jl")
18+
import TermInterface: iscall, issym, operation, arguments, head, children, similarterm, maketerm
1919

2020
# Sym, Term,
2121
# Add, Mul and Pow
2222
include("types.jl")
23-
export iscall, operation, arguments, similarterm
23+
export iscall, operation, arguments, similarterm, maketerm
2424

2525
# Methods on symbolic objects
2626
using SpecialFunctions, NaNMath

src/types.jl

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ function unsorted_arguments(x::BasicSymbolic)
157157
if isadd(x)
158158
for (k, v) in x.dict
159159
push!(args, applicable(*,k,v) ? k*v :
160-
similarterm(k, *, [k, v]))
160+
maketerm(k, *, [k, v]))
161161
end
162162
else # MUL
163163
for (k, v) in x.dict
@@ -192,6 +192,9 @@ isadd(x) = isa_SymType(Val(:Add), x)
192192
ispow(x) = isa_SymType(Val(:Pow), x)
193193
isdiv(x) = isa_SymType(Val(:Div), x)
194194

195+
TermInterface.head(::BasicSymbolic) = basicsymbolic
196+
TermInterface.children(t::BasicSymbolic) = cons(operation(t), arguments(t))
197+
195198
###
196199
### Base interface
197200
###
@@ -528,28 +531,11 @@ end
528531

529532
unflatten(t) = t
530533

531-
"""
532-
similarterm(t, f, args, symtype; metadata=nothing)
533-
534-
Create a term that is similar in type to `t`. Extending this function allows packages
535-
using their own expression types with SymbolicUtils to define how new terms should
536-
be created. Note that `similarterm` may return an object that has a
537-
different type than `t`, because `f` also influences the result.
538-
539-
## Arguments
540-
541-
- `t` the reference term to use to create similar terms
542-
- `f` is the operation of the term
543-
- `args` is the arguments
544-
- The `symtype` of the resulting term. Best effort will be made to set the symtype of the
545-
resulting similar term to this type.
546-
"""
547-
similarterm(t::Symbolic, f, args; metadata=nothing) =
548-
similarterm(t, f, args, _promote_symtype(f, args); metadata=metadata)
549-
similarterm(t::BasicSymbolic, f, args,
550-
symtype; metadata=nothing) = basic_similarterm(t, f, args, symtype; metadata=metadata)
534+
function TermInterface.maketerm(::Type{<:BasicSymbolic}, head, args, type, metadata)
535+
basicsymbolic(first(args), args[2:end], type, metadata)
536+
end
551537

552-
function basic_similarterm(t, f, args, stype; metadata=nothing)
538+
function basicsymbolic(f, args, stype, metadata)
553539
if f isa Symbol
554540
error("$f must not be a Symbol")
555541
end
@@ -647,6 +633,36 @@ function to_symbolic(x)
647633
x
648634
end
649635

636+
"""
637+
similarterm(x, op, args, symtype=nothing; metadata=nothing)
638+
639+
"""
640+
function similarterm(x, op, args, symtype=nothing; metadata=nothing)
641+
Base.depwarn("""`similarterm` is deprecated, use `maketerm` instead.
642+
See https://github.com/JuliaSymbolics/TermInterface.jl for details.
643+
The present call can be replaced by
644+
`maketerm(typeof(x), $(head(x)), [op, args...], symtype, metadata)`""", :similarterm)
645+
646+
TermInterface.maketerm(typeof(x), callhead(x), [op, args...], symtype, metadata)
647+
end
648+
649+
# Old fallback
650+
function similarterm(T::Type, op, args, symtype=nothing; metadata=nothing)
651+
Base.depwarn("`similarterm` is deprecated, use `maketerm` instead." *
652+
"See https://github.com/JuliaSymbolics/TermInterface.jl for details.", :similarterm)
653+
op(args...)
654+
end
655+
656+
export similarterm
657+
658+
659+
"""
660+
callhead(x)
661+
Used in this deprecation cycle of `similarterm` to find the `head` argument to
662+
`maketerm`. Do not implement this, or use `similarterm` if you're using this package.
663+
"""
664+
callhead(x) = typeof(x)
665+
650666
###
651667
### Pretty printing
652668
###

0 commit comments

Comments
 (0)