1

To the Racket specialists, if I run this code, can I assure that everything next will be deterministic and purely functional? Or am I missing something?

#lang r5rs

(define-syntax
  unsafe (syntax-rules ()
           ((_ fn ...)
            (begin
              (define fn #f)
              ...))))

(unsafe
 ...
 call-with-input-file
 call-with-output-file
 close-input-port
 close-output-port
 current-input-port
 current-output-port
 display
 eval
 interaction-environment
 load
 newline
 null-environment
 open-input-file
 open-output-file
 peek-char
 read
 read-char
 scheme-report-environment
 set-car!
 set-cdr!
 transcript-off
 transcript-on
 vector-fill!
 vector-set!
 with-input-from-file
 with-output-to-file
 write
 write-char)

; Remove dangerous macros "let-syntax" and "set!"
(define-syntax
  let-syntax (syntax-rules ()
          ((_ ...)
           #f)))

(define-syntax
  set! (syntax-rules ()
          ((_ ...)
           #f)))

; Remove define-syntax itself
(define-syntax
  define-syntax (syntax-rules ()
          ((_ ...)
           #f)))
Ken White
  • 123,280
  • 14
  • 225
  • 444
Felipe
  • 16,649
  • 11
  • 68
  • 92
  • What is not pure about `define`? It sets a constant when you don't have `set!` and purely functional. – Sylwester Jul 23 '22 at 19:58
  • Slightly related: https://stackoverflow.com/questions/63668227/define-a-subset-of-racket – Felipe Jul 29 '22 at 21:34
  • Hi @Sylwester - define can be used more than once to redefine the same variable. Try it: `(define a 10) (define a 20)` – Felipe Jul 30 '22 at 02:05
  • In `#!r5rs` in DrRacket I get an error `identifier already defined in: a`. There are Scheme languages where `define` is just an alias for `set!` where you can write invalid Scheme code and get resonable results in it, but it will not be portable since the report underspecify what can happen. Racket always fail miserably and try to follow the report in the most strictest way so if your code works in it it most likely is conforming to Scheme and can run in the same manner in other implementations like Ikarus. – Sylwester Jul 30 '22 at 11:17
  • @Sylwester weird... If you use `define` in the source code of a #!r5rs it won't work, but in REPL it works. Do you know why? – Felipe Aug 01 '22 at 02:23
  • 1
    Your best bet would be to [create a language](https://docs.racket-lang.org/guide/module-languages.html) based on `r5rs` that doesn’t export the mutating items. – bb94 Sep 21 '22 at 23:27

0 Answers0