caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* A basic question
@ 2004-12-30  2:47 Kathy Chen
  2004-12-30  3:29 ` [Caml-list] " Sébastien Hinderer
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Kathy Chen @ 2004-12-30  2:47 UTC (permalink / raw)
  To: caml-list

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?

Thanks.
Kathy


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] A basic question
  2004-12-30  2:47 A basic question Kathy Chen
@ 2004-12-30  3:29 ` Sébastien Hinderer
  2004-12-30  3:38 ` skaller
  2004-12-30 10:28 ` Luc Maranget
  2 siblings, 0 replies; 5+ messages in thread
From: Sébastien Hinderer @ 2004-12-30  3:29 UTC (permalink / raw)
  To: caml-list

Hi,

> 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".

This is because, for pattern-matching to work, the two lists _must_
have the same length, so that the nth element in the right-hand list can
be assigned to the nth variable specified in the left-hand list.
But, the compiler has no way to check that in every case, hence this warning.

> I think they just setting values for x, y, and z.

Of course, it is probably possible to find some heuristics to check the
equality between lengths, making your first example work, but it'd be
tricky.
Concerning your second example, it is not that obvious to attach a good
semantics to what you write, because the lists are of different lengths.

Now, if the only thing you want to do is to assign values to several
variables at the same time, you can do something like : 
let (x, y z) = (1, 2, 3);;

Of course, the types of (x y, z) and (1, 2, 3) must be compatible.
So, a phrase like 

let (x, y, z) = (1, 2, 3, 4);;

is wrong.

hth,
Sébastien.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] A basic question
  2004-12-30  2:47 A basic question Kathy Chen
  2004-12-30  3:29 ` [Caml-list] " Sébastien Hinderer
@ 2004-12-30  3:38 ` skaller
  2004-12-30 10:28 ` Luc Maranget
  2 siblings, 0 replies; 5+ messages in thread
From: skaller @ 2004-12-30  3:38 UTC (permalink / raw)
  To: Kathy Chen; +Cc: caml-list

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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] A basic question
  2004-12-30  2:47 A basic question Kathy Chen
  2004-12-30  3:29 ` [Caml-list] " Sébastien Hinderer
  2004-12-30  3:38 ` skaller
@ 2004-12-30 10:28 ` Luc Maranget
  2004-12-31 18:40   ` Kathy Chen
  2 siblings, 1 reply; 5+ messages in thread
From: Luc Maranget @ 2004-12-30 10:28 UTC (permalink / raw)
  To: Kathy Chen; +Cc: caml-list

> 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?
> 
> Thanks.
> Kathy
> 

There is nothing special with 'let pat = e1 in e2' bindings,
they get translated to 'match e1 with pat -> e2'
[Toplevel bindings 'let p = e ;;' are similar]

Then, usual pattern matching analysis is performed.

So your question can be reformulated for instance as
'match [1;2;3] with [x;y;z] -> ....'
'match [1;2;3;4] with [x;y;z] -> ....'
yield "this pattern-matching is not exhaustive" warnings.


First notice that running the second example results in a match failure.
(I mean, the warning is useful).

Then, one can argue that, since the matched expression is constant,
matching result can be known by the compiler and that it  should
produce no warning in the first case and produce an error in the second case.
One can also argue, as Skaller does that a more sophisticated typing system
would behave similarily whatever the matched expression is.

Well, the compiler is too lazy ! So it produces a warning in both examples !

Finally as said by other contributors, a good technique for performing
several bindings at time is using tuples:

let (x,y,z) = (1,2,3);;


Hope it helps,


-- Luc Maranget


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] A basic question
  2004-12-30 10:28 ` Luc Maranget
@ 2004-12-31 18:40   ` Kathy Chen
  0 siblings, 0 replies; 5+ messages in thread
From: Kathy Chen @ 2004-12-31 18:40 UTC (permalink / raw)
  To: Luc Maranget; +Cc: caml-list

Thanks for all your answers!

Kathy


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-12-31 18:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-30  2:47 A basic question Kathy Chen
2004-12-30  3:29 ` [Caml-list] " Sébastien Hinderer
2004-12-30  3:38 ` skaller
2004-12-30 10:28 ` Luc Maranget
2004-12-31 18:40   ` Kathy Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).