Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.

Slides:



Advertisements
Similar presentations
When do I need an invariant? CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Advertisements

Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.1 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Foldr and Foldl CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 © Mitchell Wand, This work is licensed under a Creative.
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Lists vs. Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative Commons.
Organization of This Course CS 5010 Program Design Paradigms “Bootcamp” Lesson 0.2 © Mitchell Wand, This work is licensed under a Creative Commons.
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
More Recursive Data Types
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
CS 5010 Program Design Paradigms "Bootcamp" Lesson 9.3
Case Study: Undefined Variables
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
Rewriting your function using map and foldr
Introduction to Universe Programs
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
Presentation transcript:

Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 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 notion of a free variable – identify the free variables in the expressions of a simple programming language – explain two algorithms for finding the free variables of an expression in a simple programming language 2

A Tiny Programming Language: Fred The Information: FredExp = Variable | (lambda (Variable) FredExp) | (FredExp FredExp) Variable = x | y | z |... | xx | yy | zz |... The setting is that we are writing a compiler for a tiny programming languages, called Fred. Here is some information about expressions in Fred. A Fred-expression is either a variable, or a lambda expression, or an application. We've written down some suggestive notation here, but we're not specifying exactly how these expressions are going to be written down; we're only saying what kind of expressions there are and what they might (repeat, might) look like. 3

The Problem: Free-Vars A variable is free if it occurs in a place that is not inside a lambda with the same name. free-vars: FredExp -> SetOf examples (in terms of information, not data): (free-vars x) => (list x) (free-vars (lambda (x) x)) => empty (free-vars (lambda (x) (x y))) => (list y) (free-vars (z (lambda (x) (x y)))) => (list z y) {(list y z) would be ok} (free-vars (x (lambda (x) (x y))) => (list x y) {(list y x) would be ok} For clarity, we've written the examples in terms of our hypothetical notation for FredExps. So we wouldn't write (free-vars (lambda (x) x)) Instead, we would write (free-vars <some Racket code that constructs a representation of the Fred-expression (lambda (x) x)>) 4

Data Design (define-struct var (name)) (define-struct lam (var body)) (define-struct app (fn arg)) ;; A FredExp is one of ;; (make-var Symbol) ;; (make-lam Symbol FredExp) ;; (make-app FredExp FredExp) ;; interpretation: the cases represent ;; variables, lambdas, and applications, ;; repectively. We will represent FredExps as recursive structures. This is our first- choice representation for information in Racket— you can almost never go wrong choosing that representation. 5

Template ;; fredexp-fn : FredExp -> ? #; (define (fredexp-fn f) (cond [(var? f) (... (var-name f))] [(lam? f) (... (lam-var f) (fredexp-fn (lam-body f)))] [(app? f) (... (fredexp-fn (app-fn f)) (fredexp-fn (app-arg f)))])) In Racket, #; marks the next S-expression as a comment. So this definition is actually a comment. This is handy for templates. 6

Contract & purpose statement ;; free-vars : FredExp -> SetOfSymbol ;; Produces the set of names that occur free in the given FredExp ;; EXAMPLE: ;; (free-vars (z (lambda (x) (x y)))) = {y, z} ;; strategy: data decomposition We will represent sets as lists without duplication. 7

(define (free-vars f) (cond [(var? f) (list (var-name f))] [(lam? f) (set-minus (free-vars (lam-body f)) (lam-var f))] [(app? f) (set-union (free-vars (app-fn f)) (free-vars (app-arg f)))])) Function Definition We can write free-vars as a straightforward structural decomposition, using set operations. At each lam, we find all the variables in the body, and then remove the lambda-variable from that set We use set-union, rather than append or something like it, because we are supposed to return a set. 8 If we represent sets as lists, removing the lambda-variable will require us to scan through the whole list at every lambda and copy some or all of the list. This is likely to be an expensive operation. Can we keep track of contexts to avoid this?

What do we lose as we descend into the structure? We lose information about which lambda- variables are above us. Fixed this up "on the way back up" with set- minus. Alternative: use an accumulator to keep track of the lambda-variables above us – when we hit a variable, see if it's already in this list. If so, it's not free in the whole expression. – like the counter in mark-depth – taking care of it "on the way down" 9

With context variable ;; free-vars/a ;; : FredExp ListOfSymbol -> SetOfSymbol ;; GIVEN: a FredExp f that is part of a larger ;; FredExp f0 ;; WHERE: los is the list of symbols that occur in ;; lambdas above f in f0 ;; RETURNS: the set of symbols from f that are free ;; in f0. ;; EXAMPLE: ;; (free-vars/a ;; (z (lambda (x) (x y))) (list z)) ;; = (list y) The invariant (WHERE clause) gives an interpretation for the context variable 10

Function Definition ;; STRATEGY: data decomp on f : FredExp (define (free-vars/a f los) (cond [(var? f) (if (my-member? (var-name f) los) empty (list (var-name f)))] [(lam? f) (free-vars/a (lam-body f) (cons (lam-var f) los))] [(app? f) (set-union (free-vars/a (app-fn f) los) (free-vars/a (app-arg f) los))])) Adds the lambda-variable to the list of bound variables in the body, so the called function's WHERE clause will become true. 11 Is the variable already bound? This accumulator function should be local to free-vars

Function Definition (part 2) ;; free-vars : FredExp -> SetOfSymbol ;; Produces the set of names that occur free in the given FredExp ;; EXAMPLE: ;; (free-vars (z (lambda (x) (x y)))) ;; = {y, z} ;; Strategy: function composition (define (free-vars f0) (free-vars/a f0 empty)) There are no variables bound above the top. 12

How to choose? Both definitions are clear Which performs better? 13

Performance Sizeno context argwith context arg , , ,621, Run time, in msec, vs. number of nodes We threw together a little function that would generate fred-expressions of increasing size and measured the results. We can see clearly that for large fred-expressions, the accumulator version is much faster. This is a toy example, so don't draw any general conclusions. 14

Summary You should now be able to: – explain the notion of a free variable – identify the free variables in the expressions of a simple programming language – explain two algorithms for finding the free variables of an expression in a simple programming language 15