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

pedromartins4 pedromartins4 at gmail.com
Fri Dec 7 15:18:35 CET 2012


Hi Alexander:
You DO know the concrete type of b in the context of get: its necessarily a String.
Therefore, 

get :: a -> Value String
get k = Value "some_data"

works ok.
Also, you can make the type of get polymorphic (accepting different data types) by writing

get :: a -> Value a
get k = Value k

where whatever the type of a is, get will return Value of that type (Value a).

This works ok because the type of your data type is also polymorphic:

data Value b = Value b deriving (Show)

i.e., b can be something of any type.
Hope this helps.

Cheers,
Pedro


On Dec 7, 2012, at 2:02 PM, Alexander _ 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




More information about the Beginners mailing list