Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Kathy Chen <kathy08@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] A basic question
Date: 30 Dec 2004 14:38:06 +1100	[thread overview]
Message-ID: <1104377886.22405.95.camel@pelican.wigram> (raw)
In-Reply-To: <2145c6e80412291847c40fc92@mail.gmail.com>

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




  parent reply	other threads:[~2004-12-30  3:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-30  2:47 Kathy Chen
2004-12-30  3:29 ` [Caml-list] " Sébastien Hinderer
2004-12-30  3:38 ` skaller [this message]
2004-12-30 10:28 ` Luc Maranget
2004-12-31 18:40   ` Kathy Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1104377886.22405.95.camel@pelican.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=kathy08@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox