Most of a Haskell rewrite

Signed-off-by: Ricky Elrod <ricky@elrod.me>