How to resolve a conflicting instance in type families?

I am trying to create a "UnMaybe" type family, but it does not compile (conflicting instance family).

Here is my code

{-# LANGUAGE TypeFamilies #-} type family UnMaybe a :: * type instance UnMaybe (Maybe a) = a type instance UnMaybe a = a 

Error message

 test.hs:4:16: Conflicting family instance declarations: type instance UnMaybe (Maybe a) -- Defined at test.hs:4:16 type instance UnMaybe a -- Defined at test.hs:5:15 

I understand why it does not work, anyway there is another way to achieve the same result (or an extension for activation that will allow this?)

+6
source share
1 answer

If you really need it, you can use a closed family (at least GHC 7.8 is required):

 {-# LANGUAGE TypeFamilies #-} type family UnMaybe a :: * where UnMaybe (Maybe a) = a UnMaybe a = a 

Now:

 GHCi> :kind! UnMaybe (Maybe Int) UnMaybe (Maybe Int) :: * = Int GHCi> :kind! UnMaybe Bool UnMaybe Bool :: * = Bool 
+11
source

Source: https://habr.com/ru/post/972913/


All Articles