1 / 61

Evaluators for Functional Programming

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.

sahara
Download Presentation

Evaluators for Functional Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  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 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

  5. 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

  6. 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

  7. evaluator structure evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value 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 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

  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)))))) 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

  30. 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

  31. Chapter 4 - Evaluators for Functional Programming

  32. Chapter 4 - Evaluators for Functional Programming

  33. evaluator structure evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value 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. 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))] <== <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

  36. Evaluated values ADTs 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 evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value 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 Chapter 4 - Evaluators for Functional Programming

  44. The global environment Impl. .... 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 evaluator Scheme expression (Global) Environment Code in: Racket-Evaluators\substitution-interpreter\ Value 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

More Related