caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jean-Marc Eber" <jeanmarc.eber@lexifi.com>
To: "caml-list" <caml-list@inria.fr>
Subject: [Caml-list] Pattern matcher no more supposed to warn on non exhaustive patterns ?
Date: Wed, 3 Oct 2001 19:37:56 +0200	[thread overview]
Message-ID: <007201c14c32$37e4c5a0$060000c0@N7YYB> (raw)

Dear OCamlers,

I don't know if the following is a bug, but it seems contrary to what
I understood from a few postings on this list. I also suspect a
(profound) modification introduced by the "new pattern matching
compiler". This modification doesn't seem (to me at least) to have
been documented somewhere.

Last Sunday, a former colleague visited me. I wanted to convince him
about the advantage of functional programming over other approaches,
when using a powerful compiler. I began with pattern matching and
exhaustiveness analysis/warnings, showing him how the compiler may
spot to the programmer some "forgotten" cases.



        Objective Caml version 3.02

# let f = function
  | 0, _ -> 1
  | _, 0 -> 2;;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(1, 1)
val f : int * int -> int = <fun>
#

OK, thats fine, and we are happy: of course, we didn't "cover the
board", and treated only some particular cases!

Now lets do it with guarded patterns:

# let f = function
  | x, _ when x=0 -> 1
  | _, y when y=0 -> 2;;
val f : int * int -> int = <fun>
#

OOPS: no more non-exhaustiveness warning!
I understand well of course that the compiler isn't able to do an
analysis of the expression in a when clause, but thought that because
of this impossibility, the compiler supposed, for exhaustiveness
analysis, that a when clause would "always be false", assuming that
this branch would not be taken. This clearly isn't the case here!

Now suppose that we "extend" a little bit function f and treat some
more cases:

# let f = function
  | x, _ when x=0 -> 1
  | _, y when y=0 -> 2
  |1, _ -> 3;;
Warning: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(0, _)
val f : int * int -> int = <fun>
#

So by *adding* a clause to the pattern matcher we "transformed" a
function without warning in a function (now correctly) spotting some
incompleteness. The exhaustiveness analyzer is no more *monotonic*, a
property one would intuitively assume.

I always thought that by making the "pessimistic" assumption about
guarded patterns, we had the following theorem: a program compiling
without exhaustiveness warning can never raise a Match_failure
exception. This is of course a very nice static property, and I always
programmed (by, in some rare cases, adding a "dummy" case I knew would
never been taken, but making the exhaustiveness analyzer happy) in
this way. This theorem is LOST!

I looked at Le Fessant's and Maranget's ICFP 2001 paper, but if I'm
not wrong, it doesn't treat the guarded pattern case (a case of non
exhaustiveness indeed, so we don't know if the OCaml derived "paper
content compiler" would warn about it ! :-))

Conclusion and personal opinion:

I think things changed between 3.00 and 3.02, no ?

If so, is this considered a bug ?

my opinion:
1. loosing any kind of static property theorem is TERRIBLE and should
be avoided if possible!
2. exhaustiveness analysis is a "big deal" for practical programming
work. In some complex datastructures, it really helps you track down
some errors in algorithms.
3. (well used) guarded patterns (when clause) are often very useful
(expressions would sometimes become much more complex without them!)
and their use should not be discouraged by poor exhautivenss analysis.

Have a nice day, OCamlers!

Jean-Marc Eber
LexiFi Technologies




-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


             reply	other threads:[~2001-10-03 16:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-03 17:37 Jean-Marc Eber [this message]
2001-10-03 16:54 ` Dan Grossman
2001-10-03 18:07   ` [Caml-list] Pattern matcher no more supposed to warn on non Luc Maranget
2001-10-03 18:23     ` Dan Grossman
2001-10-03 20:52   ` [Caml-list] Pattern matcher no more supposed to warn on non exhaustive patterns ? Pierre Weis
2001-10-04  7:55 ` Luc Maranget
2001-10-04  9:06   ` Luc Maranget
2001-10-04  4:29 Gregory Morrisett
2001-10-04  7:06 ` Luc Maranget
2001-10-04  8:17 ` Jacques Garrigue
2001-10-04 12:28 ` Xavier Leroy
2001-10-04  7:36 Damien Doligez
2001-10-04  7:51 ` Einar Karttunen

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='007201c14c32$37e4c5a0$060000c0@N7YYB' \
    --to=jeanmarc.eber@lexifi.com \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).