Algebraic Effects and Handlers in Under 50 Lines of Ruby

Some people say “effect handlers are the new monads”. So I thought, before all these “Effect Handlers are like X” (for X in { Burritos, Containers, … }) blog posts come into existence, I contribute another point-of-view. This blog post does not really explain the theory behind algebraic effects and handlers. Neither does it explain why you should use them, or how. We just look at how effect handlers work operationally, by implementing them.

This post is a bit longer. If you are in a hurry, I recommend you jump to Step 2.

I do recommend the blog post Algebraic effects for the rest of us which explores another aspect of the topic.

The Existential Unapply Trick

Did you ever run into the situation, where you thought you need higher ranked polymoprhism in Scala? While it is somewhat supported in Scala, once you go down this route, you loose some convenience that eventually disrupts your API. In this post, I show how in some cases we can use our old friend unapply in Scala to recover some of the convenience.

Even More Lightweight Monadic Regions

Delimiting the lifetime of a resource to a particular scope is a common problem. In this post, I revisit “Lightweight Monadic Regions” (Kiselyov and Shan, 2008) and show how to generalize the ST-trick to nest monadic regions in a new way.

Pointfree, Fully Annotated

When defining complex functions in pointfree style, I often find myself switching to pointful style. Sometimes, I even convert to ANF and annotate the types to understand the steps.

After completing the function definition, I often convert back to pointfree style for conciseness. End of the story: To understand it again a couple of weeks later, I start expanding to annotated pointful again.

The small 10-lines library at the end of this post allows to define pointfree functions with intermediate type annotations.

The Hitchhiker’s Guide to Morphisms

Since I always keep mixing up the names of morphisms, I finally created a cheat-sheet that now in its full glory decorates my office. The important part of the poster is, that it puts morphisms that deconstruct data (left column) with those that construct codata (right column) in contrast. So on one glance you can see that zygomorphisms are obviously dual to g-apomorphisms — of course, that’s an easy one.

The UberFunctor: Higher Order Functors

This week I was faced with the problem of abstracting over two functors like F1[O] = Int × O and F2[I, O] = (I => Int) × (I => O). Both look very similar, but not similar enough – so I had to repeat the functor with the slight changes over and over again. Then I came up with a solution…

Scala: Implicit Optionals

A few days ago I stumbled apon Paul Phillips awesome talk “Inside the Sausage Factory” – explaining some internals of the Scala compiler. In his talk he mentions that he does not like using Option since it is “tedious and syntax heavy”. He suggests to use the “null object pattern” – creating a “distinguishable object to server the role of none”.

A problem with this approach is that one looses all the nice methods Option provides such as getOrElse or map. In this post we will see how we can get the best of both worlds by using implicits.

From Term to Typelevel

During this winter break I decided to jump into developing an alternative Scala wrapper for the Java Framework Vaadin. Many parts of the API are dealing with measurements like pixels or cm, so having a good design for handling measurements is crucial. In this post we first develop an API on the termlevel and then shift parts of the validation to the typelevel using a typeclass encoding.

Lazy Attributes in ECMAScript 5

Working on some project I noticed myself using the memoization pattern many times to create lazy evaluated properties. With ES5 a new way of implementing lazy attributes becomes available, preserving the interface of an attribute.

Using Scala Type Constraints

I didn’t think I will ever write about homework assignments, but this one was too cool. The task: “Write a wrapper class for collections, that reifies all operations on it.” In other words, queries on the collection should be available as AST in order to perform optimizations on them. This post is about one small problem related to the task.