mighty-gerbils / gerbil

Gerbil Scheme
https://cons.io
GNU Lesser General Public License v2.1
1.16k stars 111 forks source link

Infer names for anonymous lambdas #551

Open fare opened 4 years ago

fare commented 4 years ago

For debugging purposes, it would be nice if (let (foo (lambda (x) x)) ...) would create a function for which ##procedure-name would return foo rather than #f.

See how they do it in Racket: https://docs.racket-lang.org/reference/syntax-model.html#%28part._infernames%29

fare commented 4 years ago

My workaround:

;; In rec, the name is bound in the body of the lambda
(defrule (rec (name . formals) body ...)
  (let () (def (name . formals) body ...) name))

;; In fn, the name is NOT bound in the body of the lambda
(defsyntax (fun stx)
  (syntax-case stx ()
    ((_ (name . formals) body ...)
     (with-syntax ((n (datum->syntax #'stx (string->uninterned-symbol (symbol->string (syntax-e #'name))))))
       #'(let () (def (n . formals) body ...) n)))))
vyzo commented 4 years ago

I am not convinced this can work reliably, only top-level procedures have names in gambit afaik.