Skip to content

Commit dcb7728

Browse files
Shashi Gowda0x0f0f0fYingboMa
committed
Term interface with comments
Co-authored-by: "Alessandro Cheli" <sudo-woodo3@protonmail.com> Co-authored-by: "Yingbo Ma" <mayingbo5@gmail.com>
1 parent b3c1da6 commit dcb7728

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

src/TermInterface.jl

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

3-
function isterm end
3+
"""
4+
isterm(x)
5+
6+
Returns `true` if `x` is a term. If true, `gethead`, `getargs`
7+
must also be defined for `x` appropriately.
8+
"""
9+
isterm(x) = false
10+
11+
"""
12+
symtype(x)
13+
14+
Returns the symbolic type of `x`. By default this is just `typeof(x)`.
15+
"""
16+
function symtype(x)
17+
typeof(x)
18+
end
19+
20+
"""
21+
issym(x)
22+
23+
Returns `true` if `x` is a symbol. If true, `nameof` must be defined
24+
on `x` and must return a Symbol.
25+
"""
426
function issym end
27+
28+
"""
29+
gethead(x)
30+
31+
If `x` is a term as defined by `isterm(x)`, `gethead(x)` returns the
32+
head of the term if `x` represents a function call, for example, the head
33+
is the function being called.
34+
"""
535
function gethead end
36+
37+
"""
38+
getargs(x)
39+
40+
Get the arguments of `x`, must be defined if `isterm(x)` is `true`.
41+
"""
642
function getargs end
7-
function metadata end
43+
44+
"""
45+
metadata(x)
46+
47+
Return the metadata attached to `x`.
48+
"""
49+
metadata(x) = nothing
50+
51+
"""
52+
metadata(x, md)
53+
54+
Returns a new term which has the structure of `x` but also has
55+
the metadata `md` attached to it.
56+
"""
57+
function metadata(x, data)
58+
error("Setting metadata on $x is not possible")
59+
end
60+
61+
62+
"""
63+
similarterm(x, head, args, type; metadata=nothing)
64+
65+
Returns a term that is in the same closure of types as `typeof(x)`,
66+
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...)`
68+
"""
69+
similarterm(x, head, args, type; metadata=nothing) = head(args...)
870

971
end # module

0 commit comments

Comments
 (0)