Skip to content

Commit 9ef4c24

Browse files
author
Alessandro
committed
refactor
1 parent 201241d commit 9ef4c24

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

src/TermInterface.jl

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

33
"""
4-
istree(x)
4+
istree(x)
55
66
Returns `true` if `x` is a term. If true, `operation`, `arguments`
77
must also be defined for `x` appropriately.
@@ -11,20 +11,20 @@ istree(x::Type{T}) where {T} = false
1111
export istree
1212

1313
"""
14-
symtype(x)
14+
symtype(x)
1515
1616
Returns the symbolic type of `x`. By default this is just `typeof(x)`.
1717
Define this for your symbolic types if you want `SymbolicUtils.simplify` to apply rules
1818
specific to numbers (such as commutativity of multiplication). Or such
1919
rules that may be implemented in the future.
2020
"""
2121
function symtype(x)
22-
typeof(x)
22+
typeof(x)
2323
end
2424
export symtype
2525

2626
"""
27-
issym(x)
27+
issym(x)
2828
2929
Returns `true` if `x` is a symbol. If true, `nameof` must be defined
3030
on `x` and must return a Symbol.
@@ -34,7 +34,7 @@ issym(x::Type{T}) where {T} = false
3434
export issym
3535

3636
"""
37-
exprhead(x)
37+
exprhead(x)
3838
3939
If `x` is a term as defined by `istree(x)`, `exprhead(x)` must return a symbol,
4040
corresponding to the head of the `Expr` most similar to the term `x`.
@@ -49,7 +49,7 @@ export exprhead
4949

5050

5151
"""
52-
operation(x)
52+
operation(x)
5353
5454
If `x` is a term as defined by `istree(x)`, `operation(x)` returns the
5555
head of the term if `x` represents a function call, for example, the head
@@ -59,7 +59,7 @@ function operation end
5959
export operation
6060

6161
"""
62-
arguments(x)
62+
arguments(x)
6363
6464
Get the arguments of `x`, must be defined if `istree(x)` is `true`.
6565
"""
@@ -68,7 +68,7 @@ export arguments
6868

6969

7070
"""
71-
unsorted_arguments(x::T)
71+
unsorted_arguments(x::T)
7272
7373
If x is a term satisfying `istree(x)` and your term type `T` orovides
7474
and optimized implementation for storing the arguments, this function can
@@ -80,7 +80,7 @@ export unsorted_arguments
8080

8181

8282
"""
83-
arity(x)
83+
arity(x)
8484
8585
Returns the number of arguments of `x`. Implicitly defined
8686
if `arguments(x)` is defined.
@@ -90,7 +90,7 @@ export arity
9090

9191

9292
"""
93-
metadata(x)
93+
metadata(x)
9494
9595
Return the metadata attached to `x`.
9696
"""
@@ -99,36 +99,36 @@ export metadata
9999

100100

101101
"""
102-
metadata(x, md)
102+
metadata(x, md)
103103
104104
Returns a new term which has the structure of `x` but also has
105105
the metadata `md` attached to it.
106106
"""
107107
function metadata(x, data)
108-
error("Setting metadata on $x is not possible")
108+
error("Setting metadata on $x is not possible")
109109
end
110110

111111

112112
"""
113-
similarterm(x, head, args, symtype=nothing; metadata=nothing, exprhead=:call)
113+
similarterm(x, head, args, symtype=nothing; metadata=nothing, exprhead=:call)
114114
115115
Returns a term that is in the same closure of types as `typeof(x)`,
116116
with `head` as the head and `args` as the arguments, `type` as the symtype
117117
and `metadata` as the metadata. By default this will execute `head(args...)`.
118118
`x` parameter can also be a `Type`. The `exprhead` keyword argument is useful
119119
when manipulating `Expr`s.
120120
"""
121-
function similarterm(x, head, args, symtype=nothing; metadata=nothing, exprhead=nothing)
122-
if exprhead === nothing
123-
similarterm(typeof(x), head, args, symtype; metadata=metadata)
124-
else
125-
similarterm(typeof(x), head, args, symtype; metadata=metadata, exprhead=exprhead)
126-
end
121+
function similarterm(x, head, args, symtype = nothing; metadata = nothing, exprhead = nothing)
122+
if exprhead === nothing
123+
similarterm(typeof(x), head, args, symtype; metadata = metadata)
124+
else
125+
similarterm(typeof(x), head, args, symtype; metadata = metadata, exprhead = exprhead)
126+
end
127127
end
128128

129-
function similarterm(x::Type{T}, head, args, symtype=nothing; metadata=nothing, exprhead=:call) where T
130-
!istree(T) ? head : head(args...)
131-
end
129+
function similarterm(x::Type{T}, head, args, symtype = nothing; metadata = nothing, exprhead = :call) where {T}
130+
!istree(T) ? head : head(args...)
131+
end
132132
export similarterm
133133

134134
include("utils.jl")

src/expr.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ istree(x::Type{Expr}) = true
55
exprhead(e::Expr) = e.head
66

77
operation(e::Expr) = expr_operation(e, Val{exprhead(e)}())
8-
arguments(e::Expr) = expr_arguments(e, Val{exprhead(e)}())
8+
arguments(e::Expr) = expr_arguments(e, Val{exprhead(e)}())
99

1010
# See https://docs.julialang.org/en/v1/devdocs/ast/
11-
expr_operation(e::Expr, ::Union{Val{:call}, Val{:macrocall}}) = e.args[1]
11+
expr_operation(e::Expr, ::Union{Val{:call},Val{:macrocall}}) = e.args[1]
1212
expr_operation(e::Expr, ::Union{Val{:ref}}) = getindex
1313
expr_operation(e::Expr, ::Val{T}) where {T} = T
1414

15-
expr_arguments(e::Expr, ::Union{Val{:call}, Val{:macrocall}}) = e.args[2:end]
15+
expr_arguments(e::Expr, ::Union{Val{:call},Val{:macrocall}}) = e.args[2:end]
1616
expr_arguments(e::Expr, _) = e.args
1717

1818

19-
function similarterm(x::Type{Expr}, head, args, symtype=nothing; metadata=nothing, exprhead=:call)
20-
expr_similarterm(head, args, Val{exprhead}())
19+
function similarterm(x::Type{Expr}, head, args, symtype = nothing; metadata = nothing, exprhead = :call)
20+
expr_similarterm(head, args, Val{exprhead}())
2121
end
2222

23-
function similarterm(x::Expr, head, args, symtype=nothing; metadata=nothing, exprhead=exprhead(x))
24-
expr_similarterm(head, args, Val{exprhead}())
23+
function similarterm(x::Expr, head, args, symtype = nothing; metadata = nothing, exprhead = exprhead(x))
24+
expr_similarterm(head, args, Val{exprhead}())
2525
end
2626

2727

src/utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
is_operation(f)
2+
is_operation(f)
33
44
Returns a single argument anonymous function predicate, that returns `true` if and only if
55
the argument to the predicate satisfies `istree` and `operation(x) == f`
@@ -9,8 +9,8 @@ export is_operation
99

1010

1111
"""
12-
node_count(t)
12+
node_count(t)
1313
Count the nodes in a symbolic expression tree satisfying `istree` and `arguments`.
1414
"""
15-
node_count(t) = istree(t) ? reduce(+, node_count(x) for x in arguments(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)