Non exhaustive pattern match error in class instance
To convert records from old versions to new, I have a typeclass with a convert function. There is an instance for every type of record. (Cf. previous question.)
class Converter c1 c2 where
convert : c1 -> c2
There exist instances for many record types like e.g.:
instance C1 C2 where
convert (C1 with ..) = C2 with ..
Now, I try to define one instance that just handles arrays of other types:
instance (Converter c1 c2) => Converter [c1] [c2] where
convert [c1] = map convert [c1]
This yields the error
Pattern match(es) are non-exhaustive
In an equation for ‘convert’:
Patterns not matched:
[]
(_:_:_)
I don’t get where the incomplete pattern match is, or how it could be made complete. Does someone understand this problem?
Thanks a lot,
mesch.
[c1] is a list with a single element. So as the error message suggests lists with 0 or 2 or more elements are not covered. You can just drop the brackets and you should be good:
convert c1 = map convert c1
That worked, thank you very much!
Cheers,
mesch.
To clarify a little bit for the benefit of future readers, there are three different [c1] in this code snippet:
-- Here, we're in the realm of types; c1 and c2 are
-- type variables and [c1] means the type "list of
-- which the elements are of type c1".
instance (Converter c1 c2) => Converter [c1] [c2] where
-- Here we've moved from the realm of types to
-- the realm of values. On the left, we're making
-- an assignment, and thus [c1] is a pattern match
-- that will match a list of exactly one element,
-- and we name that one element c1. On the right,
-- [c1] means we are constructing a list of one
-- element which happens to be the value bound to
-- the c1 symbol.
convert [c1] = map convert [c1]