Generalized Algebraic Data Types I

Part IV of the series Fun with Functional C#.
May 9, 2016

This is the first of two articles on GADTs. This first part will be a general introduction to GADTs and their utility, while the second part will show how we can wrangle GADT behaviour out of C#.

The canonical GADT introduction involves a demonstration of the inadequacy of algebraic data types. But since this is written from a C# perspective, and C# doesn’t have GADTs, we’ll start with a brief introduction to vanilla ADTs.

Algebraic Data Types

Algebraic data types allow us a sort of type-level composition that’s more rigorous than what we have in C#. There are two ways to compose types in this algebra: products and sums, which are roughly analogous1 to products and sums over the integers.

Product types

Product types allow us to combine two or more types into one compound type. In Haskell, we can combine two types into a pair:

data Pair a b = Pair a b
More...




Why is SelectMany so weird?

Part III of the series Fun with Functional C#.
March 17, 2016

This episode, we’ll be looking at why SelectMany will look so strange to Haskell users, why that’s linked to some implementation-level issues with C#, and the extremely cool way that the C# team got around those issues.

A quick recap: LINQ query syntax is actually just Haskell do-notation, specialized for the list monad.

We can see this by looking at the signature for SelectMany:

IEnumerable<B> SelectMany<A, B>(
  this IEnumerable<A> source,
  Func<A, IEnumerable<B>> selector
)

It’s a less polymorphic Haskell bind:

bind :: Monad m => m a -> (a -> m b) -> m b

When I first saw this, I immediately attempted to write my own monad instance in C#, and had a Bad Time. But figuring out why it wasn’t so easy was probably more interesting than writing the monad instances in the first place!

More...




The Reader Monad

Part II of the series Fun with Functional C#.
March 16, 2016

In this installment of Fun with Functional C#, we’re going to be implementing the reader monad, and attempting to figure out if it’s actually useful. The reader monad was the first monad I implemented when I found out that SelectMany was bind. I thought it would be a good way to get rid of runtime dependency injection errors, but it turned out not to be the panacea I’d hoped for.

Implementation

The first thing I did was implement SelectMany, but it had some quirks that I’ll discuss in the next installment of this series. For now, let’s just look at the implementation:

public class Reader<E, A>
{
    private Func<E, A> f;
    public Reader(Func<E, A> func) 
    {
        f = func;
    }

    public A RunReader(E env) 
    {
        return f(env);
    }
}

This is basically equivalent to

newtype Reader e a = Reader { runReader :: (e -> a) }
More...




Functional C#

Part I of the series Fun with Functional C#.
March 15, 2016

With the introduction of LINQ in C#3, Erik Meijer and co. rebranded monads and presented them to a huge audience, and I think the way they did it was a brilliant piece of programming language UX design.

Apparently, before Erik Meijer joined the team working on LINQ, their goal was just to reduce the impedance mismatch between SQL databases and C#. So the solution they came up with was to let developers query SQL databases by writing SQL queries directly within C#- basically a language-level ORM.

I guess someone must have pointed out, wait a minute, there’s also the same impedance mismatch when you’re interacting with XML data- why are we limiting ourselves to relational databases?

And then Erik Meijer came along1 and said, query comprehension can be represented as a monad, so we can do all these things, and make it work for in-memory objects too, and reactive streams, and any queryable we can think of!

Now Pandora’s Monad2 is open, the M-word is floating around the hallways and people are getting into heated discussions about the Yoneda Lemma and the co-Yoneda lemma. Your coworkers are saying, look at Haskell, these problems have been solved in a general way for years!

It must have been tempting to just blindly retrofit functional concepts into C# and tell developers to just fmap and fold and bind to their heart’s content. But they didn’t, because immediately grasping the utility of these concepts is almost impossible at first glance, so they stuck a particular abstraction level and stuck with it. I guess a part of it could have been that C#’s type system can’t actually represent monads, but to me it looked like a concious design decision to solve the problem at an appropriate level of abstraction.

More...




Prerendering MathJax

September 14, 2015

I use MathJax on this blog. It’s a great tool, but it’s really slow, and worse, it’s slow in a way that draws attention to itself. A page with even a bit of MathJax will go through these stages:

  1. Unrendered MathJax markup
  2. Initial render with incorrect font
  3. Render with correct font, but with incorrect spacing and alignment
  4. Render correctly

The entire process can take a few seconds, and it’s rather jarring to watch text reflow at each stage as MathJax renders.

Khan Academy has noticed this problem, so it’s developed an alternative called KaTeX, which can render at many times the speed of MathJax. Unfortunately, it only implements a small subset of MathJax’s functionality, and I wanted to draw commutative diagrams, which weren’t supported. Even quite basic things like \begin{split} aren’t yet supported, so I’m stuck with MathJax for the time being.

More...




Previous Next