Is there a way to have default type instances defined in terms of each other? I’m trying to get something like this working:
{-# 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
but this doesn’t work:
Couldn't match type `Bar Int 'B' with `Bool'
In the expression: True
In an equation for `test': test = True
Note that this doesn’t work either:
test :: Bar Int B
test = ()
Yes, default type instances can be defined in terms of each other (as you can see from your own example):
However when you redefine associated type synonym in your instance definition for
Intyou replace entire default 3-line defintion ofBar(and not just thetype Bar a A = ()) with one linetype Bar Int A = Boolwhich meansBar Int BandBar Int Care no longer defined.So I guess one of the ways to use recursive defaults the way you intended is to redefine specific synonyms instead (though it is rather verbose):
Which can fall back to defaults: