LA home

Types in programming languages are inspired by mathematical set theory.

Types/ Sets Maths Haskell SML Pascal C+/-
Z, Integer {..., -2, -1, 0, 1, 2, 3, ...} Integer, Int Int int int
Rational {..., 1/2, ..., 22/7, ...} Rational   - -
R, Real {..., 2.71, ..., 3.14, ...} Float, Double real real float, double
Tuple set product,
S×T = {<s, t> | s in S, t in T}
(S, T) S*T record s:S; t:T end struct{ S s; T t}
+, disjoint union S+T = {s0|s in S}u{t1| t in T}
(tags 0, 1)
Either S T - record case "variant" union
T* T0+T1+T2+... [T]
(list of T)
T list array ... of T T[]
function S->T S->T S->T function f(s:S):T T f(S s)

Basic Type Systems

Dynamic Types

In a language, such as J, with dynamic types and run-time type checking each value has a type and just as the value assigned to a variable can change so the type is allowed to change as the value changes, e.g.

x := 7       --Number
x := "seven" --String, etc.

Depending on the language, the action taken by an operator can vary with the type(s) of the operand(s), e.g.

function f(x, y) = x + y;

f(1, 2)         --returns 3:Number
f("one", "two") --returns "onetwo"

-- assuming `+' represents both numerical addition and string concatenation.

Static Types

Most compiled programming languages have static types and compile-time type checking, e.g.

Int x;
String s;
x := 7;
s := "seven";
x := "seven" --type error

Static type checking guarantees that a wide class of programming errors in which an operator is applied to the wrong type of operand cannot occur when the program runs.

Static type checking also allows operators to be overloaded with different meanings, different code sequences, e.g.

1 + 2                       --Int addition
1.2 + 3.4                   --Real addition
1.1 i 1.1 + 2.2 i 2.2       --Complex addition
"one" + "two"               --concatenation
{Monday, Friday} + {dayoff} --Set union

and so on, as the language designer chooses. This is achieved with no run-time penalty.

A programming language will provide some atomic types, generally including Int, Real, Boolean, and Char, and some structured types possibly including array ([ ]), tuple (record, structure), set of, pointer to (ref, ^, *). Many programming languages also allow a programmer to define new types, built from atomic types and structured types.

Polymorphic Types


Type Classes

--2004, LA.

© L. Allison   http://www.allisons.org/ll/   (or as otherwise indicated),
Created with "vi (Linux or Solaris)",  charset=iso-8859-1,  fetched Thursday, 21-Sep-2017 01:11:52 EDT.

free: Linux, Ubuntu operating-sys, OpenOffice office-suite, The GIMP ~photoshop,
Firefox web-browser, FlashBlock flash on/off.