CSCI: 4500/6500 Programming Languages Back to the Basics: Steps in Inventing a Language Programming an Evaluator Definition

CSCI: 4500/6500 Programming Languages Back to the Basics: Steps in Inventing a Language Programming an Evaluator Definition

CSCI: 4500/6500 Programming Languages Functional Programming Languages Part 3: Evaluation and Application Cycle Lazy Evaluation LuisGuillermo.com Maria Hybinette, UGA 1 Maria Hybinette, UGA 2 Back to the Basics: Steps in Inventing a Language ! Design the grammar ! Meta circular evaluaturs » What strings are in the language? ! Evaluate & Apply » Use BNF to describe all the strings in the language ! Lazy and Aggressive Evaluation ! Make up the evaluation rules » Describe what everything the grammar can produce means ! Build an evaluator » A procedure that evaluates expressions in the language – The evaluator: ! determines the meaning of expressions in the programming language, is just another program. Maria Hybinette, UGA 3 Maria Hybinette, UGA 4 Programming an Evaluator Definition: A Metacircular Evaluator ! An evaluator that is written in the same ! If a language is just a program, what language language that it evaluates is said to be should we program the language (evaluator) metacircular Sounds like recursion: It's circular recursion. There is no in? termination condition. It's a chicken-and-the-egg kind of thing. (There's actually a hidden termination condition: the bootstrapping process.) ! One more requirement: The language interpreted does not need additional definitions of semantics other than that is defined for the evaluator (sounds circular). » Example: – The C compiler is written is C but is not meta circular because the compiler specifies extremely detailed and precise semantics for each and every construct that it Maria Hybinette, UGA 5 Maria Hybinette, UGA interprets. 6 Example: Procedural Building Evaluation Basics Blocks Observation: This is recursive ( define (square x) (* x x) )! To evaluate a combination: » ( square (+ 2 5) ) ! 49! ! Evaluate each element (all the ( define (sum-of-squares x y) !; use square for! subexpressions) of the combination (+ (square x) (square y) ) !; x2 + y2! » ( sum-of-squares 3 4) ! 25! ! Apply the procedure to the value of the left- ( define (f a)! most subexpression (the operator) to the ( sum-of-squares (+ a 1) (* a 2)))! arguments that are the values of the other values of the » (f 5) ! 136 ! subexpressions (the operands) 390! operands percolate upward 26! 15! ! square - is a compound procedure which is given the name square which is represents the operation of multiplying something by itself. Evaluation rule is applied on 4 * ! Evaluating the definition creates the compound procedure and associates it combinations:! 24! with the name square (lookup) + 2 + 3 5 7 (* (+ 2 (* 4 6))! ! Application: To apply a compound procedure to arguments, evaluate the body (+ 3 5 7) )! of the procedure with each formal parameter replaced by the ‘real’ arguments. (substitution model -- an assignment model <-variable<-env ) Maria Hybinette, UGA * 4 6 7 Maria Hybinette, UGA 8 Environmental Model of Evaluation Core of the Evaluator 1. To evaluate a combination (compound expression) ! Basic cycle in which • evaluate all the subexpressions and then » expressions to be evaluated in environments are • apply the value of the operator subexpression (first » reduced to procedures to be applied to arguments, expression) to the values of the operand subexpressions ! Which in turn are reduced to new expressions (other expressions). » to be evaluated in new environments, and so on, 2. To apply a procedure to a list of arguments, » until we get down to • evaluate the body of the procedure in a new environment – symbols, whose values are looked up in the environment (by a frame) that binds the formal parameters of the procedure to the arguments to which the procedure is – primitive procedures, which are applied directly. applied to. &'(#% &'(#% procedure, procedure, expression, expression, arguments arguments environment environment !""#$% !""#$% Maria Hybinette, UGA 9 Maria Hybinette, UGA 10 The evaluator - metacircularity ( eval expression environment ) Eval ! Evaluates the the expression relative to the environment (define (eval exp env)! » Examples: environments (returns a specifies for the environment) (cond ((self-evaluating? exp) exp)! – scheme-report-environment version ((variable? exp) (lookup-variable-value exp env))! ((quoted? exp) (text-of-quotation exp))! – null-environment version ((assignment? exp) (eval-assignment exp env))! ! Primitives: ((definition? exp) (eval-definition exp env))! » self-evaluating expressions, such as numbers, eval returns the ((if? exp) (eval-if exp env))! expression itself ((lambda? exp)! (make-procedure (lambda-parameters exp)! » variables, looks up variables in the environment (lambda-body exp)! ! Some special forms (lambda, if, define etc). eval provide direct env))! implementation: ((begin? exp) ! (eval-sequence (begin-actions exp) env))! » Example: quoted: returns expression that was quoted ((cond? exp) (eval (cond->if exp) env))! ! Others lists: ((application? exp)! (apply (eval (operator exp) env)! » eval calls itself recursively on each element and then calls apply, (list-of-values (operands exp) env)))! passing as argument the value of the first element (which must be a (else! function) and a list of the remaining elements. Finally, eval returns what (error "Unknown expression type - EVAL" exp))) apply returned Maria Hybinette, UGA 11 Maria Hybinette, UGA 12 Eval: Example apply (eval ‘( * 7 3 ) (scheme-report-environment 5)) ! ! !=> 21! ! apply applies its first argument (a function) and applies it to its second argument (a list) (eval (cons '* (list 7 3)) (scheme-report-environment 5)) ! ( apply max '(3 7 2 9) ) => 9 ! !=> 21! ! Primitive function, apply invokes the actual function. ! Non-primitive function ( f ), » Retrieves the referencing environment in which the function’s lambda expression was originally evaluated and Current Scheme doesn’t recognize ‘scheme-report-environment ’ adds the names of the function’s parameters (the list) (call this resulting environment (e) ) » Retrieves the list of expressions that make up the body of f. » Passes the body’s expression together with e one at a time to eval. Finally, apply returns what the eval of the last expression in the body of f returned. Maria Hybinette, UGA 13 Maria Hybinette, UGA 14 Apply Example: Evaluating ( cadr p ) (define (apply procedure arguments)! ! ( define cadr ( lambda (x) ( car ( cdr x) ) ) ) (cond ((primitive-procedure? procedure)! ! Stored Internally as three element list C: ( E (x) ( car ( cdr (x) ) ) ) (apply-primitive-procedure procedure arguments))! ((compound-procedure? procedure)! – surrounding referencing environment (global) (eval-sequence! – list of parameters (x) (procedure-body procedure)! – list of body expressions (one element: ( car ( cdr x) ) ) (extend-environment! ! Suppose: p is defined to be a list: ( define p ‘(a b) ) (procedure-parameters procedure)! » (cadr p) => b arguments! (procedure-environment procedure))))! ! Evaluating ( cadr p ) scheme interpreter executes: (else! » ( eval ‘(cadr p) (scheme-report-environment 5) ) (error! – Note: assumes p is defined in scheme-report-environment 5 "Unknown procedure type - APPLY" procedure))))! 1. Evaluate the car of it’s car of the first argument, » cadr via a recursive call returns function c to which cadr is bound, represented internally as a three element list C. 2. Eval calls itself recursively on ‘p’ returning (a, b) 3. Execute (apply c ‘(a b)) and return results Maria Hybinette, UGA 15 Maria Hybinette, UGA 16 &'(#% Example: Evaluating ( cadr p ) Summary of Scheme !""#$% ! ( define cadr ( lambda (x) ( car ( cdr x) ) ) ) ! The core of a Scheme evaluator is eval and ! Suppose: p is defined to be a list: ( define p ‘(a b) ) apply, procedures that are defined in terms ! Evaluating ( cadr p ) scheme interpreter executes: 1. ( eval ‘(cadr p) (scheme-report-environment 5) ) of each other. – Note: assumes p is defined in scheme-report-environment 5 » The eval procedure takes an expression and an 2. Evaluate the car of it’s car of the first argument, environment and evaluates to the value of the » cadr via a recursive call returns function c to which cadr is bound, represented internally as a three element list C. expression in the environment; 3. Eval calls itself recursively on ‘p’ returning (a, b) » The apply procedure takes a procedure and its 4. Execute (apply c ‘(a b)) and return results operands and evaluates to the value of applying the 5. Apply then notice the internal list representation cadr, C. ( E (x) ( car ( cdr (x) ) )) and then apply would execute: procedure to its operands. 6. ( eval ‘(car ( cdr (x))) ( cons (cons ‘x ‘(a b)) E )) and return the results Maria Hybinette, UGA 17 Maria Hybinette, UGA 18 Evaluation Order Example ! Scheme uses applicative order evaluation (as most imperative languages, sometimes called ! (define double (lambda (x) (+ x x))) eager or aggressive evaluation) ! Eager evaluation of ( double (* 3 4) ) ( double 12 ) » Evaluate function arguments before passing them ! to functions ! (+ 12 12) ! 24 Maria Hybinette, UGA 19 Maria Hybinette, UGA 20 Evaluation Order Lazy Evaluation ! Scheme uses applicative order evaluation (as most imperative languages, sometimes called ! Don’t evaluate expressions until their value is eager or aggressive evaluation) really needed. » Evaluate function arguments before passing them » We might save work this way! to functions » We might change the meaning of some ! We can change the evaluator to evaluate expressions, since the order of evaluation matters applications “lazily” instead, by only evaluating the value of an operand when it is needed (also called

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us