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
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
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)
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
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