Case Study: Undefined Variables

Similar documents
Case Study: Free Variables

Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 4.1

ormap, andmap, and filter

More About Recursive Data Types

Rewriting your function using map and foldr

CS 5010 Program Design Paradigms Lesson 6.1

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Intro. Scheme Basics. scm> 5 5. scm>

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

Halting Measures and Termination Arguments

Observer Templates. CS 5010 Program Design Paradigms Lesson 1.4

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

The Data Design Recipe

How to Design Programs

From Templates to Folds

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Module 8: Local and functional abstraction

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

CS115 - Module 10 - General Trees

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 6.5

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Doing it in Java. CS 5010 Program Design Paradigms Bootcamp Lesson 5.4

Solving Your Problem by Generalization

Trees. CS 5010 Program Design Paradigms Lesson 6.2

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

CSCC24 Functional Programming Scheme Part 2

Contracts, Purpose Statements, Examples and Tests

CS 360 Programming Languages Interpreters

SCHEME AND CALCULATOR 5b

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Project 1: Scheme Pretty-Printer

6.001 Notes: Section 8.1

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Documentation for LISP in BASIC

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Discussion 12 The MCE (solutions)

Input, output, and sequence

Using Inheritance to Share Implementations

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

Basics of Inheritance

Introduction to Scheme

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Concepts of programming languages

Lab 7: OCaml 12:00 PM, Oct 22, 2017

CSE 12 Abstract Syntax Trees

CS 314 Principles of Programming Languages

CS 135 Fall 2018 Final Exam Review. CS 135 Fall 2018 Final Exam Review 1

CS115 - Module 9 - filter, map, and friends

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS558 Programming Languages

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Comp 311: Sample Midterm Examination

Functional Programming. Pure Functional Languages

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Principles of Programming Languages

CS1102: Adding Error Checking to Macros

Repetition Through Recursion

Functional Programming. Pure Functional Languages

1. For every evaluation of a variable var, the variable is bound.

Lisp. Versions of LISP

6.001 Notes: Section 15.1

Module 5: Lists. Readings: HtDP, Sections 9, 10.

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

Typed Racket: Racket with Static Types

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Programming Languages: Application and Interpretation

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Graphs. Directed graphs. Readings: Section 28

Functional Programming Lecture 1: Introduction

MetacircularScheme! (a variant of lisp)

Binary Search. CS 5010 Program Design Paradigms Bootcamp Lesson 8.2

CS558 Programming Languages

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Working with images and scenes

CS115 - Module 4 - Compound data: structures

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

An Explicit-Continuation Metacircular Evaluator

CS558 Programming Languages

Graphs. Readings: Section 28. CS 135 Fall : Graphs 1

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Motivation was to facilitate development of systems software, especially OS development.

Transcription:

Case Study: Undefined Variables CS 5010 Program Design Paradigms Bootcamp Lesson 7.4 Mitchell Wand, 2012-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1

Learning Objectives At the end of this lesson the student should be able to: explain the how defined and undefined variables work in our GarterSnake minilanguage identify the undefined variables in a GarterSnake program construct a data representation for a program in GarterSnake or a similar language explain an algorithm for finding undefined variables in a GarterSnake program understand how the algorithm follows the structure of the data representation write similar algorithms for manipulating programs in GarterSnake or a similar simple programming language. 2

A Tiny Programming Language: GarterSnake We are writing a compiler for a tiny language, called GarterSnake. We want to write a program that checks a GarterSnake program for undefined variables. Let's describe the GarterSnake language: 3

The GarterSnake programming language: Programs A Program is a sequence of function definitions. The function defined in each definition is available for use in all of the following definitions. 4

Example: A GarterSnake program def f1(x):f1(x) ; f1 is available in the body of f1 def f2 (x, y):f1(y) ; f1 is available in the body of f2 ; spaces are ignored def f3 (x,z): f1(f2(z,f1)) ; f1 and f2 are available in the body of f3 ; you can pass a function as an argument def f4 (x, z):x(z,z) ; you can call an argument as a function 5

GarterSnake Definitions A Definition looks like def f(x1,..,xn):exp This defines a function named f with arguments x1, x2, etc., and body exp. The arguments of the function are available in the body of the function. The function f itself is also available in the body of the function. It is legal for a function to take no arguments. 6

GarterSnake Expressions An Expression is either a variable v or a function call f(e1,..,en). v is a reference to the variable or function named v. f(e1,e2,...) is an application of f to the arguments e1, e2, etc. It is legal for a function to be applied to no arguments. There is no distinction between function names and argument names: You can pass a function as an argument, You can call an argument as a function. You can return a function as the value of a function call. 7

The Problem: Undefined variables An occurrence of a variable is undefined if it is in a place where the variable is not available. Examples: I purposely called this f7 to demonstrate that the names of the variables don't matter; it's just their position def f7(x): f2(x) ; f2 is undefined in the body of f7 def f2(x,y): f3(y,x) ; f3 is undefined in the body of f2 def f3(x,z):f7(f2(z,y),z) ; y is undefined in the body of f3 8

The Requirements Given a GarterSnake program p, determine whether there are any undefined variables in p. ;; program-all-defined? ;; : Program -> Bool ;; GIVEN: A GarterSnake program p ;; RETURNS: true iff every variable ;; occurring in p is available at the ;; place it occurs. 9

Data Definitions We want to represent only as much information as we need to do the task. So we don t need to worry about spaces, details of syntax, etc. We just need to represent the structure of the programs. All the clues are already in the definitions 10

Data Definitions: Programs We said: A Program is a sequence of function definitions. So we write a corresponding data definition: ;; A Program is represented as a DefinitionList 11

Data Definition: Definitions We wrote: A Definition looks like def f(x1,..,xn):exp So we write a data definition: ;; A Definition is a represented as a struct ;; (make-def name args body) ;; INTERPRETATION: ;; name : Variable is the name of the function being defined ;; args : VariableList is the list of arguments of the function ;; body : Exp is the body of the function. ;; IMPLEMENTATION: (define-struct def (name args body)) ;; CONSTRUCTOR TEMPLATE ;; (make-def Variable VariableList Exp) 12

Data Definition: Expressions We wrote: an Expression is either a variable v or a function call f(e1,..,en). So we write a data definition ;; An Exp is represented as one of the following structs: ;; -- (make-varexp name) ;; -- (make-appexp fn args) ;; INTERPRETATION ;; (make-varexp v) represents a use of the variable v ;; (make-appexp f (list e1... en)) represents a call to the function ;; named f, with arguments e1,..,en ;; CONSTRUCTOR TEMPLATES ;; -- (make-varexp Variable) ;; -- (make-appexp Variable ExpList) ;; IMPLEMENTATION (define-struct varexp (name)) (define-struct appexp (fn args)) 13

Data Definition: Variables We never said anything about what is or isn t a legal variable name. Based on the examples, we ll choose to represent them as Racket symbols. We could have made other choices. Data Definition: ;; A Variable is represented as a Symbol 14

Global View of the GarterSnake representation Program means contains or may contain DefinitionList A DefinitionLists may contain a Definition and a DefinitionLists Definition Variable VariableList Expression Variable Application A ExpListression may contain a Expression and a ExpListressions Variable ExpList 15

Observer Templates ;; pgm-fn : Program ->?? #; (define (pgm-fn p) (deflist-fn p)) ;; def-fn : Definition ->?? #; (define (def-fn d) (... (def-name d) (def-args d) (def-body d))) In Racket, #; marks the next S- expression as a comment. So this definition is actually a comment. This is handy for templates. ;; exp-fn : Exp ->?? #; (define (exp-fn e) (cond [(varexp? e) (... (varexp-name e))] [(appexp? e) (... (appexp-fn e) (explist-fn (appexp-args e)))])) ;; We omit the ListOf-* templates because they are standard and you should know ;; them by heart already. 16

Sidebar: Data Design in Racket We ve chosen to represent GarterSnake programs as recursive structures. This is sometimes called abstract syntax because it abstracts away all the syntactic details of the programs we are manipulating. Recursive structures are our first-choice representation for information in Racket. We would use a similar representation in Java, as we did in 05-4-javatrees.java You will almost never go wrong choosing that representation. 17

Sidebar: Symbols and Quotation Our data design uses symbols. A Symbol is a primitive data type in Racket. It looks like a variable. To introduce a symbol in a piece of code, we precede it with a quote mark. For example, 'z is a Racket expression whose value is the symbol z. 18

Sidebar: Quotation (2) You can also use a quote in front of a list. Quotation tells Racket that the thing that follows it is a constant whose value is a symbol or a list. Thus Thus (a b c) and (list a b c) are both Racket expressions that denote a list whose elements are the symbols a, b, and c. On the other hand, (a b c) is a Racket expression that denotes the application of the function named a to the values of the variables b and c. This is all you need to know about symbols and quotation for right now. There is lots more detail in HtDP/2e, in the Intermezzo entitled Quote, Unquote. But that chapter covers way more than you need for this course. 19

Data Design: Example EXAMPLE: def f1(x):f1(x) def f2(x,y):f1(y) def f3(x,y,z):f1(f2(z,y),z) is represented by (list (make-def 'f1 (list 'x) (make-appexp 'f1 (list (make-varexp 'x)))) Now that we ve briefly explained about symbols and quotation, we can give an example of the representation of a GarterSnake program (make-def 'f2 (list 'x 'y) (make-appexp 'f1 (list (make-varexp 'y)))) (make-def 'f3 (list 'x 'y 'z) (make-appexp 'f1 (list (make-appexp 'f2 (list (make-varexp 'z) (make-varexp 'y))) (make-varexp 'z))))))) 20

System Design (1) ;; We'll need to recur on the list structure of programs. When we ;; analyze a definition, what information do we need to carry forward? ;; Let's look at an example. We'll annotate each definition with a ;; list of the variables available in its body. # def f1(x):f1(x) ; f1 and x are available in the body. def f2(u,y):f1(y) ; f1, f2, u, and y, are available in the body. def f3(x,z):f1(f2(z,f1)) ; f1, f2, f3, x, and z are available in the body. def f4(x,z):x(z,z) ; f1, f2, f3, f4, x, and z are available in the body. # ;; In each case, the variables available in the body are the names of ;; the functions defined _before_ the current function, plus the names ;; of the current function and its arguments. 21

System Design (2) ;; Let's look at the "middle" of the calculation. ;; When we analyze the definition of f3, we need to know that f1 and ;; f2 are defined. When we analyze the body of f3, we need to know ;; that f1, f2, x, and z are defined. ;; So we generalize our functions to take a second argument, which is ;; the set of defined variables. ;; We'll have a family of functions that follow the data definitions; ;; program-all-defined : Program -> Boolean ;; deflist-all-defined?: DefinitionList SetOfVariable -> Boolean ;; def-all-defined? : Definition SetOfVariable -> Boolean ;; exp-all-defined? : Exp SetOfVariable -> Boolean 22

deflist-all-defined? ;; deflist-all-defined? : DefinitionList SetOfVariable -> Boolean ;; GIVEN: a list of definitions 'defs' from some program p and a set of ;; variables 'vars' ;; WHERE: vars is the set of variables available at the start of defs in ;; p. ;; RETURNS: true iff there are no undefined variables in defs. ;; EXAMPLES: See examples above (slide 8) ;; STRATEGY: Use template for DefinitionList on defs. The names ;; available in (rest defs) are those in vars, plus the variable ;; defined in (first defs). (define (deflist-all-defined? defs vars) (cond [(null? defs) true] [else (and (def-all-defined? (first defs) vars) (deflist-all-defined? (rest defs) (set-cons (def-name (first defs)) vars)))])) You can t tell if a variable is undefined unless you know something about the program it occurs in! The WHERE invariant captures this information. Don t say see examples above or see tests below unless there really are such examples or tests. 23

def-all-defined? ;; def-all-defined? : Definition SetOfVariable -> Boolean ;; GIVEN: A definition 'def' from some program p and a set of ;; variables 'vars' ;; WHERE: vars is the set of variables available at the start of def in ;; p. ;; RETURNS: true if there are no undefined variables in the body of ;; def. The available variables in the body are the ones in def, plus ;; the name and arguments of the definition. ;; EXAMPLES: See examples above (slide 8) ;; STRATEGY: Use template for Definition on def (define (def-all-defined? def vars) (exp-all-defined? (def-body def) (set-cons (def-name def) (set-union (def-args def) vars)))) 24

exp-all-defined? ;; exp-all-defined? : Exp SetOfVariable -> Boolean ;; GIVEN: A GarterSnake expression e, occurring in some program ;; p, and a set of variables vars ;; WHERE: vars is the set of variables that are available at the ;; occurrence of e in p ;; RETURNS: true iff all the variable in e are defined ;; STRATEGY: Use template for Exp on e (define (exp-all-defined? e vars) (cond [(varexp? e) (my-member? (varexp-name e) vars)] [(appexp? e) (and (my-member? (appexp-fn e) vars) (andmap (lambda (e1) (exp-all-defined? e1 vars)) (appexp-args e)))])) 25

program-all-defined? ;; And finally, we can write program-all-defined?, which ;; initializes the invariant information for the other ;; functions. ;; program-all-defined? : Program -> Bool ;; GIVEN: A GarterSnake program p ;; RETURNS: true iff there every variable occurring in p ;; is defined at the place it occurs. ;; STRATEGY: Initialize the invariant of deflist-all-defined? (define (program-all-defined? p) (deflist-all-defined? p empty)) It would be ok to write call a more general function here, but this is more informative. 26

Call Graph for this Program deflist-alldefined? program-alldefined? means calls or may call exp-alldefined? Variable def-all-defined VariableList variable case application case is function name defined? andmap exp-all-defined? on arguments We used an andmap instead of defining ExpList-all-defined? 27

See how the call graph follows the structure of the data! Program means contains or may contain DefinitionList A DefinitionLists may contain a Definition and a DefinitionLists Definition Variable VariableList Expression Variable Application A ExpListression may contain a Expression and a ExpListressions Variable ExpListression 28

Summary At the end of this lesson the student should be able to: explain how defined and undefined variables work in our GarterSnake minilanguage identify the undefined variables in a GarterSnake program construct a data representation for a program in GarterSnake or a similar language explain an algorithm for finding undefined variables in a GarterSnake program understand how the algorithm follows the structure of the data representation write similar algorithms for manipulating programs GarterSnake or a similar simple programming language. 29

Next Steps Study Examples/07-3-gartersnake.rkt If you have questions about this lesson, ask them on the Discussion Board Do Guided Practices 7.3 and 7.4 Go on to the next lesson 30