At compile-time, maybe syntax parameters can help. Here’s a nice introduction by greg http://www.greghendershott.com/fear-of-macros/Syntax_parameters.html
Thanks! Now checking it out…
@bkovitz I think the easiest thing to do is not to do it at compile time
just have a variable that holds the current graph and update it mutably
Indeed the version I’ve got working does it at run-time. :wink:
(Or, if you don’t want to mutate, use a state monad. :grimacing:)
Uh oh. I realize that what I’m asking for is almost a state monad. Whenever I’ve tried to do anything with monads, though (not in Racket), I’ve gotten burned.
Mostly I just want clean syntax that isn’t error-prone.
I’ve been avoiding mutating the graph because, with all sorts of intricate variations on a graph floating around, I’m hoping to stay away from bugs resulting from inadvertently referring to the wrong graph.
My comment was a bit of a joke; you probably don’t want to do monads in Racket.
Ah, good. :slightly_smiling_face:
@bkovitz I’m not suggesting mutating the graph, just have a variable which you mutate which is the current graph
Hmm, it looks like syntax-parameters
might solve a different problem than what I’m looking for.
@samth Ah, good. In fact, the thing I’ve been using, a little macro called gdo
, does exactly that.
Ohh, and now I see how syntax-parameters
might apply, since then I’d using a sort of anaphoric graph.
Continuing the original line of thought, though, how can you associate some metadata with an identifier at compile-time?
I think it depends on what you mean. Do you want to associate some metadata with a binding, or literally with an identifier?
For syntax-parameters
, what I’m thinking is that there is a fixed parameter where with-graph
could stash the current identifier inside, and define/g
could lookup that parameter to decide whether to set!
or return new result
arr, that might be problematic (scope issue)
maybe not, if define/g
expands to (set! g (make-node ... g ...))
If with-graph
where to syntax-parameterize
g
, then (syntax-local-value #'g)
from inside the lexical region would, indeed, find the parameterized value for g
.
@lexi.lambda I’m not sure (even about the terminology). I’ve got two ideas for how to use it. First idea: Let (define/g (value-of g node) ...)
define value-of
to have this bit of metadata added: “I’m a graph function”. Then the (with-graph ...)
macro could recognize an application of value-of
inside its body and rewrite it with the correct arguments (as in the example above). (The more I think about this, though, the more I think it really wants to be a sort of custom eval.)
Second idea: You tag functions with the meanings of their arguments and return values (like types), you put all these functions into a set, and a compositor function composes them into a single function in the correct way.
I think you probably don’t want to do that kind of thing, where a macro like with-graph
walks its body. (I think those are sometimes called “deep walking macros”, but they are extraordinarily hard to get right.) Instead, make value-of
defined as a macro that does the transformation itself, and make with-graph
install some static information (via syntax parameters or otherwise) that macros defined with define/g
inspect.
Hmm, defining value-of
as a macro rather than a function might make a lot of sense. Thanks. I’ll give this some thought.
@shu—hung I hadn’t thought of making define/g
do the set!
. Up until now, I’d been thinking that “g-functions” would only be functions. Thanks—new line of thought.
Say, where can I find the source code to eval
?
Technically, somewhere in the expander, but I don’t imagine looking at it will be very helpful. :)
OK, I was hoping to learn something from it. I just found an eval
here: https://github.com/racket/racket/blob/f47f685af9b311ad5fd939581a1345802a6aa3f6/racket/src/expander/eval/main.rkt