Homework 6
Types and type checking
This homework is written in literate Haskell; you can download the raw source to fill in yourself. You’re welcome to submit literate Haskell yourself, or to start fresh in a new file, literate or not.
Please submit homeworks via the DCI submission page.
module Hw06 where
import qualified Data.Map as Map
import Data.Map (Map, (!))Problem 1: Haskell’s type system
Explain the Haskell type for each of the following declarations:
a x y = x ++ " " ++ yb x y = x:head yc f = \y → f yd (f,x) = f (f x)e x y b = if b y then x else yf x y = foldl x Nothing (reverse y)
Because you can of course run the type checker yourself to determine their type, be sure to write a short explanation to show that you understand why the function has the type you give.
Problem 2: type checker for Expr
Write a type checker for the Expr language. Here’s the AST along with the types you should use.
type Id = String
data Expr =
    EVar Id
  | ETrue
  | EFalse
  | EIf Expr Expr Expr
  | ENum Int
  | EIncr Expr
  | EDecr Expr
  | EIsZero Expr
  | EApp Expr Expr
  | ELam Id Type Expr
  deriving (Show, Eq)
data Type =
    TyBool
  | TyNum
  | TyArrow Type Type
  deriving (Show, Eq)
type Context = Map Id Type
typeCheck :: Context -> Expr -> Either String TypetypeCheck _ _ = undefinedBe certain to return good error messages as applicable.
Problem 3: working with type theory
(a) 10 points
Suppose we wanted to add pairs to the type system for Expr. You don’t need to implement this change, just tell me everything you would have to do: how would you change the Type datatype? What about Expr?
(b) 10 points
Write the typing rules for pairs, i.e., the inference-style rules we’ve used in class.
Problem 4: static and dynamic scope
Please do problem 7.8 from Mitchell, page 196.
Problem 5: eval and scope
Please do problem 7.10 from Mitchell, page 197. You only need to do parts b and c, but please read part a so that the rest makes sense.
Problem 6: lambda calculus and scope
Please do problem 7.11 from Mitchell, page 198.