class functional depency inference?
Hi, I have resumed my problem to the following. In this small example, the type checker always complains, and do need to write something similar that would use the functional dependency to infere the type b. class SomeClass a b | a -> b instance SomeClass Int Bool test :: SomeClass a b => a -> b test 1 = True Cheers, hugo
Hugo Pacheco writes:
test :: SomeClass a b => a -> b test 1 = True
test, as you've written it, is only a function from Ints to Bools, not from any a to any b where SomeClass a b holds. It is true that SomeClass Int Bool is the only instance at the moment, but type classes are open and someone could come along and add another instance (perhaps even in a different module), like SomeClass () String, and your function promises to be able to deal with them; as you've written it, it can't. -- -David House, dmhouse@gmail.com
On 6/16/07, David House <dmhouse@gmail.com> wrote:
Hugo Pacheco writes:
test :: SomeClass a b => a -> b test 1 = True
test, as you've written it, is only a function from Ints to Bools, not from any a to any b where SomeClass a b holds. It is true that SomeClass Int Bool is the only instance at the moment, but type classes are open and someone could come along and add another instance (perhaps even in a different module), like SomeClass () String, and your function promises to be able to deal with them; as you've written it, it can't.
I've just promoted the test function to the class and it works the same, I missed it before. However, haskell functions are partial. It could be generic and fail when some pattern does not exist as usual.
participants (2)
-
David House -
Hugo Pacheco