source : Parse, donât validate
When it comes to [[type-driven development]], the right approach should be [[parse, donât validate]].
[[Static type systems]] allow for figuring out whatâs possible to implement.
Given the [[Haskell]] type signature:
head :: [a] -> a
You can reason that this would yield a [[partial function]], because it cannot be implemented for [].
However, since we canât implement that type signature fully, we can adjust the signature to be:
head :: [a] -> Maybe a
indicating that the function may return nothing in certain cases. This may be suitable, but down the road it causes problems, as it forces the rest of the program to deal with a Maybe value.
The better solution would be to return a new type, NonEmpty, that constrains the type of data the function can work with.
data NonEmpty a = a :| [a]
head :: NonEmpty a -> a
head (x:|_) = x
Through this process we have strengthened our type.
Consider: what is a [[parser]]? Really, a parser is just a function that consumes less-structured input and produces more-structured output. By its very nature, a parser is a partial functionâsome values in the domain do not correspond to any value in the rangeâso all parsers must have some notion of failure. Often, the input to a parser is text, but this is by no means a requirement, and parseNonEmpty is a perfectly cromulent parser: it parses lists into non-empty lists, signaling failure by terminating the program with an error message.
Parsing with a static type system should therefore be simple: âif the parsing and processing logic go out of sync, the program will fail to even compile.â
[[Shotgun parsing]] : an [[antipattern]] whereby parsing and input-validating code is mixed with and spread across processing codeâthrowing a cloud of checks at the input, and hoping, without any systematic justification, that one or another would catch all the âbadâ cases
In practice, parsing without validation looks like focusing on [[datatypes]]. Specifically:
Alternatively, âwrite functions on the data representation you wish you had, not the data representation that you are given.â