Interfaces for traversing terms is easy, but similarterm
is the main magic of this package. The awkwardness of the current implementation is the use of exprhead
as a keyword argument in similarterm
. SymbolicUtils only has (mostly) terms that represent function calls, but we couldn't just ignore exprhead
because Julia tries to dispatch on it. Also, exprhead
is set to :call
by default -- a clear sign of abstraction leak. I think it's time to take concept of a "head" seriously...
The proposal is to just make head
the first argument to similarterm
instead of the reference object in whose image we are trying to build the new term. It then also makes sense to rename similarterm
to maketerm
. This also allows one to compute the head in other ways than using a reference term.
So the new interface could be:
maketerm(h::H, t; type=Any, metadata=nothing) # -> to be implemented by provider of H
head(t) = h
tail(t) = t
I'm using the word tail
because arguments
is already in use to mean basically tail[2:end]
in the case that the head is a function call. It would presumably have this check programmatically, going forward.
In SymbolicUtils, we would define
struct SUApply end
maketerm(::SUApply, tail; kw...) = term(tail...; kw...)
istree(x::Symbolic) = true # replace Symbolic with concrete types that are tree
head(x::Symbolic) = SUTerm()
tail(x::Symbolic) = (operation(x), arguments(x)...) # or some efficient alternative
So this would stay within the closure of types that represent function calls.
In Metatheory you'd just need to add a MTHead type.
struct MTHead; head::Symbol; end
maketerm(h::MTHead, tail) = Expr(h.head, tail...)
head(::Expr) = MTTerm() # This will be considered piracy, however, so you might want to add a wrapper around Expr in MT eventually
I have thought about this a lot, and I think it's easiest to leave type
and metadata
as keyword arguments, and not try to cram them into head
or something like that, but other arguments are welcome. These are like the stuff after :
in a typed language definition.
This should address #10 and answer #15.
@0x0f0f0f @willow-ahrens @YingboMa