i’m back…

sorry folks… it’s been too long. i’m back with you again after a long break from blog world. i’ll try to keep you up to date for the remaining pavement shows. this weekend, we’re in montreal for the osheaga festival. next weekend we are in japan, and… Continua a leggere

Pubblicato in Senza categoria

The universal space

Take (or imagine) a blank sheet of paper. This is a plane. You can put points and vectors on it. A vector connects two points, but it is “movable”: if you can translate one vector into another, they are deemed equal. A vector doesn’t really have a begi… Continua a leggere

Pubblicato in Senza categoria

Kind polymorphism in action

Ultrecht Haskell Compiler is an experimental Haskell compiler that supports polymorphism on the kind level. This means that in

data Eq a b = Eq (forall f. f a -> f b)

Eq is given kind

Eq :: forall a . a -> a -> *

and both Eq Integer Char and Eq [] Maybe are valid types.

Using kind polymorphism, it is possible to write sigfpe’s From monoids to monads using a single type class.

To talk about monoids, you need a category (mor), multiplication (mul) and a unit.

class Monoid mor mul unit m where
one :: mor unit m
mult :: mor (mul m m) m

With mor being (->), mul being (,), unit being () this is a normal monoid (one :: () -> m and mult :: (m,m) -> m.). For example:

instance Monoid (->) (,) () Integer where
one () = 1
mult = uncurry (*)

Now, instead of functions, there will be natural transformations; instead of (,) there will be functor composition; instead of unit there will be identity functor.

data Nat f g = Nat (forall x. f x -> g x)
data Comp f g x = Comp (f (g x))
data Id x = Id x
Nat :: (* -> *) -> (* -> *) -> *
Comp :: (* -> *) -> (* -> *) -> * -> *
Id :: * -> *

And here is the list monad. Notice kinds are different than in the previous case, but it is still an instance of the same type class.

instance Monoid Nat Comp Id [] where
one = Nat $ \(Id x) -> [x] -- one :: Nat Id []
mult = Nat $ \(Comp x) -> concat x -- mult :: Nat (Comp [] []) []

So, monads are really monoids in category of endofunctors.

If you invert the arrows, you get a comonad. Here’s the product comonad.


data CoNat f g = CoNat (forall x. g x -> f x)
data CoComp f g x = CoComp (g (f x))
CoNat :: (* -> *) -> (* -> *) -> *
CoComp :: (* -> *) -> (* -> *) -> * -> *

data Product w a = Product w a

instance Monoid CoNat CoComp Id (Product w) where
one = CoNat $ \(Product a b) -> Id b
mult = CoNat $ \(Product a b) -> CoComp $ Product a (Product a b)

Question: what are kinds of mor, mul, unit and m in the Monoid type class definition?

There’s a small lie here: monads require also a liftM/fmap function. Not all Haskell types of * -> * are functors, and I used that as a poor replacement.

I didn’t write monoid laws, which if translated happen to be monad laws. You’re welcome to read sigfpe’s original post. It’s hard to write them generically since there’s no access to fmap.

The code is available on hpaste and can be run in UHC. Continua a leggere

Pubblicato in Senza categoria

Back online

Yup, fixed it. Home PC is back online. Got some major catching up to do now to get the final 40k video up and running then I’m in the clear with you guys – right?? I hope so ! Then its just a case of “what next” Continua a leggere

Pubblicato in Senza categoria