Chapter 4 - Evaluators for Functional Programming
1
Evaluators for FunctionalProgramming
Chapter 4
Chapter 4 - Evaluators for Functional Programming
2
How to describe (specify) a programming language?
1. Syntax: atoms, primitives, combination and abstraction means.
2. Semantics: values, types. 3. Operational semantics: evaluation rules,
evaluator algorithm.
Chapter 4 - Evaluators for Functional Programming
3
Evaluator for Functional Programming
• meta-circular : – Interpreted language = our flavor of Scheme–(embedding) language = Scheme
• We will see three evaluators for FP: 1. Substitution evaluator (impl. applicative-eval)2. Environment-based evaluator
(uses an environment data structure)3. Environment-based compiler
Chapter 4 - Evaluators for Functional Programming
4
Evaluator Structure
program in interpretedlanguage Value from
interpreted language values (Global)
Environment
evaluator
eval
substitute -reduce
written in Implementation (embedding) language
Chapter 4 - Evaluators for Functional Programming
5
common evaluator structure
abstract syntax parser
program in interpretedlanguage
Value from interpreted
language values
(Global) Environment
evaluator
parsed expression(parse tree)
eval
substitute -reduce
Chapter 4 - Evaluators for Functional Programming
6
basic compiler structure
abstract syntax parser
program in interpretedlanguage Value from
target language values
Global Environment
compiler
parsed expression(parse tree)
compilationprogram in
target language
execution/ evaluation
Chapter 4 - Evaluators for Functional Programming
7
evaluator structure
Scheme expression
Value
(Global) Environment
evaluator
Code in: Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional Programming
8
Input• Input: a scheme expression or an already evaluated
scheme expression (in case of repeated evaluation).
(lambda (lst) (car (car lst))
• Input is accepted in the form of constant lists.
'(lambda (lst) (car (car lst)))(list 'lambda (list 'lst) (list 'car (list 'car 'lst))
• uniformity of Scheme expressions and the printed form of lists.
Chapter 4 - Evaluators for Functional Programming
9
Input> (derive-eval '(+ 1 2) )3
> (derive-eval (list 'lambda (list 'lst) (list 'car (list 'car 'lst)) ))
(procedure (lst) ((car (car lst))))
> (derive-eval '(lambda (lst) (car (car 'lst))) )(procedure (lst) ((car (car lst))))
> (derive-eval (lambda (lst) (car (car lst)) )). . ASP.scm:247:31: car: expects argument of type
<pair>; given #<procedure>
Chapter 4 - Evaluators for Functional Programming
10
Abstract Syntax Parser (ASP)
A tool that1. Identifies the kind of an input expression (atomic,
lambda, application, etc)2. Select the components of a Scheme expression3. Construct a Scheme expression from its components
Impl. an interface for Scheme Expression, according to Abstract Syntax of Scheme.
Chapter 4 - Evaluators for Functional Programming
11
Derived Expressions
Language expression have two classes:• Kernel (core knows what to do with them)• Derived (rewritten using kernel expressions –
more on that later)
Chapter 4 - Evaluators for Functional Programming
12
Tagged-data interface and impl.
Chapter 4 - Evaluators for Functional Programming
13
Tagged-data interface and impl.
Chapter 4 - Evaluators for Functional Programming
14
Parser procedures - atomic exp.
Chapter 4 - Evaluators for Functional Programming
15
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
16
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
17
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
18
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
19
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
20
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
21
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
22
Parser procedures - compound exp.
letrec - similar functions...
Chapter 4 - Evaluators for Functional Programming
23
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional Programming
24
Parser procedures - application• The application expression is special compound expression: It
does not have a tag.
Chapter 4 - Evaluators for Functional Programming
25
ASP - Derived expressions
'derived' expression are translated into 'core' expressions (according to syntactic sugar/macro rule), before being evaluated.
Derivation procedures are part of the ASP; Signature: derive(exp); Type: [Scheme-exp -> Scheme-exp ]
Chapter 4 - Evaluators for Functional Programming
26
ASP - Derived expressions
(define let->combination(lambda (exp)
(let ((vars (let-variables exp)) (body (let-body exp))
(initial-vals (let-initial-values exp))) (make-application (make-lambda vars body)
initial-vals))))
(let ((x (+ y 2)) (y (- x 3)))
(* x y))
((lambda (x y)(* x y))
(+ y 2) (- x 3))
Chapter 4 - Evaluators for Functional Programming
27
ASP - Derived expressions
(define (f x y)(display x) (+ x y))
(define f(lambda (x y)
(display x)(+ x y)))
(define function-define->define (lambda (exp) (let ((var (function-definition-variable exp))
(params (function-definition-parameters exp))(body (function-definition-body exp)))
(make-definition var (make-lambda params body)))))
Chapter 4 - Evaluators for Functional Programming
28
ASP - Derived expressions
• cond->if
(cond ((> x 0) x) ((= x 0) (display ’zero) 0) (else (- x)))
(if (> x 0)x(if (= x 0) (begin (display ’zero) 0) (- x)))
Chapter 4 - Evaluators for Functional Programming
29
ASP - Derived expressions(cond ((> x 0) x)
(else (cond ((= x 0) 0)(else (- x))))))
(if (> x 0) x (cond ((= x 0) 0)
(else (- x))))
(if (> x 0) x (if(= x 0)
0 (- x)))
shallow derivation
deep (recursive) derivation
Chapter 4 - Evaluators for Functional Programming
30
ASP - Derived expressions(let*((x 10)
(y (+ x 2)) (+ x y))
(let((x 10)) (let ((y (+ x 2)))
(+ x y)))
((lambda(x) (let ((y ....)) 10)
shallow derivation
recursive derivation until fixed point achieved
Chapter 4 - Evaluators for Functional Programming
31
Chapter 4 - Evaluators for Functional Programming
32
Chapter 4 - Evaluators for Functional Programming
33
evaluator structure
Scheme expression
Value
(Global) Environment
evaluator
Code in: Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional Programming
34
Applicative-Eval Evaluator Core
data structures:
1. Evaluated values
2. The global environment – managing "global" variable-value bindings.
35
Evaluated values• Repeated evaluation of compound values:
applicative-eval[((lambda (lst)(car lst)) (list 1 2 3))] applicative-eval[(lambda (lst)(car lst))] <== <Closure (lst)(car lst) >applicative-eval[(list 1 2 3)] <== (1 2 3) // evaluated value of list
applicative-eval[ (car (1 2 3)) ] ==>applicative-eval[car] <== Code of car.applicative-eval[(1 2 3)] <== "error: 1 is not a procedure"
• Same problem for values of lambda, quote (and other possible compound values) and primitive procedures.
• Need to identify (tag), evaluated values. Chapter 4 - Evaluators for Functional Programming
Chapter 4 - Evaluators for Functional Programming
36
Evaluated values ADTsPrimitive-procedure make-primitive-procedure [T -> Primitive-procedure]
primitive-procedure? [T –> Boolean]primitive-implementation [Primitive-procedure –> T]
Procedure make-procedure [LIST(Symbol)*LIST –> Procedure]compound-procedure? procedure-parameters [Procedure –> LIST(Symbol)]procedure-body [Procedure –> LIST]
Other make-valuevalue?value-content
Chapter 4 - Evaluators for Functional Programming
37
Primitive procedure - Impl.
Chapter 4 - Evaluators for Functional Programming
38
Procedure - Impl.
Chapter 4 - Evaluators for Functional Programming
39
evaluator structure
Scheme expression
Value
(Global) Environment
evaluator
Code in: Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional Programming
40
The global environment• mutable binding management.
mapping from "global" variables to values.
Chapter 4 - Evaluators for Functional Programming
41
The global environment
GE procedures:
Chapter 4 - Evaluators for Functional Programming
42
The global environment Impl.The lookup procedure
Chapter 4 - Evaluators for Functional Programming
43
The global environment Impl.Adding the primitive bindings
44
The global environment Impl.
Chapter 4 - Evaluators for Functional Programming
....
Chapter 4 - Evaluators for Functional Programming
45
The global environment Impl. - lookup
Chapter 4 - Evaluators for Functional Programming
46
The global environment Impl. - mutator
Chapter 4 - Evaluators for Functional Programming
47
evaluator structure
Scheme expression
Value
(Global) Environment
evaluator
Code in: Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional Programming
48
Applicative-Eval Evaluator - core
• Implementation of applicative eval algorithm.• Derives expressions• Special form/Atomic/Application• Application: Eval-substitute-reduce (recursive).• Has 'rename' and 'substitute' sub-routines• Uses: ASP (parser), GE packages• Creates Evaluated Values and returns them.
Chapter 4 - Evaluators for Functional Programming
49
Applicative-Eval Evaluator - core
Chapter 4 - Evaluators for Functional Programming
50
Applicative-Eval Evaluator - core
Chapter 4 - Evaluators for Functional Programming
51
Applicative-Eval Evaluator - core
Chapter 4 - Evaluators for Functional Programming
52
Applicative-Eval Evaluator - coreatomic exp.
Chapter 4 - Evaluators for Functional Programming
53
Applicative-Eval Evaluator - corespecial forms
Chapter 4 - Evaluators for Functional Programming
54
Applicative-Eval Evaluator - corespecial forms
Chapter 4 - Evaluators for Functional Programming
55
Applicative-Eval Evaluator - corespecial forms
Chapter 4 - Evaluators for Functional Programming
56
Applicative-Eval Evaluator - coreapplication
Chapter 4 - Evaluators for Functional Programming
57
Applicative-Eval Evaluator - coreprimitive procedure application
Chapter 4 - Evaluators for Functional Programming
58
Applicative-Eval Evaluator - coresubstitution
Chapter 4 - Evaluators for Functional Programming
59
Applicative-Eval Evaluator - coresubstitution (continued)
Chapter 4 - Evaluators for Functional Programming
60
evaluator structure
Scheme expression
Value
(Global) Environment
evaluator
Code in: Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional Programming
61
Applicative-Eval Evaluator - tests> (derive-eval '(* 3 4)) '(value 12)> (derive-eval '((lambda (f) (f 2 1)) +)) '(value 3)
Regression tests:(test (derive-eval '(* 3 4)) => '(value 12))(test (derive-eval '(cons 3 (cons 4 (list)))) => '(value (3 4)))(test (derive-eval '((lambda (f) (f 2 1)) +)) => '(value 3))(test (derive-eval '(begin 1 2 3)) => '(value 3))(test (derive-eval '(begin 1 2 3)) => '(value 3))(test (derive-eval '(define x 2)) => 'ok) (test (derive-eval '(define (f x) (+ x x))) => 'ok) (test (derive-eval 'x) => '(value 2)) (test (derive-eval '(f x)) => '(value 4))(test (derive '(let ((x 1)) (+ x 1))) => '( (lambda (x)(+ x 1)) 1))
Top Related