Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local infix operator definition in Haskell

Tags:

haskell

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?

like image 915
Mike Spivey Avatar asked Jan 05 '17 16:01

Mike Spivey


People also ask

What is an infix operator Haskell?

Functions in Haskell default to prefix syntax, meaning that the function being applied is at the beginning of the expression rather than the middle. Operators are functions which can be used in infix style. All operators are functions.

How do you define an infix function?

What are infix functions? In R, most of the functions are “prefix” - meaning that the function name comes before the arguments, which are put between parentheses : fun(a,b) . With infix functions, the name comes between the arguments a fun b . In fact, you use this type on a daily basis with : , + , and so on.

What is operators in Haskell?

Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).

What is R infix operators?

Most of the operators that we use in R are binary operators (having two operands). Hence, they are infix operators, used between the operands. Actually, these operators do a function call in the background.


1 Answers

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)
like image 146
Daniel Wagner Avatar answered Oct 31 '22 23:10

Daniel Wagner