[Haskell-cafe] Type families not as useful over functions

Jonathan Cast jonathanccast at fastmail.fm
Thu Feb 12 18:44:35 EST 2009


On Fri, 2009-02-13 at 10:34 +1100, John Ky wrote:
> Hi Haskell Cafe,
> 
> I tried using type families over functions, but when I try it
> complains that the two lines marked conflict with each other.
> 
> class Broadcast a where
>    type Return a
>    broadcast :: a -> Return a

> instance Broadcast [a -> r] where
>    type Return [a -> r] = a -> [r] -- Conflict!
>    broadcast fs a = []
> 
> instance Broadcast [a -> b -> r] where
>    type Return [a -> b -> r] = a -> b -> [r] -- Conflict!
>    broadcast fs a b = []
> 
> Given that in Haskell, every function of n+1 arguments is also a
> function of n arguments, this is likely the cause of the conflict.

This solution is somewhat in-extensible in the ultimate result type (r,
in your code); if the number of types r can take on is limited, it
should work well, though.  For expository purposes, I assume that r is
always Int:

  -- | Conal Elliot's semantic editor combinator argument
  argument :: (a -> a') -> (a -> b) -> (a' -> b)
  argument f g = g . f

  class Broadcast a where
    type Return a
    broadcast :: [a] -> Return a
  instance Broadcast Int where
    type Return Int = [Int]
    broadcast ns = ns
  instance Broadcast r => Broadcast (a -> r) where
    type Return (a -> r) = a -> Return r
    broadcast fs x = (map.argument) (const x) fs

jcc




More information about the Haskell-Cafe mailing list