We’re so close! (to our first milestone.) We’ve built all of the machinery necessary for our first attempt at constructing the tools we’ll used to procedurally generate stories.

First we built a command functor and derived free monads. Then we dualized the whole contraption and realized we’d need to connect them with cofree comonads. In the last post, we derived an interpretation function out of the Zap machinery, and used it to find an adjunction to our command functor.

All that’s left now is to provide a particular cofree comonad over our costory functor, and we can use the rest of our machinery to use it as interpreter for our story DSL.

Cofree Comonads: You Coiter Love ’Em

As a simple example, let’s write an interpreter which counts how many character state changes occur in the main flow of a story. We’ll ignore any interrupted storylines for now, because they pose an interesting challenge we’ll attack in the next post.

A challenge for us right now is this: if a cofree comonad is an infinite (co-)data structure, how can we ever hope to construct one? Our first attempt might be to use a fix :: (a -> a) -> a point:

Observe that this is almost what we want; unfortunately Cofree f a is constructed via Cofree a (f (Cofree a)) – we can’t use self because it’s not wrapped in an f, and without an Applicative constraint, we’ve got no way of getting it into one. So what do we do?

Well, we cheat, of course, and we write a version of fix that does what we want. We’ll call it coiter (pronounced “co-iter”) for reasons that will become evident in a moment:

coiter ::Functor f => (b -> f b) -> b ->Cofree f b

It would be a good exercise to derive coiter for yourself to get into the habit of playing type-tetris. If you can’t be bothered, however, here’s what I came up with:

This is where it all clicked for me: this Cofree CoStoryF b is an infinite list with state b. The head of this list is start, and each successive cons is the result on that state after handling one StoryF a action. The reason this works as an interpreter is that because it’s infinite we can always reduce our program further. But since our program is always finite, we have a guaranteed termination condition.

Machinery in the Library is Worth Two in the Bush

So, getting back to our “count the state changes in characters” interpreter, it seems reasonable to fix our state b as Int, and because it’s addition, start should be \(0\).

So all we need is a meaningful function of type Int -> CoStoryF Int. CoStoryF itself is a product type, so we’ll write two functions–one for either side.

Remember, the return type here is a specialized version of (ChangeResult, b) where the ChangeResult will be fed back into the third parameter of StoryF:

-- GADT form of the `Change` constructor for `StoryF`Change ::Character->ChangeType-> (ChangeResult-> a) ->StoryF a

and, even more cool, our Zap StoryF CoStoryF instance was designed specifically to do this plumbing for us and feed the fst of the result of handleChange into the (ChangeResult -> a) of Change, while using the snd as the ongoing state.

This. This right here is what all of our hard work has been in service of. You’ll notice that neither handleChange nor handleInterrupt is recursive – all of the reduction is handled by what we can now consider to be “library code.” Our programs are defined in terms of individual actions, our interpreters in terms of basic reductions of those individual actions. And the library code takes care of the rest!

interpret myStory changeCounter -- result: ("Feel good story of the year", 3)

Wow! It works! That’s oddly satisfying: we’ve managed to count to three, and it only took four blog posts and one hundred lines of code! We’ve now successfully build machinery for creating DSLs and interpreters over them, but we’ve still got a long way to go. In particular, some of our command functor’s semantics are wrong, and we’ll need to clean that up before we go much further.