where imageFormatToText is used to insert the sum type into a database column. When I add a constructor for ImageFormat, I’ll get a warning that I need to update imageFormatToText. So far so good.
I also need to parse this type from text:
imageFormatFromText :: Text -> Either String ImageFormat
imageFormatFromText "jpeg" = Right JPEG
imageFormatFromText "tiff" = Right TIFF
imageFormatFromText _ = Left "Unknown image format"
If I forget to handle a case (in this instance, I forgot to parse "png"), I will not get a warning.
My coworkers use a little trick to ensure completeness, like so:
imageFormatFromText :: Text -> Either String ImageFormat
imageFormatFromText "jpeg" = Right JPEG
imageFormatFromText "tiff" = Right TIFF
imageFormatFromText _ = Left "Unknown image format"
where
_completenessCheck :: ImageFormat -> ()
_completenessCheck JPEG = ()
_completenessCheck TIFF = ()
The advantage here is that we get a warning that _completenessCheck isn’t complete, which prompts us to update imageFormatFromText. However, _completenessCheck starts with an underscore, we don’t get a warning for an unused variable, which is neat.
Are there other methods for dealing with this possible incompleteness of parsing?
I haven’t typechecked this so no guarantees that it works. But something like this is what I tend to do. Added bonus is that this is O(logn) rather than O(n), and you aren’t duplicating the string names of your variants.
Another (more heavyweight) solution is to use a generics library like generics-sop to derive a product-like type from the sum-like ImageFormat, where the components of the type would be the parsing functions for each branch. That way, when you add a branch, GHC will complain that the values of the product type are missing a component.
Here’s how I would do it with my own library by-other-names:
data ImageFormat = JPEG | TIFF | PNG deriving (Show, Generic)
newtype Parser a = Parser (Text -> Maybe a)
deriving (Functor, Applicative,Alternative) via Kleisli Maybe Text
textEquals :: Text -> a -> Parser a
textEquals txt a = Parser (\t -> if t == txt then Just a else Nothing)
runParser :: Parser a -> Text -> Maybe a
runParser (Parser p) = p
imageFormatParser :: Text -> Either String ImageFormat
imageFormatParser t =
-- asum works because Aliases is Foldable
case runParser (asum branchParsers) t of
Nothing -> Left "Unknown image format"
Just r -> Right r
where
branchParsers :: Aliases (Rep ImageFormat) (Parser ImageFormat)
branchParsers =
aliasListBegin $
alias @"JPEG" (textEquals "jpeg" JPEG) $
alias @"TIFF" (textEquals "tiff" TIFF) $
alias @"PNG" (textEquals "png" PNG) $
aliasListEnd
Basically, you provide a mapping in one way and in the other you build a map with all pairs using Enum/Bounded instance of the type and store it in a Map (which is a top level CAF so GHC computes it only once). Benchmarks are here (TL;DR: it’s fast enough).
I’m not disagreeing with your solution, but it’s actually slower.
Its worst-case performance is (roughly) in O(log(n) * |largest_shared_prefix|) whereas the case matches on every char after another and so behaves more like a trie with performance in O(|largest_shared_prefix| * log(#chars_per_index)) where #chars_per_index <= n.
Way back when, I wrote the exhaustive for this general problem - you might be interested in this. These days I tend to rely on catching this with testing though
Good point! Something like a trie would be optimal here.
In this case, we are pattern matching on Text values, and GHC doesn’t know that a Text is really a packed array of chars, so the code it generates is a linear sequence of calls to (==).
This is a bugbear of mine. I know it doesn’t impact performance most of the time, but I wish GHC exposed some sort of interface that allowed us to tell it to generate a nicer case tree for Text values when there is a lot of shared prefixes, etc. They are one of the most used types after all!
Even with String it turns out that if you use String literals, it will generate the same linear (==) code rather than a nice case tree. I’m not sure why this is the case. Perhaps to reduce code size?
But if I use a list literal to create a String then indeed we do get nice code that works like a trie.
A perfect hash function is probably even better in general, but you should also take into account the actual problem size. If there are only three or four elements, then a linear scan could still be a very good contender. Write benchmarks!
Also, only go down this rabbit hole if you actually notice that parsing these things is significant when you’re profiling your full application.