CIS 500 Software Foundations Midterm I, Review Questions



Similar documents
Types and Programming Languages

CMCS 312: Programming Languages Lecture 3: Lambda Calculus (Syntax, Substitution, Beta Reduction) Acar & Ahmed 17 January 2008

Types, Polymorphism, and Type Reconstruction

Fun with Phantom Types

How To Program In Scheme (Prolog)

A Tutorial Introduction to the Lambda Calculus

Chapter 7: Functional Programming Languages

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Summary Last Lecture. Automated Reasoning. Outline of the Lecture. Definition sequent calculus. Theorem (Normalisation and Strong Normalisation)

Chapter 15 Functional Programming Languages

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Deterministic Discrete Modeling

Monads for functional programming

Useful Number Systems

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

A Tutorial on [Co-]Inductive Types in Coq

Semantic Analysis: Types and Type Checking

Multilinear Programming with Big Data

How To Write A Type System

Observational Program Calculi and the Correctness of Translations

How To Understand The Theory Of Computer Science

CS510 Software Engineering

Blame for All. Jeremy G. Siek. Amal Ahmed. Robert Bruce Findler. Philip Wadler. Abstract. 1. Introduction

Unembedding Domain-Specific Languages

Programming Languages in Artificial Intelligence

Rigorous Software Development CSCI-GA

Chapter 1. Computation theory


Bindings, mobility of bindings, and the -quantifier

Binary Adders: Half Adders and Full Adders

SOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University

Andrew Pitts chapter for D. Sangorgi and J. Rutten (eds), Advanced Topics in Bisimulation and Coinduction, Cambridge Tracts in Theoretical Computer

3.5 RECURSIVE ALGORITHMS

NOTES ON TYPES AND PROGRAMMING LANGUAGES

Type Systems. Luca Cardelli. Microsoft Research

OPERATIONAL TYPE THEORY by Adam Petcher Prepared under the direction of Professor Aaron Stump A thesis presented to the School of Engineering of

Computational Semantics, Type Theory, and Functional Programming

An Agda Tutorial. Makoto Takeyama

Semantics and Generative Grammar. Quantificational DPs, Part 3: Covert Movement vs. Type Shifting 1

The Lean Theorem Prover

Feed-Forward mapping networks KAIST 바이오및뇌공학과 정재승

Algebraic expressions are a combination of numbers and variables. Here are examples of some basic algebraic expressions.

Adding GADTs to OCaml the direct approach

Introduction to Type Theory

Advanced Functional Programming (9) Domain Specific Embedded Languages

Unit 3 Boolean Algebra (Continued)

(a) We have x = 3 + 2t, y = 2 t, z = 6 so solving for t we get the symmetric equations. x 3 2. = 2 y, z = 6. t 2 2t + 1 = 0,

POLYNOMIALS and FACTORING

Functional programming languages

COMPUTER SCIENCE TRIPOS

8 Divisibility and prime numbers

9.3 OPERATIONS WITH RADICALS

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Why? A central concept in Computer Science. Algorithms are ubiquitous.


3201 Computer Networks 2014/2015 Handout: Subnetting Question

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

Boolean Expressions & the if Statement

Fixed-Point Logics and Computation

Logic in Computer Science: Logic Gates

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Solving Quadratic Equations by Factoring

NLP Lab Session Week 3 Bigram Frequencies and Mutual Information Scores in NLTK September 16, 2015

o-minimality and Uniformity in n 1 Graphs

Boolean Algebra Part 1

Definitional Interpreters for Higher-Order Programming Languages *

CS422 - Programming Language Design

Introduction. The Quine-McCluskey Method Handout 5 January 21, CSEE E6861y Prof. Steven Nowick

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

BRICS. From Interpreter to Compiler and Virtual Machine: A Functional Derivation

Gates, Circuits, and Boolean Algebra

9 Multiplication of Vectors: The Scalar or Dot Product

Handout #1: Mathematical Reasoning

Home Assignment 4 OCL

Paper Merges and Joins Timothy J Harrington, Trilogy Consulting Corporation

Linear Types for Continuations

Forecasting in supply chains

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

9.2 Summation Notation

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Introduction to formal semantics -

Intermediate Code. Intermediate Code Generation

Programmable Logic Controllers Definition. Programmable Logic Controllers History

Well-typed programs can t be blamed

Math Review. for the Quantitative Reasoning Measure of the GRE revised General Test

MATLAB Basics MATLAB numbers and numeric formats

Transcription:

CIS 500 Software Foundations Midterm I, Review Questions

Untyped lambda calculus 1. (2 points) We have seen that a linear expression likeλx.λy.xyx is shorthand for an abstract syntax tree that can be drawn like this: λx λy apply apply x x y Draw the abstract syntax trees corresponding to the following expressions: (a) a b c (b) (λx.b) (c d) 1

2. (10 points) Write down the normal forms of the followingλ terms: (a) (λt.λf. t)(λt.λf.f)(λx. x) (b) (λx.x) (λx.x) (λx.x) (λx.x) (c) λx.x(λx.x) (λx.x) (d) (λx.x(λx.x)) (λx.x(λx.xx)) (e) (λx.xxx)(λx. xxx) 3. (4 points) Recall the following abbreviations from Chapter 5: tru = λt. λf. t fls = λt. λf. f not = λb. b fls tru Complete this definition of a lambda term that takes two church booleans, b and c, and returns the logical exclusive or of b and c. xor = λb. λc. 2

4. (8 points) A list can be represented in the lambda calculus by itsfold function. (OCaml s name for this function isfold_right; it is also sometimes calledreduce.) For example, the list [x,y,z] becomes a function that takes two arguments c and n and returns c x(c y (c z n))). The definitions of nil and cons for this representation of lists are as follows: nil = λc. λn. n; cons = λh. λt. λc. λn. c h (t c n); Suppose we now want to define aλ termappend that, when applied to two listsl1 andl2, will append l1 to l2 i.e., it will return aλ term representing a list containing all the elements of l1 and then those of l2. Complete the following definition ofappend. append = λl1. λl2. λc. λn. 5. (6 points) Recall the call by value fixed point combinator from Chapter 5: fix = λf. (λx. f (λy. x x y)) (λx. f (λy. x x y)); We can usefix to write a function sumupto that, given a Church numeralsm, calculates the sum of all the numbers less than or equal to m, as follows. g = λf. λm. (iszro m) (λx. c 0 ) (λx. plus ( (prd m))) tru; sumupto = fix g; Fill in the two omitted subterms. 3

Nameless representation of terms 6. (4 points) Suppose we have defined the naming contextγ = a,b,c,d. What are the debruijn representations of the followingλ terms? (a) λx.λy. xyd (b) λx.c(λy.(c y) x)d 7. (4 points) Write down (in debruijn notation) the terms that result from the following substitutions. (a) [0 λ.0]((λ.01)1) (b) [0 λ.01]((λ. 01) 0) 4

Typed arithmetic expressions The full definition of the language of typed arithmetic and boolean expressions is reproduced, for your reference, on page 10. 8. (9 points) Suppose we add the following new rule to the evaluation relation: succtrue pred(succ true) Which of the following properties will remain true in the presence of this rule? For each one, write either remains true or else becomes false, plus (in either case) a one sentence justification of your answer. (a) Termination of evaluation (for every termtthere is some normal formt such that t t ) (b) Progress (if t is well typed, then eithertis a value or elset t for somet ) (c) Preservation (if t has typetand t t, then t also has type T) 9. (9 points) Suppose, instead, that we add this new rule to the evaluation relation: t if true thentelse succfalse Which of the following properties remains true? (Answer in the same style as the previous question.) (a) Termination of evaluation (for every termtthere is some normal formt such that t t ) (b) Progress (if t is well typed, then eithertis a value or elset t for somet ) (c) Preservation (if t has typetand t t, then t also has type T) 5

10. (9 points) Suppose, instead, that we add a new type, Funny, and add this new rule to the typing relation: iftrue then falseelse false:funny Which of the following properties remains true? (Answer in the same style as the previous question.) (a) Termination of evaluation (for every termtthere is some normal formt such that t t ) (b) Progress (if t is well typed, then eithertis a value or elset t for somet ) (c) Preservation (if t has typetand t t, then t also has type T) 6

Simply typed lambda calculus The definition of the simply typed lambda calculus with booleans is reproduced for your reference on page 12. 11. (6 points) Write down the types of each of the following terms (or ill typed if the term has no type). (a) λx:bool.xx (b) λf: Bool Bool. λg:bool Bool. g(f (g true)) (c) λh:bool.(λi:bool Bool. i false) (λk:bool.true) 7

Operational semantics 12. (9 points) Recall the rules for big step evaluation of arithmetic and boolean expressions from HW 3. v v t 1 true t 2 v 2 if t 1 then t 2 elset 3 v 2 t 1 false t 3 v 3 if t 1 then t 2 elset 3 v 3 t 1 nv 1 succ t 1 succ nv 1 t 1 0 predt 1 0 t 1 succ nv 1 predt 1 nv 1 t 1 0 iszero t 1 true t 1 succ nv 1 iszerot 1 false The following OCaml definitions implement this evaluation relation almost correctly, but there are three mistakes in the eval function one each in the TmIf, TmSucc, and TmPred cases of the outer match. Show how to change the code to repair these mistakes. (Hint: all the mistakes are omissions.) let rec isnumericval t = match t with TmZero(_) true TmSucc(_,t1) isnumericval t1 _ false let rec isval t = match t with TmTrue(_) true TmFalse(_) true t when isnumericval t true _ false let rec eval t = match t with v when isval v v TmIf(_,t1,t2,t3) (match t1 with TmTrue _ eval t2 TmFalse _ eval t3 _ raise NoRuleApplies) TmSucc(fi,t1) (match eval t1 with nv1 TmSucc (dummyinfo, nv1) _ raise NoRuleApplies) TmPred(fi,t1) (match eval t1 with TmZero _ TmZero(dummyinfo) _ raise NoRuleApplies) TmIsZero(fi,t1) (match eval t1 with TmZero _ TmTrue(dummyinfo) TmSucc(_, _) TmFalse(dummyinfo) _ raise NoRuleApplies) _ raise NoRuleApplies 8

9

For reference: Untyped boolean and arithmetic expressions Syntax t ::= terms true constant true false constant false if tthen t elset conditional 0 constant zero succ t successor pred t predecessor iszero t zero test v ::= true false nv values true value false value numeric value nv ::= numeric values 0 zero value succ nv successor value T ::= Bool Nat types type of booleans type of numbers Evaluation iftrue then t 2 elset 3 t 2 if falsethen t 2 else t 3 t 3 (E IfTrue) (E IfFalse) t 1 t 1 if t 1 then t 2 elset 3 if t 1 then t 2 elset 3 (E If) t 1 t 1 succt 1 succ t 1 pred0 0 pred (succnv 1 ) nv 1 (E Succ) (E PredZero) (E PredSucc) t 1 t 1 predt 1 pred t 1 iszero 0 true iszero (succnv 1 ) false (E Pred) (E IszeroZero) (E IszeroSucc) t 1 t 1 iszerot 1 iszero t 1 (E IsZero) continued on next page... 10

Typing true: Bool false: Bool t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 elset 3 :T 0:Nat t 1 :Nat succ t 1 :Nat t 1 :Nat pred t 1 :Nat t 1 :Nat iszero t 1 :Bool (T True) (T False) (T If) (T Zero) (T Succ) (T Pred) (T IsZero) 11

For reference: Simply typed lambda calculus with booleans Syntax t ::= true false if tthen t elset x λx:t.t t t terms constant true constant false conditional variable abstraction application v ::= T ::= true false λx:t.t Bool T T values true value false value abstraction value types type of booleans type of functions Evaluation Typing iftrue then t 2 elset 3 t 2 if falsethen t 2 else t 3 t 3 t 1 t 1 if t 1 then t 2 elset 3 if t 1 then t 2 elset 3 t 1 t 1 t 1 t 2 t 1 t 2 t 2 t 2 v 1 t 2 v 1 t 2 (λx:t 11.t 12 ) v 2 [x v 2 ]t 12 true: Bool false: Bool t 1 :Bool t 2 :T t 3 :T if t 1 then t 2 elset 3 :T x:t Γ Γ x:t Γ,x:T 1 t 2 :T 2 Γ λx:t 1.t 2 :T 1 T 2 Γ t 1 :T 11 T 12 Γ t 2 :T 11 Γ t 1 t 2 :T 12 (E IfTrue) (E IfFalse) (E If) (E App1) (E App2) (E AppAbs) (T True) (T False) (T If) (T Var) (T Abs) (T App) 12