610 likes | 686 Views
Evaluators for Functional Programming. Chapter 4. How to describe (specify) a programming language?. Syntax : atoms, primitives, combination and abstraction means. Semantics: values, types. Operational semantics: evaluation rules, evaluator algorithm. Evaluator for Functional Programming.
E N D
Evaluators for FunctionalProgramming Chapter 4 Chapter 4 - Evaluators for Functional Programming
How to describe (specify) a programming language? • Syntax: atoms, primitives, combination and abstraction means. • Semantics: values, types. • Operational semantics: evaluation rules, evaluator algorithm. Chapter 4 - Evaluators for Functional Programming
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
Evaluator Structure evaluator program in interpreted language Value from interpreted language values eval (Global) Environment substitute -reduce written in Implementation (embedding) language Chapter 4 - Evaluators for Functional Programming
common evaluator structure evaluator program in interpreted language abstract syntax parser parsed expression (parse tree) eval (Global) Environment Value from interpreted language values substitute -reduce Chapter 4 - Evaluators for Functional Programming
basic compiler structure program in interpreted language Value from target language values compiler abstract syntax parser parsed expression (parse tree) execution/ evaluation program in target language Global Environment compilation Chapter 4 - Evaluators for Functional Programming
evaluator structure evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value Chapter 4 - Evaluators for Functional Programming
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
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
Abstract Syntax Parser (ASP) A tool that • Identifies the kind of an input expression (atomic, lambda, application, etc) • Select the components of a Scheme expression • 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
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
Tagged-data interface and impl. Chapter 4 - Evaluators for Functional Programming
Tagged-data interface and impl. Chapter 4 - Evaluators for Functional Programming
Parser procedures - atomic exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. letrec - similar functions... Chapter 4 - Evaluators for Functional Programming
Parser procedures - compound exp. Chapter 4 - Evaluators for Functional Programming
Parser procedures - application • The application expression is special compound expression: It does not have a tag. Chapter 4 - Evaluators for Functional Programming
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
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
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
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
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
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
evaluator structure evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value Chapter 4 - Evaluators for Functional Programming
Applicative-Eval Evaluator Core data structures: 1. Evaluated values 2. The global environment • managing "global" variable-value bindings. Chapter 4 - Evaluators for Functional Programming
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
Evaluated values ADTs Chapter 4 - Evaluators for Functional Programming
Primitive procedure - Impl. Chapter 4 - Evaluators for Functional Programming
Procedure - Impl. Chapter 4 - Evaluators for Functional Programming
evaluator structure evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value Chapter 4 - Evaluators for Functional Programming
The global environment • mutable binding management. mapping from "global" variables to values. Chapter 4 - Evaluators for Functional Programming
The global environment GE procedures: Chapter 4 - Evaluators for Functional Programming
The global environment Impl.The lookup procedure Chapter 4 - Evaluators for Functional Programming
The global environment Impl.Adding the primitive bindings Chapter 4 - Evaluators for Functional Programming
The global environment Impl. .... Chapter 4 - Evaluators for Functional Programming
The global environment Impl. - lookup Chapter 4 - Evaluators for Functional Programming
The global environment Impl. - mutator Chapter 4 - Evaluators for Functional Programming
evaluator structure evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value Chapter 4 - Evaluators for Functional Programming
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
Applicative-Eval Evaluator - core Chapter 4 - Evaluators for Functional Programming
Applicative-Eval Evaluator - core Chapter 4 - Evaluators for Functional Programming