caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: maranget@yquem.inria.fr (Luc Maranget)
To: Jim Miller <gordon.j.miller@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Pattern matching question
Date: Thu, 20 Jul 2006 10:38:28 +0200	[thread overview]
Message-ID: <20060720083828.GA16258@yquem.inria.fr> (raw)
In-Reply-To: <beed19130607191148k4d94b706mf1f7ce222b1d7fba@mail.gmail.com>

> I'm trying to use an OCaml program to form an umambiguous protocol
> specification.  I've convinced myself that by proper definition of my types,
> and avoiding using the _ in matches, I can get OCaml to help me verify that
> I have a complete specification.  I'm doing this by defining a function that
> specifies the behavior of one of the protocol partners when it receives a
> message.  Here is an example of what I have.
> 
> let requestOp = REQUEST_KEY | SUCCESS;;
> 
> let message = None | Register of requestOp * int;;
> 
> let receive idList msg =
>  match message with
>     None -> None
> 
> |    Register( REQUEST_KEY, id ) -> begin
>         match idList with
>             [] -> Register(SUCCESS, id)
>         |   l when (List.mem id l) -> Register(Success, (next_available_id
> l))
>         |  _ -> Register(Success, id)
> ;;
> 

To avoid | _ -> ... matching clauses, have a look at the -w E warning.
<http://caml.inria.fr/pub/docs/manual-ocaml/manual022.html>
Section 8.2 'Options'


I cannot get your exact point, besides the above code does not even go through
parsing (missing end), let -> type etc.

In fact what you want to achieve is still unclear.
As regards your message and requestOp types there should be no problem,

Something like

match msg with
| None -> ...
| Register (REQUEST_KEY, id) -> ...
| Register (SUCCESS, id) -> ...

Is easily shown to be exhaustive.

As regards your list example, perhaps you'd like better
match idList with
| [] -> Register(SUCCESS, id)
| _::_ ->
  if List.mem id idList then  Register(SUCCESS, next_available idList)
  else Register(SUCCESS, id)

or even

match idList with
| [] -> Register(SUCCESS, id)
| _::_ when List.mem id idList -> Register(SUCCESS, next_available idList)
| _::_ -> Register(SUCCESS, id)

Both are easily shown to be exhaustive, but frankly, this brings you
no more guarantee than the much more direct code

if List.mem  id idList then
   Register(SUCCESS, next_available_id idList))
else
   Register (SUCESS, id)

-- 
Luc Maranget


      parent reply	other threads:[~2006-07-20  8:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-19 18:48 Jim Miller
2006-07-19 18:55 ` [Caml-list] " Seth J. Fogarty
     [not found] ` <ad8cfe7e0607191737l551e1d8aif8d5859fb6c398fd@mail.gmail.com>
2006-07-20  1:03   ` Jim Miller
2006-07-20  8:38 ` Luc Maranget [this message]

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=20060720083828.GA16258@yquem.inria.fr \
    --to=maranget@yquem.inria.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=gordon.j.miller@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;
as well as URLs for NNTP newsgroup(s).