TermInterface.jl icon indicating copy to clipboard operation
TermInterface.jl copied to clipboard

request for new functions: `isvariable` and `variablename`

Open nsajko opened this issue 1 year ago • 4 comments

I want to be able to convert any TermInterface.jl expression to one of the expression types defined by my CallableExpressions.jl package. However, I don't think that TermInterface.jl on its own currently makes that possible. The problem is that, for leaves of the expression tree, I'm not able to:

  • tell variables apart from constants

  • convert a foreign variable type to a native variable type, for that I need to associate the variable with a name, as each variable is determined by a single Symbol value in CallableExpressions.jl

I think it'd make sense for TermInterface.jl to introduce two additional functions:

  • isvariable (name subject to bikeshedding, of course): isvariable(x) should be true when x is a single variable, and it should be false when x is a constant or an expression (neither constant expressions nor variable expressions count as "a variable" here)

  • variablename(x) should return an object representing a unique name for the variable, ideally convertible to Symbol

Thoughts?

nsajko avatar May 31 '24 19:05 nsajko

I think it's generally a good idea, but we don't definitely want to define isvariable(x::Symbol) = true, because in Expr we won't know when a symbol is used as a variable or not. It depends on the context. I suggest to not include it in TermInterface.jl because it really does depend on the language being used.

0x0f0f0f avatar Jun 02 '24 10:06 0x0f0f0f

In Metatheory.jl I'm having

# TODO: document
Base.@inline maybe_quote_operation(x::Union{Function,DataType}) = nameof(x)
Base.@inline maybe_quote_operation(x::Symbol) = x

0x0f0f0f avatar Jun 02 '24 10:06 0x0f0f0f

we don't definitely want to define isvariable(x::Symbol) = true

Agree.

nsajko avatar Jun 02 '24 11:06 nsajko

I think this is fine. Since it's an interface addition it would be non-breaking to add.

ChrisRackauckas avatar Jun 03 '24 10:06 ChrisRackauckas