# Programming in Haskell (Powered by Category Theory) ###### tags: `cat` `hask` [TOC] ## Ref. https://en.wikibooks.org/wiki/Haskell/Category_theory http://yogsototh.github.io/Category-Theory-Presentation/categories.html#slide-0 ## Basic Types, Classes ### Types 1. Basic types: `Bool`, `Char`, `String`, `Int`, `Integer`, `Float`, `Double` 2. List types: `[T]` 3. Tuple types: `(T1, T2, ...Tn)` ($n \neq 1$) 4. Function Types: `T1 -> T2` 5. Polymorphic Types: A type that contains one or more *type variables* `a` * ex: `length :: [a] -> Int` `id :: a -> a` 6. Overloaded types: A type that contains one or more *class constraints* `C a` * ex: `(+) :: Num a => a -> a -> a` ...for any type `a` that is an instance of the class `Num`, the function `(+)` has type `a -> a -> a`. * ex: `3 :: Num a => a` ### Classes 1. `Eq` - equality types, supporting * `(==) :: a -> a -> Bool` * `(/=) :: a -> a -> Bool` 2. `Ord` - ordered types, supporting * `(<) :: a -> a -> Bool` * ...and similarly `>`, `<=`, `>=`, `min`, `max` 3. `Show` - showable types, supporting * `show :: a -> String` 4. `Read` - readable types, supporting * `read :: String -> a` 5. `Num` - numeric types, supporting * `(+)`, `(-)`, `(*)` `:: a -> a -> a` * `negate`, `abs`, `signum` `:: a -> a` 6. `Integral` 7. `Fractional` ## Declaring Types, Classes ### **Type** Declarations * `type String = [Char]` * `type Pos = (Int,Int)` * `type Pair a = (a,a)` * `type Assoc k v = [(k,v)]` ### **Data** Declarations * `data Bool = False | True` * `data Move = North | Source | East | West` * `data Shape = Circle Float | Rect Float Float` --- ## Terminoligy * *unit*, *return*, *pure* `return :: d -> m d` * *counit*, *extract* `extract :: w c -> c` ---