caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: jgm@cs.cornell.edu
Cc: caml-list@inria.fr
Subject: RE: [Caml-list] Pattern matcher no more supposed to warn on non  exhaustive patterns ?
Date: Thu, 04 Oct 2001 17:17:07 +0900	[thread overview]
Message-ID: <20011004171707E.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <706871B20764CD449DB0E8E3D81C4D4301EE6C00@opus.cs.cornell.edu>

> 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

Wouldn't the straightforward idea be to take a snapshot of the mutable
parts in the pattern before starting the matching ?
That is, convert the above to:
  let foo z =
    let z' = {contents=z.contents} in
    match z' with
      {contents=None} -> 0
    | {contents=Some f} -> f 0

The funny part is if you also have aliases in the pattern
  let foo z =
     match z with
       {contents=None} as x -> x.contents <- Some 0; 0
     | {contents=Some(f)} -> f(0);

Then you would have to make sure you return a pointer to the original
structure:
  let foo z =
    let x = z in
    let z' = {contents=z.contents} in
    match z' with
      {contents=None} -> x.contents <- Some 0; 0
    | {contents=Some f} -> f 0

It's even more complex than that, if you think of
  let foo z =
     match z with
       {contents=None} as x when (x.contents <- Some 1; false) -> 0
     | {contents=Some ({contents=None} as y)}
       when (y <- Some 1; false) -> 1
     | {contents=Some {contents=Some f}} -> f 0
     | _ -> 2

The idea is that you have to take snapshots of the type as you visit
it, so that every time you come back somewhere it has not changed.
So you build a shodow structure with both actual and copied nodes
(types are not correct):

type 'a shadow_ref = {real = 'a ref; copy = 'a ref}

let shadow_ref z = {real = z; copy = {contents = z.contents}}

let foo z =
  let zs = shadow_ref z in
  match zs.copy with
    {contents=None} ->
      zs.real.contents <- Some 1; if false then 0 else continue
  | {contents=Some z1} ->
      let z1s = shadow_ref z1 in
      zs.copy.contents <- Some z1s; (* update with a different type! *)
      begin match z1s.copy with
        {contents = None} ->
          z1s.real.contents <- Some 1; if false then 1 else continue
      | _ -> continue
      end
  | {contents=Some z1s} ->
      begin match z1s.copy with
        {contents = Some f} -> f 0
      | _ -> continue
      end
  | _ ->  2

Looks possible, but the algorithm might get heavy...

Jacques Garrigue
-------------------
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


  parent reply	other threads:[~2001-10-04  8:17 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-10-04  4:29 Gregory Morrisett
2001-10-04  7:06 ` Luc Maranget
2001-10-04  8:17 ` Jacques Garrigue [this message]
2001-10-04 12:28 ` Xavier Leroy
  -- strict thread matches above, loose matches on Subject: below --
2001-10-04  7:36 Damien Doligez
2001-10-04  7:51 ` Einar Karttunen
2001-10-03 17:37 Jean-Marc Eber
2001-10-03 16:54 ` Dan Grossman
2001-10-03 20:52   ` Pierre Weis
2001-10-04  7:55 ` Luc Maranget
2001-10-04  9:06   ` Luc Maranget

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=20011004171707E.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=jgm@cs.cornell.edu \
    /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).