Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: MicMatch
Date: Sat, 1 Dec 2007 16:00:00 +0000	[thread overview]
Message-ID: <200712011600.00627.jon@ffconsultancy.com> (raw)


I just stumbled upon this Wiki page discussing MicMatch:

  http://ocaml.pbwiki.com/Micmatch

and noted that the implementation of views disables exhaustiveness checking. I 
think it is worth keeping the static checking of view patterns.

So MicMatch uses definitions like:

  type 'a lazy_list = Empty | Cons of 'a * 'a lazy_list lazy_t

  let view Empty = fun l -> Lazy.force l = Empty
  let view Cons = fun l -> match Lazy.force l with Cons x -> Some x

  match ll with
      %Empty -> ...
    | %Cons (x, %Empty) -> ...
    | %Cons (x1, %Cons (x2, %Empty)) -> ...
    | _ -> ...

where F# would use:

  let (|PEmpty|PCons|) l =
    match Lazy.force l with
    | Empty -> PEmpty
    | Cons(h, t) -> PCons(h, t)

This basically defines a new sum type and every time a view pattern is 
encountered, it is converted using this function into the new sum type and 
then matched over. This means you cannot mix view and non-view patterns in 
the same match but you do get to keep exhaustiveness checking.

Having said all of that, the only application of F#-style views in OCaml that 
I can think of is simply matching lazy values, which could be implemented 
more easily and with no syntactic overhead.

There are other applications that MicMatch might not cater for. Specifically, 
factoring patterns and parameterizing patterns over values. For example, you 
might want an active pattern than handles commutativity:

  Commute(p1, p2)  =>  p1, p2 | p2, p1

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e


             reply	other threads:[~2007-12-01 16:09 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-01 16:00 Jon Harrop [this message]
2007-12-02 22:12 ` [Caml-list] MicMatch Martin Jambon

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=200712011600.00627.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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