10

有没有办法根据彼此定义默认类型实例?我试图让这样的工作:

{-# LANGUAGE DataKinds, KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
data Tag = A | B | C

class Foo (a :: *) where
    type Bar a (b :: Tag)

    type Bar a A = ()
    type Bar a B = Bar a A
    type Bar a C = Bar a A

instance Foo Int where
    type Bar Int A = Bool

test :: Bar Int B
test = True

但这不起作用:

Couldn't match type `Bar Int 'B' with `Bool'
In the expression: True
In an equation for `test': test = True

请注意,这也不起作用:

test :: Bar Int B
test = ()
4

1 回答 1

3

是的,默认类型实例可以相互定义(从您自己的示例中可以看出):

instance Foo Int where
--    So the default recursive definition will be used instead
--    type Bar Int A = Bool

test :: Bar Int B
test = ()

但是,当您在实例定义中重新定义关联类型同义词时,将(而不仅仅是)的整个默认 3 行定义Int替换为一行,这意味着并且不再定义。Bartype Bar a A = ()type Bar Int A = BoolBar Int BBar Int C

所以我猜想按照您的意图使用递归默认值的一种方法是重新定义特定的同义词(尽管它相当冗长):

class Foo (a :: *) where
    type Bar a (b :: Tag)
    type Bar a A = BarA a
    type Bar a B = BarB a

    type BarA a
    type BarA a = ()

    type BarB a
    type BarB a = Bar a A

-- This now works
instance Foo Int where
    type BarA Int = Bool

test :: Bar Int B
test = True

可以回退到默认值:

-- As well as this one
instance Foo Int where
--    type BarA Int = Bool

test :: Bar Int B
test = ()
于 2012-08-31T05:38:45.523 回答