n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

Similar documents
n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CMSC 330: Organization of Programming Languages. Operational Semantics

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330: Organization of Programming Languages. Lambda Calculus

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

CS558 Programming Languages

COS 320. Compiling Techniques

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Fundamentals and lambda calculus

Lexical Considerations

CS558 Programming Languages

Concepts of programming languages

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Functional Programming. Pure Functional Languages

MIDTERM EXAMINATION - CS130 - Spring 2003

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

Programs as data Parsing cont d; first-order functional language, type checking

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

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

CMSC 330: Organization of Programming Languages. Lambda Calculus

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

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

CS558 Programming Languages

Programming Languages Lecture 15: Recursive Types & Subtyping

Lecture 5: Lazy Evaluation and Infinite Data Structures

CMSC 330: Organization of Programming Languages

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

Compiler construction

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

An introduction to C++ template programming

Lexical Considerations

1 Lexical Considerations

CMSC330 Spring 2017 Midterm 2

More Lambda Calculus and Intro to Type Systems

CMSC 330: Organization of Programming Languages

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Seminar in Programming Languages

CS558 Programming Languages

Variables. Substitution

Functional Programming. Pure Functional Languages

G Programming Languages - Fall 2012

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CS 314 Principles of Programming Languages

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

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

CSCI Compiler Design

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

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

CMSC 330: Organization of Programming Languages

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

CSCI-GA Scripting Languages

Lecture 15 CIS 341: COMPILERS

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Semantic Analysis. Lecture 9. February 7, 2018

CSE 505: Concepts of Programming Languages

Lambda Calculus. Variables and Functions. cs3723 1

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

The role of semantic analysis in a compiler

Comp 311: Sample Midterm Examination

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CSCE 314 Programming Languages. Type System

G Programming Languages - Fall 2012

5. Syntax-Directed Definitions & Type Analysis

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Programming Languages Third Edition. Chapter 7 Basic Semantics

Type Checking and Type Inference

Recap: Functions as first-class values

Compilers and computer architecture: Semantic analysis

Pierce Ch. 3, 8, 11, 15. Type Systems

CMSC330. Objects, Functional Programming, and lambda calculus

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

CSC324 Principles of Programming Languages

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

7. Introduction to Denotational Semantics. Oscar Nierstrasz

CSE3322 Programming Languages and Implementation

CS558 Programming Languages. Winter 2013 Lecture 3

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Functional Programming. Pure Functional Programming

A Simple Syntax-Directed Translator

CS 415 Midterm Exam Spring 2002

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

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

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

Functional Programming

Semantic Processing (Part 2)

Lecture #23: Conversion and Type Inference

Question Bank. 10CS63:Compiler Design

Simply-Typed Lambda Calculus

Harvard School of Engineering and Applied Sciences Computer Science 152

Recursive Definitions, Fixed Points and the Combinator

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

CMSC 330: Organization of Programming Languages

Transcription:

Announcements Announcements n HW12: Transaction server n Due today at 2pm n You can use up to 2 late days, as always n Final Exam, December 17, 3-6pm, in DCC 308 and 318 n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades n HW1-11, Quiz1-8, Exam1-2 n I have emailed paper critique assignment to all students enrolled in CSCI-6430. It is due Friday, 12/21 at noon by email 1 2 Announcements n Office hours this week: n Ana: Tue 4-5pm, Wednesday 2-4pm Fri 2-4pm (TENTATIVE, may cancel if ill) Review n Sam: Thu 5-7pm n Varun: TBD n Mike: Fri 12-2pm 3 4 Final Exam is Cumulative n Programming Language Syntax (Ch. 2.1-2.3.3) n Logic Programming and Prolog (Ch. 12) n Scoping (Ch. 3.1-3.3) n Programming Language Semantics (Sc. Ch. 4.1-4.3) n Functional programming (Ch. 11) n Scheme and Haskell, map/fold questions n Lambda calculus (Ch. 11 Companion) n Data abstraction: Types (Ch. 7, 8) n Control abstraction: Parameter Passing (Ch. 9.1-9.3) n Object-oriented languages (10.1-10.2) n Scala n Concurrency (13.1). What can go wrong? questions n Dynamic languages (Ch. 14 optional) n Comparative Programming Languages 5 Practice Problems (Quiz 7) type Name = String data Expr = Var Name Val Bool And Expr Expr Or Expr Expr Not Expr Let Name Expr Expr Fill in the type signature of find: -- Looks up variable n in binding environment env. -- Returns first binding or throws Exception if no binding of n. -- Ex: find "x" [("x",true),("x",false),("y",true)] returns True find :: Name -> [(Name,Bool)] -> Bool 6 find n env = head [ bool (var,bool) <- env, var == n ] 1

Practice Problems (Quiz 7) Fill in the Or and Let arms of eval: -- Purpose: evaluates expression e in binding environment env -- Returns the boolean value of e or throws an Exception -- Ex.: eval (Var "x") [("x",true),("x",false)] returns True eval :: Expr -> [(Name,Bool)] -> Bool eval e env = case e of Var n -> find n env Val b -> b And e1 e2 -> (eval e1 env) && (eval e2 env) Or e1 e2 -> (eval e1 env) (eval e2 env) Not e1 -> not (eval e1 env) 7 Let n e1 e2 -> eval e2 ((n,(eval e1 env)):env) In programming languages types and type checking (a) Prevent runtime errors (b) Abstract data organization and implementation (c) Document variables and subroutines (d) All of the above 8 Quiz 8 Question 2 Let A denote all syntactically valid programs. Let S denote all syntactically valid programs that execute without forbidden errors. Let T denote all programs accepted by certain typesafe static type system. Which one best describes the relation between T, S and A? (a) T S A (b) T S A (c) T S A (d) T S A 9 A: All programs S: All programs that run without forbidden errors T: All programs accepted by a type-safe static type system 10 Again, let S denote all syntactically valid programs that execute without forbidden errors. Let T denote all programs accepted by certain type-unsafe static type system. T 0 6 S is (a) true (b) false Quiz 8 Question 3 A: All programs S: All programs that run without forbidden errors T : All programs accepted by a type-unsafe static type system 11 12 2

int w[10]() is an invalid declaration in C. Why? (a) true (b) false w in declaration int (*w[10])() is (a) A function (b) An array (c) A pointer In C, functions are third-class values. Thus, we cannot pass a function as argument, return a function as a result, or assign a function value to a variable, or structure. 13 14 A is a 3-dimensional array of ints: int [0..1,0..1,0..1] A. The elements are ordered in memory as: A[0,0,0],A[1,0,0],A[0,1,0],A[1,1,0],A[0,0,1], A[1,0,1],A[0,1,1],A[1,1,1] This is (a) Column-major order (b) Row-major order (c) Neither 15 typedef struct { int *i; char c; huge_record; void const_is_shallow( const huge_record* const r) { int *x = r->i; // or just *(r->i) = 0; *x = 0; Is this a compile time error? No. 16 Practice Problems (Quiz 9) c : array [1..2] of integer m : integer procedure R(k,j : integer) k := k+1 By value: 1, 1 j := j+2 /* begin main */ c[1] := 1 c[2] := 2 m := 1 R(m, c[m]) write m, c[m] /* end main */ By reference: 2, 2 By value-result: 2, 3 By name: 2, 4 17 class Account { int ammount = 0; void deposit(int x) { ammount = ammount + x; class DepositTask implements Runnable { public void run() { synchronized (this) { Main.act.deposit(10); 18 3

Practice Problems (Quiz 9) public class Main { static Account act = new Account(); public static void main(string arg[]) { ExecutorService pool = Executors.newCachedThreadPool(); pool.execute(new DepositTask()); pool.execute(new DepositTask()); pool.execute(new DepositTask()); pool.shutdown(); pool.awaittermination(60, TimeUnit.SECONDS); What can act.ammount be at the end? 10,20,30 19 Practice Problems (Quiz 9) Generic Java class MyList is defined as: public class MyList<T extends Number> { // Type parameter T is bounded // MyList uses interface of Number, // e.g., intvalue(), doublevalue()... Is MyList<Integer> valid? Yes. Is MyList<String> valid? No. 20 Programming Language (PL) Syntax n Regular expressions n Context-free grammars n Derivation, parsing, ambiguity, precedence n LL(1) grammars and parsing n FIRST, FOLLOW, PREDICT sets, LL(1) parsing table n Obstacles to LL(1)-ness n SLR(1) grammars and parsing n CFSM n Conflicts in SLR(1) 21 PL Syntax Problems n Problem 1. Given grammar Start S S TS [ S ] S ε T ( X ) X TX [ X ] X ε Fill in the FIRST and FOLLOW table: FIRST FOLLOW S T X Is this grammar an LL(1) grammar? 22 PL Syntax Problems n Problem 2. Augmented grammar S E E E + E E y Construct the CFSM and add the reduce by labels. Is the grammar a SLR(1) grammar? Prolog, Scoping n Prolog --- expect simple programming questions, as in practice tests and Exam 1 n Scoping n Static scoping n Dynamic scoping with deep and shallow binding n Scoping when functions are first-class values n Scoping questions on code in Scala and Python n Exam 2 n Jensen s device in Scala, etc. 23 24 4

Programming Language Semantics n Attribute grammars n Attributes n Synthesized vs. inherited n Semantic rules n S-attributed grammars n L-attributed grammars n Only terms, no problems that involve L-attributed grammars Attribute Grammar Problems n Problem 1 Given CFG E -> E + T T T -> T * F F F -> ( E ) id Write an attribute grammar which computes at the root of the tree a count containing the maximum depth to which parentheses are nested in the expression 25 26 Scheme n Recursion n Tail recursion n Shallow and deep recursion n Higher-order functions n map/fold problems n Scoping in Scheme Scheme Problems n Problem 1 Consider the following function: (define fun (lambda (n) (if (= n 1) 0 (+ 1 (fun (quotient (+ n 1) 2))) ))) What does this function do? Is it tail-recursive? If not, write a tail recursive version of this function. 27 28 Scheme Problems n Problem 2 Define a function filter which takes a list and returns a list containing all elements that satisfy a given predicate. For example, (filter (lambda (x) (< x 5)) (3 9)) should return (3). The entire body of filter should be one call to foldl. n (define (filter p lis) (foldl foldl foldl op lis id id ( r1 r2 r3 rn ) id 1 ( r2 r3 rn ) id 2 ( r3 rn ) id n-1 ( rn ) 29 id n 30 5

More Scheme Problems n Other problems Haskell Problems n Write all Scheme problems in Haskell too! n Problems from sample final n Part 1 from practice problem set 31 32 Lambda Calculus n Syntax and semantics n Free and bound variables n Rules of lambda calculus n α-conversion n β-reduction n Evaluation order n Applicative order reduction n Normal order reduction n Normal forms: WHNF, HNF, NF Lambda Calculus Problems n Problem 1 (from sample final). Reduce the following term until no more reductions are possible: (λx. x x) ((λy. y) (λz. z)) n Problem 2 (from sample final). Suppose we defined the following combinators: T = λx.λy.x /* T stands for true */ F = λx.λy.y /* F stands for false */ N = λx. (x F T) /* N stands for not */ Show that N (N z) à β z where z is either T or F 33 34 Rules of Lambda Calculus: Exercises Normal Forms n Use α-conversion and/or β-reduction: (λx. x) y à αβ? (λx. x) (λy. y) à αβ? (λx.λy.λz. x z (y z)) (λu. u) (λv. v) à αβ Notation: à αβ denotes that expression on the left reduces to the expression on the right, through a sequence α-conversions and β-reductions. 35 n λz. z z is in NF, HNF, or WHNF? n (λz. z z) (λx. x) is in? n λx.λy.λz. x z (y (λu. u)) is in? n (λx.λy. x) z ((λx. z x) (λx. z x)) is in? n z ((λx. z x) (λx. z x)) is in? n λz.(λx.λy. x) z ((λx. z x) (λx. z x)) is in? 36 6

Lambda Calculus Problems Definitions n Other problems n Go over problems from HW8 n What is a closure, higher-order function, tail recursion? n What is normal-order reduction, applicativeorder reduction? n What does functions are first-class values mean? n Weak-head normal form (WHNF), head normal form (HNF), normal form n Other 37 38 Types and Type Equivalence Types Problems n Type construction n Type equivalence n Structural equivalence n Name equivalence n Loose name equivalence, strict name equivalence n Type equivalence in C n Problem 1 (from HW10). Consider the following type declaration in C: double ( *foo(double (*)(double, double[]), double) )() Using appropriate type constructors (pointerto, array and à), show the type tree for foo 39 40 Types Problems Definitions n Other sample problems n Go over HW10 n What is a type system? n What is type safety? n Problem 6 from sample final n Problems from practice problem set n What is statically typed language? n Dynamically typed language? n What is recursive type? n What is structural type equivalence? n What is name type equivalence n Loose name equivalence, strict name equivalence? 41 42 7

Parameter Passing Mechanisms n Call by value n Call by reference n Call by value-result n Call by name Parameter Problems n Problem 1 (from practice problem set). Write a single program fragment that produces different result under each of the following parameter passing mechanisms n Call by value n Call by reference n Call by value-result n Call by name 43 44 Parameter Passing Problems x : int c : array [0..1] of int procedure p(y,z : int) begin y := y+1 print x,z end By value? By reference? By value-result? x := 0; c[0] := 0; c[1] := 1; By name? p(x,c[x]); print x; 45 Parameter Passing Problems n Other problems n Problem 5 from sample final n Problems from practice problem set n Note: All questions on parameter passing assume value model for variables, unless explicitly specified otherwise 46 Definitions n What is the value model for variables? n Give examples of languages that use the value model for variables n What is the reference model for variables? n Give examples of languages that use the reference model for variables Object-oriented Programming n Benefits of object-oriented programming n What is subtype polymorphism? n What is dynamic binding? n What is parametric polymorphism? n Explicit parametric polymorphism, implicit parametric polymorphism? 47 48 8

Concurrency n Definitions n Message passing vs. shared memory n Shared mutable state n Atomic action n Data race, atomicity violation, deadlock n Memory consistency models n Actor model Concurrency Problems n Other problems n Part 6 from practice problem set n Examples we did in class n What can go wrong questions 49 50 Other Topics n Comparative programming languages n Problem 7 THANK YOU! n We started at about 330 we are now at 311 n Thanks to Barb Cutler and all of you who have contributed to Submitty n Thanks to Sam, Mike, Varun, Bill, Jordan and Kasra 51 52 9