Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expr vs call? #31

Closed
nsajko opened this issue May 21, 2024 · 6 comments
Closed

expr vs call? #31

nsajko opened this issue May 21, 2024 · 6 comments

Comments

@nsajko
Copy link
Contributor

nsajko commented May 21, 2024

I don't understand the difference between isexpr and iscall. Perhaps give in the docs an example: a type T and x::T such that isexpr(x) != iscall(x)?

@nsajko
Copy link
Contributor Author

nsajko commented May 26, 2024

Example that helped me understand:

julia> using TermInterface

julia> e = :(x + 2)
:(x + 2)

julia> isexpr(e)
true

julia> iscall(e)
true

julia> head(e)
:call

julia> children(e)
3-element Vector{Any}:
  :+
  :x
 2

julia> operation(e)
:+

julia> arguments(e)
2-element view(::Vector{Any}, 2:3) with eltype Any:
  :x
 2

@nsajko nsajko closed this as completed May 26, 2024
@nsajko nsajko reopened this May 26, 2024
@nsajko
Copy link
Contributor Author

nsajko commented May 26, 2024

Actually, I still don't understand what are the semantics of head and children supposed to be. To be specific, I want to implement the TermInterface interface for several of my types.

E.g., suppose I have a type like this:

struct Expression
  operation
  arguments::Vector{Any}
end

Clearly I need these:

TermInterface.iscall(::Expression) = true
TermInterface.operation(e::Expression) = e.operation
TermInterface.arguments(e::Expression) = e.arguments

But what about isexpr, head or children?

@0x0f0f0f
Copy link
Member

0x0f0f0f commented May 29, 2024

See also this issue @nsajko : #37

  • head(x) and children(x) must always be defined when isexpr(x) is true.
  • operation and arguments should be defined and used if and only if iscall(x) is true (denotes a function call node)
  • all iscall nodes are also isexpr, but not the other way around is not always true, not all expressions are function calls.

In a functional language, all nodes are function calls (e.g. SymbolicUtils). Let's say that you have an hybrid array/functional language. iscall on v[i] is false, and iscall on f(x) is true, but both of them are nested expressions (isexpr is true).

The same goes for Julia Expr. a Expr(:block, ...) is not a function call and has no operation and arguments, but has a head and children. Expr(:call, :f, :x) has both a head and an operation, which are respectively :call and :f. Hence why we introduced this distinction!

@0x0f0f0f
Copy link
Member

Thanks for all the fixes and reports! What are you working on? Wanna get in touch on Zulip/Slack ?

@nsajko
Copy link
Contributor Author

nsajko commented May 29, 2024

Thanks for the explanation!

Thanks for all the fixes and reports!

You're welcome!

What are you working on?

CallableExpressions.jl. The main branch should now support TermInterface.jl. The package extension gives the same definition for TermInterface.children as for TermInterface.arguments. Similarly with TermInterface.operation and TermInterface.head. That's OK, right? This is the code:

https://gitlab.com/nsajko/CallableExpressions.jl/-/blob/e56fe884db08a2a17d42acb9fb74c35113a79fbe/ext/TermInterfaceExt.jl#L3-34

Wanna get in touch on Zulip/Slack ?

Sure, is there a group or something?

@nsajko nsajko closed this as completed May 29, 2024
@0x0f0f0f
Copy link
Member

Sure, is there a group or something?

Yes, the #metatheory stream on Julia Zulip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants