This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

A basic question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2004-12-30 (03:38) From: skaller Subject: Re: [Caml-list] A basic question
```On Thu, 2004-12-30 at 13:47, Kathy Chen wrote:
> Hi, all,
>
> I'm a newcomer here.
> I don't understand why the following two
>     let [x;y;z] = [1;2;3] ;;
> and
>     let [x;y;z] = [1;2;3;4] ;;
> have warnings: "this pattern-matching is not exhaustive".
> I think they just setting values for x, y, and z.
>
> Could anyone pls tell me why?

It's a shortcoming of the type system.

The RHS is just a list. The fact it has 3 elements in it
is lost by the type system. The let is then matching
a list of 3 elements against a list of unknown number
of elements.. which isn't exhaustive.

The same example using tuples will work:

let x,y,z = 1,2,3

without a warning because the number (and type) of elements
in a tuple is known to the type system.

The difficulty comes from the 'weak' interpretation
of recursive types as the union of all expansions.

For 'lists' a stronger interpretation would be useful,
and is equivalent to the notion of an array of some
length (however Ocaml arrays are distinct types which
don't have lengths either).

C++ therefore has a 'stronger' type system in this
respect, since arrays of definite length exist.

However, the generalisation to arbitrary inductive
types is probably not so useful as arrays:
the length of an array is a single value, to describe
the structure of a finite tree would require a
lot of extra data in the type system.

FYI: as an experiment, Felix has algebraic arrays.
They are identical to tuples of n elements from
a typing viewpoint (however internally the representation
is distinct, and externally the generated code is too).

--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net

```