DefaultSignatures and Related Type Families

Is there a way to use the DefaultSignatures extension with the corresponding type families.

Here is an example of why I need this.

 class Foo p where type Back p :: * type Forward p :: * customFunc :: p -> IO () newtype Bar a = Bar (Forward a) data Bat = Bat type family ForwardBar a :: * type instance ForwardBar Bat = Int instance Foo (Bar Bat) where type Back (Bar Bat) = Bat type Forward (Bar Bat) = ForwardBar Bat customFunc _ = print "I am different customFunc and this is Bat Bat" 

Now I want when p ~ Bar x , then type Back (Bar x) = x and type ForwardBar (Bar x) = Forward x . I want to automatically output this when I define an instance for some Bar x . However, the definition of customFunc is different. Is it possible?

You can also add default signatures to class functions in another file (or package). I am using some class that I want to add by default, but I do not want to change the class definition.

+6
source share
1 answer

AFAIK, there is currently no way to use DefaultSignatures with type families.

I see two options for doing what you want. Both have certain flaws, but maybe they are sufficient for your purposes.

Option 1: Use standard default type definitions

 class Foo p where type Back p :: * type Back p = UnBar p type Forward p :: * type Forward p = ForwardBar (UnBar p) customFunc :: p -> IO () 

The UnBar helper type family is UnBar :

 type family UnBar a :: * type instance UnBar (Bar a) = a 

Then the instance could be simple:

 instance Foo (Bar Bat) where customFunc _ = print "I am different customFunc and this is Bat Bat" 

Option 2: Use Type Families Instead

 class Foo p where customFunc :: p -> IO () type family Back p :: * type family Forward p :: * 

Now we can give a general example for type families for all Bar types:

 type instance Back (Bar a) = a type instance Forward (Bar a) = ForwardBar a 

And more specific instances for the class, for specific Bar types:

 instance Foo (Bar Bat) where customFunc _ = print "I am different customFunc and this is Bat Bat" 
+5
source

All Articles