Re: [Haskell] Class type constraining?

2007-06-22 Thread Benja Fallenstein

2007/6/22, Hugo Pacheco <[EMAIL PROTECTED]>:

class Functor f => C f a b | f a -> b where
   ftest :: f a -> b

I want to write some function

test :: (C f a b) => (a -> b)
test = ftest . undefined


I'm not sure whether this is what you want, but the "obvious" way to
make this type-check would seem to be to add a functional dependency
and a type signature for 'undefined,' like this:


class Functor f => C f a b | f a -> b, a b -> f where
   ftest :: f a -> b

test :: (C f a b) => (a -> b)
test = ftest . (undefined :: a -> f a)


- Benja
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


Re: [Haskell] Empty instance declaration

2007-12-28 Thread Benja Fallenstein
On Dec 28, 2007 5:14 PM, Mike Haskel <[EMAIL PROTECTED]> wrote:
> You can define Show as a data type, rather than a type class:
>
> type Show a = Either (a -> String) (Int -> a -> String -> String)
...
> The constructors for Show make explicit the two ways to define an
> instance.  This technique also has the advantage of allowing multiple,
> non-conflicting instance declarations, selectable at runtime.  Using
> Show as an example, you might have instances representing both
> formatted and unformatted display.  An obvious disadvantage is that
> the instance needs a name and gets passed explicitly.

Seems to me that using Either is entirely orthogonal from type-vs-class :-)

class Show a where
show0 :: Either (a -> String) (Int -> a -> String -> String)

show :: Show a => a -> String
show = case show0 of
Left s -> s
Right sp -> \x -> sp 0 x ""

etc.

- Benja
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell