arrows pointing right and left

Ambidexter tutorial
lesson 5
and/or types

email Scott Turner


lesson 1
lesson 2
lesson 3
lesson 4
lesson 5

There's an interesting contrast between and-types and or-types, if you count the values produced by these type constructs.

Consider an and-type that indicates a time of the week in days and hours. (& Day_of_Week & Hour_of_Day &) where the Day_of_Week type has the obvious 7 possible values and the Hour_of_Day type has 24 possible values. An example value would be
    (& tuesday & eighth_hour &)
There are 7 X 24 = 168 possible values of this type.

Consider on the other hand an or-type composed from the same two types.
    (| Day_of_Week | Hour_of_Day |)
A type similar to this might be used in the process of composing a messaging system which reports simplified times such as "yesterday" and "this morning". Example values would be
    (|x|| <+ tuesday$x)
 or (||x| <+ eighth_hour$x)
There are 7 + 24 = 31 possible values of this type.

Note the special cases of these type constructs. The type (& Day_of_Week &) and (| Day_of_Week |) both have 7 values, as the results of adding up "7" and the result of multiplying together "7" are the same.

Every value of an or-type must designate one of the component types and a value of the designated type. But type (|) has no component types, so it can have no values. This corresponds to the fact that adding up no numbers yields "0".

The (&) type is, naturally, different. A value of an and-type must designate a value for each component type. Since there are no component types it is trivial to satisfy this requirement. That trivial value is the only possible value of this type. It is denoted (&). Most people are unfamiliar with what you get by multiplying together zero numbers. The result is 1. That corresponds to the (&) type having 1 value.

Because of the relationships to addition and multiplication, or-types and and-types are often called sum types and product types respectively. The OCaml language even uses the multiplication (*) symbol to denote and-types.

Similar to the (&) and (|) types are Ambidexter's True and False types. The latter types also have 1 and 0 values respectively. The built-in name for the one value of type True is void. The rationale is that the void value conveys no information.

Support open standards!  
Valid XHTML 1.0!