The problem you are facing is related to language restriction for beginners: usually higher-order functions are a syntax error in the language, because beginners do not yet know about them.
There is a way to discard this. Your procedure source may be flagged as one of the exceptions to this limitation using provide-higher-order-primitive , which is specifically designed to work with BSL.
Here is what your library looks like with it:
#lang racket/base
(require lang/prim
racket/bool
(for-syntax racket/base))
(provide define/save-source)
(provide-higher-order-primitive procedure-name (fn))
(provide-higher-order-primitive procedure-source (fn))
(define *procedure-name-hash* (make-hash))
(define *procedure-source-hash* (make-hash))
(define (save-source! fn name body)
(hash-set! *procedure-name-hash* fn name)
(hash-set! *procedure-source-hash* fn body))
(define (procedure-name fn)
(hash-ref *procedure-name-hash* fn false))
(define (procedure-source fn)
(hash-ref *procedure-source-hash* fn false))
(define-syntax define/save-source
(syntax-rules ()
((_ (name formals ...) body-expressions ...)
(begin
(define name (λ(formals ...) body-expressions ...))
(save-source! name 'name '(λ(formals ...) body-expressions ...))))
((_ name (λ(formals ...) body-expressions ...))
(begin
(define name (λ(formals ...) body-expressions ...))
(save-source! name 'name '(λ(formals ...) body-expressions ...))))
((_ name value)
(define name value))))
BSL programs that use this should be able to use the name of the procedure and the source of the procedures.
source
share