[Haskell-cafe] class-instance

Henning Thielemann lemming at henning-thielemann.de
Mon Jan 17 12:39:59 CET 2011


On Mon, 17 Jan 2011, Patrick Browne wrote:

> -- My intension is that the PERSON class should *specify*
> -- that a person has a constant id called p1
> -- and that a person has a name that can be found from the id.
> class PERSON a b where
>  p1 :: a
>  name :: a -> b
>
> -- My intension is that the instance should  implement the PERSON class
> instance PERSON  Int String where
>  p1 = 1
>  name p1 = "john"
>
> -- Why does the evaluations of p1 or name p1 produce errors?
> -- how do I fix them and still keep the basic instance-implements-class relation?

The problem is certainly, that a Haskell interpreter has no way to infer 
types, thus you have to annotate type of both argument and result of 
'name', and type annotation for 'p1' is not possible at all, because 'b' 
does not occur in its type. You have to write

name (12::Int) :: String


But I suspect, what you try to do is not what you need. If you tell us 
what you want to do, we can certainly give more helpful answers.

Maybe you want to do something with functional dependencies, such that 
'String' result type is automatically chosen if the argument type is 
'Int'. But before suggesting this type extension, I'd like to know more 
about your problem.



More information about the Haskell-Cafe mailing list