Lecture 13 — 2015-10-14

Still more types

This lecture is written in literate Haskell; you can download the raw source.

We finally got around to implementing the intrinsic simply typed lambda calculus (STLC). The code below is what we had when we were done, having added a string type.

import qualified Data.Map as Map
import Data.Map (Map, (!))

type Id = String

data STLC =
    Var Id
  | Lam Id Ty STLC
  | Str String
  | Concat STLC STLC
data Ty = 
  | TArrow Ty Ty deriving (Eq, Show)
type Context = Map Id Ty

x = Var "x"
y = Var "y"
z = Var "z"
s = Var "s"

typeCheck :: Context -> STLC -> Ty
typeCheck ctx (Var x) = ctx ! x
typeCheck ctx (App e1 e2) = 
  case typeCheck ctx e1 of
    TArrow t1 t2 -> 
      if typeCheck ctx e2 == t1
      then t2
      else error "double borked"
    _ -> error "borked"
typeCheck ctx (Lam x t1 e) = 
  let t2 = typeCheck (Map.insert x t1 ctx) e in
  TArrow t1 t2
typeCheck ctx (Str s) = TString
typeCheck ctx (Concat e1 e2) =
  case (typeCheck ctx e1, typeCheck ctx e2) of
    (TString, TString) -> TString
    _ -> error "double secret borked"

At the end of class we had some spare time, so I spoke about the Curry-Howard correspondence, wherein we discover that the structure of STLC’s types corresponds precisely to propositional logic and natural deduction. Minds were blown.