Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Eval-Apply • Mutual recursion between eval and apply • To evaluate a compound expression means to evaluate the sub-expressions recursively, then apply the operator to the arguments. • To apply a function to arguments means to evaluate the body of the function in a new environment. 6
Eval • Evaluate expression in given environment • (define (mc-eval exp env) • (cond • ((self-evaluating? exp) exp) • … • ((application? exp) • (mc-apply (mc-eval (operator exp) env) • (list-of-values (operands exp) env))) • The name mc-eval is used to differ from the primitive eval 7
Apply • Apply procedure to list of arguments • (define (mc-apply procedure arguments) • … • (eval-sequence (procedure-body procedure) • (extend-environment • (procedure-parameters procedure) • arguments • (procedure-environment procedure)))) • The name mc-apply is used to differ from the primitive apply 8
Change definition from (define x 7) to (x := 7) and from (define (square x) (* x x)) to ((square x) := (* x x)) 9
let* • The bindings of are performed sequentially. • Each binding is made in an environment in • which all of the preceding bindings are • visible. • (let* ((x 3) • (y (+ x 2)) • (z (+ x y 5))) • (* x z)) • > 39 18
Frames • A frame is a set of bindings, represented as a pair of two lists: variables and values • Constructor • (make-frame variables values) • Selectors • (frame-variables frame) • (frame-values frame) • Mutator • (add-binding-to-frame! var val frame)
Environments • An environment consists of its first frame and an enclosing environment • Constructor • (extend-environment vars vals base-env) • Selectors • (first-frame env) • (enclosing-environment env) • Predefined environments • the-global-environment • the-empty-environment
Frame Scanning • Performed by: • lookup-variable-valueinvoked when evaluating names • set-variable-value!invoked when evaluating set! expressions • define-value!invoked when evaluating definitions
define-variable! • (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))))
All procedures are alike! • Scan the frame for the variable’s name • If found, success operation (return value / set value) • If not in frame, failure operation(continue to next frame / add binding) • Error on empty environment (if this can happen) • We can get better abstraction • Capturing common patterns • Hiding frame scanning / mutation
Practice Question • When a procedure with no parameters is applied, an empty frame is opened • Change the MCE code such that parameter-less procedures will be applied in the parent environment • Is the modified evaluator equivalent to the original one? Explain or give a contradicting example