[Haskell-cafe] OverlappingInstances-extension-like for type families

11 views
Skip to first unread message

Leza Morais Lutonda

unread,
Jul 21, 2014, 5:29:45 PM7/21/14
to haskel...@haskell.org

Hello haskell-café,

I just started working with type families, and come with some
difficulties. Now I have a class:

class (...) ⇒ Complexable s where
type ComplexSignalType s
...

Is there a possibility to have this two instances (in any way)?

instance (RealFloat e) ⇒ Complexable [e] where
type ComplexSignalType [e] = [ℂ e]

instance (RealFloat e) ⇒ Complexable [ℂ e] where
type ComplexSignalType [ℂ e] = [ℂ e]


Thanks.
--
Leza Morais Lutonda, Lemol-C
http://lemol.github.io

50 Aniversario de la Cujae. Inaugurada por Fidel el 2 de diciembre de 1964 http://cujae.edu.cu


_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

adam vogt

unread,
Jul 21, 2014, 11:03:03 PM7/21/14
to Leza Morais Lutonda, haskell-cafe
Hi Leza,

You can make a closed type family (not associated with the class as you have):

type family ComplexSignalType e where
ComplexSignalType [C e] = [C e]
ComplexSignalType [e] = [C e]

But then you pretty much have to define all instances in the same
module. If you instead use functional dependencies (FDs):

class Complexable s cs | s -> cs
instance Complexable [e] [C e]
instance Complexable [C e] [C e]

you can put these instances in separate modules. But the disadvantage
is that you have to deal with more type variables.

Regards,
Adam

Leza Morais Lutonda

unread,
Jul 22, 2014, 12:40:32 AM7/22/14
to adam vogt, haskell-cafe
On 21/07/14 23:02, adam vogt wrote:
> If you instead use functional dependencies (FDs):
>
> class Complexable s cs | s -> cs
> instance Complexable [e] [C e]
> instance Complexable [C e] [C e]
>
> you can put these instances in separate modules. But the disadvantage
> is that you have to deal with more type variables.

Hi Adam,

Actually, the Complexable class has a superclass, and the definition has
a constraint like this:

class (Signal s,
ComplexSignal (ComplexSignalType s)) => Complexable s where
type ComplexSignalType s
...

Trying to use functional dependencies, I end refactoring the Complexable
class to:

class (ComplexSignal csx rsx cel rel) ⇒ Complexable csx rsx cel rel | cel → csx, rel → rsx, rsx → csx

But these instances still conflicting:

instance (RealFloat e) ⇒ Complexable [ℂ e] [e] (ℂ e) e
instance (RealFloat e) ⇒ Complexable [ℂ e] [ℂ e] (ℂ e) (ℂ e)

Richard Eisenberg

unread,
Jul 22, 2014, 7:58:58 AM7/22/14
to Leza Morais Lutonda, haskel...@haskell.org
Hi Leza,

It's not clear that what you want to do is sound: according to your desired definitions, `ComplexSignalType [C Double]` would be *both* `[C (C Double)]` (from the first type instance) and `[C Double]` (from the second). How do you want to reconcile this? If you want to use the first instance only when the second doesn't match (which is what OverlappingInstances does, as the first is more general than the second), you'll want to use closed type families. If not, then I'm not sure what you want.

Here the closed type families approach:

type family ComplexSignalType e where
ComplexSignalType [C e] = [C e]
ComplexSignalType [e] = [C e]

Now, the second equation will trigger only when GHC is sure that the first equation won't.

I hope this helps!
Richard

adam vogt

unread,
Jul 22, 2014, 2:02:21 PM7/22/14
to Leza Morais Lutonda, haskell-cafe
On Tue, Jul 22, 2014 at 12:40 AM, Leza Morais Lutonda
<lez...@fecrd.cujae.edu.cu> wrote:
> Trying to use functional dependencies, I end refactoring the Complexable
> class to:
>
> class (ComplexSignal csx rsx cel rel) ⇒ Complexable csx rsx cel rel | cel →
> csx, rel → rsx, rsx → csx
>
> But these instances still conflicting:
>
> instance (RealFloat e) ⇒ Complexable [ℂ e] [e] (ℂ e) e
> instance (RealFloat e) ⇒ Complexable [ℂ e] [ℂ e] (ℂ e) (ℂ e)

Hi Leza,

I believe those instances are accepted in ghc-7.6. In future ghcs, it
looks like there will be an extension that allows them again:
https://phabricator.haskell.org/D69

In the meantime you could add another parameter for the 'e':

class Complexable csx rsx cel rel e | cel → csx, rel → rsx, e rsx →
csx, csx -> e
instance (RealFloat e) ⇒ Complexable [C e] [e] (C e) e e
instance (RealFloat e) ⇒ Complexable [C e] [C e] (C e) (C e) e


Or you could keep the Complexable class as you had it, but write the
instances for particular 'e' like:

instance Complexable [C Double] [Double] (C Double) Double


Regards,
Adam

Leza Morais Lutonda

unread,
Jul 23, 2014, 5:55:27 PM7/23/14
to adam vogt, haskell-cafe
On 22/07/14 14:02, adam vogt wrote:
> Hi Leza,
>
> I believe those instances are accepted in ghc-7.6. In future ghcs, it
> looks like there will be an extension that allows them again:
> https://phabricator.haskell.org/D69
>
> In the meantime you could add another parameter for the 'e':
>
> class Complexable csx rsx cel rel e | cel → csx, rel → rsx, e rsx →
> csx, csx -> e
> instance (RealFloat e) ⇒ Complexable [C e] [e] (C e) e e
> instance (RealFloat e) ⇒ Complexable [C e] [C e] (C e) (C e) e
>
>
> Or you could keep the Complexable class as you had it, but write the
> instances for particular 'e' like:
>
> instance Complexable [C Double] [Double] (C Double) Double
>
>
> Regards,
> Adam
>

Ok Adam,
I read more about this situation with type families and functional
dependencies, and hope it will come soon to ghc.
Meantime I will stay with type families and not to have these two
instances simultaneously.

Thanks.

--
Leza Morais Lutonda, Lemol-C
http://lemol.github.io

50 Aniversario de la Cujae. Inaugurada por Fidel el 2 de diciembre de 1964 http://cujae.edu.cu

Reply all
Reply to author
Forward
0 new messages