exercise-4.scm (19543B)
1 (define-library (sicp solutions chapter-4 exercise-4) 2 (export 3 eval 4 main 5 6 run-internal-tests 7 8 make-environment 9 setup-environment 10 extend-environment 11 ) 12 (import 13 (rename (scheme base) (apply scheme:base:apply)) 14 (scheme cxr) 15 (scheme read) 16 (scheme write) 17 18 (srfi srfi-1) 19 (srfi srfi-9) 20 (srfi srfi-64) 21 22 (system vm trace) 23 24 (ice-9 match) 25 (ice-9 pretty-print) 26 27 (sicp utils) 28 ) 29 30 (begin 31 ;; XXX: In 4.1.3 we see that the procedure `true?` is defined 32 ;; using the host-variable `false` whose value is 'false, but we 33 ;; don't see it defined anywhere in the good book, so we need to 34 ;; define the host-variable false ourselves. 35 ;; 36 ;; In MIT Scheme, the one the SICP people seem to love to use for 37 ;; SICP, the values of `false` and `true` are #f and #t, #f being 38 ;; the only value that is falsituous and #t there for having a 39 ;; truthious value that has no other meaning other than being 40 ;; truthitiousic. 41 ;; 42 ;; ``` 43 ;; # `scheme` is the MIT Scheme executable: 44 ;; $ guix shell mit-scheme -- scheme 45 ;; MIT/GNU Scheme running under GNU/Linux 46 ;; Type `^C' (control-C) followed by `H' to obtain information about interrupts. 47 ;; 48 ;; Copyright (C) 2020 Massachusetts Institute of Technology 49 ;; This is free software; see the source for copying conditions. There is NO warranty; not even for 50 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 51 ;; 52 ;; Image saved on Sunday March 7, 2021 at 3:24:56 PM 53 ;; Release 11.2 || SF || LIAR/x86-64 54 ;; 55 ;; 1 ]=> false 56 ;; 57 ;; ;Value: #f 58 ;; 59 ;; 1 ]=> true 60 ;; 61 ;; ;Value: #t 62 ;; 63 ;; 1 ]=> 64 ;; ``` 65 (define false #f) 66 (define true #t) 67 68 (define (make-environment vars vals) 69 "A comfort procedure because calling extend on the-empty-environment is a PITA." 70 (extend-environment vars 71 vals 72 the-empty-environment)) 73 74 ;; XXX: As described in chapter 4.1.4 note 2, we need to name the 75 ;; host `apply`, and the guest `apply` differently. I define the 76 ;; host's `apply` to be the name used in the book. 77 (define apply-in-underlying-scheme scheme:base:apply) 78 79 ;;; Start: 80 81 ;;; 4.1.1 82 83 (define *form-table* '()) 84 85 (define (add-form-handler! form? handler) 86 (set! *form-table* 87 (cons (cons form? 88 handler) 89 *form-table*))) 90 91 (define (get-form-handler exp) 92 (let loop ((table *form-table*)) 93 (match table 94 ('() 95 (error "Unknown form" exp)) 96 (`((,form? . ,handler) . ,rest) 97 (if (form? exp) 98 handler 99 (loop rest)))))) 100 101 (define (eval exp env) 102 (cond ((self-evaluating? exp) 103 exp) 104 ((variable? exp) 105 (lookup-variable-value exp env)) 106 ((pair? exp) 107 ((get-form-handler exp) exp env)) 108 (else 109 (error "Unknown expression type -- EVAL" exp)))) 110 111 (define (apply procedure arguments) 112 (cond ((primitive-procedure? procedure) 113 (apply-primitive-procedure 114 procedure 115 arguments)) 116 ((compound-procedure? procedure) 117 (eval-sequence 118 (procedure-body procedure) 119 (extend-environment 120 (procedure-parameters 121 procedure) 122 arguments 123 (procedure-environment 124 procedure)))) 125 (else 126 (error "Unknown procedure type -- APPLY" 127 procedure)))) 128 129 ;;; Procedure arguments: 130 131 (define (list-of-values exps env) 132 (if (no-operands? exps) 133 '() 134 (cons (eval (first-operand exps) env) 135 (list-of-values (rest-operands exps) env)))) 136 137 ;;; Conditionals: 138 139 (define (eval-if exp env) 140 (if (true? (eval (if-predicate exp) env)) 141 (eval (if-consequent exp) env) 142 (eval (if-alternative exp) env))) 143 144 ;;; Sequences: 145 146 (define (eval-sequence exps env) 147 (cond ((last-exp? exps) 148 (eval (first-exp exps) env)) 149 (else 150 (eval (first-exp exps) env) 151 (eval-sequence (rest-exps exps) 152 env)))) 153 154 ;;; Assignments and definitions 155 156 (define (eval-assignment exp env) 157 (set-variable-value! 158 (assignment-variable exp) 159 (eval (assignment-value exp) env) 160 env) 161 'ok) 162 163 (define (eval-definition exp env) 164 (define-variable! 165 (definition-variable exp) 166 (eval (definition-value exp) env) 167 env) 168 'ok) 169 170 ;;; end 4.1.1 171 172 ;;; 4.1.2 173 174 175 (define (self-evaluating? exp) 176 (cond ((number? exp) true) 177 ((string? exp) true) 178 (else false))) 179 180 (define (variable? exp) (symbol? exp)) 181 182 (define (quoted? exp) 183 (tagged-list? exp 'quote)) 184 185 (define (text-of-quotation exp) 186 (cadr exp)) 187 188 (define (tagged-list? exp tag) 189 (if (pair? exp) 190 (eq? (car exp) tag) 191 false)) 192 193 (define (assignment? exp) 194 (tagged-list? exp 'set!)) 195 196 (define (assignment-variable exp) 197 (cadr exp)) 198 199 (define (assignment-value exp) (caddr exp)) 200 201 (define (definition? exp) 202 (tagged-list? exp 'define)) 203 204 (define (definition-variable exp) 205 (if (symbol? (cadr exp)) 206 (cadr exp) 207 (caadr exp))) 208 209 (define (definition-value exp) 210 (if (symbol? (cadr exp)) 211 (caddr exp) 212 (make-lambda 213 (cdadr exp) ; formal-parameters 214 (cddr exp)))) ; body 215 216 (define (lambda? exp) 217 (tagged-list? exp 'lambda)) 218 (define (lambda-parameters exp) (cadr exp)) 219 (define (lambda-body exp) (cddr exp)) 220 221 (define (make-lambda parameters body) 222 (cons 'lambda (cons parameters body))) 223 224 (define (if? exp) (tagged-list? exp 'if)) 225 (define (if-predicate exp) (cadr exp)) 226 (define (if-consequent exp) (caddr exp)) 227 (define (if-alternative exp) 228 (if (not (null? (cdddr exp))) 229 (cadddr exp) 230 'false)) 231 232 (define (make-if predicate 233 consequent 234 alternative) 235 (list 'if 236 predicate 237 consequent 238 alternative)) 239 240 (define (begin? exp) 241 (tagged-list? exp 'begin)) 242 (define (begin-actions exp) (cdr exp)) 243 (define (last-exp? seq) (null? (cdr seq))) 244 (define (first-exp seq) (car seq)) 245 (define (rest-exps seq) (cdr seq)) 246 247 (define (sequence->exp seq) 248 (cond ((null? seq) seq) 249 ((last-exp? seq) (first-exp seq)) 250 (else (make-begin seq)))) 251 252 (define (make-begin seq) (cons 'begin seq)) 253 254 (define (application? exp) (pair? exp)) 255 (define (operator exp) (car exp)) 256 (define (operands exp) (cdr exp)) 257 (define (no-operands? ops) (null? ops)) 258 (define (first-operand ops) (car ops)) 259 (define (rest-operands ops) (cdr ops)) 260 261 (define (cond? exp) 262 (tagged-list? exp 'cond)) 263 (define (cond-clauses exp) (cdr exp)) 264 (define (cond-else-clause? clause) 265 (eq? (cond-predicate clause) 'else)) 266 (define (cond-predicate clause) 267 (car clause)) 268 (define (cond-actions clause) 269 (cdr clause)) 270 (define (cond->if exp) 271 (expand-clauses (cond-clauses exp))) 272 (define (expand-clauses clauses) 273 (if (null? clauses) 274 'false 275 (let ((first (car clauses)) 276 (rest (cdr clauses))) 277 (if (cond-else-clause? first) 278 (if (null? rest) 279 (sequence->exp 280 (cond-actions first)) 281 (error "ELSE clause isn't last: COND->IF" 282 clauses)) 283 (make-if (cond-predicate first) 284 (sequence->exp 285 (cond-actions first)) 286 (expand-clauses 287 rest)))))) 288 289 ;;; end 4.1.2 290 291 ;;; 4.1.3 292 293 ;; XXX: By default, false's value is #f, which is the only true 294 ;; false. All others are fake and would have the truthitousness 295 ;; property. Consult `info r7rs-small` or `C-h 296 (define (true? x) 297 (not (eq? x false))) 298 299 (define (false? x) 300 (eq? x false)) 301 302 (define (make-procedure parameters body env) 303 (list 'procedure parameters body env)) 304 (define (compound-procedure? p) 305 (tagged-list? p 'procedure)) 306 (define (procedure-parameters p) (cadr p)) 307 (define (procedure-body p) (caddr p)) 308 (define (procedure-environment p) (cadddr p)) 309 310 (define (enclosing-environment env) (cdr env)) 311 (define (first-frame env) (car env)) 312 (define the-empty-environment '()) 313 314 (define (make-frame variables values) 315 (cons variables values)) 316 (define (frame-variables frame) (car frame)) 317 (define (frame-values frame) (cdr frame)) 318 (define (add-binding-to-frame! var val frame) 319 (set-car! frame (cons var (car frame))) 320 (set-cdr! frame (cons val (cdr frame)))) 321 322 (define (extend-environment vars vals base-env) 323 (if (= (length vars) (length vals)) 324 (cons (make-frame vars vals) base-env) 325 (if (< (length vars) (length vals)) 326 (error "Too many arguments supplied" 327 vars 328 vals) 329 (error "Too few arguments supplied" 330 vars 331 vals)))) 332 333 (define (lookup-variable-value var env) 334 (define (env-loop env) 335 (define (scan vars vals) 336 (cond ((null? vars) 337 (env-loop 338 (enclosing-environment env))) 339 ((eq? var (car vars)) 340 (car vals)) 341 (else (scan (cdr vars) 342 (cdr vals))))) 343 (if (eq? env the-empty-environment) 344 (error "Unbound variable -- LOOKUP-VARIABLE-VALUE" var) 345 (let ((frame (first-frame env))) 346 (scan (frame-variables frame) 347 (frame-values frame))))) 348 (env-loop env)) 349 350 (define (set-variable-value! var val env) 351 ;; XXX: Just trying to see what it would look like if I just used named lets. 352 ;; 353 ;; (let loop-over-environments ((env env)) 354 ;; (cond 355 ;; ((eq? env the-empty-enviornment) 356 ;; (error "Unbound variable: SET!" var)) 357 ;; (else 358 ;; (let ((frame (first-frame env))) 359 ;; (let loop-over-frame ((vars (frame-variables frame)) 360 ;; (vals (frame-values frame))) 361 ;; (cond 362 ;; ((null? vars) 363 ;; (loop-over-environments (enclosing-environment env))) 364 ;; ((eq? var (car vars)) 365 ;; (set-car! vals val)) 366 ;; (else 367 ;; (loop-over-frame ((cdr vars) 368 ;; (cdr vals)))))))))) 369 370 (define (env-loop env) 371 (define (scan vars vals) 372 (cond ((null? vars) 373 (env-loop 374 (enclosing-environment env))) 375 ((eq? var (car vars)) 376 (set-car! vals val)) 377 (else (scan (cdr vars) 378 (cdr vals))))) 379 (if (eq? env the-empty-environment) 380 (error "Unbound variable: SET!" var) 381 (let ((frame (first-frame env))) 382 (scan (frame-variables frame) 383 (frame-values frame))))) 384 (env-loop env)) 385 386 (define (define-variable! var val env) 387 "Add a new variable VAR with value VAL in the top frame of environment ENV. 388 389 If variable VAR already exists in the top frame, set VAR in the top 390 frame to VAL." 391 392 ;; We will CHANGE (why did I all uppercase "CHANGE" here? I 393 ;; don't remember. Maybe it is some sort of a joke?) only 394 ;; the first frame. 395 (let ((frame (first-frame env))) 396 (define (scan vars vals) 397 (cond ((null? vars) 398 (add-binding-to-frame! 399 var val frame)) 400 ((eq? var (car vars)) 401 (set-car! vals val)) 402 (else (scan (cdr vars) 403 (cdr vals))))) 404 (scan (frame-variables frame) 405 (frame-values frame)))) 406 407 ;;; 4.1.4 408 409 (define (setup-environment) 410 (dpp 'moo) 411 (let* (;; XXX: See bug below on the quoted (define 412 ;; primitive-procedures ...) 413 (primitive-procedures 414 (list (list 'car car) 415 (list 'cdr cdr) 416 (list 'cons cons) 417 (list 'null? null?) 418 (list '+ +) 419 (list '- -) 420 (list '* *) 421 (list '/ /) 422 (list '= =) 423 (list 'display display) 424 (list 'write write) 425 (list 'read read))) 426 (initial-env 427 (extend-environment 428 (primitive-procedure-names primitive-procedures) 429 (primitive-procedure-objects primitive-procedures) 430 the-empty-environment))) 431 (define-variable! 'true true initial-env) 432 (define-variable! 'false false initial-env) 433 initial-env)) 434 435 (define the-global-environment 436 (begin 437 (dpp 'moo) 438 (setup-environment))) 439 440 (define (primitive-procedure? proc) 441 (tagged-list? proc 'primitive)) 442 443 (define (primitive-implementation proc) 444 (cadr proc)) 445 446 ;; XXX: There is a bug here in SICP or GNU Guile or both. 447 ;; primitive-procedures is `#<unspecified>` when this library is 448 ;; being loaded. 449 '(define primitive-procedures 450 (list (list 'car car) 451 (list 'cdr cdr) 452 (list 'cons cons) 453 (list 'null? null?) 454 (list '+ +) 455 (list '- -) 456 (list '* *) 457 (list '/ /) 458 (list 'display display) 459 (list 'write write) 460 (list 'read read))) 461 462 (define (primitive-procedure-names primitive-procedures) 463 (map car 464 primitive-procedures)) 465 466 (define (primitive-procedure-objects primitive-procedures) 467 (map (lambda (proc) 468 (list 'primitive (cadr proc))) 469 primitive-procedures)) 470 471 (define (apply-primitive-procedure proc args) 472 (apply-in-underlying-scheme 473 (primitive-implementation proc) args)) 474 475 (define input-prompt ";;; M-Eval input:") 476 (define output-prompt ";;; M-Eval value:") 477 478 (define (driver-loop) 479 (prompt-for-input input-prompt) 480 (let ((input (read))) 481 (let ((output 482 (eval input 483 the-global-environment))) 484 (announce-output output-prompt) 485 (user-print output))) 486 (driver-loop)) 487 488 (define (prompt-for-input string) 489 (newline) (newline) 490 (display string) (newline)) 491 492 (define (announce-output string) 493 (newline) (display string) (newline)) 494 495 (define (user-print object) 496 (if (compound-procedure? object) 497 (display 498 (list 'compound-procedure 499 (procedure-parameters object) 500 (procedure-body object) 501 '<procedure-env>)) 502 (display object))) 503 504 ;; XXX: Already defined above: 505 ;; (define the-global-environment 506 ;; (setup-environment)) 507 508 (define (main args) 509 (match args 510 ('() 511 (error "Arguments should have something rather than nothing.")) 512 (`(,arg0) 513 (display arg0) 514 (newline)) 515 (`(,arg0 "--debug") 516 (display "DEBUG TURN ON!\n") 517 (set! *debug* #t)) 518 (args (error "Unknown arguments:" args))) 519 520 (driver-loop)) 521 522 (define (run-internal-tests) 523 (test-eq 1 524 (lookup-variable-value 'a 525 (extend-environment '(a +) 526 `(1 ,+) 527 the-empty-environment))) 528 (test-eq 1 529 (lookup-variable-value 'a 530 (extend-environment '(+ a) 531 `(,+ 1) 532 the-empty-environment))) 533 (test-eq + 534 (lookup-variable-value '+ 535 (extend-environment '(a +) 536 `(1 ,+) 537 the-empty-environment)))) 538 539 (add-form-handler! application? 540 (lambda (exp env) 541 (apply (eval (operator exp) env) 542 (list-of-values 543 (operands exp) 544 env)))) 545 (add-form-handler! (lambda (exp) 546 (tagged-list? exp 'or)) 547 (lambda (exp env) 548 (eval (let loop ((seq (cdr exp))) 549 (match seq 550 ('() 'false) 551 ((expression . rest) 552 (list (list 'lambda (list 'value) 553 (list 'if 554 'value 555 'value 556 (loop rest))) 557 expression)) 558 (otherwise 559 (error "Weird expression -- AND" 560 otherwise)))) 561 env))) 562 (add-form-handler! (lambda (exp) 563 (tagged-list? exp 'and)) 564 (lambda (exp env) 565 (eval (let loop ((seq (cdr exp))) 566 (match seq 567 ('() 'false) 568 ((expression) 569 `((lambda (value) 570 (if value 571 value 572 false)) 573 ,expression) 574 ) 575 ((expression . rest) 576 `((lambda (value) 577 (if value 578 ,(loop rest) 579 false)) 580 ,expression)) 581 (otherwise 582 (error "Weird expression -- AND" 583 otherwise)))) 584 env))) 585 (add-form-handler! cond? 586 (lambda (exp env) 587 (eval (cond->if exp) env))) 588 (add-form-handler! begin? 589 (lambda (exp env) 590 (eval-sequence 591 (begin-actions exp) 592 env))) 593 (add-form-handler! lambda? 594 (lambda (exp env) 595 (make-procedure (lambda-parameters exp) 596 (lambda-body exp) 597 env))) 598 (add-form-handler! if? eval-if) 599 (add-form-handler! definition? eval-definition) 600 (add-form-handler! assignment? eval-assignment) 601 (add-form-handler! quoted? (lambda (exp env) (text-of-quotation exp))) 602 603 ))