Referential Transparency An expression is referentially transparent if you can replace that expression with its corresponding value without changing the behavior of the program.
Referential Transparency Referential transparency means that when you read code, you only have to think of the outcome of an expression. You can forget about state, and the way that evaluating an expression can change that state.
Pointfree Programming Topology (a branch of mathematics) works with spaces composed of points, and functions between those spaces. So a pointfree definition of a function is one which does not explicitly mention the points (values) of the space on which the function acts.
Hindley-Milner Type System The Hindley-Milner Type System is a system that allows you to do type inference on lambda calculus, in order to derive the most generic type of every expression in a program.
Type Inference data List a = Nil | Cons a (List a) -- length :: List a -> Int length Nil = 0 length (Cons x xs) = "1" ++ length xs -- <- oops -- No instance for (Num [Char]) arising from the literal ‘0’
Type Inference data List a = Nil | Cons a (List a) length :: List a -> Int length Nil = 0 length Cons x xs = "1" ++ length xs -- <- oops -- Couldn't match expected type ‘Int’ -- with actual type ‘[Char]’
Type Inference data List a = Nil | Cons a (List a) length :: List a -> Int length Nil = 0 length Cons x xs = length -- <- totale nonsense -- Couldn't match expected type ‘Int’ -- with actual type ‘List a0 -> Int’
Type Inference the algorithm (roughly): 1. assign type variables to all expressions (both left and right of an ‘=’) 2. start at the top 3. move to the next piece of code that imposes a constraint 4. unify the two type variables 5. go back to step 3, until you reach the bottom
Type Inference constraints: ● the left and right hand side of ‘=’ must have the same type ● the result of ‘+’ has the same type as the expressions on either side ● applying a function with type a0 -> a1 is only valid on a parameter of type a0 ● applying a function with type a0 -> a1 always produces a result of type a1
The Maybe Functor class Functor f where fmap :: (a -> b) -> f a -> f b data Maybe a = Nothing | Just a instance Functor Maybe where fmap _ Nothing = Nothing fmap f (Just a) = Just (f a)
Derp examples of categories: ● Grp – a category with groups for objects and group homomorphisms as morphisms (see also group theory) ● Set – the category whose objects are sets, and where functions form the morphisms
The Maybe Functor -- fmap id Nothing = id Nothing -- Nothing = Nothing instance Functor Maybe where fmap _ Nothing = Nothing fmap f (Just a) = Just (f a)
The Maybe Functor -- fmap id (Just a) = id (Just a) -- Just (id a) = Just a -- Just a = Just a instance Functor Maybe where fmap _ Nothing = Nothing fmap f (Just a) = Just (f a)