Parsing with proofs

I was asked what sort of Haskell program is simple enough for newcomers to understand, deep enough to be interesting, and short enough to demo at lunchtime. Parsing immediately came to mind. It’s a good portal to (applicative) functors and monads, as well as compositional/combinatorial program construction in general. I threw together a tutorial starring this type

data StringParser a = StringParser (String -> Either NoParse (a, String))

but I noticed (and somehow this never bugged me until now) that we can do nefarious things like

badParser :: StringParser ()

badParser = StringParser $ \_ -> Right ((), "oops, sorry about your input")

It would be nice to somehow rule that out at compile time: to get a static guarantee that the String in the parser output is a substring of the input. Maybe GHC is capable of this, but I didn’t give it much thought; I immediately went to Idris to learn more about programming with dependent types.

This repository contains a definition of a parser such that the output string must be a tail of the input string (unless I got something wrong!). Check it out.