CSCI 3155: Homework Assignment 3

Similar documents
CSCI 3155: Homework Assignment 2

CSCI 3155: Homework Assignment 4

CSCI 3155: Lab Assignment 2

CSCI 3155: Lab Assignment 6

CSCI 3155: Lab Assignment 6

CSCI 3155: Lab Assignment 2

CSCI 3155: Lab Assignment 6

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

COMPUTER SCIENCE TRIPOS

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Exercise 1: Tail Sum of Squares (2 points) Homework 3 READ THIS BEFORE YOU BEGIN:

Lecture 3: Recursion; Structural Induction

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

CPSC 121: Models of Computation Assignment #5

Lecture 6: Sequential Sorting

Exercise 1: Graph coloring (10 points)

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

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

To illustrate what is intended the following are three write ups by students. Diagonalization

Homework 4 Solutions

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

6.001 Notes: Section 15.1

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

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

CMSC 330: Organization of Programming Languages. Operational Semantics

Programming Languages Lecture 14: Sum, Product, Recursive Types

15 212: Principles of Programming. Some Notes on Induction

In Our Last Exciting Episode

Homework 7: Sudoku. Preliminaries. Overview

by the evening of Tuesday, Feb 6

15-122: Principles of Imperative Computation, Fall 2015

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Project 4 Due 11:59:59pm Thu, May 10, 2012

Recursive Data and Recursive Functions

Reasoning About Programs Panagiotis Manolios

CMPSCI 187 / Spring 2015 Hanoi

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

CS 125 Section #4 RAMs and TMs 9/27/16

An Introduction to Functions

Project 5 - The Meta-Circular Evaluator

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Meeting14:Denotations

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Part I: Written Problems

SCHEME AND CALCULATOR 5b

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

Functional abstraction

Implementation of a Sudoku Solver Using Reduction to SAT

Handout 9: Imperative Programs and State

CS115 - Module 10 - General Trees

8 Understanding Subtyping

CSCI-GA Scripting Languages

Principles of Programming Languages 2017W, Functional Programming

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Mutable References. Chapter 1

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

Lecture 6: Arithmetic and Threshold Circuits

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

From the λ-calculus to Functional Programming Drew McDermott Posted

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

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

School of Informatics, University of Edinburgh

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Programming Languages Third Edition

Assignment 7: Due Wednesday May 11 at 6pm UPDATES on Monday May 9

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Programming Languages and Techniques (CIS120)

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

15-110: Principles of Computing, Spring 2018

CSE413 Midterm. Question Max Points Total 100

Formal semantics of loosely typed languages. Joep Verkoelen Vincent Driessen

COMP 105 Homework: Type Systems

Lab 7 1 Due Thu., 6 Apr. 2017

2. CONNECTIVITY Connectivity

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

(Refer Slide Time 3:31)

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

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

Program Verification & Testing; Review of Propositional Logic

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Homework 6. What To Turn In. Reading. Problems. Handout 15 CSCI 334: Spring, 2017

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Chapter 3 Data Representation

Lecture 1: Overview

AXIOMS FOR THE INTEGERS

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

CpSc 421 Final Solutions

Lab 6: P.S. it s a stack Due 11:59 pm Monday, Mar 31, 2008

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

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

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Project 5 - The Meta-Circular Evaluator

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers

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

Transcription:

CSCI 3155: Homework Assignment 3 Spring 2012: Due Monday, February 27, 2012 Like last time, find a partner. You will work on this assignment in pairs. However, note that each student needs to submit a write-up and are individually responsible for completing the assignment. You are welcome to talk about these questions in larger groups. However, we ask that you write up your answers in pairs. Also, be sure to acknowledge those with which you discussed, including your partner and those outside of your pair. Recall the evaluation guideline from the course syllabus. Make sure that your file compiles and runs. A program that does not compile will not be graded. Submission Instructions. To submit, upload to the moodle exactly two files named as follows: Homework3-YourIdentiKey.pdf with your answers to the written questions (scanned, clearly legible handwritten write-ups are acceptable) Homework3-YourIdentiKey.scala with your answers to the coding exercises Replace YourIdentiKey with your IdentiKey. To help with managing the submissions, we ask that you rename your uploaded files in this manner. Getting Started. Download the code template Homework3.scala from the assignment page. Be sure to also write your name, your partner, and collaborators there. 1. Feedback. Complete the survey on the linked from the moodle after completing this assignment. Any non-empty answer will receive full credit. 2. Syntax: MYSTERY. In this exercise, we look the language MYSTERY used by the PL-Detective tool. The premise behind MYSTERY is that its syntax is fixed but its semantics can be changed (i.e., aspects of its semantics are a mystery). You can play with MYSTERY s changeable semantics here: http://csci3155.cs.colorado.edu/pl-detective/hw/pldwelcome.htm MYSTERY uses a syntax that is likely foreign to you, so this question asks you to figure out how write some simple MYSTERY programs based solely on the grammar specification and experimenting with the compiler. 1

Some aspects of MYSTERY s semantics are fixed. The underlying computational model behind MYSTERY is imperative. Variables are the main state element, which are mutable. The keywords name constructs that are familiar from other programming languages From the syntax of MYSTERY, we see that a MYSTERY program is a block. A MYSTERY program starts by executing this block. MYSTERY is designed to be as small as possible, yet large enough to illustrate many of the concepts that we will cover in this class. Thus, MYSTERY is missing many features that you may be used to in your favorite programming language. On the other hand, you will find later this semester that MYSTERY has some features that are more powerful than features in your favorite programming language. For this exercise, write the following MYSTERY programs: i. Write a program that prints out 1 through 100 (inclusive). You should test your program using this form: http://csci3155.cs.colorado.edu/pl-detective/hw/pldsyntax.htm You will receive a User id for the PL-Detective by e-mail. Use your account name as the User id, enter your code in the Input space, and click Submit. This will compile and run your program and present any errors or output in a new browser window. You may use as many attempts as you wish. As an additional challenge to yourself, try to do this in as few attempts as you can. Be sure to brag about how many tries it took in your submission! ii. Write a program that has two procedures, sum and main. The procedure sum takes two integers as arguments and returns their sum. The procedure main calls sum with two arguments and prints out the result. You should use the same form to test your program: http://csci3155.cs.colorado.edu/pl-detective/hw/pldsyntax.htm 3. Induction: Equivalence of Evaluation. Consider the following simple arithmetic expression language represented in Scala: sealed abstract class Expr case class Value(i: Int) extends Expr case class Neg(sub: Expr) extends Expr case class Plus(left: Expr, right: Expr) extends Expr We can define an evaluator for this language called aeval as follows: def aeval(e: Expr): Int = e match { case Value(i) => i case Neg(e) => -aeval(e) case Plus(e1,e2) => aeval(e1) + aeval(e2) } Alternatively, we can also define a function redex that performs local simplification (i.e., local reduction) and a visit function that applies a transformation to an expression bottomup (like the exercise in Homework 2): 2

def redex(e: Expr): Expr = e match { case Neg(Value(i)) => Value(-i) case Plus(Value(i1), Value(i2)) => Value(i1 + i2) case _ => e } def visit(v: Expr => Expr, e: Expr): Expr = e match { case Value(i) => v(value(i)) case Neg(e) => v(neg(visit(v, e))) case Plus(e1, e2) => v(plus(visit(v, e1), visit(v, e2))) } We can see that visit(redex, e) should behave quite similarly to aeval(e) for any Expr value e. In fact, visit(redex, e) is also an evaluator. To make this precise, we can prove the following theorem. Theorem 1. For all Expr values e and Int values i, if aeval(e) i then visit(redex, e) Value(i). For this exercise, prove Theorem 1. Be sure to state clearly the method of induction, the induction variable, and where you apply the induction hypothesis. You may assume that aeval(e) terminates for any Expr value e. In other words, you may assume the following lemma: Lemma 1 (Termination and Determinism of aeval). 1. Termination. For all Expr values e, for some Int value i. aeval(e) i 2. Determinism. For all Expr values e and all Int values i and i, if then i = i. aeval(e) i and aeval(e) i, Note that the other direction of Theorem 1 also holds, though you do not need show it for this exercise. 4. Higher-Order Functions: Evaluator with Binding. Suppose that we extend our arithmetic expression language with a let-binding construct: case class Let(bind: Expr, body: Int => Expr) extends Expr 3

to represent something like let x = e bind in e body where x is bound to the value of e bind in expression e body. Our representation does not actually introduce variables in our expression language. Instead, we represent e body as a function that given an integer value, yields an expression (i.e., something like an expression with a hole to be filled in). (a) Extend the aeval function from the previous question with a case to evaluate Lets. Make sure your test case uses Let and that the body of the Let uses the binding in some way. (b) Compare and contrast this aeval function over this arithmetic language Expr and the eval function over the Boolean formulas Formula from Homework 2. Why does eval take an Assignment? Why don t we need something analogous for aeval? Explain. 5. Using Higher-Order Functions: Magic Squares. Recall the sat function that you wrote in Homework 2. This part will enable you to use your SAT solver from Homework 2 to do something fun: solve a puzzle. Your SAT solver likely not very efficient; given a formula with n free variables, it probably always runs in O(2 n ) time. That s fine for that homework assignment, and that s the worst case bound, but there has been significant progress in recent years in developing SAT solvers that can test for satisfiability much more efficiently in the common case. One of the major applications of SAT solvers is in reasoning about the kinds of arithmetic hardware does, which is all carried out by bit twiddling. Note that you don t actually need your solver working to complete these functions (i.e., you never need to call it in these functions). Here, we write functions to encode a puzzle into SAT. Once you have a puzzle encoded, you can try it out with your (inefficient) SAT solver for fun! Binary Arithmetic with SAT. A BitsFormula is a list of boolean formulae representing the bits of a machine word, with the low order bit at the head of the list: type BitsFormula = List[Formula] For example, we would represent the number 13 as follows: val x = List(B(true), B(false), B(true), B(true)) What is interesting about this type is that we can represent, for example, an unknown 4-bit number as val y = List(Var("a"), Var("b"), Var("c"), Var("d")) Below when we refer to x and y, we will mean these two BitsFormulas. For this problem, we will only work with non-negative numbers. We interpret the empty list Nil as 0 and allow padding of B(false)s at the end. For example, List(B(false)) and List(B(false), B(false)) are also 0. Similarly, List(B(true), B(false), B(true), B(true), B(false)) 4

is also 13. Conversions. We have provided some conversion functions that allow to go between bit vectors and integers. def intofvec(v: BitsFormula): Int. This function takes a BitsFormula composed solely of B(true)s and B(false)s and returns the integer equivalent. For example, intofvec(list(b(false), B(true))) returns 2. This function does not handle the case when some element of the bit vector is neither B(true) nor B(false). def vecofint(n: Int): BitsFormula. This function takes a non-negative integer and returns the corresponding BitsFormula. For example, vecofint(13) returns x above. def subst(a: Assignment, v: BitsFormula): BitsFormula. This function reduces the v argument to a BitsFormula of all B(true)s and B(false)s by replacing the variables in v according to the Assignment a and then evaluating each bit. For example, subst(map("x" -> true,"y" -> false), List(Var "x", B(true), And(Var "y", B(true)))) returns List(B(true), B(true), B(false)). We assume that all free variables in the input v are given in a. Requirements and Hints. Unless otherwise specified in this question, do not implement any functions by direct recursion. Instead use library methods from List and Tuple2. You might have been clamoring to use library methods in the last assignment. Now s your chance! For your convenience, we have provided some wrappers around the Scala library. should be able to implement all of the exercises (concisely) using these wrappers: def listmap[a,b](l: List[A])(f: A => B): List[B] def foldleft[a,b](l: List[A])(z: B)(f: (B,A) => B): B def listpairmap[a1,a2,b](p: (List[A1], List[A2]))(f: (A1,A2) => B): List[B] If your curious about the wrappers, a note about method calls in Scala. We have not talked specifically about objects, but for the moment, it is sufficient to rely on your prior background on objects. All values in Scala are actually objects (i.e., a record bundling data and operations). Methods are invoked on a receiver object using the same syntax as in Java, such as o.m() that calls method m on the receiver object o. Like in previous homework assignments, each function can be written quite concisely. If you are writing pages for a function, then you might reconsider your design. (a) Warm Up. Write three functions that return the bit-wise not, and, and or, respectively: def bitnot(v: BitsFormula): BitsFormula def bitand(v1: BitsFormula, v2: BitsFormula): BitsFormula def bitor(v1: BitsFormula, v2: BitsFormula): BitsFormula Each of these functions should take 1 line of code using the library methods. You 5

(b) Binary Arithmetic. Implement the following functions that manipulate BitFormulas to create Formulas that capture a property of interest. i. def iszero(v: BitsFormula): Formula. This function returns a boolean formula representing whether the input BitsFormula represents zero. For example, if y is the four-bit number defined above, iszero(y) could return And(Not(Var("a")), And(Not(Var("b")), And(Not(Var("c")), Not(Var("d"))))) This is really cool. Once we have this, we can ask a question like sat(iszero(y)), and the sat function will tell us, yes, there are choices for Var("a"), Var("b"), Var("c"), and Var("d") that make the formula true. When you re testing your code, you can use the functions subst and intofvec above to look at your answers more easily. ii. def eqbitsformula(v1: BitsFormula, v2: BitsFormula): Formula. This function returns a formula representing whether the two bit vectors are equal. For example, eqbitsformula(list(var("a")), List(B(true))) could return Or(And(Var("a"), B(true)), And(Not(Var("a")), Not(B(true)))) In other words, the two vectors are equal if either both a and B(true) are true, or if both are false. Now we can ask questions like, sat(eqbitsformula(y, bitand(x, y))), that is, is there a 4-bit number such that if you bitwise and it with x, you get itself? You can assume for this problem that the two bit vectors have the same length. To implement this function, we ask that you write a helper function def eqbit(f1: Formula, f2: Formula): Formula that returns a formula that represents if the two bits are equal. iii. def add(v1: BitsFormula, v2: BitsFormula): BitsFormula. This function returns a new BitsFormula representing the sum of the two vectors. Since there may be a carry out of the last bit, the resulting vector may have one more element than the input vectors. Your implementation for add should work for two bit vectors with different lengths. However, you may want to start by writing a version first that assumes two bit vectors with the same length. This function is the one exception. You can use direct recursion for this function, and hint: you will probably want to write a recursive helper function that has an extra parameter for the carry bit. We have also provided some other helper functions in the template. (c) Magic Squares. A magic square is an n-by-n grid of the numbers 1 through n 2 such that every number appears exactly once and the sum of each row, column, and diagonal is the same. For example, here is a 3x3 magic square: 6

8 1 6 3 5 7 4 9 2 In this part, you will write a function that enables you to solve a magic squares problem. In particular, what we will do is reduce the problem of finding a solution to the magic squares problem to a boolean formula. We can then use the sat function to actually find a solution. To simplify things, we will consider only 3x3 squares. Here are the functions you should write: i. def disjoint(l: List[BitsFormula]): Formula. This function takes a list of BitsFormulas and returns a formula representing whether all the BitsFormulas are different from each other. You may assume that all the BitsFormulas in the list have the same length. To help you write this function, we ask that you write a helper function def disjointone(v: BitsFormula, l: List[BitsFormula]): Formula that returns a formula representing whether v is different from all the bit vectors in l. ii. def isdigit(v: BitsFormula): Formula. The input is a BitsFormula of length 4 (i.e., exactly 4 boolean formulae). This function returns a formula that is true if and only if the BitsFormula is greater than or equal to 1 and less than or equal to 9. iii. def ismagic(v1: BitsFormula, v2: BitsFormula, v3: BitsFormula, v4: BitsFormula, v5: BitsFormula, v6: BitsFormula, v7: BitsFormula, v8: BitsFormula, v9: BitsFormula): Formula This function takes exactly nine BitsFormulas and returns a formula representing whether the tuple is a magic square. The tuple (v1, v2, v3, v4, v5, v6, v7, v8, v9) represents the following square: v1 v2 v3 v4 v5 v6 v7 v8 v9 You can assume that the given BitsFormulas consist of exactly four boolean formulae each (i.e., they are four-bit numbers). Hint: you will want to call isdigit and disjoint in your implementation of ismagic. You can try out your ismagic function by seeing if it can decide that the square above is magic. You can also try replacing one or two entries by variables, and then seeing if your code correctly finds the right numbers to fill in. However, you ll have to wait a long, long time if you try making a lot of the entries in the magic square variables, since your sat function probably takes exponential time. 7

(d) Testing. Write unit tests in the same way as in previous homework assignments. Templates for the tests are given in the homework skeleton. Recall that a test case is a Boolean expression that evaluates to true to indicate success. Acknowledgements. This project on Magic Squares is based on a similar one designed by Jeff Foster. 8