scheme-to-c remove-anonymous-lambda bug?

19 views
Skip to first unread message

Amirouche Boubekki

unread,
Aug 7, 2019, 9:22:55 AM8/7/19
to nanopass-framework
In the code of the remove-anonymous-lambda, it seems to me it is
missing a case for letrec.

Here is the code:

;; pass: remove-anonymous-lambda : L8 -> L9
;;
;; since we are generating a C function for each Scheme lambda, we need to
;; have a name for each of these lambdas. In addition we need a name to use
;; as the code pointer label, so that we can lift the lambdas to the top
;; level of the program. The transformation is fairly simple. If we find a
;; lambda in expression position (i.e. not in the right-hand side of a
;; letrec binding) then we wrap a letrec around it that gives it a new name.
;;
;; (letrec ([l* (lambda (x** ...) body*)] ...) body) => (no change)
;; (letrec ([l* (lambda (x** ...) body*)] ...) body)
;;
;; (lambda (x* ...) body) => (letrec ([t0 (lambda (x* ...) body)]) t0)
;;
(define-pass remove-anonymous-lambda : L8 (e) -> L9 ()
(Expr : Expr (e) -> Expr ()
[(lambda (,x* ...) ,[abody])
(let ([t (unique-var 'anon)])
`(letrec ([,t (lambda (,x* ...) ,abody)]) ,t))]))

As such, the pattern (lambda (,x* ...) ,[abody]) will also match
lambdas that are inside letrec, I think... Or there is something I
don't understand.

Given the following code:

(letrec ((g (lambda () 42)))
(g))

It will generate:

(letrec ((g (letrec ((t0 (lambda () 42))) t0))
(g))

What I am missing?


Thanks in advance!

Matt Jadud

unread,
Aug 7, 2019, 11:20:23 AM8/7/19
to Amirouche Boubekki, nanopass-framework
Without looking at the code...

Was letrec removed from the language before it gets to this pass? Put another way, the point of the nanopass framework is to grow/shrink/transform the language in a series of steps. As a result, the intermediate languages change from the front to the back. By the point this pass is executed, does letrec still exist in the language?

If not, then the pass does not need to handle letrec.

However, I want to be clear that I'm talking about this from the place of "how nanopass compilers tend to be built," not "this is how this particular code works." At some point, letrec may have (for example) been transformed into lambdas, and lifted. As a result, there would be no need to handle letrec any more.

Yours,
Matt


--
You received this message because you are subscribed to the Google Groups "nanopass-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nanopass-framew...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nanopass-framework/CAL7_Mo-jsM8yKf-rbR6aRDCKwiB2nKjD6NdsEDyo7RhLuxJcaQ%40mail.gmail.com.

Amirouche Boubekki

unread,
Aug 8, 2019, 9:04:31 AM8/8/19
to Matt Jadud, nanopass-framework
Hello Matt,

Le mer. 7 août 2019 à 17:20, Matt Jadud <ma...@jadud.com> a écrit :
>
> Without looking at the code...
>
> Was letrec removed from the language before it gets to this pass? Put another way, the point of the nanopass framework is to grow/shrink/transform the language in a series of steps. As a result, the intermediate languages change from the front to the back. By the point this pass is executed, does letrec still exist in the language?

Indeed letrec was changed previously to only bind lambdas:

(define-language L8
(extends L7)
(Expr (e body)
(- (lambda (x* ...) abody)
(letrec ([x* e*] ...) abody))
(+ le
(letrec ([x* le*] ...) body)))
(LambdaExpr (le)
(+ (lambda (x* ...) abody))))

Mind the "+ le" which says that LambdaExpr can appear as a body of an
Expr. Then then L9 will remove that "le" from Expr:

(define-language L9
(extends L8)
(Expr (e body)
(- le)))

In the remove-anonymous-lambda pass, the matching rule is over Expr:

(define-pass remove-anonymous-lambda : L8 (e) -> L9 ()
(Expr : Expr (e) -> Expr ()
[(lambda (,x* ...) ,[abody])
(let ([t (unique-var 'anon)])
`(letrec ([,t (lambda (,x* ...) ,abody)]) ,t))]))

Which means it only match LambdaExpr that appear as expression.

The bug I was talking about would appear if instead of Expr it was LambdaExpr...

Thanks!
Reply all
Reply to author
Forward
0 new messages