Haskell – Why Does Haskell Have Built-in If/Then/Else?

haskell

Why does Haskell have a built-in if/then/else, which is dependent on the Bool type, instead of having a simple library function? Such as

if :: Bool -> a -> a -> a
if True  x _ = x
if False _ y = y

Best Answer

It's purely for the nice sugar of the if, then, and else keywords; in fact, GHC (with the RebindableSyntax extension enabled) will desugar the syntax by simply calling whatever ifThenElse function is in scope.