Skip to content

Minor bugfixes and two new definitions #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
compiled
doc
doc
*.bak
*.css
*.js
*.html
10 changes: 9 additions & 1 deletion sicp-doc/sicp.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@(require scribble/manual scribble/eval
(for-label (except-in sicp #%app #%datum #%top true false identity error)
(only-in racket require true false identity error
natural-number/c any/c)))
natural-number/c any/c any namespace?)))

@title{SICP Language}
@defmodule[sicp #:lang]
Expand Down Expand Up @@ -67,4 +67,12 @@ then use @racket[#%require].
The amb operator.
}

@defproc[(apply-in-underlying-scheme [proc procedure?] [args list?]) any]{
An alias for @racket[(apply proc args)].
}

@defthing[user-initial-environment namespace?]{
The current namespace.
}

Additionally, @racket[true], @racket[false], @racket[identity], and @racket[error] are provided from Racket.
32 changes: 26 additions & 6 deletions sicp/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

(require racket/provide
(prefix-in r5rs: r5rs)
(rename-in racket [random racket:random]))
(only-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]))
(except-out (all-from-out r5rs) r5rs:#%module-begin r5rs:set!))
(rename-out [module-begin #%module-begin]
[amb-set! set!]))

(define-syntax (define+provide stx)
(syntax-case stx ()
Expand All @@ -24,8 +25,8 @@
(define+provide nil '())
(define+provide the-empty-stream '())
(define+provide stream-null? null?)
(define+provide (inc x) (+ x 1))
(define+provide (dec x) (- x 1))
(define+provide inc add1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We intentionally do not do this because:

(define inc add1)
(println inc) ;=> #<procedure:add1>

prints a name that we do not want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't thought of that. I guess I can revert that part, then. What about the other changes?

(define+provide dec sub1)
(define+provide (runtime)
(inexact->exact (truncate (* 1000 (current-inexact-milliseconds)))))
(define+provide (random n)
Expand All @@ -38,10 +39,12 @@
(syntax-rules ()
[(_ A B) (r5rs:cons A (r5rs:delay B))]))

(define+provide apply-in-underlying-scheme r5rs:apply)

(provide amb)

(define (amb-fail) (error "amb tree exhausted"))
(define (base-amb-fail) (error "amb tree exhausted"))
(define amb-fail base-amb-fail)
(define (set-amb-fail! x) (set! amb-fail x))

(define-syntax-rule (explore +prev-amb-fail +sk alt)
Expand All @@ -60,6 +63,21 @@
(explore +prev-amb-fail +sk alt) ...
(+prev-amb-fail)))))

(define-syntax-rule (amb-set! var val)
(if (eq? amb-fail base-amb-fail)
(r5rs:set! var val)
(let ([+prev-amb-fail amb-fail]
[old-value var])
(set-amb-fail!
(thunk
(r5rs:set! var old-value)
(+prev-amb-fail)))
(r5rs:set! var val))))

(define+provide user-initial-environment #f)
(define (set-user-initial-environment! namespace)
(set! user-initial-environment namespace))

(define-syntax module-begin
(syntax-rules ()
((_ . forms)
Expand All @@ -68,4 +86,6 @@
(print-as-expression #f)
(print-pair-curly-braces #t)
(print-mpair-curly-braces #f))
(define-namespace-anchor tmp)
(set-user-initial-environment! (namespace-anchor->namespace tmp))
. forms))))