Presentation is loading. Please wait.

Presentation is loading. Please wait.

Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming.

Similar presentations


Presentation on theme: "Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming."— Presentation transcript:

1 Evaluators for Functional Programming Chapter 4 1 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 2

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 3 Chapter 4 - Evaluators for Functional Programming

4 Evaluator Structure Chapter 4 - Evaluators for Functional Programming 4 program in interpreted language Value from interpreted language values (Global) Environment evaluator eval substitute - reduce written in Implementation (embedding) language

5 common evaluator structure Chapter 4 - Evaluators for Functional Programming 5 abstract syntax parser program in interpreted language Value from interpreted language values (Global) Environment evaluator parsed expression (parse tree) eval substitute - reduce

6 basic compiler structure Chapter 4 - Evaluators for Functional Programming 6 abstract syntax parser program in interpreted language Value from target language values Global Environment compiler parsed expression (parse tree) compilation program in target language execution/ evaluation

7 evaluator structure Chapter 4 - Evaluators for Functional Programming 7 Scheme expression Value (Global) Environment evaluator Code in: Racket-Evaluators\substitution-interpreter\

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. 8 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 ; given # Chapter 4 - Evaluators for Functional Programming 9

10 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. 10 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 11

12 Tagged-data interface and impl. 12 Chapter 4 - Evaluators for Functional Programming

13 Tagged-data interface and impl. 13 Chapter 4 - Evaluators for Functional Programming

14 Parser procedures - atomic exp. 14 Chapter 4 - Evaluators for Functional Programming

15 Parser procedures - compound exp. 15 Chapter 4 - Evaluators for Functional Programming

16 Parser procedures - compound exp. 16 Chapter 4 - Evaluators for Functional Programming

17 Parser procedures - compound exp. 17 Chapter 4 - Evaluators for Functional Programming

18 Parser procedures - compound exp. 18 Chapter 4 - Evaluators for Functional Programming

19 Parser procedures - compound exp. 19 Chapter 4 - Evaluators for Functional Programming

20 Parser procedures - compound exp. 20 Chapter 4 - Evaluators for Functional Programming

21 Parser procedures - compound exp. 21 Chapter 4 - Evaluators for Functional Programming

22 Parser procedures - compound exp. 22 Chapter 4 - Evaluators for Functional Programming letrec - similar functions...

23 Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming 23

24 Parser procedures - application The application expression is special compound expression: It does not have a tag. 24 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 ] 25 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)) 26 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))))) 27 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))) 28 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)))) 29 Chapter 4 - Evaluators for Functional Programming (if (> x 0) x (if(= x 0) 0 (- x))) shallow derivation deep (recursive) derivation

30 ASP - Derived expressions (let*((x 10) (y (+ x 2)) (+ x y)) (let((x 10)) (let ((y (+ x 2))) (+ x y))) 30 Chapter 4 - Evaluators for Functional Programming ((lambda(x) (let ((y....)) 10) shallow derivation recursive derivation until fixed point achieved

31 Chapter 4 - Evaluators for Functional Programming 31

32 Chapter 4 - Evaluators for Functional Programming 32

33 evaluator structure Chapter 4 - Evaluators for Functional Programming 33 Scheme expression Value (Global) Environment evaluator Code in: Racket-Evaluators\substitution-interpreter\

34 Applicative-Eval Evaluator Core data structures: 1. Evaluated values 2. The global environment – managing "global" variable-value bindings. 34 Chapter 4 - Evaluators for Functional Programming

35 Evaluated values Repeated evaluation of compound values: applicative-eval[((lambda (lst)(car lst)) (list 1 2 3))] applicative-eval[(lambda (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. 35 Chapter 4 - Evaluators for Functional Programming

36 Evaluated values ADTs Primitive-proceduremake-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] Othermake-value value? value-content 36 Chapter 4 - Evaluators for Functional Programming

37 Primitive procedure - Impl. Chapter 4 - Evaluators for Functional Programming 37

38 Procedure - Impl. Chapter 4 - Evaluators for Functional Programming 38

39 evaluator structure Chapter 4 - Evaluators for Functional Programming 39 Scheme expression Value (Global) Environment evaluator Code in: Racket-Evaluators\substitution-interpreter\

40 The global environment mutable binding management. mapping from "global" variables to values. 40 Chapter 4 - Evaluators for Functional Programming

41 The global environment GE procedures: 41 Chapter 4 - Evaluators for Functional Programming

42 The global environment Impl. The lookup procedure 42 Chapter 4 - Evaluators for Functional Programming

43 The global environment Impl. Adding the primitive bindings Chapter 4 - Evaluators for Functional Programming 43

44 The global environment Impl. Chapter 4 - Evaluators for Functional Programming

45 The global environment Impl. - lookup Chapter 4 - Evaluators for Functional Programming 45

46 The global environment Impl. - mutator Chapter 4 - Evaluators for Functional Programming 46

47 evaluator structure Chapter 4 - Evaluators for Functional Programming 47 Scheme expression Value (Global) Environment evaluator Code in: Racket-Evaluators\substitution-interpreter\

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. 48 Chapter 4 - Evaluators for Functional Programming

49 Applicative-Eval Evaluator - core 49 Chapter 4 - Evaluators for Functional Programming

50 Applicative-Eval Evaluator - core 50 Chapter 4 - Evaluators for Functional Programming

51 Applicative-Eval Evaluator - core 51 Chapter 4 - Evaluators for Functional Programming

52 Applicative-Eval Evaluator - core atomic exp. 52 Chapter 4 - Evaluators for Functional Programming

53 Applicative-Eval Evaluator - core special forms 53 Chapter 4 - Evaluators for Functional Programming

54 Applicative-Eval Evaluator - core special forms 54 Chapter 4 - Evaluators for Functional Programming

55 Applicative-Eval Evaluator - core special forms 55 Chapter 4 - Evaluators for Functional Programming

56 Applicative-Eval Evaluator - core application 56 Chapter 4 - Evaluators for Functional Programming

57 Applicative-Eval Evaluator - core primitive procedure application 57 Chapter 4 - Evaluators for Functional Programming

58 Applicative-Eval Evaluator - core substitution 58 Chapter 4 - Evaluators for Functional Programming

59 Applicative-Eval Evaluator - core substitution (continued) 59 Chapter 4 - Evaluators for Functional Programming

60 evaluator structure Chapter 4 - Evaluators for Functional Programming 60 Scheme expression Value (Global) Environment evaluator Code in: Racket-Evaluators\substitution-interpreter\

61 61 Chapter 4 - Evaluators for Functional Programming 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 '(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))


Download ppt "Evaluators for Functional Programming Chapter 4 1 Chapter 4 - Evaluators for Functional Programming."

Similar presentations


Ads by Google