From a2a390e8774eb74ac6b0090886de381b8d3093d9 Mon Sep 17 00:00:00 2001 From: DataSnake Date: Sat, 18 Dec 2021 18:03:37 -0500 Subject: [PATCH 1/5] Fixed import bug Changed rename-in to only-in, as re-importing the rest of the Racket package would be redundant. --- .gitignore | 3 ++- sicp/main.rkt | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a1f33d6..a32087b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ compiled -doc \ No newline at end of file +doc +sicp/main.bak diff --git a/sicp/main.rkt b/sicp/main.rkt index 5548679..313021d 100644 --- a/sicp/main.rkt +++ b/sicp/main.rkt @@ -2,7 +2,7 @@ (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)) From 7b2291cdf96494c1cdd29ae6836458c819e2784d Mon Sep 17 00:00:00 2001 From: DataSnake Date: Sat, 18 Dec 2021 18:53:59 -0500 Subject: [PATCH 2/5] Added apply-in-underlying-scheme and user-initial-environment --- .gitignore | 5 ++++- sicp-doc/sicp.scrbl | 10 +++++++++- sicp/main.rkt | 7 +++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a32087b..f223cee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ compiled doc -sicp/main.bak +*.bak +*.css +*.js +*.html diff --git a/sicp-doc/sicp.scrbl b/sicp-doc/sicp.scrbl index 61cba28..79eb4bf 100644 --- a/sicp-doc/sicp.scrbl +++ b/sicp-doc/sicp.scrbl @@ -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] @@ -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. diff --git a/sicp/main.rkt b/sicp/main.rkt index 313021d..e260891 100644 --- a/sicp/main.rkt +++ b/sicp/main.rkt @@ -38,6 +38,7 @@ (syntax-rules () [(_ A B) (r5rs:cons A (r5rs:delay B))])) +(define+provide apply-in-underlying-scheme r5rs:apply) (provide amb) @@ -60,6 +61,10 @@ (explore +prev-amb-fail +sk alt) ... (+prev-amb-fail))))) +(define+provide user-initial-environment #f) +(define (set-user-initial-environment! namespace) + (set! user-initial-environment namespace)) + (define-syntax module-begin (syntax-rules () ((_ . forms) @@ -68,4 +73,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)))) From cfe63097b7fb02cb699770f82f8a19fed95b93e3 Mon Sep 17 00:00:00 2001 From: DataSnake Date: Sat, 18 Dec 2021 22:41:14 -0500 Subject: [PATCH 3/5] Update main.rkt Modified set! so that if there's an amb expression active, backtracking will undo assignments. This is necessary to complete exercise 4.44. --- sicp/main.rkt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/sicp/main.rkt b/sicp/main.rkt index e260891..c3a441f 100644 --- a/sicp/main.rkt +++ b/sicp/main.rkt @@ -5,8 +5,9 @@ (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 () @@ -42,7 +43,8 @@ (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) @@ -61,6 +63,17 @@ (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)) From 623ac5904afa5ecb70899b524d705635cbcea82b Mon Sep 17 00:00:00 2001 From: DataSnake Date: Sun, 19 Dec 2021 11:42:10 -0500 Subject: [PATCH 4/5] Update main.rkt Changed inc and dec to refer to the existing Racket functions add1 and sub1 rather than being redefined from scratch. --- sicp/main.rkt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sicp/main.rkt b/sicp/main.rkt index c3a441f..bea27b8 100644 --- a/sicp/main.rkt +++ b/sicp/main.rkt @@ -25,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) +(define+provide dec sub1) (define+provide (runtime) (inexact->exact (truncate (* 1000 (current-inexact-milliseconds))))) (define+provide (random n) From 4697b981dab18fd79029a094b293518f95863455 Mon Sep 17 00:00:00 2001 From: DataSnake Date: Sun, 19 Dec 2021 12:35:18 -0500 Subject: [PATCH 5/5] Revert "Update main.rkt" This reverts commit 623ac5904afa5ecb70899b524d705635cbcea82b. --- sicp/main.rkt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sicp/main.rkt b/sicp/main.rkt index bea27b8..c3a441f 100644 --- a/sicp/main.rkt +++ b/sicp/main.rkt @@ -25,8 +25,8 @@ (define+provide nil '()) (define+provide the-empty-stream '()) (define+provide stream-null? null?) -(define+provide inc add1) -(define+provide dec sub1) +(define+provide (inc x) (+ x 1)) +(define+provide (dec x) (- x 1)) (define+provide (runtime) (inexact->exact (truncate (* 1000 (current-inexact-milliseconds))))) (define+provide (random n)