One of the most common gripes people have when learning Haskell is the fact that typeclass “laws” are only laws by convention, and aren’t enforced by the language and compiler. When asked why, the typical response is “Haskell can’t do that”, followed by a well-intentioned redirection to quickcheck or some other fuzzing library.

But, to any experienced Haskeller, “Haskell’s type system can’t express X” is always interpreted as a (personal) challenge.

GHC Haskell’s type system has been advanced enough to provide verified typeclasses for a long time, since the introduction of data kinds and associated types. And with the singletons library, it’s now as easy as ever.

(The code for this post is available here if you want to follow along!)

Semigroups

Let’s start simple – everyone’s favorite structural addition to magmas, semigroups. A semigroup is a type with an associative binary operation, (<>) :

class Semigroup a where (<>) :: a -> a -> a

Its one law is associativity:

<> y) <> z = x <> (y <> z) (xy)(yz)

But, this class stinks, because it’s super easy to write bad instances:

data List a = Nil | Cons a ( List a) a (a) deriving Show infixr 5 `Cons` instance Semigroup ( List a) where a) Nil <> ys = ys ysys Cons x xs <> ys = Cons x (ys <> xs) x xsysx (ysxs)

This instance isn’t associative:

> (( 1 `Cons` 2 `Cons` Nil ) <> ( 3 `Cons` 4 `Cons` Nil )) <> ( 5 `Cons` 6 `Cons` Nil ) ghci(()) 1 `Cons` 5 `Cons` 3 `Cons` 6 `Cons` 2 `Cons` 4 `Cons` Nil > ( 1 `Cons` 2 `Cons` Nil ) <> (( 3 `Cons` 4 `Cons` Nil ) <> ( 5 `Cons` 6 `Cons` Nil )) ghci(()) 1 `Cons` 3 `Cons` 2 `Cons` 5 `Cons` 4 `Cons` 6 `Cons` Nil

But if you try to compile it, GHC doesn’t complain at all. Is this an error on the part of Haskell? Not quite; it’s an error on the part of the Semigroup typeclass not requiring proofs that the instance is indeed associative.

Let’s try again.

Verify me, Captain

We will now define Semigroup on the kind List , using -XDataKinds , instead of the type.

class Semigroup a where type ( x :: a) <> ( y :: a) :: a a)a) (%<>) :: Sing ( x :: a) -> Sing ( y :: a) -> Sing (x <> y) a)a)(xy) appendAssoc :: Sing ( x :: a) a) -> Sing ( y :: a) a) -> Sing ( z :: a) a) -> ((x <> y) <> z) :~: (x <> (y <> z)) ((xy)z)(x(yz))

Now, <> exists not as a function on values, but as a function on types. %<> is a function that performs <> at the value level, written to work with singletons representing the input types, so that GHC can verify that it is identical to the type family <> . (it’s 100% boilerplate and should pretty much exactly match the <> type family). Finally, appendAssoc is a proof that the type family <> is associative, using :~: (type equality witness) from Data.Type.Equality .

This means that, if a type is an instance of Semigroup , it not only has to provide <> / %<> , but also a proof that they are associative. You can’t write the full instance without it!

Semigroup is a “kind-class”, because it is a bunch of methods and types associated with a certain kind. Which <> is dispatched when you do something like x <> y depends on the kind of x and y . GHC does “kind inference” and uses the <> corresponding to the kinds of x and y .

Using the SingKind typeclass from the singletons library, we can move back and forth from Sing x and x , and get our original (value-level) <> back:

( <> ) :: ( SingKind m, Semigroup m) m,m) => Demote m -> Demote m -> Demote m x <> y = withSomeSing x $ \sX -> withSomeSing x\sX $ \sY -> withSomeSing y\sY %<> sY) fromSing (sXsY)

Now, let’s write the instance for List . First, we need to define the singletons:

data instance Sing ( xs :: List a) where a) SNil :: Sing Nil SCons :: Sing x -> Sing xs -> Sing ( Cons x xs) xsx xs)

Then, we can define the instance, using the traditional (++) appending that lists famously have:

instance Semigroup ( List a) where a) type Nil <> ys = ys ysys type Cons x xs <> ys = Cons x (xs <> ys) x xsysx (xsys) SNil %<> ys = ys ysys SCons x xs %<> ys = SCons x (xs %<> ys) x xsysx (xsys) = \ case appendAssoc SNil -> \_ _ -> Refl \_ _ SCons x xs -> \ys zs -> x xs\ys zs case appendAssoc xs ys zs of appendAssoc xs ys zs Refl -> Refl

Like I promised, %<> is a boilerplate re-implementation of <> , to manipulate value-level witnesses. appendAssoc is the interesting bit: It’s our proof. It reads like this:

If the first list is Nil : -- left hand side ( Nil <> ys) <> zs ys)zs = ys <> zs -- definition of `(Nil <>)` yszs -- right hand side Nil <> (ys <> zs) (yszs) = ys <> zs -- definition of `(Nil <>)` yszs So, no work needed. QED! (Or, as we say in Haskell, Refl !) If the first list is Cons x xs : -- left hand side ( Cons x xs <> ys) <> zs x xsys)zs = ( Cons x (xs <> ys)) <> zs -- definition of `(Cons x xs <>)` x (xsys))zs = Cons x ((xs <> ys) <> zs) -- definition of `(Cons x xs <>)` x ((xsys)zs) -- right hand side Cons x xs <> (ys <> zs) x xs(yszs) = Cons x (xs <> (ys <> zs)) -- definition of `(Cons x xs <>)` x (xs(yszs)) So, the problem reduces to proving that (xs <> ys) <> zs is equal to xs <> (ys <> zs) . If we can do that, then we can prove that the whole things are equal. We generate that proof using appendAssoc xs ys zs , and, wit that proof in scope…QED!

And, we’re done!

Note that if you had tried any non-associative implementation of <> (and %<> ), GHC would reject it because you wouldn’t have been able to write the proof!

Automatic Singletons

Deriving Sing and SingKind and both versions of <> is kind of tedious, so it’s useful to use template haskell to do it all for us:

$ (singletons [d| (singletons [d| data List a = Nil | Cons a (List a) deriving (Show) infixr 5 `Cons` appendList :: List a -> List a -> List a appendList Nil ys = ys appendList (Cons x xs) ys = Cons x (appendList xs ys) |]) instance Semigroup ( List a) where a) type xs <> ys = AppendList xs ys xsysxs ys ( %<> ) = sAppendList sAppendList = \ case appendAssoc SNil -> \_ _ -> Refl \_ _ SCons _ xs -> \ys zs -> _ xs\ys zs case appendAssoc xs ys zs of appendAssoc xs ys zs Refl -> Refl

The boilerplate of re-defining <> as %<> goes away!

And now, we we can do:

> print $ (( 1 :: Integer ) `Cons` 2 `Cons` Nil ) <> ( 3 `Cons` 4 `Cons` Nil ) ghci(( 1 `Cons` 2 `Cons` 3 `Cons` 4 `Cons` Nil

Ta dah!

Naturally, Maybe

Now that we have our basic infrastructure, let’s implement some other famous semigroups:

First, the inductive nats, data N = Z | S N:

$ (singletons [d| (singletons [d| data N = Z | S N deriving (Show) plus :: N -> N -> N plus Z y = y plus (S x) y = S (plus x y) |]) instance Semigroup N where type xs <> ys = Plus xs ys xsysxs ys ( %<> ) = sPlus sPlus = \ case appendAssoc SZ -> \_ _ -> Refl \_ _ SS x -> \y z -> \y z case appendAssoc x y z of appendAssoc x y z Refl -> Refl

And the standard instance for Maybe , which lifts the underlying semigroup:

$ (singletons [d| (singletons [d| data Option a = None | Some a deriving (Show) |]) instance Semigroup a => Semigroup ( Option a) where a) type None <> y = y type x <> None = x type Some x <> Some y = Some (x <> y) (xy) SNone %<> y = y x %<> SNone = x SSome x %<> SSome y = SSome (x %<> y) (xy) = \ case appendAssoc SNone -> \_ _ -> Refl \_ _ SSome x -> \ case SNone -> \_ -> Refl \_ SSome y -> \ case SNone -> Refl SSome z -> case appendAssoc x y z of appendAssoc x y z Refl -> Refl

> print $ S ( S Z ) <> S Z ghci S ( S ( S Z )) )) > print $ Some ( S Z ) <> Some ( S ( S ( S Z ))) ghci))) Some ( S ( S ( S ( S Z )))) )))) > print $ None <> Some ( S ( S ( S Z ))) ghci))) Some ( S ( S ( S Z ))) )))

Going Monoidal

Of course, we can now introduce the Monoid typeclass, which introduces a new element empty , along with the laws that appending with empty leaves things unchanged:

class Semigroup a => Monoid a where type Empty a :: a sEmpty :: Sing ( Empty a) a) emptyIdentLeft :: Sing x -> ( Empty a <> x) :~: x x) emptyIdentRight :: Sing x -> (x <> Empty a) :~: x (xa) empty :: ( SingKind m, Monoid m) m,m) => Demote m = fromSing sEmpty emptyfromSing sEmpty

Because working implicitly return-type polymorphism at the type level can be annoying sometimes, we have Empty take the kind a as a parameter, instead of having it be inferred through kind inference like we did for <> . That is, Empty (List a) is Empty for the kind List a .

As usual in Haskell, the instances write themselves!

instance Monoid ( List a) where a) type Empty ( List a) = Nil a) = SNil sEmpty = Refl emptyIdentLeft _ = \ case emptyIdentRight SNil -> Refl SCons _ xs -> _ xs case emptyIdentRight xs of emptyIdentRight xs Refl -> Refl instance Monoid N where type Empty N = Z = SZ sEmpty = Refl emptyIdentLeft _ = \ case emptyIdentRight SZ -> Refl SS x -> case emptyIdentRight x of emptyIdentRight x Refl -> Refl instance Semigroup a => Monoid ( Option a) where a) type Empty ( Option a) = None a) = SNone sEmpty = Refl emptyIdentLeft _ = Refl emptyIdentRight _

Play that Funcy Music

How about some higher-kinded typeclasses?

class Functor f where type Fmap a b ( g :: a ~> b) ( x :: f a) :: f b a b (b) (f a)f b sFmap :: Sing ( g :: a ~> b) b) -> Sing ( x :: f a ) f a ) -> Sing ( Fmap a b g x :: f b ) a b gf b ) -- | fmap id x == x fmapId :: Sing ( x :: f a) f a) -> Fmap a a IdSym0 x :~: x a a -- | fmap f (fmap g x) = fmap (f . g) x fmapCompose :: Sing ( g :: b ~> c) c) -> Sing ( h :: a ~> b) b) -> Sing ( x :: f a ) f a ) -> Fmap b c g ( Fmap a b h x) :~: Fmap a c ((( :.$ ) @@ g) @@ h) x b c g (a b h x)a c (((g)h) x

Fmap a b g x maps the type-level function g :: a ~> b over x :: f a , and returns a type of kind f b . Like with Empty , to help with kind inference, we have Fmap explicitly require the kinds of the input and results of g ( a and b ) so GHC doesn’t have to struggle to infer it implicitly.

And, of course, along with sFmap (the singleton mirror of Fmap ), we have our laws: fmap id x = x , and fmap g (fmap h) x = fmap (g . h) x .

But, what are a ~> b , IdSym0 , :.$ , and @@ ? They’re a part of the defunctionalization system that the singletons library uses. A g :: a ~> b means that g represents a type-level function taking a type of kind a to a type of kind b , but, importantly, encodes it in a way that makes Haskell happy. This hack is required because you can’t partially apply type families in Haskell. If g was a regular old a -> b type family, you wouldn’t be able to pass just g into Fmap a b g (because it’d be partially applied, and type families always have to appear fully saturated).

You can convert a g :: a ~> b back into a regular old g :: a -> b using Apply , or its convenient infix synonym @@ , like g @@ (x :: a) :: b

The singletons library provides type family Id a where Id a = a , but we can’t pass in Id directly into Fmap . We have to pass in its “defunctionalized” encoding, IdSym0 :: a ~> a .

For the composition law, we use (:.$) (which is a defunctionalized type-level . ) and apply it to g and h to get, essentially, g :. h , where :. is type-level function composition.

Now we Haskell.

$ (singletons [d| (singletons [d| mapOption :: (a -> b) -> Option a -> Option b mapOption _ None = None mapOption f (Some x) = Some (f x) mapList :: (a -> b) -> List a -> List b mapList _ Nil = Nil mapList f (Cons x xs) = Cons (f x) (mapList f xs) |]) instance Functor Option where type Fmap a b g x = MapOption g x a b g xg x = sMapOption sFmapsMapOption = \ case fmapId SNone -> Refl SSome _ -> Refl = \ case fmapCompose _ _ SNone -> Refl SSome _ -> Refl instance Functor List where type Fmap a b g x = MapList g x a b g xg x = sMapList sFmapsMapList = \ case fmapId SNil -> Refl SCons _ xs -> _ xs case fmapId xs of fmapId xs Refl -> Refl = \ case fmapCompose g h SNil -> Refl SCons _ xs -> _ xs case fmapCompose g h xs of fmapCompose g h xs Refl -> Refl

And there you have it. A verified Functor typeclass, ensuring that all instances are lawful. Never tell me that Haskell’s type system can’t do anything ever again!

Note that any mistakes in implementation (like, for example, having mapOption _ _ = None ) will cause a compile-time error now, because the proofs are impossible to provide.

As a side note, I’m not quite sure how to implement the value-level fmap from this, since I can’t figure out how to promote functions nicely. Using sFmap is the only way to work with this at the value level that I can see, but it’s probably because of my own lack of understanding. If anyone knows how to do this, please let me know!

Anyway, what an exciting journey and a wonderful conclusion. I hope you enjoyed this and will begin using this in your normal day-to-day Haskell. Goodbye, until next time!

Just one more

Hah! Of course we aren’t done. I wouldn’t let you down like that. I know that you probably saw that the entire last section’s only purpose was to build up to the pièce de résistance: the crown jewel of every Haskell article, the Monad.

class Functor f => Monad f where type Return a ( x :: a) :: f a a (a)f a type Bind a b ( m :: f a) ( g :: a ~> f b) :: f b a b (f a) (f b)f b sReturn :: Sing ( x :: a) a) -> Sing ( Return a x :: f a) f a) sBind :: Sing ( m :: f a) f a) -> Sing ( g :: a ~> f b) f b) -> Sing ( Bind a b m g) a b m g) -- | (return x >>= f) == f x returnIdentLeft :: Sing ( x :: a) a) -> Sing ( g :: a ~> f b) f b) -> Bind a b ( Return a x) g :~: (g @@ x) a b (a x) g(gx) -- | (m >>= return) == m returnIdentRight :: Sing ( m :: f a) f a) -> Bind a a m ReturnSym0 :~: m a a m -- | m >>= (\x -> f x >>= h) == (m >>= f) >>= h bindCompose :: Sing ( m :: f a) f a) -> Sing ( g :: a ~> f b) f b) -> Sing ( h :: b ~> f c) f c) -> Bind a c m ( KCompSym2 a b c g h) :~: Bind b c ( Bind a b m g) h a c m (a b c g h)b c (a b m g) h data ReturnSym0 :: a ~> f a f a type instance Apply ( ReturnSym0 :: a ~> f a) ( x :: a) = Return a x f a) (a)a x type KComp a b c ( g :: a ~> f b) ( h :: b ~> f c) ( x :: a) = Bind b c (g @@ x) h a b c (f b) (f c) (a)b c (gx) h data KCompSym2 a b c g h :: (a ~> f c) a b c g(af c) type instance Apply ( KCompSym2 a b c g h :: a ~> f c) ( x :: a) = KComp a b c g h x a b c gf c) (a)a b c g h x return :: ( SingKind a, SingKind (f a), Monad f) a,(f a),f) => Demote a -> Demote (f a) (f a) return x = withSomeSing x $ \sX -> withSomeSing x\sX fromSing (sReturn sX)

To help with kind inference, again, we provide explicit kind arguments for Return (the kind of the thing that is being lifted) and Bind (the original a and the resulting b ).

Some boilerplate exists there at the bottom — it’s the plumbing for the defunctionalization system. returnIdentRight requires a defunctionalized version of Return , so we can provide that by defining ReturnSym0 , and writing an Apply instance for it (which “applies” it the parameter x ).

We introduce KComp (kleisli composition) and its defunctionalized version in order to express the third law, because we don’t yet have type-level lambdas in Haskell. The actual function it is expressing is \x -> f x >>= g , and that definition is given on the type KComp a b c ... = Bind ... line. KCompSym2 is the defunctioanlized version, which is not a a -> f c but rather an a ~> f c , which allows it to be partially applied (like we do for composeBind ). And, finally, to hook all of this up into the defunctionalization system, we write an Apply instance yet again.

And, again, if anyone knows how I can write a value-level Bind , I’d definitely appreciate hearing!

Let’s see some sample implementations.

$ (singletons [d| (singletons [d| bindOption :: Option a -> (a -> Option b) -> Option b bindOption None _ = None bindOption (Some x) f = f x concatMapList :: (a -> List b) -> List a -> List b concatMapList _ Nil = Nil concatMapList f (Cons x xs) = f x `appendList` concatMapList f xs |]) instance Monad Option where type Return a x = Some x a x type Bind a b m g = BindOption m g a b m gm g = SSome sReturn = sBindOption sBindsBindOption = Refl returnIdentLeft _ _ = \ case returnIdentRight SNone -> Refl SSome x -> case sReturn x of sReturn x SSome _ -> Refl = \ case bindCompose SNone -> \_ _ -> Refl \_ _ SSome _ -> \_ _ -> Refl \_ _ instance Monad List where type Return a x = PureList x a x type Bind a b m g = ConcatMapList g m a b m gg m = sPureList sReturnsPureList = sConcatMapList f x sBind x fsConcatMapList f x = case sReturn x of returnIdentLeft x gsReturn x SCons y SNil -> case emptyIdentRight (unSingFun1 g y) of emptyIdentRight (unSingFun1 g y) Refl -> Refl = \ case returnIdentRight SNil -> Refl SCons _ xs -> case returnIdentRight xs of _ xsreturnIdentRight xs Refl -> Refl = \ case bindCompose SNil -> \_ _ -> Refl \_ _ SCons x xs -> \g h -> case bindCompose xs g h of x xs\g hbindCompose xs g h Refl -> case unSingFun1 g x of unSingFun1 g x SNil -> Refl SCons y ys -> y ys let gxs = sConcatMapList g xs gxssConcatMapList g xs = sConcatMapList h gxs hgxssConcatMapList h gxs = unSingFun1 h y hyunSingFun1 h y = sConcatMapList h ys hyssConcatMapList h ys in case distribConcatMap h ys gxs of distribConcatMap h ys gxs Refl -> case appendAssoc hy hys hgxs of appendAssoc hy hys hgxs Refl -> Refl -- | Proving that concatMap distributes over <> distribConcatMap :: Sing ( g :: a ~> List b) b) -> Sing ( xs :: List a) a) -> Sing ( ys :: List a) a) -> ConcatMapList g (xs <> ys) :~: ( ConcatMapList g xs <> ConcatMapList g ys) g (xsys)g xsg ys) = \ case distribConcatMap g SNil -> \_ -> Refl \_ SCons x xs -> \ys -> x xs\ys case distribConcatMap g xs ys of distribConcatMap g xs ys Refl -> let gx = unSingFun1 g x gxunSingFun1 g x = sConcatMapList g xs cmgxssConcatMapList g xs = sConcatMapList g ys cmgyssConcatMapList g ys in case appendAssoc gx cmgxs cmgys of appendAssoc gx cmgxs cmgys Refl -> Refl

Here we use unSingFun1 , which converts a singleton of a type-level function into a value-level function on singletons:

unSingFun1 :: Sing ( f :: a ~> b) b) -> Sing ( x :: a) a) -> Sing (f @@ x :: b) (fb)

The crux is that, given a Sing (f :: a ~> b) and a Sing (x :: a) , we can “apply” them to get Sing (f @@ x :: b)

The proofs for the list instance is admittedly ugly to write, due to the fact that List is a recursive type. It’s also tricky because Haskell has poor to little support for theorem proving and no real tools to help you write them efficiently. But, the proofs for Option are really something, aren’t they? It’s kind of amazing how much GHC can do on its own without requiring any manual proving on the part of the user.

Disclaimer

Don’t do this in actual code, please (why?). This post started off as an April Fools joke that accidentally compiled correctly for reasons which I cannot explain.

While I don’t recommend that you do this in actual code, but definitely do recommend that you do it for fun! The code in this post is available here if you want to play around!