Skip to content

Instantly share code, notes, and snippets.

@hdgarrood
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdgarrood/5ee5ccf6310cade96fd3 to your computer and use it in GitHub Desktop.
Save hdgarrood/5ee5ccf6310cade96fd3 to your computer and use it in GitHub Desktop.
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