07 Hindley–Milner type notation
Lesson goal Learn to read Hindley–Milner signatures — which arguments a function takes, what it returns and whether it is curried — and to write a function from its signature.
You have already come across Hindley–Milner type notation in the code — above functions, in comment blocks.
This type notation is used in Haskell and other languages with a functional bent. In JS it helps us write more abstract code, and it also helps maintain that code.
It explains which argument types a function accepts and what it returns.
// without the notation
const foo = (a, b) => a + b
// from the function above we can't tell whether it adds or concatenates
// of course, we could give foo a proper name
// of course, we could write the functions like this
const concat = (a, b) => a + b // concatenation
const add = (a, b) => a + b // addition
// with the notation
// functionName :: FirstArgumentType -> SecondArgumentType -> ResultType
// this is how a curried function is written: it takes its arguments one at a time
// addCurried :: Number -> Number -> Number
const addCurried = (a) => (b) => a + b // addition
// for an uncurried function, the argument types are listed in parentheses, separated by commas
// add :: (Number, Number) -> Number
// concat :: (String, String) -> String
// map :: (a -> b) -> [a] -> [b]
const map = (mappable) => (arr) => arr.map(mappable)
// the same thing, but in the more general form used in category theory
// and the Fantasy-land specification; don't worry, we will get to that later
// Spoiler: a functor is any type that implements the map interface
// and obeys its laws (more on them in the "Fantasy-land" lesson),
// so this same map function works not only with an array but with any value that has a map method
// it reads like this: for any functor f, it takes a function a -> b, then f a, and returns f b
// map :: Functor f => (a -> b) -> f a -> f b
// How to read the notation
// a, b, c type variables: any type, but within a single signature, the same letter always means the same type
// a -> b -> c a curried function: takes a and returns a function b -> c
// (a, b) -> c an uncurried function of two arguments
// * any type
// a | b a value of type a or of type b
// () -> a a function with no arguments
// ...args any number of arguments
// [a] an array of values of type a
// Maybe a a value of type a, or its absence
// Functor f => a constraint: f must be a functor
// f a ~> (a -> b) -> f b ~> denotes a method: it is called on a value of type f a, e.g. u.map(fn)Key points
- Hindley–Milner notation in a comment above a function explains which argument types the function accepts and what it returns. In JS it helps write more abstract code and maintain it.
- From the code (a, b) => a + b you can't tell whether the function adds numbers or concatenates strings. The signatures add :: (Number, Number) -> Number and concat :: (String, String) -> String tell you.
- a -> b -> c is a curried function: it takes a and returns a function b -> c. For an uncurried function, the argument types are listed in parentheses, separated by commas: (a, b) -> c.
- a, b, c are type variables: any type, but within a single signature the same letter always means the same type. [a] is an array of values of type a, Maybe a is a value of type a or its absence.
Exercises
Exercise 1
Write the functions from the signatures in the starter. between takes a lower bound, an upper bound and a number, and tells whether the number lies between them, inclusive. countWhere takes a predicate and an array and returns how many elements match the predicate. The signature of between shows that between(18)(65) is already a Number -> Boolean predicate.
Starter code
// between :: Number -> Number -> Number -> Boolean
// your code
// countWhere :: (a -> Boolean) -> [a] -> Number
// your code
const ages = [12, 17, 25, 33, 41, 70]
// isWorkingAge :: Number -> Boolean
const isWorkingAge = between(18)(65)
console.log(countWhere(isWorkingAge)(ages))
console.log(typeof isWorkingAge)Expected output
3 function
Show solution
// between :: Number -> Number -> Number -> Boolean
const between = (min) => (max) => (x) => x >= min && x <= max
// countWhere :: (a -> Boolean) -> [a] -> Number
const countWhere = (predicate) => (arr) => arr.filter(predicate).length
const ages = [12, 17, 25, 33, 41, 70]
// isWorkingAge :: Number -> Boolean
// two of the three arguments are passed: what is left is a function Number -> Boolean
const isWorkingAge = between(18)(65)
console.log(countWhere(isWorkingAge)(ages))
console.log(typeof isWorkingAge)Exercise 2
The signature safeDivide :: Number -> Number -> Maybe Number says that there may be no result. Write safeDivide: it divides a by b and returns S.Just(quotient), or S.Nothing if b is zero. The last line of the starter takes the number out of the Maybe with S.fromMaybe and a fallback of 0, as in the "Functional programming" lesson.
Starter code
// safeDivide :: Number -> Number -> Maybe Number
// your code
console.log(safeDivide(10)(2))
console.log(safeDivide(1)(0))
console.log(S.fromMaybe(0)(safeDivide(1)(0)))Expected output
Just (5) Nothing 0
Show solution
// safeDivide :: Number -> Number -> Maybe Number
// Maybe Number: a value of type Number, or its absence
const safeDivide = (a) => (b) => b === 0 ? S.Nothing : S.Just(a / b)
console.log(safeDivide(10)(2))
console.log(safeDivide(1)(0))
console.log(S.fromMaybe(0)(safeDivide(1)(0)))Playground
Write and run JavaScript right here — Ramda (R) and Sanctuary (S) are available.