Yes (+) is part of the Num typeclass, and everyone seems to feel you can’t define (*) etc for your type, but I strongly disagree. newtype Pair a b = Pair (a,b) deriving (Eq,Show) I think Pair a b would be nicer, or we could even just use the type (a,b) directly, but… This is … Read more

Suppose a function has side effects. If we take all the effects it produces as the input and output parameters, then the function is pure to the outside world. So, for an impure function f’ :: Int -> Int we add the RealWorld to the consideration f :: Int -> RealWorld -> (Int, RealWorld) — … Read more

We begin with the type of foldMap: foldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m foldMap works by mapping the a -> m function over the data structure and then running through it smashing the elements into a single accumulated value with mappend. Next, we note that, given … Read more

## Composing function composition: How does (.).(.) work?

Let’s first play typechecker for the mechanical proof. I’ll describe an intuitive way of thinking about it afterward. I want to apply (.) to (.) and then I’ll apply (.) to the result. The first application helps us to define some equivalences of variables. ((.) :: (b -> c) -> (a -> b) -> a … Read more

## Why is GHC complaining about non-exhaustive patterns?

It’s because the pattern matching is incomplete. There’s no guarantee that one of x==a, x<a, or x>a holds. For instance, if the type is Double and x is NaN then none of them are True.

## Composing Monads v. Applicative Functors

There are several notions by which types of kind * -> * might “compose”. The more important one is you can compose them “sequentially”. newtype Compose f g x = Compose { getCompose :: f (g x) } Here you can see that Compose has kind (* -> *) -> (* -> *) -> (* … Read more

## How does deriving work in Haskell?

The short answer is, magic :-). This is to say that automatic deriving is baked into the Haskell spec, and every compiler can choose to implement it in its own way. There’s lots of work on how to make it extensible however. Derive is a tool for Haskell to let you write your own deriving … Read more

## Why is there “data” and “newtype” in Haskell? [duplicate]

Both newtype and the single-constructor data introduce a single value constructor, but the value constructor introduced by newtype is strict and the value constructor introduced by data is lazy. So if you have data D = D Int newtype N = N Int Then N undefined is equivalent to undefined and causes an error when … Read more

## What does the `forall` keyword in Haskell/GHC do?

Let’s start with a code example: foob :: forall a b. (b -> b) -> b -> (a -> b) -> Maybe a -> b foob postProcess onNothin onJust mval = postProcess val where val :: b val = maybe onNothin onJust mval This code doesn’t compile (syntax error) in plain Haskell 98. It requires … Read more

## What part of Hindley-Milner do you not understand?

The horizontal bar means that “[above] implies [below]”. If there are multiple expressions in [above], then consider them anded together; all of the [above] must be true in order to guarantee the [below]. : means has type ∈ means is in. (Likewise ∉ means “is not in”.) Γ is usually used to refer to an … Read more