caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Pattern matcher no more supposed to warn on non exhaustive patterns ?
@ 2001-10-03 17:37 Jean-Marc Eber
  2001-10-03 16:54 ` Dan Grossman
  2001-10-04  7:55 ` Luc Maranget
  0 siblings, 2 replies; 13+ messages in thread
From: Jean-Marc Eber @ 2001-10-03 17:37 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 13+ messages in thread
* RE: [Caml-list] Pattern matcher no more supposed to warn on non  exhaustive patterns ?
@ 2001-10-04  4:29 Gregory Morrisett
  2001-10-04  7:06 ` Luc Maranget
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Gregory Morrisett @ 2001-10-04  4:29 UTC (permalink / raw)
  To: Pierre Weis, Daniel Grossman; +Cc: caml-list

The issue with threads is a bit more troublesome.  Consider:

  let x : (int->int) option ref = ref (Some (fun x -> x));;

  let foo z =
      match z with
        {contents=None} -> 0
      | {contents=Some(f)} -> f(0);

Now suppose I fork two threads:

Thread 1:  foo x
Thread 2:  x := None

And suppose that Thread 1 runs long enough that it does the first
match, so it assumes the contents of x is not of the form None.  
Now Thread 1 gets descheduled, Thread 2 runs, and sets the
contents of x to None.  Then Thread 2 continues with the second
match...

The question is, does Caml core dump because the pattern matcher
assumes that the contents *has* to be a Some(-) in the second
case?  Or does it do the derefence and check atomically?  Or
does it add a default case that raises a Match exception?  

The same thing shows up in SML/NJ with CML.  The problem is that
in the presence of threads, you really shouldn't be able to 
dereference a mutable value in your patterns.

-Greg
-------------------
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


^ permalink raw reply	[flat|nested] 13+ messages in thread
* RE: [Caml-list] Pattern matcher no more supposed to warn on non  exhaustive patterns ?
@ 2001-10-04  7:36 Damien Doligez
  2001-10-04  7:51 ` Einar Karttunen
  0 siblings, 1 reply; 13+ messages in thread
From: Damien Doligez @ 2001-10-04  7:36 UTC (permalink / raw)
  To: danieljg, jgm, pierre.weis; +Cc: caml-list

>From: "Gregory Morrisett" <jgm@cs.cornell.edu>

>The same thing shows up in SML/NJ with CML.  The problem is that
>in the presence of threads, you really shouldn't be able to=20
>dereference a mutable value in your patterns.

I'd agree that core dump is surprising, but if your multi-threaded
program does anything with a mutable value without the protection of a
mutex, then it is incorrect (i.e. its semantics is unspecified).  In
O'Caml, none of the operations is specified as atomic (except a few
things in the threads library), and you shouldn't assume that they are.

-- Damien
-------------------
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


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

end of thread, other threads:[~2001-10-04 12:28 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-03 17:37 [Caml-list] Pattern matcher no more supposed to warn on non exhaustive patterns ? Jean-Marc Eber
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

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