FAE parse : 一成不變 FAE Value : interp的最終轉讓值 numV: value closureV: param FAE(或value,或function) pair list DefrdSub : 傳達environment 以前WAE中使用的DefrdSub只用於wit ...
FAE-parse : 一成不變
FAE-Value : interp的最終轉讓值
numV: value
closureV: param-FAE(或value,或function) pair list
(define-type FAE-Value
[numV (n number?)]
[closureV (param symbol?) (body FAE?) (ds DefrdSub?)])
DefrdSub : 傳達environment
以前WAE中使用的DefrdSub只用於with,但是在這裡with的置換和function call這兩種作用是同時進行的。所以在FAE-Value上有numV和closureV兩種組合。
(define-type DefrdSub
[mtSub]
[aSub (name symbol?) (value FAE-Value?) (ds DefrdSub?)])
lookup : symbol DefrdSub -> FAE-Value
(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 : FAE - >FAE 修改為 FAE DefrdSub -> FAE-Value
如果interp f ds中有f存在於ds中,在id (s)的lookup s ds中,closureV重新出現,那麼f-val中的closureV a-val將成為numV。
app中aSub為什麼不是mtsub ?F1WAE with deferred substitution中是mtSub的理由是static scope。這裡是由ds決定lambda函數的scope。這時的ds不是叫app時的ds,而是相當於function被定義時scope的ds。
(define (interp fae ds)
(type-case FAE fae
[num (n) (numV n)]
[add (l r) (num+ (interp l ds) (interp r ds))]
[sub (l r) (num- (interp l ds) (interp r ds))]
[id (s) (lookup s ds)]
[fun (x b) (closureV x b ds)]
[app (f a) (local [(define f-val (interp f ds))
(define a-val (interp a ds))]
(interp (closureV-body f-val)
(aSub (closureV-param f-val)
a-val
(closureV-ds f-val))))]))