CS 320 Homework One Due 2/5 11:59pm

Similar documents
CS 320: Concepts of Programming Languages

CS 320 Week 8 Homework Due W 3/27 11:59pm. incm 5 >>= (\x -> incm x >>= (\y -> incm y) )

CS 320: Concepts of Programming Languages

CS 320 Midterm Exam. Fall 2018

CS 320 Midterm Exam Solution

CS 320: Concepts of Programming Languages

Programming Languages

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 320: Concepts of Programming Languages

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

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

Functional Programming - 2. Higher Order Functions

CSCE 314 Programming Languages

Exercise 1 ( = 24 points)

CS 61A Discussion 8: Scheme. March 23, 2017

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Exercise 1 (2+2+2 points)

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

CSE Programming Languages Final exam - Winter Answer Key

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Exercise 1 ( = 22 points)

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

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

Programming with Math and Logic

Programming Languages and Techniques (CIS120)

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

Programming Languages ML Programming Project Due 9/28/2001, 5:00 p.m.

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Concepts of programming languages

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Exercise 1 ( = 18 points)

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

An introduction introduction to functional functional programming programming using usin Haskell

CS 314 Principles of Programming Languages. Lecture 21

Comp 311: Sample Midterm Examination

Type-indexed functions in Generic Haskell

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

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

Getting Started with Haskell (10 points)

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

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

Introduction to Haskell

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

Random Testing in 321

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 360: Programming Languages Lecture 12: More Haskell

Type families and data kinds

Problem 1: Binary Trees: Flatten

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Principles of Programming Languages

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

Bindings & Substitution

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

CS 320: Concepts of Programming Languages

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

CS 314 Principles of Programming Languages. Lecture 16

Introduction to Functional Programming in Haskell 1 / 56

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

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

CIS 120 Midterm I February 16, 2015 SOLUTIONS

Programming Paradigms

Overview. Elements of Programming Languages. Evaluation order. Evaluation order

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

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

IA014: Advanced Functional Programming

Project 1: Scheme Pretty-Printer


QIR Specification. Romain Vernoux. January 6, Notations 2

Getting Started with Haskell (10 points)

Graphical Untyped Lambda Calculus Interactive Interpreter

CS61A Midterm 2 Review (v1.1)

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

CSc 520 Principles of Programming Languages

Lecture #23: Conversion and Type Inference

Programming Language Concepts, CS2104 Lecture 7

CS115 - Module 10 - General Trees

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

INF2220: algorithms and data structures Series 1

cs173: Programming Languages Final Exam

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Lecture 5: Lazy Evaluation and Infinite Data Structures

Sample Final Exam Questions

LECTURE 16. Functional Programming

CSE Programming Languages Midterm - Winter 2010

Understanding Recursion

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Programming in Omega Part 1. Tim Sheard Portland State University

6.001 Notes: Section 8.1

Introduction to Programming, Aug-Dec 2006

Template Haskell based implementation of printf

CS1 Lecture 5 Jan. 26, 2018

Transcription:

Name: BU ID (no dashes): CS 320 Homework One Due 2/5 11:59pm Write your answers to the problems in the space indicated. Scan your solution and submit to Gradescope as a PDF file. You will receive an email about the Gradescope account. You may do this from your phone using free scanning apps, or with a desktop scanner. Do NOT edit this file and move things around, the format must remain the same. NOTE: You may be able to test some of your solutions to these using Haskell; but you will learn more if you try these first by hand, and remember that you won t have your computer with you on the midterms! Problem One: Consider the following data declarations: G H C (A) Draw tree diagrams of the following four data expressions. The first one is done for you above and to the right. (i) (G H C) (ii) (E (D C)) (iii) (G (G H B) (D B)) (iv) (G (G (E B) (D (D C) )) C ) (ii) (iii) (iv) (B) For the following, cross out with a big X any expression which would not be legal (i.e., would be flagged in Haskell as having a type error if you typed it into the Haskell interpreter). Haskell pays attention to parentheses and you should too. You don t have to explain any errors, just cross them out. A D G H (E (D C)) (D D A) (G (E B) C) G (E (D C)) (D A) G H (D (D C)) G (D H) (G H C)

Problem Two: Consider the following data and function declarations (assume this is only a part of the program, and that it works correctly). (A) Underline all occurrences of constructors in this program fragment. (B) Circle all occurrences of variables (including names of functions, wildcards, and type variables) (C) Put an asterisk (*) on each occurrence of a type name. data Either a b = Left a Right b eq :: Either Nat Bool ->Either Nat Bool -> Bool eq (Left x) (Left y) = x == y eq (Right x) (Right y) = x == y eq = False eqlist :: List (Either Nat Bool) -> List (Either Nat Bool) -> Bool eqlist Nil Nil = True eqlist (Cons x xs) (Cons y ys) = and (eq x y) (eqlist xs ys) eqlist = False

Problem Three: Consider the following declarations: data Nat = Zero Succ Nat data List a = Nil Cons a (List a) data Expr = Val Nat Plus Expr Expr Times Expr Expr For each of the following terms and patterns, give the bindings for the variables in the pattern which would match it to the term, AND give the type of each variable binding, OR state that such a matching substitution is not possible and describe where the conflict is that prevents matching. (A) term1 = (Cons Zero (Cons (Succ Zero) (Cons Zero Nil)) ) -- pattern1 = (Cons x (Cons (Succ Zero) y )) (B) term2 = Plus (Times (Val Zero) (Val (Succ Zero)) ) (Val Zero) -- pattern2 = (Plus (Times x (Val (Succ y))) (Val Zero)) (C) term3 = (Plus (Val Zero) ( Plus (Val (Succ Zero)) (Val (Succ Zero)))) -- pattern3 = Plus (Val y) (Plus (Val Zero) (Val x)) (D) term4 = Cons (Cons Zero Nil) (Cons (Cons (Succ Zero) Nil) Nil) -- pattern4 = (Cons (Cons x y) (Cons z Nil))

Problem Four: Consider the following declarations: data Bool = F T data Nat = Z S Nat -- Same as hw1 but abbreviated -- to make solutions easier to write. not :: Bool -> Bool not T = F not F = T if' :: Bool -> Nat -> Nat -> Nat if' T x _ = x if' F _ y = y -- same as cond in lecture red :: Nat -> Nat red Z = Z red (S x) = red x inf :: Nat -> Nat inf x = inf x Recall that evaluation of expressions proceeds by searching for a redex (a subexpression where some rule defining a function will match) and rewriting the subexpression (using the bindings generated by the match). Consider the following algorithm, which thinks of the expression as a tree (as in Problem One): while the expression contains some function symbol: for each subexpression in a preorder search of the expression: for each rule in order (top to bottom) in the program: if the rule matches: then do one rewrite and start over at the top of the while loop For each of the following, show the expressions at each step, ending in the final value; if the sequence would not terminate, show a few steps and indicate that the rewrite sequence is infinite. (A) if (not T) (red (S (S Z))) (if T Z (red Z)) (B) if (not F) (red (S Z)) (red (inf Z)) => if T (red (S Z)) (red (inf Z)) => (red (S Z)) => (red Z) => Z

NOW, for the next two problems, I want you to do the exact same expressions, but in the rewriting algorithm, search for the redex in a postorder (bottom-up, left to right) traversal of the expression (considered as a tree). (C) if (not T) (red (S (S Z))) (if T Z (red Z)) => if F (red (S (S Z))) (if F Z (red Z)) => if F (red (S Z)) (if F Z (red Z)) => if F (red Z) (if F Z (red Z)) => if F Z (if F Z (red Z)) => if F Z (if F Z Z) => if F Z Z => Z (D) if (not F) (red (S Z)) (red (inf Z)) => if T (red (S Z)) (red (inf Z)) => if T (red Z) (red (inf Z)) => if T Z (red (inf Z)) => if T Z (red (inf Z)) => if T Z (red (inf Z)) INFINITE REWRITE SEQUENCE! (E) Short answer: What was the difference between preorder (top down) and postorder (bottom up) in these examples? Be sure to comment on what happened in A compared with C, and B compared with D. Which seems to be a better strategy overall?