I have a type something like this:
data MyType = I Int | C Char -- and lots of other options I want to be able to find out if a value of this type is a specific variant. I could define functions isInt, isChar, and so on using pattern matching. But I'd rather be able to write just one function, something like this:
hasBaseType :: MyType -> (a -> MyType) -> Bool hasBaseType (f _) = True hasBaseType _ = False I would pass the appropriate constructor (I or C) as the second parameter. Unfortunately, you can't pattern match like that.
I also want to "unwrap" the value. I could write the funtions unwrapInt, unwrapChar, and so on, again using pattern matching. But I'd rather be able to write just one function, something like this:
unwrap :: MyType -> (a -> MyType) -> a unwrap (f x) = x unwrap _ = error "wrong base type" Is there some fancy type magic that would allow me to do this? I thought maybe PatternSynonyms would help here, but I couldn't figure out how.