csci 4500 6500 programming languages
play

CSCI: 4500/6500 Programming Languages Functional Programming - PDF document

CSCI: 4500/6500 Programming Languages Functional Programming Languages Part 3: Evaluation and Application Cycle Lazy Evaluation LuisGuillermo.com 1 2 Maria Hybinette, UGA Maria Hybinette, UGA Back to the Basics: Steps in Inventing a


  1. CSCI: 4500/6500 Programming Languages Functional Programming Languages Part 3: Evaluation and Application Cycle Lazy Evaluation LuisGuillermo.com 1 2 Maria Hybinette, UGA Maria Hybinette, UGA 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 ! Make up the evaluation rules ! Lazy and Aggressive Evaluation » 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. 3 4 Maria Hybinette, UGA Maria Hybinette, UGA Definition: A Metacircular Evaluator Programming an Evaluator ! An evaluator that is written in the same language that it evaluates is said to be ! If a language is just a program, what language metacircular should we program the language (evaluator) in? Sounds like recursion: It's circular recursion. There is no 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 interprets. 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. 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 � ; x 2 + y 2 � (+ (square x) (square y) ) » ( 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 ! square - is a compound procedure which is given the name square which is 26 ! 15 ! 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 ) * 4 6 7 8 Maria Hybinette, UGA Maria Hybinette, UGA 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 – primitive procedures, which are applied directly. procedure to the arguments to which the procedure is applied to. &'(#% &'(#% procedure, expression, procedure, arguments expression, environment arguments !""#$% environment !""#$% 9 10 Maria Hybinette, UGA Maria Hybinette, UGA The evaluator - metacircularity Eval ( eval expression environment ) ! 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) � ((variable? exp) (lookup-variable-value exp env)) � – scheme-report-environment version ((quoted? exp) (text-of-quotation exp)) � – null-environment version ((assignment? exp) (eval-assignment exp env)) � ! Primitives: ((definition? exp) (eval-definition exp env)) � ((if? exp) (eval-if exp env)) � » self-evaluating expressions, such as numbers, eval returns the ((lambda? exp) � expression itself (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 � (error "Unknown expression type - EVAL" exp))) function) and a list of the remaining elements. Finally, eval returns what apply returned 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. Eval: Example apply (eval ‘ ( * 7 3 ) (scheme-report-environment 5)) � ! apply applies its first argument (a function) and applies it to its � � => 21 � 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. 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Apply Example: Evaluating ( cadr p ) ( define cadr ( lambda (x) ( car ( cdr x) ) ) ) (define (apply procedure arguments) � ! (cond ((primitive-procedure? procedure) � Stored Internally as three element list C: ( E (x) ( car ( cdr (x) ) ) ) ! (apply-primitive-procedure procedure arguments)) � – surrounding referencing environment (global) ((compound-procedure? procedure) � – list of parameters (x) (eval-sequence � – list of body expressions (one element: ( car ( cdr x) ) ) (procedure-body procedure) � Suppose: p is defined to be a list: ( define p ‘ (a b) ) (extend-environment � ! (procedure-parameters procedure) � » (cadr p) => b arguments � Evaluating ( cadr p ) scheme interpreter executes: ! (procedure-environment procedure)))) � » ( eval ‘ (cadr p) (scheme-report-environment 5) ) (else � – Note: assumes p is defined in scheme-report-environment 5 (error � "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 15 16 Maria Hybinette, UGA Maria Hybinette, UGA &'(#% 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: ! of each other. 1. ( eval ‘ (cadr p) (scheme-report-environment 5) ) – 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, expression in the environment; represented internally as a three element list C. 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. procedure to its operands. ( E (x) ( car ( cdr (x) ) )) and then apply would execute: 6. ( eval ‘ (car ( cdr (x))) ( cons (cons ‘ x ‘ (a b)) E )) and return the results 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

Recommend


More recommend