r/ProgrammingLanguages • u/mhinsch • Jun 27 '26
Auto-eval of variables containing quoted expressions?
Generally my language is eagerly evaluated. It also does not have automatic quoting, so any situation where evaluation is supposed to be deferred has to be made explicit (using []). This is used heavily for example for control structures which are regular functions that take a quoted expression as a(n) argument(s).
Some design issues I am currently having would resolve neatly if variables that contain quoted expressions would automatically evaluate. I.e. any occurrence of a variable var that is not bound to a value, but to an expression would effectively be read as eval var.
This seems a bit unorthodox, but so far I haven't been able to come up with a scenario that makes this problematic. Am I missing something?
3
u/Skepfyr Jun 28 '26
I think what you're describing is much closer to laziness than traditional quoting, although I agree they are very similar. I'm assumimg you capture the values of free variables rather than just their name, otherwise evaluating them later would have very surprising results.
As to your question, treating
varaseval varwill be surprising to users in a few ways:First, It will cause repeated re-evaluation if you use the variable more than once, if your language isn't pure then then this is obviously strange, and if it is then it's still observable in time and could make some algorithms have significantly worse time complexity than expected accidentally.
Second, If you re-bind the variable but don't always needed use it then it will still always get evaluated. Imagine let-binding it to rename it and then using it in a quote, people thinking of it as laziness might be surprised.
Third, it will have confusing behaviours in polymorphic functions (I'm assuming the fact that it's quoted is in the type). Consider the polymorphic identity function, that either breaks the rule that referring to a quote automatically evaluate it, or it introduces weirdness to the type system because the type it turns isn't the same as it takes.
I would either keep eval explicit or make it work much more like a fully lazy value (I'm something like Haskell).
2
u/useerup ting language Jun 28 '26
Does your language have first class functions? It sounds like you can pass a function/closure (quoted expression) to another function (control structure?).
One issue I can see with automatic evaluation is that you will be unable to store a quoted expression in a variable with the intention to use that variable as the parameter instead of a literal [...].
6
u/initial-algebra Jun 27 '26 edited Jun 27 '26
I don't really know what you mean by "not bound to a value, but to an expression", but a clean/principled way to automatically eval (or unquote) when needed is to use "let box" bindings, i.e.
let [x] = M in N. Wheneverxis used in a quoted context inN, it will mean the unquote ofM, and otherwise, it will mean the eval ofM. This comes from A Modal Analysis of Staged Computation.1
u/busres Jun 28 '26
In my Mesgjs, your quoted expressions would be (first class) code block objects. They execute when you send them a "run" message.
Their value in e.g. a list is just the object itself. Specific contexts, such as loops, are documented as taking "RIC" (run-if-code) values. In those contexts, if the value is a code object, it is run and the resulting value (at the time) is used.
The automatic evaluation belongs to the evaluation context.
Does that help?
1
u/evincarofautumn Jun 28 '26
This sounds similar to how (GNU) Make does evaluation. A variable’s definition can be deferred or immediate. If it’s deferred, it doesn’t expand until it’s used in an immediate context, but at that point it’s expanded fully.
This is more or less call-by-name evaluation, since it expands each time the variable is evaluated; if you cache the value after the first evaluation, you have call-by-need instead.
It just sounds like instead of attaching this to the variable, you might be attaching it to a value instead, with quote and eval operators, but either way can be made to work.
1
u/Inconstant_Moo 🧿 Pipefish Jun 28 '26
No, that's not a problem. It's also not unorthodox, only the direction you've come at it is. Instead of "variables that contain quoted expressions that automatically evaluate" try saying "variables that are declared rather than assigned (and so, as an implementation detail, are evaluated by need)".
This does indeed solve a bunch of design issues.
https://www.cs.cmu.edu/~crary/819-f09/Landin66.pdf