[Haskell-beginners] Re: dependent types
Maurício
briqueabraque at yahoo.com
Mon Jul 20 21:39:45 EDT 2009
> Specifically, the type of a name depends on the type of
> the object. For example a Person with a Name would give a
> Persons-Name and a Dog with a Name would give a Dogs-Name.
One radically simple way to get that would be something like this
(you can also use 'newtype' instead of 'data'):
-- warning: untested code --
data (Eq b) => Named a b = Named a b
equivalent :: Named a b -> Named c b -> Bool
equivalent (Named _ a1 ) (Named _ a2) = (a1 == a2)
Then you can check Persons-Name and Dogs-Name (Named Persons Name
and Named Dogs Name) for equivalence based only on their values
of Name type. Note that you can't use (==) from Eq class because
Persons-Name and Dogs-Name are different types, and Eq class uses
only one when instanciated. If you would like to use a equivalence
relashionship that is not limited to your parametric type you
could write a class like this:
class Equivalent a b where
equivalent :: a -> b -> Bool
instance (Eq b) => Equivalent (Named a b) (Named c b) where ...
> Thanks for your rapid feedback, I will study your code.
Instead, study Felipe's example. It shows a nice way to "attach" a
name to anything, and shows some interesting classes you will like
to learn.
Best,
Maurício
More information about the Beginners
mailing list