Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define fixity declaration in the REPL?

Tags:

haskell

I love to play around in the REPL as it is a really fast and simple way of trying out new things. Certain aspects of the language is, as you know, done differently in the REPL as opposed to within modules, multi-line definitions being one of them.

Now, I wonder if, and in that case how, I can declare operator fixity in the REPL. The naive attempt...surprise...does not work.

Prelude> let (f · g) x = f(g(x))
Prelude> infixl 7 ·
like image 523
Magnus Kronqvist Avatar asked Jul 19 '13 09:07

Magnus Kronqvist


1 Answers

You can declare fixity using multi-line definitions like this

>>> :{
>>> let infixl 7 ***
>>>     (f *** g) (a,b) = (f a, g b)
>>> :}
>>> (negate *** show) (1,2)
(-1,"2")

Edit: Although, interestingly, the fixity isn't displayed when you ask GHCI about the function

>>> :i ***
(***) :: (a -> a') -> (b -> b') -> (a, b) -> (a', b')
        -- Defined at <interactive>:10:8

compared to

>>> :i &&
(&&) :: Bool -> Bool -> Bool     -- Defined in `GHC.Classes'
infixr 3 &&
like image 161
Chris Taylor Avatar answered Nov 04 '22 23:11

Chris Taylor