
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.

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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

head :: [a] -> a
head "asdf"
get :: a -> Value b
See, with a function like the "a" is a type variable that's _specified_ by the caller of the function. E.g. if I call 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 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 _
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Alexander _
-
Kim-Ee Yeoh
-
pedromartins4