[Haskell-beginners] Couldn't match expected type with actual type

Kim-Ee Yeoh ky3 at atamo.com
Fri Dec 7 15:33:16 CET 2012


See, with a function like
> head :: [a] -> a
the "a" is a type variable that's _specified_ by the caller of the
function.

E.g. if I call
> head "asdf"
the head function gets the type Char, and therefore, it's obligated to also
return a Char.

So if you stick with the original signature of
> get :: a -> Value b
the caller gets to decide the type of both "a" and "b". How do we even know
whether "Value b" is inhabited for any given type "b"?

The only way we can be sure is if the type constructor Value is a constant,
e.g.
> data Value b = Value ()
which makes your get function trivial and therefore highly likely /not/
what you want:
> get = const $ Value ()

If you read up on parametric polymorphism all of this will become clear to
you.

Also, there's a whiff of OO-ness about what you're trying to do. As with
all questions of this type the standard answer applies: consider
re-evaluating what you're trying to accomplish.

-- Kim-Ee



On Fri, Dec 7, 2012 at 9:02 PM, Alexander _
<anotherworldofworld at gmail.com>wrote:

> Hello,
>
> I have function with following signature:
>
> get :: a -> Value b
>
> I don't know concrete type of b. It can be Value Int or Value String or
> something else. How can i to write function only for testing? I need
> something like this:
>
> get :: a -> Value b
> get k = (Value "some_data"')
>
> but i got error:
>
> Couldn't match expected type `b' with actual type `[Char]'
> `b' is a rigid type variable bound by
>   the type signature for get :: a -> Value b
>   at Test.hs:39:8
>   In the first argument of `Value', namely `"some_data"'
>   In the expression: (Value "some_data")
>   In an equation for `get': get k = (Value "some_data")
> Failed, modules loaded: none.
>
> Value data declaration:
>
> data Value b = Value b deriving (Show)
>
>
> Thank you.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121207/c7376cab/attachment.htm>


More information about the Beginners mailing list