SML# Document Version 3.7.1
19 Expressions

19.20 Builtin types and builtin primitives

Among the set of built-in types, exception type (exn) is manipulated by handle and raise expressions defined in section 19.14. There is no primitive functions for the unit type (unit). All the other built-in types are manipulated by built-in primitive functions through function application.

Values of built-in types other than reference type (’a ref) and array types (’a array) are ordinary values in a standard functional language semantics, and their built-in primitive functions are those that take values and return values.

Values of reference types (’a ref) and array types (’a array) are pointers to mutable memory blocks, and their built-in primitive function may have side effect of destructive memory update.

For values of type ty ref, the following built-in primitive functions are provided.

ref : ty -> ty ref
  ! :
ty ref -> ty
 := : ty ref * ty -> unit
infix 3 :=

ref exp creates a reference, i.e. a pointer to a memory block containing a value denoted by exp. ! exp dereferences the pointer and return the stored value. exp1 := exp2 destructively updates the memory block pointed to by the denotation of exp1 with the value denoted by exp2.

The built-in primitive functions for any other types, including ty array, are provided by the following structures of the library defined in Chapter 25. General structure includes the reference primitives and defines exceptions that may raised by built-in primitives.

builtin type structure signature
int8 Int8 INTEGER
int16 Int16 INTEGER
int Int, Int32 INTEGER
int64 Int64 INTEGER
intInf IntInf INTINF
word8 Word8 WORD
word16 Word16 WORD
word Word, Word32 WORD
word64 Word64 WORD
real32 Real32 REAL
real Real, Real64 REAL
char Char CHAR
string String STRING
τ array Array ARRAY
τ vector Vector VECTOR
τ ref, exn General GENERAL

The set of built-in primitive functions for a built-in type can be displayed by evaluating a structure replication declaration of the structure that implements the built-in type, as seen in the following example.

# structure X = Int;
structure X =
  struct
    type int = Int32.int
    val * = <builtin> : int * int -> int
    val + = <builtin> : int * int -> int
    val - = <builtin> : int * int -> int
    val < = <builtin> : int * int -> bool
    val <= = <builtin> : int * int -> bool
    val > = <builtin> : int * int -> bool
    val >= = <builtin> : int * int -> bool
    val abs = <builtin> : int -> int
    val compare = fn : int * int -> General.order
    val div = <builtin> : int * int -> int
    val fmt = fn : StringCvt.radix -> int -> string
    val fromInt = fn : int -> int
    val fromLarge = fn : intInf -> int
    val fromString = fn : string -> int option
    val max = fn : int * int -> int
    val maxInt = SOME 2147483647 : int option
    val min = fn : int * int -> int
    val minInt = SOME  2147483648 : int option
    val mod = <builtin> : int * int -> int
    val precision = SOME 32 : int option
    val quot = <builtin> : int * int -> int
    val rem = <builtin> : int * int -> int
    val sameSign = fn : int * int -> bool
    val scan = fn
      : [’a. StringCvt.radix
          -> (’a -> (char * ’a) option) -> ’a -> (int * ’a) option]
    val sign = fn : int -> int
    val toInt = fn : int -> int
    val toLarge = fn : int -> intInf
    val toString = fn : int -> string
    val ~ = <builtin> : int -> int
  end