Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a type an instance of Eq

I have a data type called Praat. I want Praat to be an instance of Eq so that two Praats are equal if and only if mx are equal. How does one do this?

-- data type
data Praat t = Praat [k] [(k,k,k,k)] 

-- praat gives the maximum frequency
Praat t -> Int
mx (Praat [] _) = 0
mx (Praat (e:es) pt) = ...........

This is how I am trying to define the instance but it is not working.

-- I want to make Praat instance of Eq so that two Praat are equal
-- when their respective `mx` are equal
instance Eq Praat where
   mx :: (Praat k)->Int
   (mx k) == (mx k) = True
   _ == _ = False
like image 876
Eddy Freeman Avatar asked Dec 02 '11 04:12

Eddy Freeman


People also ask

What is Eq a in Haskell?

class Eq a where Source # The Eq class defines equality ( == ) and inequality ( /= ). All the basic datatypes exported by the Prelude are instances of Eq , and Eq may be derived for any datatype whose constituents are also instances of Eq . The Haskell Report defines no laws for Eq .

What is an instance in Haskell?

An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.

What is a data type in Haskell?

The Haskell standard data type Maybe is typically declared as: data Maybe a = Just a | Nothing. What this means is that the type Maybe has one type variable, represented by the a and two constructors Just and Nothing. (Note that Haskell requires type names and constructor names to begin with an uppercase letter).


1 Answers

instance Eq Praat where
    x == y = mx x == mx y

This is pretty much a direct translation of what you said. x is equal to y when mx x == mx y.

like image 134
hammar Avatar answered Oct 13 '22 13:10

hammar