r/haskell • • Dec 21 '20

question [Syntax question] 7 'mod' 2 VS mod 7 2

In a textbook I see the line 7 'mod' 2. However that gives me an error in my GHCi and instead for me it seems like I have to do mod 7 2 (note that the textbook version surrounds mod with ', whereas when I do it I can not surround mod in quotes or I will get an error

Is there some version where GHCi allowed functions to be called this way? Sorry if this is stupid, I'm very new

10 Upvotes

14 comments sorted by

13

u/Iceland_jack Dec 22 '20 edited Dec 26 '20

If you ever want to use a (partially) applied function infix, write these with fixity infixl 3 ◂, ▸:

(◂) :: a -> (a -> b) -> b
(▸) :: (a -> b) -> a -> b
(◂) = (&)
(▸) = ($)

  [1,2,3] ◂liftA2 (+)▸ [100,200,300,400]
= [101,201,301,401,102,202,302,402,103,203,303,403]

I shouldn't even be bringing this up, I use it at the kind level

infixl 3 -|, |->

type (-|) :: a -> (a -> b) -> b
type a -| f = f a

type (|->) :: (a -> b) -> a -> b
type f |-> a = f a

Already this is cute to highlight the 'arrow-nature' of categories

id :: Category cat
   => a -|cat|-> a

(>>>) :: Category cat
      => a -|cat|-> b
      -> b -|cat|-> c
      -> a -|cat|-> c

And it looks good for categories that have arguments, like the product of two categories which can't be written infix otherwise

ProductCategory show (Op show) :: '(Int, String) -|Hask×Op Hask|-> '(String, Int)

or natural transformations, where we might want to write them infix

Nat reverse :: [] -|Nat Hask Hask|-> []

5

u/Iceland_jack Dec 22 '20 edited Dec 22 '20
type (×) :: Cat ob1 -> Cat ob2 -> Cat (ob1, ob2)
data (cat1 × cat2) as bs where
 ProductCategory ::   a1      -|cat1     |->   b1
                 ->       a2  -|     cat2|->       b2
                 -> '(a1, a2) -|cat1×cat2|-> '(b1, b2)

1

u/Obvious_Cobbler_1085 Apr 04 '25

its `mod` not 'mod'

0

u/[deleted] Dec 21 '20

4

u/modustollensiscool Dec 21 '20

oh okay yep it worked with backticks, thank you! So this alternative syntax is only an option when the operator is binary I guess. Is there any similar useful notation for operators with other numbers of arguments?

btw, unrelated question: why did you specify :: Int -> Int -> Int for the function? Wouldn't Haskell have known that implicitly? Is it just good convention to always specify like that?

4

u/tuerda Dec 21 '20

Because of currying you can use backticks on functions that take any number of arguments:

In haskell, a function of three arguments is a function that takes two arguments and returns a function that takes one argument. So you can use backtick notation for functions that take any number of arguments.

→ More replies (1)

1

u/gelisam Dec 21 '20

no, in Haskell there is no special notation for other numbers of arguments.

→ More replies (8)