Programming Languages_04 Deferred Substitution

Deferred Substitution

在执行泛起with时,行使“substitution”,每次with的泛起,它都绕着整个body置换。这一方式是由F1WAE到env再到list-of-FunDef为止,

java基础

然后再到substitution列表中,以env的形式举行。

In Case of WAE

DefrdSub

(define-type DefrdSub
  [mtSub]
  [aSub (name symbol?)
        (value number?)
        (rest DefrdSub?)])

lookup : symbol DefrdSub -> number

(define (lookup name ds)
  (type-case DefrdSub ds
    [mtSub () (error 'lookup "free variable")]
    [aSub (x val rest) (if (symbol=? x name)
                           val
                           (lookup name rest))]))

interp : WAE -> number 换成 WAE DefrdSub -> number

(define (interp wae ds)
  (type-case WAE wae
    [num (n) n]
    [add (l r) (+ (interp l ds) (interp r ds))]
    [sub (l r) (- (interp l ds) (interp r ds))]
    [with (x i b) (interp b (aSub x (interp i ds) ds))]
    [id (s) (lookup s ds)]))

In Case of F1WAE

不经思索,会造成如下结果:

{deffun {f x} {+ y x}}
(interp (parse '{with {y 2} {f 10}})), env:[]
->(interp (parse '{f 10})), env:[y=2]
->(interp (parse '{+ y x})), env:[x=10 y=2]
->12 wrong!
更准确地说,这是static scope不适合的体现。

interp : F1WAE list-of-FunDef DefrdSub -> number

(define (interp f1wae fundefs ds)
  (type-case F1WAE f1wae
    ...
    [app (ftn arg)
         (local [(define a-fundef (lookup-fundef ftn fundefs))])
            (interp (fundef-body a-fundef)
                    fundefs
                    (aSub (fundef-arg-name a-fundef)
                          (interp arg fundefs ds)
                          (mtSub)))]))
在举行“function call”时,将DefrdSub重新装入env,使其成为“arg substitution”。(local设计)

原创文章,作者:28x29新闻网,如若转载,请注明出处:https://www.28x29.com/archives/7612.html