Suppose we have the following:
data Foo x from_list :: [x] -> Foo x to_list :: Foo x -> [x]
Suppose I want to declare instance (Show x) => Show (Foo x) such that showing a value produces the appropriate call to from_list. How exactly do I do that? In particular, how do I implement showsPrec so that the trixy fiddly precedence rules are satisfied? (That is, put the expression in brackets if and only if it’s necessary.)
What you want here is basically the same as what
Data.Setdoes, so we can just modify that a little:In other words, we use the
Showinstance for lists, prepend"from_list "to that, and useshowParento add brackets around it if the surrounding context has higher precedence than function application (which has precedence 10).