关于环境的表示和操作
将环境表示为一个框架的表,一个环境的外围环境就是这个表的cdr,空环境则直接用空表表示
(define (enclosing-environment env) (cdr env))
(define (first-frame env) (car env))
(define the-empty-environment '())
;每个框架都是一对表形成的序对:一个是这一框架中的所有变量的表,还是就是它们的约束值的表
(define (make-frame variables values)
(cons variables values))
(define (frame-variables frame) (car frame))
(define (frame-values frame) (cdr frame))
(define (add-binding-to-frame! var val frame)
(set-car! frame (cons var (car frame)))
(set-cdr! frame (cons val (cdr frame))))
创建初始环境,在其中建立起基本过程的名字与一个唯一对象的关联。
(define (setup-environment)
(let ((initial-env
(extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment)))
(define-variable! 'true true initial-env)
(define-variable! 'false false initial-env)
initial-env))
(define the-global-environment (setup-environment))
;定义基本过程的名字和相应的实现过程
(define primitive-procedures
(list (list 'car car)
(list 'cdr cdr)
(list 'cons cons)
(list 'null? null?)
;;其他基本过程
))
(define (primitive-procedure-names)
(map car primitive-procedures))
(define (primitive-procedure-objects)
(map (lambda (proc) (list 'primitive (cadr proc)))
primitive-procedures))
eval对表达式进行分类,依此引导自己的求值工作。eval的构造就像是一个针对被求值表达式的语法类型的分情况分析。针对每类表达式有一个谓词完成相应的检测,有一套抽象方法去选择表达式里的各个部分。
(define (eval exp env) ;参数是一个表达式和一个环境
(cond ((self-evaluating? exp) exp)
((variable? exp) (lookup-variable-value exp env))
((quoted? exp) (text-of-quotation exp))
((assignment? exp) (eval-assignment exp env))
((definition? exp) (eval-definition exp env))
((if? exp) (eval-if exp env)
((lambda? exp)
(make-procedure (lambda-parameters exp) (lambda-body exp) env))
((begin? exp)
(eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
((application? exp)
(apply (eval (operator exp) env) (list-of-values (operands exp) env)))
(else
(error "Unknown expression type -- EVAL" exp))))
谓词检测,把除了false对象之外的所有东西就接受为真
(define (true? x)
(not (eq? x false)))
(define (false? x)
(eq? x false))
过程tagged-list?确定一个表的开始是不是某个给定符号
(define (tagged-list? exp tag)
(if (pair? exp)
(eq? (car exp) tag)
false))
对于自求值表达式,例如各种数,eval直接返回这个表达式本身。
(define (self-evaluating? exp)
(cond ((number? exp) true)
((string? exp) true)
(else false)))
对于变量,eval必须在环境中查找变量,找出它们的值。
(define (variable? exp)
(symbol? exp))
;返回exp在环境env里的约束值
(define (lookup-variable-value var env)
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars) (env-loop (enclosing-environment env)))
((eq? var (car vars)) (car vals))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env) (scan (frame-variables frame) (frame-values frame)))))
(env-loop env))
对于加引号的表达式,eval返回被引的表达式。
;;求值器看到的引号表达式是以quote开头的表,即使这种表达式在输入时用的是一个引号
(define (quoted? exp)
(tagged-list? exp 'quote))
(define (text-of-quotation exp) (cadr exp))
对于变量的赋值(或者定义),就需要递归地调用eval去计算出需要关联于这个对象的新值。而后修改环境,以改变(或者建立)相应变量的约束。
;赋值
(define (assignment-variable exp) (cadr exp))
(define (assignment-value exp) (caddr exp))
(define (assignment? exp)
(tagged-list? exp 'set!))
(define (eval-assignment exp env)
(set-variable-value! (assignment-variable exp)
(assignment-value exp)
env)
'ok)
;修改变量var在环境env里的约束,使得该变量现在约束到值value
(define (set-variable-value! var val env)
(define (env-loop env)
(define (scan vars vals)
(cond ((null? vars) (env-loop (enclosing-environment env)))
((eq? var (car vars)) (set-car! vals val))
(else (scan (cdr vars) (cdr vals)))))
(if (eq? env the-empty-environment)
(error "Unbound variable" var)
(let ((frame (first-frame env) (scan (frame-variables frame) (frame-values frame)))))
(env-loop env))
;定义
(define (definition-variable exp)
(if (symbol? (cadr exp) (cadr exp) ;若为变量定义,则获取变量名 (caadr exp) ;若为过程定义,则获取过程名 (define (definition-value exp) (if (symbol? (cadr exp) (caddr exp) (make-lambda (cdadr exp) (cddr exp)))) (define (make-lambda parameters body) (cons 'lambda (cons parameters body))) (define (definition? exp) (tagged-list? exp 'define)) (define (eval-definiton exp env) (define-variable! (definition-variable exp) (eval (definition-value exp) env) env)) ;在环境env的第一个框架里加入一个新约束,关联起变量var和值value (define (define-variable! var val env) (let (frame (first-frame env)) (define (scan vars vals) (cond ((null? vars) (add-binding-to-frame! var val frame)) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (scan (frame-variables frame) (frame-values frame))))
一个if表达式要求对其中各部分的特殊处理方式,在谓词为真时求值其推论部分,否则求值其替代部分。
(define (if-predicate exp) (cadr exp))
(define (if-consequent exp) (caddr exp))
(define (if-alternative exp)
(if (not (null? (cdddr exp)))
(cadddr exp)
'false))
(define (if? exp)
(tagged-list? exp 'if))
(define (eval-if exp env)
(if (true? (eval (if-predicate exp) env))
(eval (if-consequent exp) env)
(eval (if-alternative exp) env)))
一个lambda必须转换成一个可以应用的进程,方式就是将这个lambda表达式所描述的参数表和体与相应的求值环境包装起来。
(define (lambda-parameters exp) (cadr exp))
(define (lambda-body exp) (cddr exp))
(define (lambda? exp)
(tagged-list? exp 'lambda))
(define (make-procedure parameters body env)
(list 'procedure parameters body env))
一个begin表达式要求求值其中的一系列表达式,按照它们出现的顺序。
(define (begin-actions exp) (cdr exp))
(define (last-exp? exp) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
(define (begin? exp)
(tagged-list? exp 'begin))
(define (eval-sequence exps env)
(cond ((last-exp? exps) (eval (first-exp exps) env))
(else (eval (first-exp exps) env)
(eval-sequence (rest-exps exps) env))))
分情况分析(cond)将被变换为一组嵌套的if表达式,而后求值。
(define (cond-clauses exp) (cdr exp))
(define (cond-else-clause? clause)
(eq? (cond-predicate clause) 'else))
(define (cond-predicate clause) (car clause))
(define (cond-actions clause) (cdr clause))
(define (cond? exp)
(tagged-list? exp 'cond))
(define (cond->if exp)
(expand-clauses (cond-clauses exp)))
(define (expand-clauses clauses)
(if (null? clauses)
'false
(let ((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest) (sequence->exp (cond-actions first)) (error "ELSE clause isn't last --COND->IF" clauses))
(make-if (cond-predicate first) (sequence->exp (cond-actions first)) (expand-clauses rest))))))
;;将cond表达式变换为if表达式
(define (make-if predicate consequent alternative)
(list 'if predicate consequent alternative))
;;把一个序列变换为一个表达式,如果需要的话就加上begin作为开头
(define (sequence->exp seq)
(cond ((null? seq) seq)
((last-exp? seq) (first-exp seq))
(else (make-begin seq))))
(define (make-begin seq)
(cons 'begin seq))
对于一个过程应用,eval必须递归地求值组合式的运算符部分和运算对象部分。而后将这样得到的过程和参数送给apply,由它去处理实际的过程应用。
apply在求值复合过程的体时需要建立相应的环境,这个环境的构造方式就是扩充该过程所携带的基本环境,并加入一个框架,其中将过程的各个形式参数约束于过程调用的实际参数。
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
(define (no-operands? ops) (null? ops))
(define (first-operand ops) (car ops))
(define (rest-operand ops) (cdr ops))
(define (application? exp) (pair? exp))
(define (list-of-values exps env)
(if (no-operands? exps)
'()
(cons (eval (first-operand exps) env)
(list-of-values (rest-operands exps) env))))
;;两个参数,一个是过程,一个是该过程应该去应用的实际参数的表
(define (apply procedure arguments)
(cond ((primitive-procedure? procedure)
;应用基本过程
(apply-primitive-procedure procedure arguments))
((compound-procedure? procedure)
;应用复合过程的方式是顺序地求值组成该过程体的那些表达式
(eval-sequence
(procedure-body procedure)
(extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure))))
(else
(error
"Unknown procedure type -- APPLY" procedure))))
;检查procedure是否为一个基本过程
(define (primitive-procedure? procedure)
(tagged-list? procedure 'primitive))
;将给定过程应用于arguments里的参数值,并返回这一应用的结果
(define (apply-primitive-procedure procedure arguments)
(apply-in-underlying-scheme
(primitive-implementation procedure) arguments))
(define (primitive-implementation procedure)
(cadr procedure))
(define (compound-procedure? procedure)
(tagged-list? procedure 'procedure))
(define (procedure-parameters procedure)
(cadr procedure))
(define (procedure-body procedure)
(caddr procedure))
(define (procedure-environment procedure)
(cadddr procedure))
;返回一个新环境,包含一个新的框架,其中的所有位于表vars里的符号约束到约束到表vals里对应的元素,而其外围环境是env
(define (extend-environment vars vals env)
(if (= (length vars) (length vals))
(cons (make-frame vars vals)
env)
(if (< (length vars) (length vals))
(error "Too many arguments supplied" vars vals)
(error "Too few arguments supplied" vars vals))))
关于apply-in-underlying-scheme过程:由于此处实现的是简易Scheme元循环求值器,所以我们应用的是基本过程的apply定义,由于和求值器中的apply重名,所以将apply-in-underlying-scheme作为基本过程apply的一个引用。