1

I want to be able to create a function to see if two types are equal. I have a type class called finite defined as follows,

class (Bounded a, Enum a) => Finite a

and want to be able to write a equality comparison function

equals :: (Finite a, Eq b) => (a -> b) -> (a -> b) -> Bool

for functions who's domain is of type Finite. For example, for a negation function:

neg :: Int8 -> Int8
neg n = -n

the return in the Main would be:

*Main> equals neg (\x -> x)
False
3
  • 3
    You create a list of the "domain", and check if both functions give the same answer for all these items. Commented Oct 13, 2021 at 16:11
  • this sounds like a property check/test, no? Commented Oct 13, 2021 at 16:13
  • 1
    What is your question? Commented Oct 14, 2021 at 0:05

1 Answer 1

3

Basically what you need is to enumerate over all possible values for a, and check if the two functions produce the same result.

You can generate a list of all the items with [minBound ..], and you can use all :: Foldable f => (a -> Bool) -> f a -> Bool to check if for all items a certain predicate is satisfied.

You thus can implement such function with:

equals :: (Finite a, Eq b) => (a -> b) -> (a -> b) -> Bool
equals = all (…) [minBound ..]

where I leave implementing the part as an exercise. It should call f and g with the element of the list, and return True if and only if the two functions produce the same result.

Sign up to request clarification or add additional context in comments.

3 Comments

@GregOSullivanne: it's not [minBound], there are two dots, that is not an exercise, but simply a way to create a range list, so [minBound ..] :: Char will construct a list of all possible Characters.
@GregOSullivanne: you can use a lambda-expression \x -> ... where x is an item from the list, and you need to map it to a bool that is true if both functions f and g produce the same result
@GregOSullivanne: no, and map is not a keyword: map is simply a function exported from the Prelude. The all will call the lambda-expression for all elements (or until it finds one item for which the lambda-expression returns False).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.