This is my attempt to describe monoids and comonoids and how to use them. Hope you’ll enjoy it. You should understand what is monoid, in other case go and read Understanding monoids before reading this post. Also you need to understand what is dual. Info about duals could be find here.
I spend a lot of time trying to understand monads, but never look at monoid because it feels so simple. Actually in pure languages monoid is really simple, just a pair of funcs () -> ‘a and (‘a * ‘a) -> ‘a. Second one is some join operation and is ok, but the first one has unit as an input argument. As we remember unit type usually tell us that function can have side effects. But in language like Haskell there are no side effects. So implementation for that function is usually trivial. And if we look at monoid’s dual comonoid then we will see a pair of funcs: ‘a -> () and ‘a -> (‘a * ‘a). Actually in haskell every type has comonoid implementation for free: fun x -> () and fun x -> x,x. First has a name destroy and the second one is clone or pair. All articles about comonoids usually say nothing. And I didn’t find anyone for fsharp. So I decided to write my own. Because in fsharp we have side effects everywhere and monoids and comonoids could be used to compose them.
Lets imagine that we are in the body of Don Syme and have a task to invent a brand new language codename Fsharp vNeXXXt. So lets start. We want to be cool guys, so we have to use functional programming. Our main building block will be a function. Inventing a language is a task like an universe creation. And in our universe we have nothing. This universe is a place where programs written in our language will live. Our universe is inside of other universe “The Interpreter”. So we can communicate to this external world somehow. Currently we have no types and we have a name for nothing, it is unit and could be writen as (). Any program written in our language should start from nothing and finish with nothing. It will be a function () -> (). And in our language without types, every function has the same signature () -> (). But they are not the same, because they actually forces our interpreter to do something. But we need a way to compose our program from small building blocks. We could connect output of one func to input of other func. Lets introduce a function composition func/operator(») and a pair. Composition func will take a pair of funcs (()->(),()->()) and return a single one. But lets add some fsharp like syntactic sugar (»)(f1,f2) = f1 » f2. So a program in our language will be something like that: fn1 » fn2 » fn3. But for correctness we also need an associativity rule: (»)(fn1,(») (fn2,fn3))) = (»)((»)(fn1,fn2), fn3)). So far so good. But to do something useful we need types. But we have no types and doesn’t know how to create them. So lets invent a constructor funcs. They have a signature of () -> ‘a where ‘a is a placeholder for created type, for example () -> int will create a number. We need to have several such functions for every possible int number. It is ok for now. But how can we compose our functions? We can use » to () -> () and () -> ‘a. But we cant compose them in different order. We don’t know how to destroy objects(yet). But could we magically convert any ()->() into () -> ‘a. Yes we can, but for every type ‘a in our system, we need a special func () -> ‘a’ which returns some kind of Empty representation of a type. Lets name that func as mempty. So now we could compose () -> () » mempty and it will give us a new func which will looks like constructor. () -> ‘a. Now we need to find a way to compose two funcs ()->’a. Hm, only way to do that is to merge two results into a single one. So our constructors composition func, will look like that (() -> ‘a,() -> ‘a) -> (() -> ‘a) and internally it will use some func lets name it as mappend which will take two inputs and will return one (‘a,’a) -> ‘a and this func also has to be associative. Interesting, but this func takes two inputs and gives us only one output. But we need a way to destroy one of inputs and return other (or destroy both of them and create a new one) or in other case our universe will be full of unused objects. Oh no, our language starts to grow too fast. It is pity, but we forced to add a new construct. Destructor: ‘a -> (). The point of destructors is to destroy an instance of a type. Now we have everything to express basic mappend func. How to compose destructors with both ()->() and constructors ()->’a? Constructor and destructor can be composed with function composition in both ways and ‘a -> () and () -> () also could be composed and form a new destructor, but ()->() and ‘a -> () cannot. What could we do? We could compose () -> () with mempty and after that compose empty constructor ()->’a with our destructor ‘a -> (). But how could we compose destructors? We have to introduce a destructors compose func (‘a -> (),’a -> ()) -> (‘a -> ()). Inside this func we have to somehow clone input value and pass it into two destructors and return nothing. So internally we have to use split func ‘a -> (‘a,’a) defined for our type ‘a. It is also have to be associative. Ok lets add … Wake up Neo. Wake up. Oh no our language was just a dream, and we are not Don Syme and we have to go to work and write some big enterprise code in fsharp. But lets think about that tiny language. It has interesting properties we could compose destructors and constructors and also every created resource will be disposed when program finishes. Could we use something like that in our fsharp code. Definitely yes. So any type which has a pair of functions mempty and mappend is a monoid. And every type which has a destructor and split functions is a comonoid.
Lets play with some simple type to warm up our brain. For example string. Lets define required functions for it.
It was trivial and there are no side effects. Lets create compose functions and some additional constructors and destructors with side effects.
Yes it is funny. I have never thought that printf “%s” is a string destructor. Unfortunately this code is not really interesting. But could we use it for something more real. For example for composable resource disposing? Lets define simple type which will hold destroy function(for example Dispose method invocation) and implement comonoid and monoid funcs.
Now when we create some object we will return dispose func with it.
Now lets think how could we compose functions which use that type. We have constructors () -> Dispose<’r’>, destructors Dispose<’r’> -> (), pure funcs ‘a -> ‘b and consumer funcs Dispose<’r’> -> ‘res. Seems that we have to find some general representation for that funcs, but how to find it. Hm seems that funcs really similar to funcs in reader,writer and state monads. So actually we can use something like that and create common representation and computation builder for that. If you want to refresh memories about these monads then you need to read Stateful computations in F# with update monads
The idea is quite simple, we just need to follow several rules.
Now we could move previously described constructors into our builder and use our new builder.
if you execute it, you will see that resources are disposed in expected way and we didn’t use “use” keyword. Now we are able to pass objects into async computations and run them in parallel.
That it. Hope you found something interesting for you.
Code is here