Lecture 13 CIS 341: COMPILERS

Similar documents
Lecture 7: The Untyped Lambda Calculus

Lecture 15 CIS 341: COMPILERS

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

CMSC 350: COMPILER DESIGN

CMSC 330: Organization of Programming Languages

Lecture 9 CIS 341: COMPILERS

CMSC 330: Organization of Programming Languages

A Functional Evaluation Model

Closures & Environments. CS4410: Spring 2013

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Functional Languages. Hwansoo Han

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Formal Systems and their Applications

CS 415 Midterm Exam Spring 2002

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Variables. Substitution

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CMSC330. Objects, Functional Programming, and lambda calculus

Harvard School of Engineering and Applied Sciences Computer Science 152

Lecture 2: Big-Step Semantics

Lambda Calculus. Variables and Functions. cs3723 1

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

The Environment Model. Nate Foster Spring 2018

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Chapter 11 :: Functional Languages

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

CS 242. Fundamentals. Reading: See last slide

CSE505, Fall 2012, Midterm Examination October 30, 2012

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Concepts of Programming Languages

To figure this out we need a more precise understanding of how ML works

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS 360 Programming Languages Interpreters

CMSC 330: Organization of Programming Languages

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Fundamentals and lambda calculus

The Environment Model

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

CS 314 Principles of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Introduction to the Lambda Calculus

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CMSC 330: Organization of Programming Languages

Functional Programming

Programming Language Pragmatics

Lambda the Ultimate. Corky Cartwright Vivek Sarkar Department of Computer Science Rice University

Last class. CS Principles of Programming Languages. Introduction. Outline

CS422 - Programming Language Design

CIS 500 Software Foundations Fall September 25

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Type Systems Winter Semester 2006

Introduction to Lambda Calculus. Lecture 7 CS /08/09

To figure this out we need a more precise understanding of how ML works

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

More Lambda Calculus and Intro to Type Systems

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Functional Programming

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

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

Lecture08: Scope and Lexical Address

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

CSE 505: Concepts of Programming Languages

Programming Languages and Techniques (CIS120)

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Lambda Calculus.

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Programming Systems in Artificial Intelligence Functional Programming

Functional Programming. Pure Functional Languages

Functions & First Class Function Values

dynamically typed dynamically scoped

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Programming Languages

Syntax and Grammars 1 / 21

CSE 341: Programming Languages

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Formal Compiler Implementation in a Logical Framework. Jason Hickey, Aleksey Nogin, Adam Granicz, Brian Aydemir Caltech Computer Science

Metaprogramming assignment 3

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

G Programming Languages - Fall 2012

Programming Languages

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

CS558 Programming Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Environments

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CS558 Programming Languages

4/19/2018. Chapter 11 :: Functional Languages

> module Lambda where > import Data.List -- I need some standard list functions

Transcription:

Lecture 13 CIS 341: COMPILERS

Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th START EARLY! Midterm Exam Grades Available on Gradescope Zdancewic CIS 341: Compilers 2

Midterm Exam Scores.4.2 1 Max: 72 (out of 80) Average: 58 Median: 60 Std.Dev: 9.4 average median.8.6.4.2 0 0 20 40 60 8 Zdancewic CIS 341: Compilers 3

See HW4 OAT V 1.0 Zdancewic CIS 341: Compilers 4

OAT Simple C-like Imperative Language supports 64-bit integers, arrays, strings top-level, mutually recursive procedures scoped local, imperative variables See examples in hw04/atprograms directory How to design/specify such a language? Grammatical constructs Semantic constructs Zdancewic CIS 341: Compilers 5

Compilation in a Nutshell Source Code (Character stream) if (b == 0) { a = 1; } Token stream: Lexical Analysis if ( b == 0 ) { a = 0 ; } Abstract Syntax Tree: Eq If b 0 a 1 Assembly Code l1: cmpq %eax, $0 jeq l2 jmp l3 Assn None Intermediate code: l1: %cnd = icmp eq i64 %b, 0 br i1 %cnd, label %l2, label %l3 l2: store i64* %a, 1 br label %l3 l3: Parsing Analysis & Transformation Backend l2: CIS 341: Compilers 6

Untyped lambda calculus Substitution Evaluation FIRST-CLASS FUNCTIONS Zdancewic CIS 341: Compilers 7

Functional languages Languages like ML, Haskell, Scheme, Python, C#, Java 8, Swift Functions can be passed as arguments (e.g. map or fold) Functions can be returned as values (e.g. compose) Functions nest: inner function can refer to variables bound in the outer function let add = fun x -> fun y -> x + y let inc = add 1 let dec = add -1 let compose = fun f -> fun g -> fun x -> f (g x) let id = compose inc dec How do we implement such functions? in an interpreter? in a compiled language? CIS 341: Compilers 8

(Untyped) Lambda Calculus The lambda calculus is a minimal programming language. Note: we re writing (fun x -> e) lambda-calculus notation: λ x. e It has variables, functions, and function application. That s it! It s Turing Complete. It s the foundation for a lot of research in programming languages. Basis for functional languages like Scheme, ML, Haskell, etc. Abstract syntax in OCaml: type exp = Var of var (* variables *) Fun of var * exp (* functions: fun x -> e *) App of exp * exp (* function application *) Concrete syntax: exp ::= x variables fun x -> exp functions exp 1 exp 2 function application ( exp ) parentheses CIS 341: Compilers 9

Values and Substitution The only values of the lambda calculus are (closed) functions: To substitute a (closed) value v for some variable x in an expression e Replace all free occurrences of x in e by v. In OCaml: written subst v x e In Math: written e{v/x} val ::= fun x -> exp functions are values Function application is interpreted by substitution: (fun x -> fun y -> x + y) 1 = subst 1 x (fun y -> x + y) = (fun y -> 1 + y) CIS 341: Compilers 10

Lambda Calculus Operational Semantics Substitution function (in Math): x{v/x} = v (replace the free x by v) y{v/x} = y (assuming y x) (fun x -> exp){v/x} = (fun x -> exp) (x is bound in exp) (fun y -> exp){v/x} = (fun y -> exp{v/x}) (assuming y x) (e 1 e 2 ){v/x} = (e 1 {v/x} e 2 {v/x}) (substitute everywhere) Examples: x y {(fun z ->z)/y} x (fun z -> z) (fun x -> x y){(fun z -> z) / y} (fun x -> x (fun z -> z)) (fun x -> x){(fun z -> z) / x} (fun x -> x) // x is not free! Zdancewic CIS 341: Compilers 11

Free Variables and Scoping let add = fun x -> fun y -> x + y let inc = add 1 The result of add 1 is a function After calling add, we can t throw away its argument (or its local variables) because those are needed in the function returned by add. We say that the variable x is free in fun y -> x + y Free variables are defined in an outer scope We say that the variable y is bound by fun y and its scope is the body x + y in the expression fun y -> x + y A term with no free variables is called closed. A term with one or more free variables is called open. CIS 341: Compilers 12

Free Variable Calculation An OCaml function to calculate the set of free variables in a lambda expression: let rec free_vars (e:exp) : VarSet.t = begin match e with Var x -> VarSet.singleton x Fun(x, body) -> VarSet.remove x (free_vars body) App(e1, e2) -> VarSet.union (free_vars e1) (free_vars e2) end A lambda expression e is closed if free_vars e returns VarSet.empty In mathematical notation: fv(x) = {x} fv(fun x -> exp) = fv(exp) \ {x} ( x is a bound in exp) fv(exp 1 exp 2 ) = fv(exp 1 ) fv(exp 2 ) Zdancewic CIS 341: Compilers 13

Variable Capture Note that if we try to naively "substitute" an open term, a bound variable might capture the free variables: (fun x -> (x y)) {(fun z -> x) / y} Note: x is free in (fun x -> x) = fun x -> (x (fun z -> x)) free x is captured!! Usually not the desired behavior This property is sometimes called "dynamic scoping" The meaning of "x" is determined by where it is bound dynamically, not where it is bound statically. Some languages (e.g. emacs lisp) are implemented with this as a "feature" But, leads to hard to debug scoping issues Zdancewic CIS 341: Compilers 14

Alpha Equivalence Note that the names of bound variables don't matter. i.e. it doesn't matter which variable names you use, as long as you use them consistently (fun x -> y x) is the "same" as (fun z -> y z) the choice of "x" or "z" is arbitrary, as long as we consistently rename them Two terms that differ only by consistent renaming of bound variables are called alpha equivalent The names of free variables do matter: (fun x -> y x) is not the "same" as (fun x -> z x) Intuitively: y an z can refer to different things from some outer scope Zdancewic CIS 341: Compilers 15

Fixing Substitution Consider the substitution operation: {e 2 /x} e 1 To avoid capture, we define substitution to pick an alpha equivalent version of e 1 such that the bound names of e 1 don't mention the free names of e 2. Then do the "naïve" substitution. For example: (fun x -> (x y)) {(fun z -> x) / y} = (fun x' -> (x' (fun z -> x)) rename x to x' Zdancewic CIS 341: Compilers 16

Operational Semantics Specified using just two inference rules with judgments of the form exp val Read this notation a as program exp evaluates to value val This is call-by-value semantics: function arguments are evaluated before substitution v v Values evaluate to themselves exp 1 (fun x -> exp 3 ) exp 2 v exp 3 {v/x} w exp 1 exp 2 w To evaluate function application: Evaluate the function to a value, evaluate the argument to a value, and then substitute the argument for the function. Zdancewic CIS 341: Compilers 17

See fun.ml IMPLEMENTING THE INTERPRETER Zdancewic CIS 341: Compilers 18