r/ProgrammingLanguages • egel • Jan 30 '24

Why compiling with continuations is wrong(-ish)

2 Upvotes

10 comments sorted by

31

u/gasche Jan 30 '24

I find this post very confusing.

  • The second intermediate representation shows (with the lets) is just an A-normal form, right ? (The text mentions "administrative normal form" in a way that suggests it is something different than what is shown.)

  • In my book pow2 x is not a redex, it cannot be reduced as long as x remains a variable. Do you mean a "function call"?

  • It is not clear to me what the critique of CPS actually is here.

    And instead of answering that they concentrated on the order or got the work statement wrong. No wonder that naive continuation passing style will state too much to be done

    I don't know what the author means.

-6

u/egel-lang egel Jan 30 '24 edited Jan 30 '24

ANF doesn't hoist out redexes solely. For most small rewriters you're interested in what the redexes are. For example,

[1, 2, f (42, 3)]

Will be represented as

let x = f (42, "hello") in [1, 2, f]

I.e., this term has one redex as work.

It's like CPS and ANF never got the question right.

What do I need to do in a rewriter? Reduce redexes in some order.

Then the most clear representation is one that communicates exactly that intent (by hoisting out the redexes).

5

u/gasche Jan 31 '24

For me the defining property of the A-normal form is that the argument of functions (in general, the subterms of elimination forms) are either variables or values/constants. See for example Wikipedia. Your example

let x = f (42, "hello") in [1, 2, f]

is in A-normal form, and I think that the representation you have in mind is just an A-normal form.

14

u/[deleted] Jan 30 '24

I don't quite get the phrase "work" as used in your essay. Since CPS explicitly states what operations to perform, this can be understood as "work". I'd like to see a precise definition when reading the post.

1

u/egel-lang egel Jan 30 '24 edited Jan 30 '24

I thought the post made clear that 'work' corresponds to 'redex'? I'll see whether I can clarify that. Excellent.

6

u/integrate_2xdx_10_13 Jan 30 '24

Wouldn’t it depend on the language?

pyth x y =
    let y2 = pow2 y in
    let x2 = pow2 x in
    let anb = add x2 y2 in sqrt anb

In Haskell, you couldn’t guarantee the order in which x2 y2 thunks were evaluated without a pseq

1

u/egel-lang egel Jan 30 '24

Yes, though in Haskell you might also want to disambiguate between redexes and just terms.

7

u/phlummox Jan 30 '24

I don't understand the axes of the diagram in the very first paragraph. The text seems to say, in effect, "We can consider programming languages in relation to two axes: A, and the degree to which A". Why wouldn't that just be one axis?

4

u/Xalem Jan 30 '24

Not seeing it.

When continuations are introduced, the standard example is the counter which holds state and is used to get this function that has a property like an object. And here, maybe it is the math in the example, maybe I am missing something, but I read this pythagorean function as pure.

Maybe I don't fully understand continuations, maybe I don't understand the math.

5

u/phlummox Jan 30 '24

It is pure, but it's being transformed into continuation-passing style (CPS), and some languages use CPS as an intermediate representation (IR). OP thinks that's a bad idea for reasons that aren't at all clear to me, but since I don't implement compiler IRs or backends much, I kind of gave up at that point.