Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Syntactic sugar/Cons
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Infix notation === ==== Precedences ==== Infix notation is problematic for both human readers and source code formatters. The reader doesn't know the precedences of custom infix operators, he has to read the modules which the operators are imported from. This is even more difficult because infix operators are usually imported unqualified, that is you don't know from which module an operator is imported. The same problem arises for source code formatters. You certainly prefer the formatting <haskell> a + b * c </haskell> to <haskell> a + b * c </haskell> because the first formatting reflects the high precedence of <hask>*</hask>. A source code formatter can format this properly only if it has access to the imported modules. This is certainly uncommon for a plain source code formatter. The problem also occurs if you use an infix operator, that you did forget to import. E.g. GHC-6.4.1 may say then <code> Main.hs:52:6: precedence parsing error cannot mix `($)' [infixl 9] and `(.)' [infixr 9] in the same infix expression Main.hs:52:13: Not in scope: `$' </code> Actually, only the second error is relevant. It has been noticed by many people, that the integer numbered precedences are not enough for describing the relations of all the infix operators. http://www.haskell.org/pipermail/haskell-cafe/2005-February/009260.html Fractional and negative fixities were already proposed: http://www.haskell.org/pipermail/haskell-cafe/2006-November/019293.html Indeed, rules like "multiplication and division precede addition and subtraction" would be more natural. However, the <hask>Show</hask> class would no longer be so simple. ==== "Infixisation" ==== You can't pass an argument to a function written in infix notation. <hask>x `rel c` y</hask> or <hask>x `lift rel` y</hask> is not allowed. Some library functions are designed for a "reversed" order of arguments, this means that you will most oftenly leave out the first argument on partial application rather than the second one. E.g. the functions <hask>div</hask> and <hask>mod</hask> have parameters in the order of common mathematical notation. But you will more oftenly use <hask>flip div x</hask> than <hask>div x</hask> and <hask>flip mod x</hask> more often than <hask>mod x</hask>. This is because the library designer expect that the user will prefer the infix style, writing <hask>x `div` y</hask> and thus <hask>`div` y</hask>. For functions which are not bound to a traditional notation one should avoid this order! A bad example in this respect is the module <hask>Data.Bits</hask> in the version that comes with GHC-6.2. Many of the functions of this module alter some bits in a machine word, thus they can be considered as update functions and their type signature should end with <hask>a -> a</hask>. Then you could easily combine several operations by <haskell> shiftL 2 . clearBit 7 . setBit 4 . setBit 1 </haskell> instead of <haskell> flip shiftL 2 . flip clearBit 7 . flip setBit 4 . flip setBit 1 </haskell> or <haskell> (`shiftL` 2) . (`clearBit` 7) . (`setBit` 4) . (`setBit` 1) </haskell> .
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width