Skip to content

Instantly share code, notes, and snippets.

@hdgarrood hdgarrood/Main.purs
Last active Aug 29, 2015

Embed
What would you like to do?
Infix declarations
module Main where
import Debug.Trace
data Two a b = Two a b
instance showTwo :: (Show a, Show b) => Show (Two a b) where
show (Two x y) = "(" <> show x <> " : " <> show y <> ")"
-- Non-associative
(^^) = Two
infix 0 ^^
-- left-associative
(<<) = Two
infixl 0 <<
-- right-associative
(>>) = Two
infixr 0 >>
-- Precedence
(<<%) = Two
infixl 1 <<%
(<<%%) = Two
infixl 9 <<%%
(^^^) = Two
main = do
trace "Declared left-associative"
print (1 << 1 << 1 << 1 << 1 << 1)
trace "Declared left-associative"
print (1 >> 1 >> 1 >> 1 >> 1 >> 1)
trace "Declared non-associative"
print (1 ^^ 1 ^^ 1 ^^ 1 ^^ 1 ^^ 1)
trace "Without a fixity declaration, it defaults to being left-associative"
print (1 ^^^ 1 ^^^ 1 ^^^ 1 ^^^ 1 ^^^ 1)
trace "Default precedence"
print (1 ^^^ 1 << 1)
print (1 << 1 ^^^ 1)
print (1 ^^^ 1 << 1 ^^^ 1 << 1 ^^^ 1)
print (1 <<% 1 <<%% 1)
Declared left-associative
(((((1 : 1) : 1) : 1) : 1) : 1)
Declared left-associative
(1 : (1 : (1 : (1 : (1 : 1)))))
Declared non-associative
(((1 : 1) : (1 : 1)) : (1 : 1))
Without a fixity declaration, it defaults to being left-associative
(((((1 : 1) : 1) : 1) : 1) : 1)
Default precedence
(1 : (1 : 1))
((1 : 1) : 1)
(((1 : (1 : 1)) : (1 : 1)) : 1)
(1 : (1 : 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.