11

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?

2
  • 1
    Is your infixl 5 @@ actually being applied to the local (@@)? Because that ought to be a bug I would think Commented Jan 5, 2017 at 17:56
  • looks like it isn't Commented Jan 5, 2017 at 18:02

1 Answer 1

16

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) 
Sign up to request clarification or add additional context in comments.

3 Comments

I was just about to post this. According to the Haskell 2010 Report, it's 100% legal to put fixity declarations in a where-block. Surprising, but true.
Ooh, I didn't think of that!
... and I suppose I didn't think of it because as a compiler writer, I wouldn't fancy implementing that, or trying to do it and generate reasonable error messages when it went wrong. Of course it can be done, but it's a job for someone with more youthful enthusiasm than I possess.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.