Chapter 4 Labels and variants

(Chapter written by Jacques Garrigue)

This chapter gives an overview of the new features inOCaml 3: labels, and polymorphic variants.

4.1 Labels

If you have a look at modules ending in Labels in the standardlibrary, you will see that function types have annotations you did nothave in the functions you defined yourself.

  1. ListLabels.map;;
  2. - : f:('a -> 'b) -> 'a list -> 'b list = <fun>
  1. StringLabels.sub;;
  2. - : string -> pos:int -> len:int -> string = <fun>

Such annotations of the form name: are called labels. They aremeant to document the code, allow more checking, and give moreflexibility to function application.You can give such names to arguments in your programs, by prefixing themwith a tilde ~.

  1. let f ~x ~y = x - y;;
  2. val f : x:int -> y:int -> int = <fun>
  1. let x = 3 and y = 2 in f ~x ~y;;
  2. - : int = 1

When you want to use distinct names for the variable and the labelappearing in the type, you can use a naming label of the form~name:. This also applies when the argument is not a variable.

  1. let f ~x:x1 ~y:y1 = x1 - y1;;
  2. val f : x:int -> y:int -> int = <fun>
  1. f ~x:3 ~y:2;;
  2. - : int = 1

Labels obey the same rules as other identifiers in OCaml, that is youcannot use a reserved keyword (like in or to) as label.

Formal parameters and arguments are matched according to theirrespective labels1, the absence of labelbeing interpreted as the empty label.This allows commuting arguments in applications. One can alsopartially apply a function on any argument, creating a new function ofthe remaining parameters.

  1. let f ~x ~y = x - y;;
  2. val f : x:int -> y:int -> int = <fun>
  1. f ~y:2 ~x:3;;
  2. - : int = 1
  1. ListLabels.fold_left;;
  2. - : f:('a -> 'b -> 'a) -> init:'a -> 'b list -> 'a = <fun>
  1. ListLabels.fold_left [1;2;3] ~init:0 ~f:( + );;
  2. - : int = 6
  1. ListLabels.fold_left ~init:0;;
  2. - : f:(int -> 'a -> int) -> 'a list -> int = <fun>

If several arguments of a function bear the same label (or no label),they will not commute among themselves, and order matters. But theycan still commute with other arguments.

  1. let hline ~x:x1 ~x:x2 ~y = (x1, x2, y);;
  2. val hline : x:'a -> x:'b -> y:'c -> 'a * 'b * 'c = <fun>
  1. hline ~x:3 ~y:2 ~x:5;;
  2. - : int * int * int = (3, 5, 2)

As an exception to the above parameter matching rules, if anapplication is total (omitting all optional arguments), labels may beomitted.In practice, many applications are total, so that labels can often beomitted.

  1. f 3 2;;
  2. - : int = 1
  1. ListLabels.map succ [1;2;3];;
  2. - : int list = [2; 3; 4]

But beware that functions like ListLabels.fold_left whose resulttype is a type variable will never be considered as totally applied.

  1. ListLabels.fold_left ( + ) 0 [1;2;3];;
  2. Error: This expression has type int -> int -> int
  3. but an expression was expected of type 'a list

When a function is passed as an argument to a higher-order function,labels must match in both types. Neither adding nor removing labelsare allowed.

  1. let h g = g ~x:3 ~y:2;;
  2. val h : (x:int -> y:int -> 'a) -> 'a = <fun>
  1. h f;;
  2. - : int = 1
  1. h ( + ) ;;
  2. Error: This expression has type int -> int -> int
  3. but an expression was expected of type x:int -> y:int -> 'a

Note that when you don’t need an argument, you can still use a wildcardpattern, but you must prefix it with the label.

  1. h (fun ~x:_ ~y -> y+1);;
  2. - : int = 3

4.1.1 Optional arguments

An interesting feature of labeled arguments is that they can be madeoptional. For optional parameters, the question mark ? replaces thetilde ~ of non-optional ones, and the label is also prefixed by ?in the function type.Default values may be given for such optional parameters.

  1. let bump ?(step = 1) x = x + step;;
  2. val bump : ?step:int -> int -> int = <fun>
  1. bump 2;;
  2. - : int = 3
  1. bump ~step:3 2;;
  2. - : int = 5

A function taking some optional arguments must also take at least onenon-optional argument. The criterion for deciding whether an optionalargument has been omitted is the non-labeled application of anargument appearing after this optional argument in the function type.Note that if that argument is labeled, you will only be able toeliminate optional arguments by totally applying the function,omitting all optional arguments and omitting all labels for allremaining arguments.

  1. let test ?(x = 0) ?(y = 0) () ?(z = 0) () = (x, y, z);;
  2. val test : ?x:int -> ?y:int -> unit -> ?z:int -> unit -> int * int * int =
  3. <fun>
  1. test ();;
  2. - : ?z:int -> unit -> int * int * int = <fun>
  1. test ~x:2 () ~z:3 ();;
  2. - : int * int * int = (2, 0, 3)

Optional parameters may also commute with non-optional or unlabeledones, as long as they are applied simultaneously. By nature, optionalarguments do not commute with unlabeled arguments appliedindependently.

  1. test ~y:2 ~x:3 () ();;
  2. - : int * int * int = (3, 2, 0)
  1. test () () ~z:1 ~y:2 ~x:3;;
  2. - : int * int * int = (3, 2, 1)
  1. (test () ()) ~z:1 ;;
  2. Error: This expression has type int * int * int
  3. This is not a function; it cannot be applied.

Here (test () ()) is already (0,0,0) and cannot be furtherapplied.

Optional arguments are actually implemented as option types. Ifyou do not give a default value, you have access to their internalrepresentation, type 'a option = None | Some of 'a. You can thenprovide different behaviors when an argument is present or not.

  1. let bump ?step x =
  2. match step with
  3. | None -> x * 2
  4. | Some y -> x + y
  5. ;;
  6. val bump : ?step:int -> int -> int = <fun>

It may also be useful to relay an optional argument from a functioncall to another. This can be done by prefixing the applied argumentwith ?. This question mark disables the wrapping of optionalargument in an option type.

  1. let test2 ?x ?y () = test ?x ?y () ();;
  2. val test2 : ?x:int -> ?y:int -> unit -> int * int * int = <fun>
  1. test2 ?x:None;;
  2. - : ?y:int -> unit -> int * int * int = <fun>

4.1.2 Labels and type inference

While they provide an increased comfort for writing functionapplications, labels and optional arguments have the pitfall that theycannot be inferred as completely as the rest of the language.

You can see it in the following two examples.

  1. let h' g = g ~y:2 ~x:3;;
  2. val h' : (y:int -> x:int -> 'a) -> 'a = <fun>
  1. h' f ;;
  2. Error: This expression has type x:int -> y:int -> int
  3. but an expression was expected of type y:int -> x:int -> 'a
  1. let bump_it bump x =
  2. bump ~step:2 x;;
  3. val bump_it : (step:int -> 'a -> 'b) -> 'a -> 'b = <fun>
  1. bump_it bump 1 ;;
  2. Error: This expression has type ?step:int -> int -> int
  3. but an expression was expected of type step:int -> 'a -> 'b

The first case is simple: g is passed ~y and then ~x, but fexpects ~x and then ~y. This is correctly handled if we know thetype of g to be x:int -> y:int -> int in advance, but otherwisethis causes the above type clash. The simplest workaround is to applyformal parameters in a standard order.

The second example is more subtle: while we intended the argumentbump to be of type ?step:int -> int -> int, it is inferred asstep:int -> int -> 'a.These two types being incompatible (internally normal and optionalarguments are different), a type error occurs when applying bump_itto the real bump.

We will not try here to explain in detail how type inference works.One must just understand that there is not enough information in theabove program to deduce the correct type of g or bump. That is,there is no way to know whether an argument is optional or not, orwhich is the correct order, by looking only at how a function isapplied. The strategy used by the compiler is to assume that there areno optional arguments, and that applications are done in the rightorder.

The right way to solve this problem for optional parameters is to adda type annotation to the argument bump.

  1. let bump_it (bump : ?step:int -> int -> int) x =
  2. bump ~step:2 x;;
  3. val bump_it : (?step:int -> int -> int) -> int -> int = <fun>
  1. bump_it bump 1;;
  2. - : int = 3

In practice, such problems appear mostly when using objects whosemethods have optional arguments, so that writing the type of objectarguments is often a good idea.

Normally the compiler generates a type error if you attempt to pass toa function a parameter whose type is different from the expected one.However, in the specific case where the expected type is a non-labeledfunction type, and the argument is a function expecting optionalparameters, the compiler will attempt to transform the argument tohave it match the expected type, by passing None for all optionalparameters.

  1. let twice f (x : int) = f(f x);;
  2. val twice : (int -> int) -> int -> int = <fun>
  1. twice bump 2;;
  2. - : int = 8

This transformation is coherent with the intended semantics,including side-effects. That is, if the application of optionalparameters shall produce side-effects, these are delayed until thereceived function is really applied to an argument.

4.1.3 Suggestions for labeling

Like for names, choosing labels for functions is not an easy task. Agood labeling is a labeling which

  • makes programs more readable,
  • is easy to remember,
  • when possible, allows useful partial applications. We explain here the rules we applied when labeling OCamllibraries.

To speak in an “object-oriented” way, one can consider that eachfunction has a main argument, its object, and other argumentsrelated with its action, the parameters. To permit thecombination of functions through functionals in commuting label mode, theobject will not be labeled. Its role is clear from the functionitself. The parameters are labeled with names reminding oftheir nature or their role. The best labels combine nature androle. When this is not possible the role is to be preferred, since thenature willoften be given by the type itself. Obscure abbreviations should beavoided.

  1. ListLabels.map : f:('a -> 'b) -> 'a list -> 'b list
  2. UnixLabels.write : file_descr -> buf:bytes -> pos:int -> len:int -> unit

When there are several objects of same nature and role, they are allleft unlabeled.

  1. ListLabels.iter2 : f:('a -> 'b -> 'c) -> 'a list -> 'b list -> unit

When there is no preferable object, all arguments are labeled.

  1. BytesLabels.blit :
  2. src:bytes -> src_pos:int -> dst:bytes -> dst_pos:int -> len:int -> unit

However, when there is only one argument, it is often left unlabeled.

  1. BytesLabels.create : int -> bytes

This principle also applies to functions of several arguments whosereturn type is a type variable, as long as the role of each argumentis not ambiguous. Labeling such functions may lead to awkward errormessages when one attempts to omit labels in an application, as wehave seen with ListLabels.fold_left.

Here are some of the label names you will find throughout thelibraries.

LabelMeaning
f:a function to be applied
pos:a position in a string, array or byte sequence
len:a length
buf:a byte sequence or string used as buffer
src:the source of an operation
dst:the destination of an operation
init:the initial value for an iterator
cmp:a comparison function, e.g.Pervasives.compare
mode:an operation mode or a flag list

All these are only suggestions, but keep in mind that thechoice of labels is essential for readability. Bizarre choices willmake the program harder to maintain.

In the ideal, the right function name with right labels should beenough to understand the function’s meaning. Since one can get thisinformation with OCamlBrowser or the ocaml toplevel, the documentationis only used when a more detailed specification is needed.

4.2 Polymorphic variants

Variants as presented in section 1.4 are apowerful tool to build data structures and algorithms. However theysometimes lack flexibility when used in modular programming. This isdue to the fact that every constructor is assigned to a unique typewhen defined and used. Even if the same name appears in the definitionof multiple types, the constructor itself belongs to only one type.Therefore, one cannot decide that a given constructor belongs tomultiple types, or consider a value of some type to belong to someother type with more constructors.

With polymorphic variants, this original assumption is removed. Thatis, a variant tag does not belong to any type in particular, the typesystem will just check that it is an admissible value according to itsuse. You need not define a type before using a variant tag. A varianttype will be inferred independently for each of its uses.

Basic use

In programs, polymorphic variants work like usual ones. You just haveto prefix their names with a backquote character `.

  1. [`On; `Off];;
  2. - : [> `Off | `On ] list = [`On; `Off]
  1. `Number 1;;
  2. - : [> `Number of int ] = `Number 1
  1. let f = function `On -> 1 | `Off -> 0 | `Number n -> n;;
  2. val f : [< `Number of int | `Off | `On ] -> int = <fun>
  1. List.map f [`On; `Off];;
  2. - : int list = [1; 0]

[>Off|On] list means that to match this list, you should atleast be able to match Off andOn, without argument.[<On|Off|Number of int] means that f may be applied toOff,On (both without argument), orNumbern wheren is an integer.The > and < inside the variant types show that they may still berefined, either by defining more tags or by allowing less. As such, theycontain an implicit type variable. Because each of the variant typesappears only once in the whole type, their implicit type variables arenot shown.

The above variant types were polymorphic, allowing further refinement.When writing type annotations, one will most often describe fixedvariant types, that is types that cannot be refined. This isalso the case for type abbreviations. Such types do not contain < or>, but just an enumeration of the tags and their associated types,just like in a normal datatype definition.

  1. type 'a vlist = [`Nil | `Cons of 'a * 'a vlist];;
  2. type 'a vlist = [ `Cons of 'a * 'a vlist | `Nil ]
  1. let rec map f : 'a vlist -> 'b vlist = function
  2. | `Nil -> `Nil
  3. | `Cons(a, l) -> `Cons(f a, map f l)
  4. ;;
  5. val map : ('a -> 'b) -> 'a vlist -> 'b vlist = <fun>

Advanced use

Type-checking polymorphic variants is a subtle thing, and someexpressions may result in more complex type information.

  1. let f = function `A -> `C | `B -> `D | x -> x;;
  2. val f : ([> `A | `B | `C | `D ] as 'a) -> 'a = <fun>
  1. f `E;;
  2. - : [> `A | `B | `C | `D | `E ] = `E

Here we are seeing two phenomena. First, since this matching is open(the last case catches any tag), we obtain the type [> A |B]rather than [< A |B] in a closed matching. Then, since x isreturned as is, input and return types are identical. The notation as 'a denotes such type sharing. If we apply f to yet another tag`E, it gets added to the list.

  1. let f1 = function `A x -> x = 1 | `B -> true | `C -> false
  2. let f2 = function `A x -> x = "a" | `B -> true ;;
  3. val f1 : [< `A of int | `B | `C ] -> bool = <fun>
  4. val f2 : [< `A of string | `B ] -> bool = <fun>
  1. let f x = f1 x && f2 x;;
  2. val f : [< `A of string & int | `B ] -> bool = <fun>

Here f1 and f2 both accept the variant tags A andB, but theargument of A is int for f1 and string for f2. In f’stypeC, only accepted by f1, disappears, but both argument typesappear for A as int &amp; string. This means that if wepass the variant tagA to f, its argument should be _both_int and string. Since there is no such value, f cannot beapplied to A, andB is the only accepted input.

Even if a value has a fixed variant type, one can still give it alarger type through coercions. Coercions are normally written withboth the source type and the destination type, but in simple cases thesource type may be omitted.

  1. type 'a wlist = [`Nil | `Cons of 'a * 'a wlist | `Snoc of 'a wlist * 'a];;
  2. type 'a wlist = [ `Cons of 'a * 'a wlist | `Nil | `Snoc of 'a wlist * 'a ]
  1. let wlist_of_vlist l = (l : 'a vlist :> 'a wlist);;
  2. val wlist_of_vlist : 'a vlist -> 'a wlist = <fun>
  1. let open_vlist l = (l : 'a vlist :> [> 'a vlist]);;
  2. val open_vlist : 'a vlist -> [> 'a vlist ] = <fun>
  1. fun x -> (x :> [`A|`B|`C]);;
  2. - : [< `A | `B | `C ] -> [ `A | `B | `C ] = <fun>

You may also selectively coerce values through pattern matching.

  1. let split_cases = function
  2. | `Nil | `Cons _ as x -> `A x
  3. | `Snoc _ as x -> `B x
  4. ;;
  5. val split_cases :
  6. [< `Cons of 'a | `Nil | `Snoc of 'b ] ->
  7. [> `A of [> `Cons of 'a | `Nil ] | `B of [> `Snoc of 'b ] ] = <fun>

When an or-pattern composed of variant tags is wrapped inside analias-pattern, the alias is given a type containing only the tagsenumerated in the or-pattern. This allows for many useful idioms, likeincremental definition of functions.

  1. let num x = `Num x
  2. let eval1 eval (`Num x) = x
  3. let rec eval x = eval1 eval x ;;
  4. val num : 'a -> [> `Num of 'a ] = <fun>
  5. val eval1 : 'a -> [< `Num of 'b ] -> 'b = <fun>
  6. val eval : [< `Num of 'a ] -> 'a = <fun>
  1. let plus x y = `Plus(x,y)
  2. let eval2 eval = function
  3. | `Plus(x,y) -> eval x + eval y
  4. | `Num _ as x -> eval1 eval x
  5. let rec eval x = eval2 eval x ;;
  6. val plus : 'a -> 'b -> [> `Plus of 'a * 'b ] = <fun>
  7. val eval2 : ('a -> int) -> [< `Num of int | `Plus of 'a * 'a ] -> int = <fun>
  8. val eval : ([< `Num of int | `Plus of 'a * 'a ] as 'a) -> int = <fun>

To make this even more comfortable, you may use type definitions asabbreviations for or-patterns. That is, if you have defined type myvariant = [Tag1 of int |Tag2 of bool], then the pattern #myvariant isequivalent to writing (Tag1(_ : int) |Tag2(_ : bool)).

Such abbreviations may be used alone,

  1. let f = function
  2. | #myvariant -> "myvariant"
  3. | `Tag3 -> "Tag3";;
  4. val f : [< `Tag1 of int | `Tag2 of bool | `Tag3 ] -> string = <fun>

or combined with with aliases.

  1. let g1 = function `Tag1 _ -> "Tag1" | `Tag2 _ -> "Tag2";;
  2. val g1 : [< `Tag1 of 'a | `Tag2 of 'b ] -> string = <fun>
  1. let g = function
  2. | #myvariant as x -> g1 x
  3. | `Tag3 -> "Tag3";;
  4. val g : [< `Tag1 of int | `Tag2 of bool | `Tag3 ] -> string = <fun>

4.2.1 Weaknesses of polymorphic variants

After seeing the power of polymorphic variants, one may wonder whythey were added to core language variants, rather than replacing them.

The answer is twofold. One first aspect is that while being prettyefficient, the lack of static type information allows for lessoptimizations, and makes polymorphic variants slightly heavier thancore language ones. However noticeable differences would onlyappear on huge data structures.

More important is the fact that polymorphic variants, while beingtype-safe, result in a weaker type discipline. That is, core languagevariants do actually much more than ensuring type-safety, they alsocheck that you use only declared constructors, that all constructorspresent in a data-structure are compatible, and they enforce typingconstraints to their parameters.

For this reason, you must be more careful about making types explicitwhen you use polymorphic variants. When you write a library, this iseasy since you can describe exact types in interfaces, but for simpleprograms you are probably better off with core language variants.

Beware also that some idioms make trivial errors very hard to find.For instance, the following code is probably wrong but the compilerhas no way to see it.

  1. type abc = [`A | `B | `C] ;;
  2. type abc = [ `A | `B | `C ]
  1. let f = function
  2. | `As -> "A"
  3. | #abc -> "other" ;;
  4. val f : [< `A | `As | `B | `C ] -> string = <fun>
  1. let f : abc -> string = f ;;
  2. val f : abc -> string = <fun>

You can avoid such risks by annotating the definition itself.

  1. let f : abc -> string = function
  2. | `As -> "A"
  3. | #abc -> "other" ;;
  4. Error: This pattern matches values of type [? `As ]
  5. but a pattern was expected which matches values of type abc
  6. The second variant type does not allow tag(s) `As

  • 1
  • This correspond to the commuting label modeof Objective Caml 3.00 through 3.02, with some additional flexibilityon total applications. The so-called classic mode (-nolabelsoptions) is now deprecated for normal use.