I think I have a problem with Haskell type inference.
I made my own data type and made it instance of the class Read. My data type is actually take an other type as parameter, it’s a type parameter.
I redefined readPresc in a way that parse the string and give back my new data. When I write:
read "string that represent MyType a" :: MyType a
it works perfectly fine (at least it does what I expected)
Now I have a function, let’s call it insert, that takes an element of type a, MyType a, and gives back a new MyTape a.
insert :: a -> MyType a -> a
but when I write:
insert 3 "string that rapresent MyType Int"
I got Ambigous type.
How can I tell haskell to infer to the read the same type that is the parameter of the insert?
You don’t need to, that is inferred from the type of
insert.The problem is that in
(I inserted the
readfor it to be possibly type correct), the literal3is polymorphic. Its type isso that is still not enough information to determine what type
readshould produce, hence the error.You need to provide the necessary information, for example
or by using the result in a context that determines the type variable
a.