
Hello, I had defined the follwing data type: data Step = Step Id Scenario Action State Response How can I define Step as an "Eq Instance", in such way that two steps are equals if they have the same Id (Id is defined as a synonimous for the String type). I tried the following code, but something is wrong.... instance Eq Step where Step id1 scenario1 action1 state1 response1 == Step id2 scenario2 action2 state2 response2 = id == id _ == _ = False ps.: sorry, this kind of basic question can be sent to this list? Thanks in advance. Rodrigo.

From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of rodrigo.bonifacio
data Step = Step Id Scenario Action State Response
How can I define Step as an "Eq Instance", in such way that two steps are equals if they have the same Id (Id is defined as a synonimous for the String type).
I tried the following code, but something is wrong....
instance Eq Step where Step id1 scenario1 action1 state1 response1 == Step id2 scenario2 action2 state2 response2 = id == id _ == _ = False
What error messages are you getting? Anyway, you have the right idea, you just need a bit of cleaning up and correct syntax. As I don't have your definitions for Scenario, Action, etc, I'll just ignore them (we don't need them for your Eq anyway):
data Step = Step String String instance Eq Step where Step id1 _ == Step id2 _ = id1 == id2
Note: - the _ == _ case will never be reached (AFAICT) - I assume " = id == id" is a typo, and that you intended "id1 == id2" Alistair ***************************************************************** Confidentiality Note: The information contained in this message, and any attachments, may contain confidential and/or privileged material. It is intended solely for the person(s) or entity to which it is addressed. Any review, retransmission, dissemination, or taking of any action in reliance upon this information by persons or entities other than the intended recipient(s) is prohibited. If you received this in error, please contact the sender and delete the material from any computer. *****************************************************************

rodrigo.bonifacio wrote:
instance Eq Step where Step id1 scenario1 action1 state1 response1 == Step id2 scenario2 action2 state2 response2 = id == id _ == _ = False
Almost. You just used 'id' and 'id' when you meant 'id1' and 'id2'.
instance Eq Step where Step id1 scenario1 action1 state1 response1 == Step id2 scenario2 action2 state2 response2 = id1 == id2
but it's a common idiom not to bother to name unused parameters, so:
instance Eq Step where Step id1 _ _ _ _ == Step id2 _ _ _ _ = id1 == id2
(and the default case is never reached so you don't need it)
ps.: sorry, this kind of basic question can be sent to this list?
Yes, that's fine. You may get quicker answers if you hop onto IRC at #haskell, though. Jules

rb> data Step = Step Id Scenario Action State Response rb> instance Eq Step where rb> Step id1 scenario1 action1 state1 response1 == Step id2 rb> scenario2 action2 state2 response2 = id == id rb> _ == _ = False "id == id" must be replaced with "id1 == id2". Error message you've got might be confusing, since "id" is already defined as an identity function, and functions are not Eq instances. I'd suggest something like data Step = Step {stepId :: Id, stepScen :: Scenario, steAction :: Action, stepState :: State, stepResp :: Responce} instance Eq Step where step1 == step2 = stepId step1 == stepId step2
participants (4)
-
Bayley, Alistair
-
Jules Bean
-
Miguel Mitrofanov
-
rodrigo.bonifacio