Slide 1

Slide 1 text

Haskell Types Revisited Lucian Mogosanu 25.02.2015

Slide 2

Slide 2 text

Type systems “Source code is for humans to read, and only incidentally for machines to run.” Tools for abstraction Tools for documentation Safeguards against errors

Slide 3

Slide 3 text

“Advanced” type systems Curry-Howard isomorphism: Proofs as programs Propositions as types

Slide 4

Slide 4 text

Haskell typing philosophy Strong Static Algebraic “If it passes the type checker, then it’s correct”

Slide 5

Slide 5 text

Primitive types 3 :: Int 120894192912541294198294982 :: Integer 3.14 :: Float a :: Char "The cake is a lie" :: String True :: Bool

Slide 6

Slide 6 text

Basic type constructors data Void data Unit = MkUnit data Bool = False | True data Kg = MkKg Int -- newtype?

Slide 7

Slide 7 text

Canonical types data Pair a b = Pair a b -- (a, b) data Either a b = Left a | Right b type Maybe a = Either Unit a -- Nothing | Just a data Identity a = Identity a data Const a b = Const a

Slide 8

Slide 8 text

Recursive data structures data Nat = Z | S Nat data List a = Empty | Cons a (List a)

Slide 9

Slide 9 text

Digression: type classes class Eq a where (==) :: a -> a -> Bool instance Eq Bool where True == True = True False == False = True _ == _ = False

Slide 10

Slide 10 text

Digression: kinds Types also have types They are called kinds > :k Bool Bool :: * > :k List List :: * -> * > :k List Int List Int :: * Well-formed typed expressions have the kind *

Slide 11

Slide 11 text

Type-level numbers data Z data S n class Card n where instance Card Z where instance Card n => Card (S n) where

Slide 12

Slide 12 text

Fixed-length lists newtype Vector n a = Vector { fromVect :: List a } Vector constructor is not sufficient Use smart constructors

Slide 13

Slide 13 text

Fixed-length lists: smart constructors newtype Vector n a = Vector { fromVect :: List a } vnil :: Vector Z a vnil = Vector Empty vcons :: Card n => a -> Vector n a -> Vector (S n) a vcons x (Vector xs) = Vector (x Cons xs)

Slide 14

Slide 14 text

Fixed-length lists: useful functions head and tail are type safe vhead :: Card n => Vector (S n) a -> a vhead (Vector (Cons x _)) = x vtail :: Card n => Vector (S n) a -> Vector n a vtail (Vector (Cons _ xs)) = Vector xs

Slide 15

Slide 15 text

Fixed-length lists: problems vappend? vappend :: Card n => Vector n a -> Vector m a -> Vector (n + m) a Type family for Z and S n Undecidable instances

Slide 16

Slide 16 text

Conclusion hackage.haskell.org/package/mono-traversable Dependently-typed languages: Idris, Agda, Coq Require (mostly) manual typechecking!