DefaultSignatures和关联的类型系列

Sat*_*vik 6 haskell type-families

有没有办法使用DefaultSignatures相关类型系列的扩展.

这是我需要它的一个例子.

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"
Run Code Online (Sandbox Code Playgroud)

现在我想,只要p ~ Bar xtype Back (Bar x) = xtype ForwardBar (Bar x) = Forward x.每当我为某些人定义实例时,我想自动派生这个Bar x.但是,customFunc的定义是不同的.这可能吗?

还可以将默认签名添加到另一个文件(或包)中的类的函数中.我正在使用一些我想添加默认签名的类,但我不想修改类定义本身.

kos*_*kus 6

AFAIK,目前没有可能DefaultSignatures与类型系列一起使用.

我可以看到两个选项来做你想要的.两者都有一些缺点,但也许它们足以满足您的目的.

选项1:使用常规关联类型默认定义

class Foo p where
  type Back p :: *
  type Back p = UnBar p
  type Forward p :: *
  type Forward p = ForwardBar (UnBar p)
  customFunc :: p -> IO ()
Run Code Online (Sandbox Code Playgroud)

需要助手类型系列UnBar:

type family UnBar a :: *
type instance UnBar (Bar a) = a
Run Code Online (Sandbox Code Playgroud)

那么实例可以只是:

instance Foo (Bar Bat) where
  customFunc _ = print "I am different customFunc and this is Bat Bat"
Run Code Online (Sandbox Code Playgroud)

选项2:改为使用类型系列

class Foo p where
  customFunc :: p -> IO ()

type family Back p :: *
type family Forward p :: *
Run Code Online (Sandbox Code Playgroud)

现在我们可以给出所有Bar类型的类型族的一般实例:

type instance Back (Bar a) = a
type instance Forward (Bar a) = ForwardBar a
Run Code Online (Sandbox Code Playgroud)

具体Bar类型的更具体的类实例:

instance Foo (Bar Bat) where
  customFunc _ = print "I am different customFunc and this is Bat Bat"
Run Code Online (Sandbox Code Playgroud)