popa.bogdanp
2022-4-26 07:05:10

Looks good on my end. Thanks!


hj93
2022-4-26 16:46:19

hello, I was wondering if someone here can explain to me what C is in the notation [E]C where it means the continuation passing style conversion of expression E in the continuation C for example [c]C ==> (C c) ;; application what is C in this case? heres some slides by Marc Feeley, the creator of gambit scheme http://churchturing.org/y/90-min-scc.pdf (scroll down to cps conversion rules) Another example is [(if E1 E2 E3)] C ==> [E1] (lambda (r1) (if r1 [E2] [E3])) C C but I just dont know what C is. any help will be appreciated


soegaard2
2022-4-26 16:51:50

The notation [E]C is defined on slide 24 to mean “the Scheme expression that is the CPS-conversion of the Scheme expression E where the Scheme expression C represents E’s continuation”.

Suppose we have an expression (+ 1 2) and focus on the expression E=1. Then the continuation of E is “what happens next”. In this case, after E evaluates to 1, the computation continues adding 2. Thus C=(lambda (v) (+ v 2).


hj93
2022-4-26 16:55:48

can you do this examples please: (if (= 1 1) 3 4)


hj93
2022-4-26 16:55:54

so i can get a better idea


hj93
2022-4-26 16:56:25

thanks


soegaard2
2022-4-26 16:57:38

The continuation of (= 1 1) is what happens next. That is, (lambda (v) (if v 3 4).


hj93
2022-4-26 17:03:34

@soegaard2 will a cps converter turn (if (= 1 1) 3 4) into (lambda (v) (if v 3 4))?


hj93
2022-4-26 17:04:53

@soegaard2 how should I go about writing a cps converter of scheme expressions?


soegaard2
2022-4-26 17:05:06

No, I was just giving an example what the C in [E]C meant.


soegaard2
2022-4-26 17:06:05

Let’s figure out what the CPS conversion of the program (if (= 1 2) 3 4) is together. While rule in the slide is the first rule?


hj93
2022-4-26 17:07:53

so while C is (lambda (r) (%halt r))?


hj93
2022-4-26 17:09:18

I wrote The conversion rule for if statements above


soegaard2
2022-4-26 17:10:34

Yes. The converter gets the program: (if (= 1 2) 3 4) and rewrites it to

[(if (= 1 2) 3 4)] (λ (r) (%halt r) The [ ] is meant to be a box.


soegaard2
2022-4-26 17:11:26

Everything in box still needs to be converted. The next rule is:


soegaard2
2022-4-26 17:11:40

What is the result?


hj93
2022-4-26 17:12:51

so the first rule is used first in every type of expression?


soegaard2
2022-4-26 17:13:04

Yes.


hj93
2022-4-26 17:13:48

I dont know what the result is


soegaard2
2022-4-26 17:13:58

What’s E1, E2 and E3?


hj93
2022-4-26 17:14:49

e1 = #t, e2 = 3, and e3 = 4


hj93
2022-4-26 17:15:27

so (lambda (a) 3)


hj93
2022-4-26 17:15:40

and (lambda (b) 4)


hj93
2022-4-26 17:15:42

?


soegaard2
2022-4-26 17:16:07

CPS only rewrites the rule, so E1 is `(= 1 2) in my example but E2=3 and E3=4.


soegaard2
2022-4-26 17:17:55

So (if (= 1 2) 3 4)] (λ (r) (%halt r) becomes [(= 1 2)] (λ (r1) (if r1 [3] [4]) C C where C is short for (λ (r) (%halt r)


hj93
2022-4-26 17:20:03

so it becomes (lambda (r1) (if 1 (lambda (r) (%halt 3) (lambda (r) (%halt 4)


hj93
2022-4-26 17:20:07

?


soegaard2
2022-4-26 17:23:04

Almost. The C part becomes: (λ (r1) (if r1 (C 3) (C 4))) or (λ (r1) (if r1 ((λ (r) (%halt r) 3) ((λ (r) (%halt r) 4)))


soegaard2
2022-4-26 17:23:59

The result so far is: [(= 1 2)] (λ (r1) (if r1 ((λ (r) (%halt r) 3) ((λ (r) (%halt r) 4)))


soegaard2
2022-4-26 17:25:08

If we look at = as a primitive, we can use the + rule as a template:


hj93
2022-4-26 17:26:08

to rewrite r1 right?


soegaard2
2022-4-26 17:26:45

To rewrite (= 1 2). Only things in boxes need to be rewritten.


soegaard2
2022-4-26 17:27:38

Let’s put C=(λ (r1) (if r1 ((λ (r) (%halt r) 3) ((λ (r) (%halt r) 4))) and look at: [(= 1 2)] C


soegaard2
2022-4-26 17:27:56

If we use the rule with = instead of +, we get:


soegaard2
2022-4-26 17:28:49

[1] (λ (r1) [2] (λ (r2) (C (+ r1 r2))))


hj93
2022-4-26 17:30:15

you mean (= r1 r2) right?


soegaard2
2022-4-26 17:30:51

Yes., I meant r2 not r3.


soegaard2
2022-4-26 17:31:18

So the box [1] contains a value v. We can use the rule:


soegaard2
2022-4-26 17:31:29

soegaard2
2022-4-26 17:31:58

((λ (r1) [2] (λ (r2) (C (+ r1 r2)))) 1)


soegaard2
2022-4-26 17:34:51

We can use the same rule on the box [2]. ((λ (r1) ((λ (r2) (C (+ r1 r2)))) 2) 1)


soegaard2
2022-4-26 17:35:26

And the only thing left to do, is to insert our old C = (λ (r1) (if r1 ((λ (r) (%halt r) 3) ((λ (r) (%halt r) 4))).


soegaard2
2022-4-26 17:35:46

((λ (r1) ((λ (r2) ((λ (r1) (if r1 ((λ (r) (%halt r) 3) ((λ (r) (%halt r) 4))) (+ r1 r2)))) 2) 1)


hj93
2022-4-26 17:36:51

Im going to write this down


soegaard2
2022-4-26 17:37:26

@sorawee Do you know of some examples/exercises Job can read/do ?


hj93
2022-4-26 17:39:51

thanks a lot


soegaard2
2022-4-26 17:40:21

Examples are great to look at when learning this.


hj93
2022-4-26 17:44:35

@soegaard2 yes more examples would be great. you should write a blog about cps conversion. there isnt a lot of info about this


evan.minsk
2022-4-26 20:23:52

@evan.minsk has joined the channel


sorawee
2022-4-26 22:42:38