News for hackers. Stuff that matters.
Re:How to slice your code into continuations by tel on Monday May 06, 2019 @05:08PM (#19843896)

    Many people seem to write DSLs these days. My goal 
    here was simply to encourage people who might not 
    typically work in a functional style to think about 
    how continuations might be useful in the design of 
    their languages, even if it seems their language is 
    imperative.
And since you quote a line a few after that paragraph, it's not like you didn't give yourself the opportunity to read it.

He's not trying to do very simple math. He's teaching fundamental DSL design techniques.

Re:How to slice your code into continuations by neilv on Monday May 06, 2019 @06:42PM (#19844686)
Continuations are a powerful thing to know, but, for promoting DSLs, I hope no one thinks they have to wrangle continuations to implement DSLs.

One way, if you're using Racket (or another Scheme or descendant), is to translate your DSL to normal syntax in Racket much like you'd write manually (and you can use imperative or only pure-functional features, your choice).

For example, were I implementing the language used by the first example in the article, I might say each assignment establishes a lexical scope, and simply translate the source syntax to nested `let` form syntax:

    (let ((a (/ 1 (+ 1 7))))
      (let ((b (* 2 (+ a (sqrt a)))))
        (let ((c (/ (atan b) (+ 1 c))))
          (+ c (* 2 d)))))
Or (since there wasn't any non-assignment code in between the assignments), how you might normally write it manually in Racket:

    (let* ((a (/ 1 (+ 1 7)))
           (b (* 2 (+ a (sqrt a))))
           (c (/ (atan b) (+ 1 c))))
      (+ c (* 2 d)))
To get to that target syntax, I might have my parser produce intermediate syntax as a kind of abstract syntax tree, such as:

    (block
      (assignment a (/ 1 (+ 1 7)))
      (assignment b (* 2 (+ a (sqrt a))))
      (assignment c (/ (atan b) (+ 1 c)))
      (return (+ c (* 2 d))))
Which, in this case, is a simple translation of syntax objects to the nested `let`s or `let*` examples above.

Of course, for a real-world DSL, you might have very different semantics than in the simple example, but the above translation method still likely applies.

You might start your language implementation by typing examples in your language, then the equivalents in Racket, then the intermediate parse trees of your examples... and then Racket `syntax-parse` or `syntax-case` generalizations your parse tree syntax to the Racket syntax.

The examples you made might then be test cases for your translation.

And if your DSL is embeddable in Racket code, you probably don't need the parser at all -- you might just need a `define-syntax`, with `syntax-parse`, `syntax-case`, or arbitrary code to do the translation.

Re:How to slice your code into continuations by tel on Monday May 06, 2019 @06:50PM (#19844741)
Sure, agreed. "Fundamental technique" wouldn't imply "necessary technique" in my vernacular. The CPS mindset is sure down there in fundamentals town, though, with respect to thinking about evaluation, etc.
Re:How to slice your code into continuations by tel on Monday May 06, 2019 @06:54PM (#19844757)
I do want to add that Lisp metaprogramming does one big trick for DSL design in that it works at the syntactic level and ignores binding. Thus all the tricks around hygenics and anaphoria etc.

Since ignoring binding means that you only deal with the call graph implicitly through working with the AS tree, you get an opportunity to pareto your way through tougher DSL nuggets.

Re:How to slice your code into continuations by TeMPOraL on Tuesday May 07, 2019 @04:31AM (#19847531)
What do you mean by "ignoring binding"? That doesn't sound right. Binding is one of the things I think about the most when building DSLs in Common Lisp.
Re:How to slice your code into continuations by tel on Tuesday May 07, 2019 @07:14AM (#19848346)
Yes, the user has to since the system ignores it. A lisp macro operates, generally, over the abstract syntax tree as opposed to the "abstract binding tree" where alpha equivalences have already been factored out.

Or, concretely, a lisp macro distinguishes between syntax `(my-let (x 3) x)` and `(my-let (y 3) y)` even though they are, under implied semantics, identical. In that way, lisp macros have little to say about binding.

Re:How to slice your code into continuations by TeMPOraL on Tuesday May 07, 2019 @09:42AM (#19849196)
I still don't understand; probably because I'm not proficient in the mathematical underpinning of this.

The way I see it, a (Common) Lisp macro can, but does not have to, distinguish between the two forms you've shown. Assuming `my-let' is the macro, from it's POV, `x' is a value. A reasonable implementation of "let" won't care about that value beyond it being a symbol comparable to other symbols by `eq'. This way, `(my-let (x 3) x)' and `(my-let (y 3) y)' will expand to effectively the same code - and after the compiler gets rid of symbol names altogether, the resulting code will be actually the same.

Re:How to slice your code into continuations by tel on Tuesday May 07, 2019 @03:15PM (#19852511)
The syntactic approach subsumes the more semantic approach where the graph structure is made explicit. So, yeah, you can "compile" list structures to the binding graph and indeed you must.

Having more granular distinctions always helps in that it's easy to "forget" how things are different. It's also more primitive in that you have to actually do that step. It also leads to opportunities to do "wrong" things like anaphoric macros.

This is essentially a tradeoff where more power is offered to the author of the macro and less to the reader.

To make another comparison, we can take it one step further: a macro preprocessor which had access to the actual character stream can elect to do things not possible in lisp macros—e.g., distinguish on indentation or something to that effect—but also can recover all of the lisp macro techniques by merely parsing the character stream.

---

The heart of all of this, popping the stack up to my original comment, is that for lots of macros you don't really need to care at all about binding structure—or at least only minimally—and thus you can get lots of practical work done doing "just" syntactic transforms.

Once you are really working over a complex call graph—which is common in more complex DSLs like probabilistic programming languages—then CPS becomes really important as you want a way to split call graphs into pieces and a practical way of doing that is (value + continuation).

Worth noting, CPS transformations are a popular technique in more sophisticated lispy macro transforms, too.

Re:How to slice your code into continuations by TeMPOraL on Wednesday May 08, 2019 @05:09AM (#19857114)
> a macro preprocessor which had access to the actual character stream can elect to do things not possible in lisp macros—e.g., distinguish on indentation or something to that effect—but also can recover all of the lisp macro techniques by merely parsing the character stream.

Common Lisp has the mechanism called "reader macros" that can do that. It's a separate mechanism, because it works at "read time", which is much earlier than the "macroexpansion time" when the regular macros are processed.

Re:How to slice your code into continuations by tel on Tuesday May 14, 2019 @06:21PM (#19914393)
Yep, "macros" can operate at any point during the compilation/interpretation process to transform the inputs. Generally speaking, the earlier on the more simplistic the structures and the less inferential products available. Also, the greater flexibility and power. Later on, early stages of compilation/interpretation will strip away information, simplify products, add inferential information, etc. These pieces can be made available to macro systems staged later on.

Lisp's most famous macros exist somewhat early in the compilation process, where trees have been parsed but variables aren't interpreted and the data structures are still simple.

Re:How to slice your code into continuations by neilv on Tuesday May 07, 2019 @11:01PM (#19855550)
BTW, Racket syntax transformations work with Racket syntax objects [1], which is one way to do the indentation sensitivity you mention. Racket also has something called `#lang` for conveniently using arbitrary parsers, and a way to embed use of arbitrary readers for chunks of code within normal Racket code.

[1] https://docs.racket-lang.org/reference/stxops.html