[Haskell-cafe] Problems with type family in a class
Leza Morais Lutonda
leza.ml at fecrd.cujae.edu.cu
Sun Jul 20 04:33:00 UTC 2014
On 17/07/14 02:32, Niklas Haas wrote:
> The reason you're getting this error is because all you know about your
> instance is that the type of fft is :: [Complex Double] -> [Complex
> Double].
>
> Since by definition this [Complex Double] is ComplexSignalType sx, all
> we know about the associated instance (parametrized by sx) is that
> ComplexSignalType sx ~ [Complex Double]. Since ComplexSignalType is a
> type family, it's not injective, so this does not suffice to uniquely
> identify our actual instance (sx, here called s0 by GHC).
>
> For example, we could have a second instance:
>
> instance Complexable () where
> type ComplexSignalType () = [Complex Double]
>
> instance FourierTransformable () where
> fft = error "oops"
>
> And with this instance, fft would also be [Complex Double] -> [Complex
> Double] but the behavior of would clearly be different to your actually
> intended function.
>
> If you think your signal function is bijective, you could use a two-way
> functional dependency like this:
>
> class Complexable sx cx | sx -> cx, cx -> sx
>
> class Complexable sx cx => FourierTransformable sx cx where
> fft :: cx -> cx
>
> Or if you want to avoid the overhead of redesigning Complexable you
> could even do something like this:
>
> class (cx ~ ComplexSignalType sx, Complexable sx) => FourierTransformable sx cx | cx -> sx where
> fft :: cx -> cx
>
> Either way, if instance selection should be possible based solely on the
> type of ‘cx’, then ‘cx’ must imply everything contained in the instance
> head.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
Thanks for the explanation, Niklas, now I understand (I'm understanding)
type families and I left with:
class (Signal s,
ComplexSignal (ComplexSignalType s),
RealSignalType (ComplexSignalType s) ~ s) ⇒ Complexable s where
type ComplexSignalType s
class (Complexable s) ⇒ FourierTransformable s where
fft ⦂ ∀ csx. (csx ~ ComplexSignalType s, ComplexSignal csx) ⇒ csx → csx
Maybe more complex than normal?
Thanks again.
--
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
More information about the Haskell-Cafe
mailing list