Skip to content

Commit 70f0af0

Browse files
author
Alessandro
committed
changed names of operations for compatibility with symbolicutils and symbolics
1 parent 3381fff commit 70f0af0

File tree

3 files changed

+34
-34
lines changed

3 files changed

+34
-34
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ You should define the following methods for an expression tree type `T` with sym
99
with TermInterface.jl, and therefore with [SymbolicUtils.jl](https://github.com/JuliaSymbolics/SymbolicUtils.jl)
1010
and [Metatheory.jl](https://github.com/0x0f0f0f/Metatheory.jl).
1111

12-
#### `isterm(x::T)` or `isterm(x::Type{T})`
12+
#### `istree(x::T)` or `istree(x::Type{T})`
1313

1414
Check if `x` represents an expression tree. If returns true,
15-
it will be assumed that `gethead(::T)` and `getargs(::T)`
15+
it will be assumed that `operation(::T)` and `arguments(::T)`
1616
methods are defined. Definining these three should allow use
1717
of `SymbolicUtils.simplify` on custom types. Optionally `symtype(x)` can be
1818
defined to return the expected type of the symbolic expression.
1919

20-
#### `gethead(x::T)`
20+
#### `operation(x::T)`
2121

2222
Returns the head (a function object) performed by an expression
23-
tree. Called only if `isterm(::T)` is true. Part of the API required
24-
for `simplify` to work. Other required methods are `getargs` and `isterm`
23+
tree. Called only if `istree(::T)` is true. Part of the API required
24+
for `simplify` to work. Other required methods are `arguments` and `istree`
2525

26-
#### `getargs(x::T)`
26+
#### `arguments(x::T)`
2727

2828
Returns the arguments (a `Vector`) for an expression tree.
29-
Called only if `isterm(x)` is `true`. Part of the API required
30-
for `simplify` to work. Other required methods are `gethead` and `isterm`
29+
Called only if `istree(x)` is `true`. Part of the API required
30+
for `simplify` to work. Other required methods are `operation` and `istree`
3131

3232
In addition, the methods for `Base.hash` and `Base.isequal` should also be implemented by the types for the purposes of substitution and equality matching respectively.
3333

@@ -71,16 +71,16 @@ ex = 1 + (:x - 2)
7171
```
7272

7373

74-
How can we use SymbolicUtils.jl to convert `ex` to `(-)(:x, 1)`? We simply implement `isterm`,
74+
How can we use SymbolicUtils.jl to convert `ex` to `(-)(:x, 1)`? We simply implement `istree`,
7575
`head`, `arguments` and we'll be able to do rule-based rewriting on `Expr`s:
7676
```julia
7777
using TermInterface
7878
using SymbolicUtils
7979

8080

81-
TermInterface.isterm(ex::Expr) = ex.head == :call
82-
TermInterface.gethead(ex::Expr) = ex.args[1]
83-
TermInterface.getargs(ex::Expr) = ex.args[2:end]
81+
TermInterface.istree(ex::Expr) = ex.head == :call
82+
TermInterface.operation(ex::Expr) = ex.args[1]
83+
TermInterface.arguments(ex::Expr) = ex.args[2:end]
8484
TermInterface.similarterm(x::Type{Expr}, head, args; type=nothing, metadata=nothing) = Expr(:call, head, args...)
8585

8686
@rule(~x => ~x - 1)(ex)

src/TermInterface.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module TermInterface
22

33
"""
4-
isterm(x)
4+
istree(x)
55
6-
Returns `true` if `x` is a term. If true, `gethead`, `getargs`
6+
Returns `true` if `x` is a term. If true, `operation`, `arguments`
77
must also be defined for `x` appropriately.
88
"""
9-
isterm(x) = isterm(typeof(x))
10-
isterm(x::Type{Expr}) = true
11-
isterm(x::Type{T}) where {T} = false
12-
export isterm
9+
istree(x) = istree(typeof(x))
10+
istree(x::Type{Expr}) = true
11+
istree(x::Type{T}) where {T} = false
12+
export istree
1313

1414
"""
1515
symtype(x)
@@ -34,31 +34,31 @@ function issym end
3434
export issym
3535

3636
"""
37-
gethead(x)
37+
operation(x)
3838
39-
If `x` is a term as defined by `isterm(x)`, `gethead(x)` returns the
39+
If `x` is a term as defined by `istree(x)`, `operation(x)` returns the
4040
head of the term if `x` represents a function call, for example, the head
4141
is the function being called.
4242
"""
43-
function gethead end
44-
gethead(e::Expr) = e.head
45-
export gethead
43+
function operation end
44+
operation(e::Expr) = e.head
45+
export operation
4646

4747
"""
48-
getargs(x)
48+
arguments(x)
4949
50-
Get the arguments of `x`, must be defined if `isterm(x)` is `true`.
50+
Get the arguments of `x`, must be defined if `istree(x)` is `true`.
5151
"""
52-
getargs(e::Expr) = e.args
53-
export getargs
52+
arguments(e::Expr) = e.args
53+
export arguments
5454

5555
"""
5656
arity(x)
5757
5858
Returns the number of arguments of `x`. Implicitly defined
59-
if `getargs(x)` is defined.
59+
if `arguments(x)` is defined.
6060
"""
61-
arity(x) = length(getargs(x))
61+
arity(x) = length(arguments(x))
6262
export arity
6363

6464

@@ -99,7 +99,7 @@ similarterm(x, head, args; metadata=nothing) =
9999

100100
similarterm(x::Type{Expr}, head, args, symtype=nothing; metadata=nothing) = Expr(head, args...)
101101
function similarterm(x::Type{T}, head::T, args, symtype=nothing; metadata=nothing) where T
102-
if !isterm(T) head else head(args...) end
102+
if !istree(T) head else head(args...) end
103103
end
104104
export similarterm
105105

src/utils.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
is_operation(f)
33
44
Returns a single argument anonymous function predicate, that returns `true` if and only if
5-
the argument to the predicate satisfies `isterm` and `gethead(x) == f`
5+
the argument to the predicate satisfies `istree` and `operation(x) == f`
66
"""
7-
is_operation(f) = @nospecialize(x) -> isterm(x) && (gethead(x) == f)
7+
is_operation(f) = @nospecialize(x) -> istree(x) && (operation(x) == f)
88
export is_operation
99

1010

1111
"""
1212
node_count(t)
13-
Count the nodes in a symbolic expression tree satisfying `isterm` and `getargs`.
13+
Count the nodes in a symbolic expression tree satisfying `istree` and `arguments`.
1414
"""
15-
node_count(t) = isterm(t) ? reduce(+, node_count(x) for x in getargs(t), init=0) + 1 : 1
15+
node_count(t) = istree(t) ? reduce(+, node_count(x) for x in arguments(t), init=0) + 1 : 1
1616
export node_count

0 commit comments

Comments
 (0)