Local infix operator definition in Haskell

Question

In this Haskell program, @@ is an infix operator that I want to define only locally within the body of function f. (Naturally enough, my actual program is more complicated than this, and there is a good reason to use infix notation.)

infixl 5 @@

(@@) = undefined

f x = x @@ 5 where x @@ y = (x+1) * (y+1)

main = print (f 7)

However, unless I also make the global definition, written here as (@@) = undefined, GHC complains that 'The fixity signature for @@ lacks an accompanying binding.' Is there any way of getting round this without a global definition of the operator symbol?


Show source
| haskell   2017-01-05 17:01 1 Answers

Answers to Local infix operator definition in Haskell ( 1 )

  1. 2017-01-05 17:01

    Just putting the fixity declaration in the where clause seems to work fine:

    f x = x @@ 5 where
        infixl 5 @@
        x @@ y = (x+1) * (y+1)
    

Leave a reply to - Local infix operator definition in Haskell

◀ Go back