Flux
Flux

Reputation: 10920

Is there a way to get back special forms after redefining them?

Scheme and Racket do not protect special forms, so it's possible to redefine them. Just purely out of curiosity, is there a way to get back special forms after they have been overwritten?

Example situation:

$ racket
Welcome to Racket v6.11.
> (define define 1)
> (+ define define)
2
> (define x 3)  ; How to get the original 'define' back?
; x: undefined;
;  cannot reference undefined identifier
; [,bt for context]
>

If I redefine a special form, is there a way to get back the special form?

Upvotes: 0

Views: 143

Answers (1)

Sorawee Porncharoenwase
Sorawee Porncharoenwase

Reputation: 6502

Specifically in the REPL, where definition overriding is possible, you can simply (require (only-in racket/base define)) to get define back.

Welcome to Racket v7.3.
> (define define 1)
> (+ define define)
2
> (require (only-in racket/base define))
> (define x 3)
> x
3

For Racket files/modules, a simple answer is that once you make a top-level definition, you can't redefine it. However, you can still (require (only-in racket/base [define racket:define])) (rename define from racket/base as racket:define), and then use racket:define instead. Also note that while you can't redefine a top-level definition, you can shadow it, so you can use define in the body of (let-syntax ([define (make-rename-transformer #'racket:define)]) ...) for instance.

But note that in Racket, you can override #%module-begin which has a complete control of your file/module, so you can use this feature to create a new language that allows you to redefine top-level definitions, though at that point you are no longer using the "real" Racket.

Upvotes: 5

Related Questions