Why is const (<=5) 5 6 == False?

Viewed 121

Can you please break down how the Haskell expression

const (<=5) 5 6

evaluates to False ?

What is this expression made of? Looking at the definition of const didn't help me.

2 Answers

const :: a -> b -> a is a function that takes two values. It ignores the second value, and returns then the first one. It thus can be implemented for example as:

const :: a -> b -> a
const x _ = x

If you write const (<=5) 5 6, then this is short for ((const (<= 5) 5) 6. So it means that (<= 5) is the value for x, and 5 is the value we ignore.

This thus means that const (<= 5) 5 is equivalent to (<= 5). We then apply 6 to the function (<= 5). The (<= 5) is an operator sectioning of an infix operator, it is thus equivalent to \x -> (<=) x 5. If we thus evaluate (<= 5) 6, we evaluate (\x -> (<=) x 5) 6, or (<=) 6 5. We thus check if 6 is less than or equal to 5, and this is not the case, so we return False.

Because of currying, const (<=5) 5 6 is equivalent to this:

(((const) (<=5)) 5) 6 -- yes, that's a lot of parentheses

Or, in other words:

const_le_5 = const (<=5)
const_le_5_apply_to_5 = const_le_5 5
final_result = const_le_5_apply_to_5 6

BUT we see from the definition of const that it simply evaluates to its first argument, which is (<=5) in this case, so we get this:

  1. const_le_5_apply_to_5 is the same as const (<=5) 5
  2. ...which is, by the definition of const, the same as (<=5)

Because const_le_5_apply_to_5 evaluated to (<=5), final_result actually is the same as (<=5) 6, which is false since 6 is not less than or equal to 5.

Related