Using namespaces to work with shadowed identifiers - random

I'm working on a simple game using Racket (homework assignment). The requirements instruct me to create a computer player named random which utilizes the default random number generator.
This snippet of code illustrates my issue:
(define (random)
(random (10))) ; should be random number call
I'm used to the C convention for namespaces:
std::string
Does Racket offer anything along those lines?
Thanks

You can use prefix-in to prefix the exports of the standard library with some namespacing prefix. For example:
#lang racket
(require (prefix-in std:: racket))
(define (* x)
(std::* x x))
shows that we can shadow the bindings from #lang racket, but still get at them through the prefixed identifiers.
Traditionally, the prefixes that people use are a little simpler, like:
#lang racket
(require (prefix-in r: racket))
(define (* x)
(r:* x x))

Related

call a foreign program to do work in your current program in scheme

I was wondering if you're allowed to call another program you made to do work in a new program you are about to make in scheme, or would I have to copy and paste all the code from the previous program, though I can do this and make my life simple, I'm wondering if there is a more sophisticated elegant way to import in scheme r5rs.
;; Here is an example of what I mean
;; for example I have a file name add.scm which adds 2 numbers
;; now I want to make a new function that squares the numbers
;;is there a way I can do something like this
#lang add.scm as add
;;and use it here like so
(define (square x)
(* x x))
(square add)
;;where add already returns a value
This may seem like a silly piece of code, but this is the best pesudo code I can give, if the example needs more editing to understand please let me know, thank you.
If I understand your question, you have this module named add.scm:
;; add.scm
#lang racket
(+ 5 7) ;; prints 12 when run
and you want to write a new module that gets the value that the add.scm module prints out, bind it to a variable, and use it in a computation, like this:
;; square.scm
#lang racket
(??? add ??? "add.scm" ???) ;; bind `add` to "value" of "add.scm" module
(* add add) ;; prints 144 when run
No, there is no easy way to do that. It's possible, by using dynamic-require or a subprocess to run the module, capturing the output and then reading it to get the value, but that's an awful, complicated, brittle way to do things.
In Racket, a better approach would be for add.rkt to define and export a variable and have a main submodule that prints the value:
;; add.rkt
#lang racket
(provide x)
(define x (+ 5 7))
(module+ main
x)
Then you can use (require "add.rkt") from another module to get x to use in computations, and if you run add.rkt as a program (for example, with racket add.rkt), then it prints the value.
Racket (which has #lang as in the question, and module/provide/require as explained in another answer) is not Scheme (let alone R5RS).
In R6RS Scheme, Scheme source can be divided between library files and top-level programs
If the file add-library.scm contains:
(library (add-library)
(export add)
(import (rnrs))
(define add (+ 5 7)) )
and add-toplevel.scm contains:
(import (add-library))
(define (square x)
(* x x))
(display (square add))
Then:
$ scheme
> (load "add-toplevel.scm")
144
>

How to restore built in procedure in Scheme (Racket SICP) [duplicate]

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?
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.

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?
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.

SICP Ch5 eceval compiler in Racket: set-cdr! into quoted list (not a dup)

This is not a duplicate of
set-car!, set-cdr! unbound in racket? or
of
Implement SICP evaluator using Racket
or of
How to install sicp package module in racket?,
but rather a follow-up question because the solutions proposed therein do not
work for me. First, the need: Section 5.5.5 of SICP, the compiler plus
explicit-control evaluator (code here in "ch5-eceval-compiler.scm"), are
entirely dependent on set-car! and set-cdr! into explicit quoted lists. I
would like to copy and modify this code without a complete, bottom-up rewrite in
immutable form. I'd also accept a reference to a scheme implementation that can
run the code out-of-the-box or with some minimal, straightforward adaptation,
i.e., a scheme that has set-car! and set-cdr! or some
work-around. Neither guile nor racket give me an easy time running this code.
EDIT: mit scheme will load the eceval compiler. I'm leaving the question up for those who might want to get it going in racket (I would rather, for example).
Here is a deeper explanation, including the things I explored and tried out, and
how I diagnosed the quoted list as the deepest problem. When I hand-converted
the quoted list into an mquoted nest of mlists, the code broke in much worse
ways and the rabbit hole got much deeper. I had to revert after a couple of
hours of delicate brain surgery that failed.
Here is an MVE of the kind of structure that section 5.5.5 relies on. This is small, but structurally like the real thing:
(define foo '(a b))
(set-cdr! foo '(c))
The real thing starts like this:
(define eceval
(make-machine
'(exp env val proc argl continue unev
compapp ;*for compiled to call interpreted
)
eceval-operations ;; ----------------------------------------------
'( ;; <<<<<<<<======== BIG QUOTED LIST CAUSING TROUBLE / NOT MCONSES!
;;SECTION 5.4.4, as modified in 5.5.7 ;; -------------------------------
;;*for compiled to call interpreted (from exercise 5.47)
(assign compapp (label compound-apply))
;;*next instruction supports entry from compiler (from section 5.5.7)
(branch (label external-entry))
read-eval-print-loop
(perform (op initialize-stack))
(perform
(op prompt-for-input) (const ";;; EC-Eval input:"))
...
and goes on for quite a while. The evaluator is the "machine-code" in the quoted
list, and various generated code does set-car! and set-cdr! into registers
and environment frames and other things. The code fails on load.
There seems to be no easy way to convert the evaluator into immutable form
without a full rewrite, and I'm trying to avoid that. Of course, set-car! and
set-cdr! are not available in #lang racket, and I don't think they're in
guile, either (at least guile refused to load "ch5-eceval-compiler.scm,"
throwing a mutability error, on which I did not dig deeper).
One solution proposed in
set-car!, set-cdr! unbound in racket? is
to rewrite the code using mcons, mcar, mlist, etc. according to (require
compatibility/mlist) (require rnrs/mutable-pairs-6). Those compatibility
packages have no replacement for quote, so I tried writing my own mquote. I
spent a couple of hours on such a refactoring, but the exercise was not
converging, just going deeper and deeper down the rabbit hole and ending up with
even deeper problems. It seems that to pursue even a refactoring I must
understand more semantics about "ch5-eceval-compiler.scm," and if I must, I
might as well rewrite it in immutable form.
Easier solutions proposed in
set-car!, set-cdr! unbound in racket?
are to use #lang sicp or #lang r5rs. There follows three experiments that
referenced other answers on stack overflow:
#lang r5rs
(define foo '(a b))
(set-cdr! foo '(c))
foo
Error: struct:exn:fail:contract:variable
set-cdr!: undefined;
cannot reference an identifier before its definition
in module: "/usr/share/racket/pkgs/r5rs-lib/r5rs/main.rkt"
-----------------------------------------------
which points to a place where set-cdr! is clearly defined:
...
(module main scheme/base
(require scheme/mpair
racket/undefined
(for-syntax scheme/base syntax/kerncase
"private/r5rs-trans.rkt")
(only-in mzscheme transcript-on transcript-off))
(provide (for-syntax syntax-rules ...
(rename-out [syntax-rules-only #%top]
[syntax-rules-only #%app]
[syntax-rules-only #%datum]))
(rename-out
[mcons cons]
[mcar car]
[mcdr cdr]
[set-mcar! set-car!] ;; --------------------------
[set-mcdr! set-cdr!] ;; <<<<<<<<======== LOOK HERE
[mpair? pair?] ;; --------------------------
[mmap map]
[mfor-each for-each])
= < > <= >= max min + - * /
abs gcd lcm exp log sin cos tan not eq?
call-with-current-continuation make-string
symbol->string string->symbol make-rectangular
exact->inexact inexact->exact number->string string->number
...
Here is a similar failure with #lang sicp
#lang sicp
(define foo '(a b))
(set-cdr! foo '(c))
foo
Error: struct:exn:fail:contract:variable
set-cdr!: undefined;
cannot reference an identifier before its definition
in module: "/home/rebcabin/.racket/7.2/pkgs/sicp/sicp/main.rkt"
----------------------------------------------------
pointing to code that only indirectly defines set-cdr!, but clearly in the
appropriate package:
....
#lang racket
(require racket/provide ;; --------------------------------------------
(prefix-in r5rs: r5rs) ;; <<<<<<<<======== PULL IN SET-CDR! ETC. HERE?
(rename-in racket [random racket:random])) ;; ------------------------
(provide (filtered-out (λ (name) (regexp-replace #px"^r5rs:" name ""))
(except-out (all-from-out r5rs) r5rs:#%module-begin))
(rename-out [module-begin #%module-begin]))
(define-syntax (define+provide stx)
(syntax-case stx ()
[(_ (id . args) . body) #'(begin
(provide id)
(define (id . args) . body))]
[(_ id expr) #'(begin
(provide id)
(define id expr))]))
...
I dig deeper into
Implement SICP evaluator using Racket
and find
(require (only-in (combine-in rnrs/base-6
rnrs/mutable-pairs-6)
set-car!
set-cdr!))
(define foo '(a b))
(set-cdr! foo '(c))
foo
yielding
Error: struct:exn:fail:contract
set-mcdr!: contract violation
expected: mpair?
given: '(a b)
argument position: 1st
other arguments...:
'(c)
This error implies that the problem really is the quoted list. I don't have an
easy way to make the big quoted list in eceval into an mlist or chain of
mcons. I tried it and it's very verbose and error prone, plus I think the code
that loads eceval scans and patches that list, so it uses other list operations.
I had to revert after going south in a bad way.
Perhaps I missed some way to automate the transformation, a macro, but that's a
deeper rabbit hole (my scheme macro-fu is too old).
So I'm stuck. Nothing easy or recommended works. I'd like to either (1) know a scheme implementation that will run this code (2) some way I can implement set-car! and set-cdr! in racket (3) some other kind of work around (4) or maybe I just made a stupid mistake that one of you kind people will easily fix.
I tried (by either running racket directly or running it via DrRacket)
#lang sicp
(define foo '(a b))
(set-cdr! foo '(c))
foo
and it outputs (a c).
This also works:
#lang r5rs
(define foo '(a b))
(set-cdr! foo '(c))
(display foo)
To answer your question regarding the implementation of SICP (which I am currently maintaining), you are correct that (prefix-in r5rs: r5rs) will import set-cdr!.
Update: I just installed Geiser and now experienced the same problem that you did when I C-c C-b. However, C-c C-a works as expected.
Personally, I would use racket-mode instead of Geiser.
Mit-scheme will load the eceval compiler from the code drop mentioned in the question. On Ubuntu, mit-scheme loads with sudo apt-install mit-scheme. The geiser package of emacs finds mit via run-mit. The problem is solved.

How do I use with-type to use Typed Racket fragments within untyped modules?

The Typed Racket reference indicates that it's possible to use with-type to created “typed regions” within untyped code.
The with-type form allows for localized Typed Racket regions in otherwise untyped code.
It’s a little unclear how to actually use this, though. Obviously, using such a feature needs to take place within an untyped module using #lang racket or something similar. How should the with-type binding be imported?
A naïve attempt is just to require Typed Racket, but this causes failures with how TR overwrites existing syntactic forms.
(require typed/racket)
(struct point (x y)) ; complains about missing type annotations
Trying to use only-in to simply require with-type and nothing else sort of works, but then none of the required type bindings (such as Number or ->) exist.
It seems like the only way to do this would be to manually use only-in to import only the things that I need, but this feels laborious. I could also use prefix-in, but then of course everything would be scattered with prefixes.
Is there a recommended way of doing this, or is this feature somewhat deprecated?
I don't know the fundamental answer. A guess is that it's the sort of thing that would be useful when writing macros, as opposed to code you'd write directly?
A practical idea: You can use local-require to limit the "contamination" of the TR require. And you can flip to using except-in if that's less work then only-in.
For instance this example from the docs get close, but gives a weird error presumably because it's using TR's quote:
#lang racket/base
(let ([x 'hello])
(local-require typed/racket)
(with-type
#:result String
#:freevars ([x String])
(string-append x ", world")))
; /tmp/so.rkt:7:21: quote: identifier used out of context
; in: quote
Excluding that with except-in gives the desired error:
(let ([x 'hello])
(local-require (except-in typed/racket quote))
(with-type
#:result String
#:freevars ([x String])
(string-append x ", world")))
; x: broke its contract
; promised: String
; produced: 'hello
; in: String
; contract from: /tmp/so.rkt
; blaming: /tmp/so.rkt
; (assuming the contract is correct)
; at: /tmp/so.rkt:14.17
But yeah. This is just fiddling with the edges and not getting to the heart of it.
I would just use except-in (or perhaps rename-in) to avoid the few identifiers that don't work in both typed and untyped programs. Like this modification of Greg's program:
#lang racket/base
(require (except-in typed/racket struct))
(struct point (x y))
(let ([x 'hello])
(with-type
#:result String
#:freevars ([x String])
(string-append x ", world")))

Resources