Squash the irrelevant bits

Posted on 2018-05-28 by Oleg Grenrus notes

Squashed c x let a library writer provide x in "c-irrelevant" way to a library user.

The definition is simple:

Squashed is almost like Cont 1 or Codensity 2, so Squashed is a Monad:

Monad-instance allows to work on the wrapped value, for example

However, we cannot extract the original value, only as much as the constraint let us:

This restriction maybe be useful to enforce correctness, without relying on the module system!

Squash c x is a generalised notion of "free c over x", e.g. Monoid as described in Free Monoids in Haskell 3. It should be possible to write c (Squashed c x) instances for all (reasonable) c. Or actually (forall x. c' x => c x) => c (Squashed c' a) after Quantified Constraints -proposal 4 is implemented. (TODO: amend when we have the extension in released GHC).

As with Singleton containers 5, tell me if you have seen this construction in the wild!

Addendum: As Iceland_jack pointed on Twitter 6 7 there is a free-functors 8 package on Hackage, and more is written about Squash:

Note, that Squash doesn’t let us turn a thing into something it isn’t...

... though we can foolishly think so:

So Squash let’s only forget, not to "remember" anything new.

By the way, this post is genuine Literate Haskell file, using LaTeX, not Markdown. If interested on how, check the gists repository 9. I’m weird, as after some point of markup complexity, I actually prefer LaTeX.


  1. https://hackage.haskell.org/package/transformers-0.5.5.0/docs/Control-Monad-Trans-Cont.html#t:ContT

  2. http://hackage.haskell.org/package/kan-extensions-5.1/docs/Control-Monad-Codensity.html#t:Codensity

  3. http://comonad.com/reader/2015/free-monoids-in-haskell/

  4. https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0018-quantified-constraints.rst

  5. http://oleg.fi/gists/posts/2018-05-12-singleton-container.html

  6. https://twitter.com/Iceland_jack/status/1001081879045525504

  7. https://twitter.com/Iceland_jack/status/1001083326965407745

  8. https://hackage.haskell.org/package/free-functors

  9. https://github.com/phadej/gists

Site proudly generated by Hakyll