What characters are permitted for Haskell operators?

HaskellSyntaxOperators

Haskell Problem Overview


Is there a complete list of allowed characters somewhere, or a rule that determines what can be used in an identifier vs an operator?

Haskell Solutions


Solution 1 - Haskell

From the Haskell report, this is the syntax for allowed symbols:

a | b means a or b and

a<b> means a except b

special    ->   ( | ) | , | ; | [ | ] | `| { | } 
symbol     ->   ascSymbol | uniSymbol<special | _ | : | " | '>
ascSymbol  ->   ! | # | $ | % | & | * | + | . | / | < | = | > | ? | @
                \ | ^ | | | - | ~
uniSymbol  ->   any Unicode symbol or punctuation 

So, symbols are ASCII symbols or Unicode symbols except from those in special | _ | : | " | ', which are reserved.

Meaning the following characters can't be used: ( ) | , ; [ ] ` { } _ : " '

A few paragraphs below, the report gives the complete definition for Haskell operators:

varsym     -> ( symbol {symbol | :})<reservedop | dashes>
consym     -> (: {symbol | :})<reservedop>
reservedop -> .. | : | :: | = | \ | | | <- | -> | @ | ~ | =>

> Operator symbols are formed from one or more symbol characters, as > defined above, and are lexically distinguished into two namespaces > (Section 1.4): > > - An operator symbol starting with a colon is a constructor. > - An operator symbol starting with any other character is an ordinary identifier. > > Notice that a colon by itself, ":", is reserved solely for use as the > Haskell list constructor; this makes its treatment uniform with other > parts of list syntax, such as "[]" and "[a,b]". > > Other than the special syntax for prefix negation, all operators are > infix, although each infix operator can be used in a section to yield > partially applied operators (see Section 3.5). All of the standard > infix operators are just predefined symbols and may be rebound.

Solution 2 - Haskell

From the Haskell 2010 Report §2.4:

> Operator symbols are formed from one or more symbol characters...

§2.2 defines symbol characters as being any of !#$%&*+./<=>?@\^|-~: or "any [non-ascii] Unicode symbol or punctuation".

NOTE: User-defined operators cannot begin with a : as, quoting the language report, "An operator symbol starting with a colon is a constructor."

Solution 3 - Haskell

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter HallView Question on Stackoverflow
Solution 1 - HaskellRiccardo T.View Answer on Stackoverflow
Solution 2 - Haskelldave4420View Answer on Stackoverflow
Solution 3 - HaskellPeter HallView Answer on Stackoverflow