I have seen Haskell code like this:
infix 4 ~=
(~=) :: Double -> Double -> Bool
x ~= y = abs (x-y) < epsilon
where epsilon = 1 / 1000
Despite that I know what this code is doing, I would like to know what infix 4 means.
I have seen Haskell code like this:
infix 4 ~=
(~=) :: Double -> Double -> Bool
x ~= y = abs (x-y) < epsilon
where epsilon = 1 / 1000
Despite that I know what this code is doing, I would like to know what infix 4 means.
Copyright © 2021 Jogjafile Inc.
That is a fixity declaration:
In particular the
infixpart ofinfix 4 ~=means that the operator~=is not right or left associative, sox ~= y ~= zis not a valid expression.And the precedence is 4 which means that it binds tighter than for example
&&, sox ~= y && zparses as(x ~= y) && z, but less tight than for example++, sox ~= y ++ zparses asx ~= (y ++ z)(but that fails to type check). In that respect it is the same as comparison operators like==, so alsox == y ~= zwill give a parse error.Here is a table of the fixities of standard operators (from the Haskell 2010 report linked above):