Substitution Evaluator

Report
Evaluators for Functional
Programming
Chapter 4
Chapter 4 - Evaluators for Functional
Programming
1
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
2
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. applicativeeval)
2. Environment-based evaluator
(uses an environment data structure)
3. Environment-based compiler
Chapter 4 - Evaluators for Functional
Programming
3
Evaluator Structure
evaluator
program in
interpreted
language
eval
(Global)
Environment
substitute reduce
Value from
interpreted
language
values
written in Implementation (embedding) language
Chapter 4 - Evaluators for Functional
Programming
4
common evaluator structure
evaluator
program in
interpreted
language
(Global)
Environment
abstract
syntax
parser
parsed
expression
(parse tree)
eval
substitute reduce
Chapter 4 - Evaluators for Functional
Programming
Value from
interpreted
language
values
5
basic compiler structure
program in
interpreted
language
Value from
target
language
values
compiler
abstract
syntax
parser
parsed
expression
(parse tree)
compilation
execution/
evaluation
program in
target
language
Chapter 4 - Evaluators for Functional
Programming
Global
Environment
6
evaluator structure
evaluator
Scheme
expression
(Global)
Environment
Code in:
Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional
Programming
Value
7
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
8
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
9
Abstract Syntax Parser (ASP)
A tool that
1. Identifies the kind of an input expression (atomic,
lambda, application, etc)
2. Select the components of a Scheme expression
3. 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
10
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
11
Tagged-data interface and impl.
Chapter 4 - Evaluators for Functional
Programming
12
Tagged-data interface and impl.
Chapter 4 - Evaluators for Functional
Programming
13
Parser procedures - atomic exp.
Chapter 4 - Evaluators for Functional
Programming
14
Parser procedures - compound 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.
letrec - similar functions...
Chapter 4 - Evaluators for Functional
Programming
22
Parser procedures - compound exp.
Chapter 4 - Evaluators for Functional
Programming
23
Parser procedures - application
• The application expression is special compound expression: It
does not have a tag.
Chapter 4 - Evaluators for Functional
Programming
24
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
25
ASP - Derived expressions
(let ((x (+ y 2))
(y (- x 3)))
(* x y))
((lambda (x y)
(* x y))
(+ y 2)
(- x 3))
(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))))
Chapter 4 - Evaluators for Functional
Programming
26
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
27
ASP - Derived expressions
(cond ((> x 0) x)
((= x 0) (display ’zero) 0)
(else (- x)))
(if (> x 0)
x
(if (= x 0)
(begin (display ’zero) 0)
(- x)))
• cond->if
Chapter 4 - Evaluators for Functional
Programming
28
ASP - Derived expressions
(cond ((> x 0) x)
(else (cond ((= x 0) 0)
(else (- x))))))
shallow derivation
(if (> x 0)
x (cond ((= x 0) 0)
(else (- x))))
deep (recursive)
derivation
(if (> x 0)
x (if(= x 0)
0
(- x)))
Chapter 4 - Evaluators for Functional
Programming
29
ASP - Derived expressions
(let*((x 10)
(y (+ x 2))
(+ x y))
shallow derivation
(let((x 10))
(let ((y (+ x 2)))
(+ x y)))
recursive derivation until
fixed point achieved
((lambda(x) (let ((y ....))
10)
Chapter 4 - Evaluators for Functional
Programming
30
Chapter 4 - Evaluators for Functional
Programming
31
Chapter 4 - Evaluators for Functional
Programming
32
evaluator structure
evaluator
Scheme
expression
(Global)
Environment
Code in:
Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional
Programming
Value
33
Applicative-Eval Evaluator Core
data structures:
1. Evaluated values
2. The global environment
– managing "global" variable-value bindings.
Chapter 4 - Evaluators for Functional
Programming
34
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
35
Evaluated values ADTs
Primitive-procedure
Procedure
Other
make-primitive-procedure [T -> Primitive-procedure]
primitive-procedure?
[T –> Boolean]
primitive-implementation [Primitive-procedure –> T]
make-procedure
[LIST(Symbol)*LIST –> Procedure]
compound-procedure?
procedure-parameters [Procedure –> LIST(Symbol)]
procedure-body
[Procedure –> LIST]
make-value
value?
value-content
Chapter 4 - Evaluators for Functional
Programming
36
Primitive procedure - Impl.
Chapter 4 - Evaluators for Functional
Programming
37
Procedure - Impl.
Chapter 4 - Evaluators for Functional
Programming
38
evaluator structure
evaluator
Scheme
expression
(Global)
Environment
Code in:
Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional
Programming
Value
39
The global environment
• mutable binding management.
mapping from "global" variables to values.
Chapter 4 - Evaluators for Functional
Programming
40
The global environment
GE procedures:
Chapter 4 - Evaluators for Functional
Programming
41
The global environment Impl.
The lookup procedure
Chapter 4 - Evaluators for Functional
Programming
42
The global environment Impl.
Adding the primitive bindings
Chapter 4 - Evaluators for Functional
Programming
43
The global environment Impl.
....
Chapter 4 - Evaluators for Functional
Programming
44
The global environment Impl. - lookup
Chapter 4 - Evaluators for Functional
Programming
45
The global environment Impl. - mutator
Chapter 4 - Evaluators for Functional
Programming
46
evaluator structure
evaluator
Scheme
expression
(Global)
Environment
Code in:
Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional
Programming
Value
47
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
48
Applicative-Eval Evaluator - core
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
atomic exp.
Chapter 4 - Evaluators for Functional
Programming
52
Applicative-Eval Evaluator - core
special forms
Chapter 4 - Evaluators for Functional
Programming
53
Applicative-Eval Evaluator - core
special forms
Chapter 4 - Evaluators for Functional
Programming
54
Applicative-Eval Evaluator - core
special forms
Chapter 4 - Evaluators for Functional
Programming
55
Applicative-Eval Evaluator - core
application
Chapter 4 - Evaluators for Functional
Programming
56
Applicative-Eval Evaluator - core
primitive procedure application
Chapter 4 - Evaluators for Functional
Programming
57
Applicative-Eval Evaluator - core
substitution
Chapter 4 - Evaluators for Functional
Programming
58
Applicative-Eval Evaluator - core
substitution (continued)
Chapter 4 - Evaluators for Functional
Programming
59
evaluator structure
evaluator
Scheme
expression
(Global)
Environment
Code in:
Racket-Evaluators\substitution-interpreter\
Chapter 4 - Evaluators for Functional
Programming
Value
60
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))
Chapter 4 - Evaluators for Functional
Programming
61

similar documents