The F# Operators and Basic Functions

F# comes with a number of essential basic functions and operators pre-defined. For example, even "+" is an operator defined in Microsoft.FSharp.Core.Operators, which is opened by default. Likewise "abs" is a function to take the absolute value of a signed integer or floating point number, and "int64" is a function that converts its input to a 64-bit signed integer.

In this post I've listed the most important of the pre-defined operators. These operators are also now listed in the F# manual, Chapter 16. Importantly, many of these operators use a form of statically resolved, extensible overloading in the form of F# member constraints. That is, a function such as "int64" (convert to int64) can be used with any type that has a static ToInt64 member with an appropriate signature. The relevant corresponding member name is shown in F# intellisense in Visual Studio and may appear in error messages, and the full required signature is specified in prim-types.fsi in the F# distribution. F# simulates the presence of certain methods on basic types to ensure consistency and uniformity in the overloading scheme.

Note that some functions have beeen added in the CTP release and may be unfamiliar to existing users, e.g. pown. Also, some functions an operators are given special types when applied to floating point (float, float32 or decimal) arguments with unit-of-measure annotations.

 Basic Type Abbreviations

Type Name

Short Description

obj

System.Object

exn

System.Exception

nativeint

System.IntPtr

unativeint

System.UIntPtr

string

System.String

float32, single

System.Single

float, double

System.Double

sbyte

System.SByte

byte

System.Byte

int16

System.Int16

uint16

System.UInt16

int32, int

System.Int32

uint32

System.UInt32

int64

System.Int64

uint64

System.UInt64

char

System.Char

bool

System.Boolean

decimal

System.Decimal

Basic Arithmetic Operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

(+)

x + y

Overloaded addition

(-)

x - y

Overloaded subtraction

(*)

x * y

Overloaded multiplication

(/)

x / y

Overloaded division

(%)

x % y

Overloaded modulus

(~-)

-x

Overloaded unary negation

not

not x

Boolean negation

 

Generic Equality and Comparison Operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

(<)

x < y

Generic less-than

(<=)

x <= y

Generic less-than-or-equal

(>)

x > y

Generic greater-than

(>=)

x >= y

Generic greater-than-or-equal

(=)

x = y

Generic equality

(<>)

x <> y

Generic disequality

max

max x y

Generic maximum

min

min x y

Generic minimum

Bitwise manipulation operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

(<<<)

x <<< y

Overloaded bitwise shift-left

(>>>)

x >>> y

Overloaded bitwise arithmetic shift-right

(^^^)

x ^^^ y

Overloaded bitwise exclusive or

(&&&)

x &&& y

Overloaded bitwise and

(|||)

x ||| y

Overloaded bitwise or

(~~~)

~~~x

Overloaded bitwise negation

Math operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

abs

abs x

Overloaded absolute value

acos

acos x

Overloaded inverse cosine

asin

asin x

Overloaded inverse sine

atan

atan x

Overloaded inverse tangent

atan2

atan2 x y

Overloaded inverse tangent of x/y

ceil

ceil x

Overloaded floating point ceiling

cos

cos x

Overloaded cosine

cosh

cosh x

Overloaded hyperbolic cosine

exp

exp x

Overloaded exponent

floor

floor x

Overloaded floating point floor

log

log x

Overloaded natural logarithm

log10

log10 x

Overloaded base-10 logarithm

(**)

x ** y

Overloaded exponential

pown

pown x y

Overloaded integer exponential

round

round x

Overloaded rounding

sign

sign x

Overloaded sign function

sin

sin x

Overloaded sine function

sinh

sinh x

Overloaded hyperbolic sine function

sqrt

sqrt x

Overloaded square root function

tan

tan x

Overloaded tangent function

tanh

tanh x

Overloaded hyperbolic tangent function

Function Pipelining and Composition Operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

(|>)

x |> f

Pipelining

(>>)

f >> g

Function composition

(<|)

f <| x

Backward pipelining

(<<)

g << f

Backward function composition

ignore

ignore x

Compute and discard a value

Object Transformation Operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

box

box x

Convert to object representation

hash

hash x

Generic hashing operator

sizeof

sizeof<type>

Compute the size of a value of the given type

typeof

typeof<type>

Compute the System.Type representation of the given type

typedefof

typedefof<type>

Compute the System.Type representation of the given type and calls GetGenericTypeDefinition if this is a generic type.

unbox

unbox x

Convert form object representation

ref

ref x

Allocate a mutable reference cell

(!)

!x

Read a mutable reference cell

 Pair Operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

fst

fst p

Take the first element of a pair

snd

snd p

Take the second element of a pair

 Exception Operators

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

failwith

failwith x

Raise a FailureException exception

invalid_arg

invalid_arg x

Raise an ArgumentException exception

raise

raise x

Raise an exception

rethrow

rethrow()

Special operator to raise an exception

Input/Output Handles

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

stdin

stdin

Computes System.Console.In

stdout

stdout

Computes System.Console.Out

stderr

stderr

Computes System.Console.Error

Overloaded Conversion Functions

The following operators are defined in Microsoft.FSharp.Core.Operators:

Operator/Function Name

Expression Form

Short Description

byte

byte x

Overloaded conversion to a byte

sbyte

sbyte x

Overloaded conversion to a signed byte

int16

int16 x

Overloaded conversion to a 16 bit integer

uint16

uint16 x

Overloaded conversion to an unsigned 16 bit integer

int32, int

int32 x

int x

Overloaded conversion to a 32 bit integer

uint32

uint32 x

Overloaded conversion to an unsigned 32 bit integer

int64

int64 x

Overloaded conversion to a 64 bit integer

uint64

uint64 x

Overloaded conversion to an unsigned 64 bit integer

nativeint

nativeint x

Overloaded conversion to an native integer

unativeint

unativeint x

Overloaded conversion to an unsigned native integer

float, double

float x

double x

Overloaded conversion to a 64-bit IEEE floating point number

float32, single

float32 x

single x

Overloaded conversion to a 32-bit IEEE floating point number

decimal

decimal x

Overloaded conversion to a System.Decimal number

char

char x

Overloaded conversion to a System.Char value

enum

enum x

Overloaded conversion to a typed enumeration value

Checked Arithmetic Operators

The module Microsoft.FSharp.Core.Operators.Checked defines runtime-overflow-checked versions of the following operators:

Operator/Function Name

Expression Form

Short Description

(+)

x + y

Checked overloaded addition

(-)

x – y

Checked overloaded subtraction

(*)

x * y

Checked overloaded multiplication

(~-)

-x

Checked overloaded unary negation

byte

byte x

Checked overloaded conversion to a byte

sbyte

sbyte x

Checked overloaded conversion to a signed byte

int16

int16 x

Checked overloaded conversion to a 16 bit integer

uint16

uint16 x

Checked overloaded conversion to an unsigned 16 bit integer

int32, int

int32 x

int x

Checked overloaded conversion to a 32 bit integer

uint32

uint32 x

Checked overloaded conversion to an unsigned 32 bit integer

int64

int64 x

Checked overloaded converstion to a 64 bit integer

uint64

uint64 x

Checked overloaded conversion to an unsigned 64 bit integer

nativeint

nativeint x

Checked overloaded conversion to an native integer

unativeint

unativeint x

Checked overloaded conversion to an unsigned native integer

char

char x

Checked overloaded conversion to a System.Char value