2.6. Operators

Orc has a standard set of infix, prefix, and postfix operators. Operators in Orc are syntactic sugar for site calls; they publish at most once, and their operands are deflated. For example, 1 + 2 is equivalent to (+)(1, 2) and 1 + 2 * 3 is equivalent to (+)(1, (*)(2, 3)).

Operators are parsed using the precedence and associativity rules of Orc. In particular, these rules describe the relationship between operators and combinators.

2.6.1. Syntax

[8]PrefixOperation::= PrefixOperator Expression  
[9]InfixOperation::= Expression InfixOperator Expression  
[10]PostfixOperation::= Expression PostfixOperator  

2.6.2. Standard Orc Operators

Arithmetic Operators

+Addition
-Subtraction
*Multiplication
/Division
%Modulus
**Exponentiation
- (unary prefix form)Arithmetic negation

Numeric literals with no decimal part, such as 3, are treated as integers. Arithmetic operators with two integer arguments will perform an integer operation and return an integer result; for example, 5 / 2 performs integer division and returns 2. However, if either argument to an operator has a decimal part (even if it is trivial, as in 3.0), the other argument will be promoted, and a decimal operation will be performed. For example, 5 / 2.0 and 5.0 / 2 both perform decimal division and return 2.5.

Comparison Operators

=Equal to
/=Not equal to
<:Less than
:>Greater than
<=Less than or equal to
>=Greater than or equal to

The = operator can compare values of any type. Values of different type are always unequal; for example, 10 = true publishes false.

Logical Operators

&&Logical and
||Logical or
~Logical negation

String Operators

+Concatenation

List Operators

:List construction

Record Operators

+Extension

Reference Operators

?Dereference
:=Assignment

2.6.3. Redefining Operators

The operators' syntax (fixity, precedence, and associativity) is fixed by the Orc grammar; however, the site called is defined by the library and can be changed by a def or import site declaration.

Orc permits a special identifier form for operators: the operator name surrounded by parenthesis, as in (+). To disambiguate the unary prefix operator - from the binary infix operator -, Orc translates the unary prefix operator as (0-). Binding a value to such an identifier redefines the corresponding operator in the scope of that binding.

2.6.4. Type

Assuming that an operator has not been redefined, it obeys the following typing rules:

  • An arithmetic operation publishes an Integer if all of its operands have type Integer. If any operand has type Number (but not Integer), the operation publishes a Number.

  • A comparison operation allows operands of any type, and publishes a Boolean.

  • A logical operation allows only Boolean operands, and publishes a Boolean.

  • A string concatenation requires at least one String operand. Other operands may be of type Top; they are converted to strings. String concatenation publishes a String.

  • List construction is polymorphic. It takes an operand of type T and an operand of type List[T]. It returns a value of type List[T].

  • Record extension takes two records and publishes a record.

  • Dereference takes an operand of type Ref[T] and publishes a T.

  • Assignment takes operands of types Ref[T] and T, and publishes a Signal.

2.6.5. Examples

Operator precedence
1 + 2 * 3
  
{-
OUTPUT:
7
-} 
Operators publish once
1 + (2 | 3)

{-
OUTPUT:
3
-}
{-
OUTPUT:
4
-}
Redefine an operator
{- Redefine the "||", "~", and "?" operators -}

def (||)(x,y) = x + ", or " + y
def (~)(x) = "not " + x
def (?)(r) = r + ", that is the question."

# ("To be" || ~ "to be")?

{-
OUTPUT:
"To be, or not to be, that is the question."
-}

2.6.6. Related Links

Related Tutorial Sections