Hacker News new | past | comments | ask | show | jobs | submit login

The `Payment` example in the post is effectively an implementation of algebraic datatypes. `PaymentPattern` is how the code specifies "these are the only such shapes", and exhaustiveness is checked because the typechecker won't let you provide a `PaymentPattern` that omits any cases. It's certainly not as flexible as pattern matching like you'd get in a serious functional language, but I think it handles the use case "condition over cases of an algebraic type" just fine.

> The `Payment` example in the post is effectively an implementation of algebraic datatypes.

Algebraic data types are what we call "initial": they're characterized by how they're built up structurally. The 'PaymentMatcher' example, while using the terms "matcher" and "pattern" in the variable names, is actually closer to a "final" solution, since they're characterized by how they behave.

This is a bit abstract, but to put it simply, the key difference between what you and I are talking about is the same difference between algebraic datatypes and type classes. Algebraic datatypes are built up with constructors, and then exist structurally. On the other hand, type classes are more ephemeral. When declaring a type class, we say "these N methods are the ways you can 'poke' and 'prod' me to get a response," that is, it's the behavior of the type class that's most important.

So in fact, it's not that algebraic datatypes are more flexible than type classes or vice versa, but rather that they're duals of each other; they codify similar concepts, but approach the problem from two different sides.

I find this sort of stuff fascinating! Some of my favorite articles dealing with these sorts of nuances:

- Typed Tagless Final Interpretters[1]

- Practical Foundations for Programming Languages, Chapter 15 "Inductive and Coinductive Types"[2]

[1]: http://okmij.org/ftp/tagless-final/course/lecture.pdf

[2]: http://www.cs.cmu.edu/~rwh/pfpl.html

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact