Skip to content

Commit f6cb58d

Browse files
authored
Merge pull request #1 from JuliaSymbolics/improvements
improvements for supporting metatheory.jl
2 parents dcb7728 + a742c5b commit f6cb58d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/TermInterface.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Returns `true` if `x` is a term. If true, `gethead`, `getargs`
77
must also be defined for `x` appropriately.
88
"""
99
isterm(x) = false
10+
isterm(x::Type{Expr}) = true
1011

1112
"""
1213
symtype(x)
@@ -33,13 +34,22 @@ head of the term if `x` represents a function call, for example, the head
3334
is the function being called.
3435
"""
3536
function gethead end
37+
gethead(e::Expr) = e.head
3638

3739
"""
3840
getargs(x)
3941
4042
Get the arguments of `x`, must be defined if `isterm(x)` is `true`.
4143
"""
42-
function getargs end
44+
getargs(e::Expr) = e.args
45+
46+
"""
47+
arity(x)
48+
49+
Returns the number of arguments of `x`. Implicitly defined
50+
if `getargs(x)` is defined.
51+
"""
52+
arity(x) = length(getargs(x))
4353

4454
"""
4555
metadata(x)
@@ -64,8 +74,11 @@ end
6474
6575
Returns a term that is in the same closure of types as `typeof(x)`,
6676
with `head` as the head and `args` as the arguments, `type` as the symtype
67-
and `metadata` as the metadata. By default this will execute `head(args...)`
77+
and `metadata` as the metadata. By default this will execute `head(args...)`.
78+
`x` parameter can also be a `Type`.
6879
"""
69-
similarterm(x, head, args, type; metadata=nothing) = head(args...)
80+
similarterm(x, head, args; type=nothing, metadata=nothing) = head(args...)
81+
similarterm(x::Type{Expr}, head, args; type=nothing, metadata=nothing) = Expr(head, args...)
7082

7183
end # module
84+

0 commit comments

Comments
 (0)