Procedures App B: SLLGEN 1.

Slides:



Advertisements
Similar presentations
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Variables, Environments and Closures. Overview We will Touch on the notions of variable extent and scope Introduce the notions of lexical scope and.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
A Simple Syntax-Directed Translator
6.001 SICP Variations on a Scheme
The interpreter.
Introduction to Scheme
Variables, Environments and Closures
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
The Environment Model*
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Organization of Programming Languages
Corky Cartwright January 18, 2017
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
Semantics of PLs via Interpreters: Getting Started
Variables, Environments and Closures
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Explicit-control evaluator
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
Classes, Objects and Methods
Assignments and Procs w/Params
CSE 341 Lecture 11 b closures; scoping rules
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Principles of Programming Languages
Principles of Programming Languages
Corresponds with Chapter 5
Presentation transcript:

Procedures App B: SLLGEN 1

The PROC language Expression ::= proc (Identifier) Expression AST: proc-exp (var body) Expression ::= (Expression Expression) AST: call-exp (rator rand) PROC includes all of LET language Anonymous procedure One parameter always one arg only CS7100(pm) 2 2

Semantics of Procedures (This slide is for procedures in general.) Procedure Definition Store formal parameters and body Procedure Invocation Evaluate body in an environment that binds formals to actual argument values Interpretation of free-variables: Two methods Use env at proc definition (lexical/static scoping) Use env at proc call (dynamic scoping) Overloading : Static binding – Dynamic binding :: association of code to function call (cf. deep and shallow binding in the realm of dynamic scoping) (cf. static typing vs dynamic typing) CS7100(pm) 3 3

Scoping and Binding references declarations lexical scoping rules (f x y) f, x, and y declarations (lambda (x) (+ x 3)) (let ((x (+ y 7))) (+ x 3)) y, and second/right x are refs first/left x is a declaration lexical scoping rules CS7100(pm) 4 4

Kinds of Scope Static or Lexical scope Dynamic scope Global Scope determined by structure of program Scheme, C++, Java, and many compiled languages Dynamic scope determined by path of execution Lisp dialects, Perl, and many interpreted languages Global Scope File scope Local Scope Block Body of a procedure Body of a loop Scope alters the meaning CS7100(pm) 5 5

Example-1 of PROC let f = proc (x) --(x,11) in (f (f 77)) Defines an anonymous procedure with one formal parameter named x. Body of the procedure: --(x,11) Binds the name f to this procedure. Invokes f with actual argument 77. Invokes f again with the result of above. (will use two -- just for visibility) CS7100(pm) 6 6

Example-2 of PROC (proc (f) (f (f 77)) proc (x) --(x,11)) This example is derived from the production Expression ::= (Expression Expression) so is (f (f 77)) so is (f 77) proc (f) (f (f 77)) is the rator. It defines an anonymous procedure with one formal parameter named f. proc (x) --(x,11)) is the rand. It also defines an anonymous procedure with one formal parameter named x. CS7100(pm) 7 7

Example-3 of PROC in --((f 1), (g 1)) Illustrates scope issues x and z appear four times each. Lexical scoping In --((f 1), (g 1)), the bodies of f and g must be evaluated in the env they were defined. In f, x is bound to 200 In g, x is bound to 100 CS7100(pm) 8 8

Example Programs of PROC Example-1 and -2 produce same result, but different mechanisms. Previous two slides gave semantics informally Watch out: a very seductive approach Next few slides: interpreter based CS7100(pm) 9 9

Example Calc w/ Spec 1 CS7100(pm) 10 10

Example Calc w/ Spec 2 CS7100(pm) 11 11

Example Calc w/ Spec 3 CS7100(pm) 12 12

Example Calc w/ Spec 4 CS7100(pm) 13 13

Recall value-of value-of is an operator with two operands an AST an environment (value-of ast env) PROC = LET + two more productions Bring in all value-of specs from LET Additions are shown on next few slides … CS7100(pm) 14 14

additional value-of specs (value-of (proc-exp var body) ρ) = (proc-val (procedure var body ρ)) (value-of (call-exp rator rand) ρ) ( (proc (expval->proc (value-of rator ρ))) (arg (value-of rand ρ))) (apply-procedure proc arg)) To be defined: proc-val, apply-procedure CS7100(pm) 15 15

Spec of apply-procedure = (value-of body [var=val]ρ ) apply-procedure takes two arguments: an AST of a procedure definition an argument for the parameter of the procedure yields an expressed value CS7100(pm) 16 16

Impl of apply-procedure (define proc? (lambda (pc) (procedure? pc))) (define procedure (lambda (var body env) (lambda (val) (value-of body (extend-env var val env))))) (define apply-procedure (lambda (pc val) (pc val))) procedure?  provided from r5rs Names being bound: proc? procedure apply-procedure env is an environment ASTs: body, pc, val, var Use of procedure? is too liberal. procedure is not self-contained; takes three arguments: param name var body AST environment CS7100(pm) 17 17

Alternate impl called Closures (define-datatype proc proc? (procedure (var identifier?) (body expression?) (saved-env environment?))) (define apply-procedure (lambda (pc val) (cases proc pc (var body saved-env) var val saved-env)))))) Defining a new data type called “proc” Has only one variant procedure That has three parts var which must be an id body an expression saved-env an environment apply-procedure takes pc and val. “cases proc pc” pc is expected to be of type proc code for each variant of proc only one variant “procedure” here These data structures are often called closures, because they are self- contained: they contain everything the procedure needs in order to be applied. We sometimes say the procedure is closed over or closed in its creation environment. CS7100(pm) 18 18

the data type expval is now … (define-datatype expval expval? (num-val (num number?)) (bool-val (bool boolean?)) (proc-val (proc proc?))) CS7100(pm) 19 19

value-of: two new clauses (proc-exp (var body) (proc-val (procedure var body env))) (call-exp (rator rand) (value-of rator env))) (arg (value-of rand env))) (apply-procedure proc arg))) CS7100(pm) 20 20

Curried procedures In PROC, procedures with multiple arguments can be had as in: in ((f 3) 4) proc (x) … yields a procedure Named after Haskell Brooks Curry (1900 – 1982), a combinatory logician. CS7100(pm) 21 21

chapter3/proc-lang/ Two subdirectories chapter3/proc-lang/proc-rep: procedural implementation chapter3/proc-lang/ds-rep: data type based (i.e., closure) Both directories have the following files data-structures.scm drscheme-init.scm environments.scm interp.scm lang.scm tests.scm top.scm CS7100(pm) 22 22

EOPL3 Appendix B SLLGEN (define scanner-spec-1 ...) (define grammar-1 ...) (sllgen:make-define-datatypes scanner-spec-1 grammar-1) (define list-the-datatypes (lambda () (sllgen:list-define-datatypes scanner-spec-1 grammar-1))) (define just-scan (sllgen:make-string-scanner scanner-spec-1 grammar-1)) (define scan&parse (sllgen:make-string-parser scanner-spec-1 grammar-1)) (define read-eval-print (sllgen:make-rep-loop "--> " value-of--program (sllgen:make-stream-parser scanner-spec-1 grammar-1))) sllgen:make-define-datatypes: generates a define-datatype for each production of the grammar, for use by cases. sllgen:make-string-scanner takes a scanner spec and a grammar and generates a scanning procedure read-eval-print loop CS7100(pm) 23 23

Lexical Analysis skip) (identifier "_" "-" "?"))) symbol) number) )) the-lexical-spec proc-lang/*/lang.scm scanners are specified by reg exp – next slide All our languages use this lexical analysis. CS7100(pm) 24 24

SLLGEN Scanner Spec ({Regexp-and-action}∗) Regexp-and-action ::= (Name ({Regexp}∗) Action) Name ::= Symbol Regexp | digit| whitespace|any | (or {Regexp}∗) ::= (arbno Regexp) | (concat {Regexp}∗) | number | string A scanner specification in SLLGEN is a list that satisfies the grammar at left CS7100(pm) 25 25

The SLLGEN Parsing System (define the-grammar '((program (expression) a-program) (expression (number) const-exp) (expression ("-" "(" expression "," expression ")") diff-exp) ("zero?" "(" expression ")") zero?-exp) ("if" expression "then" expression "else" expression) if-exp) (expression (identifier) var-exp) ("let" identifier "=" expression "in" expression) let-exp) ("proc" "(" identifier ")" expression) proc-exp) ("(" expression expression ")") call-exp) )) the-grammar of PROC proc-lang/*/lang.scm Double-quoted items are terminals/tokens. CS7100(pm) 26 26

Specifying Grammars Grammar ::= ({Production}∗) (Lhs ({Ritem}∗) Prod-name) Lhs ::= Symbol Ritem ::= Symbol | String ::= (arbno {Ritem}∗) ::= (separated-list {Ritem}∗ String) Prod-name ::= Symbol A grammar in SLLGEN is a list described by the grammar at left CS7100(pm) 27 27

HW2 Problem (let saved-env))))))) http://www.cs.wright.edu/~pmateti/Courses/7100/Top/7100-HW2.html In our data-structure representation of procedures, we have kept the entire environment in the closure. But of course all we need are the bindings for the free variables. Modify the representation of procedures to retain only the free variables. flat closure rep shown left consists of exactly one rib of free variables and their values. free-vars: ykwim ;-) set-diff:  difference of two sets map  provided from r5rs CS7100(pm) 28 28