From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 8CFF97EE4B for ; Thu, 10 Oct 2013 21:38:18 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of yotambarnoy@gmail.com) identity=pra; client-ip=209.85.128.49; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="yotambarnoy@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of yotambarnoy@gmail.com designates 209.85.128.49 as permitted sender) identity=mailfrom; client-ip=209.85.128.49; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="yotambarnoy@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qe0-f49.google.com) identity=helo; client-ip=209.85.128.49; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="postmaster@mail-qe0-f49.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArkCALwAV1LRVYAxlGdsb2JhbABZhBHBLoEdCBYOAQEBAQcLCwkSKoIlAQEEAScZARsdAQMBCwYFBAc7IQEBEQEFARwGE4dzAQMJBpwMjFWDCoQVChknDWSJAQEFDIxOgm0HhCMDlhyBaYxMg0sYKYRqIA X-IPAS-Result: ArkCALwAV1LRVYAxlGdsb2JhbABZhBHBLoEdCBYOAQEBAQcLCwkSKoIlAQEEAScZARsdAQMBCwYFBAc7IQEBEQEFARwGE4dzAQMJBpwMjFWDCoQVChknDWSJAQEFDIxOgm0HhCMDlhyBaYxMg0sYKYRqIA X-IronPort-AV: E=Sophos;i="4.90,1074,1371074400"; d="scan'208";a="29865725" Received: from mail-qe0-f49.google.com ([209.85.128.49]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 10 Oct 2013 21:38:17 +0200 Received: by mail-qe0-f49.google.com with SMTP id ff1so2234168qeb.22 for ; Thu, 10 Oct 2013 12:38:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=G94Lw/PNcg82slbWaSOJAPUvvZP7iHglJbofJWTSfgY=; b=fhC7U3pbcITJG9EWwvjapzouE3krQCxYY4Ondpk8LZH8MvPWpg3+5qGWj5v7yc8LL4 3zwMKjN7PBkALdAXZX/11meOGTYOrCwFkMziVdy6K+/esC5iOeKKZQ90Pv65/RmeEqlp 5UmHuELGXxrLrPN/EnXD5rMLauA2G3ZrwW80Cvu5og7QA39l7U7U+97aekej6hb1iORX AjpW3nMrtCOikwvWRIOacGl9qXMXZoqSFklu1BuN39Z3Y7tGYtNHc9eeTJAUfhpZSG3S v0WXD5/md6b7i+dB18EqB8oXGSvM3w5oCYlB8cPEY38qtH/Q7AVR1FhJycMl9U9JrAzc 8y3Q== X-Received: by 10.49.95.233 with SMTP id dn9mr3504133qeb.54.1381433896841; Thu, 10 Oct 2013 12:38:16 -0700 (PDT) MIME-Version: 1.0 Received: by 10.224.139.20 with HTTP; Thu, 10 Oct 2013 12:37:56 -0700 (PDT) In-Reply-To: References: From: Yotam Barnoy Date: Thu, 10 Oct 2013 15:37:56 -0400 Message-ID: To: Ashish Agarwal Cc: Ocaml Mailing List Content-Type: multipart/alternative; boundary=047d7b6d9e1cb3b97f04e868249d Subject: Re: [Caml-list] Pattern matching on refs --047d7b6d9e1cb3b97f04e868249d Content-Type: text/plain; charset=ISO-8859-1 It wouldn't solve the problem, because in reality I'm matching something like this piece of code implementing a doubly-linked list: type 'a cell = { data : 'a; next : 'a link ref; last : 'a link ref; } and 'a link = Cons of 'a cell | Nil type 'a t = { firstl: 'a link ref; lastl: 'a link ref; } let pop s = match s with | {firstl = {contents=Nil}; lastl = _ } -> raise Empty | {lastl = {contents=Cons {data; _}}} when s.lastl == s.firstl -> (* one term *) s.lastl := Nil; s.firstl := Nil; data | {firstl = {contents=Cons {data; next={contents=Cons n}; last={contents=Nil}}}} -> n.last := Nil; s.firstl := Cons n; data | _ -> failwith "Error in pop" See how unwieldy that is without the ability to clean it up by matching on refs? -Yotam On Thu, Oct 10, 2013 at 3:34 PM, Ashish Agarwal wrote: > Would it solve your problem to instead write: > > match !x with > | y -> ... > > > On Thu, Oct 10, 2013 at 3:17 PM, Yotam Barnoy wrote: > >> I recently found out how ugly it is to pattern-match on a ref, using >> {contents=...}. This should be extremely easy to fix in the parser. Can it >> please be put into the next version of ocaml? >> ie. >> >> match x with >> | ref y -> ... >> >> -Yotam >> > > --047d7b6d9e1cb3b97f04e868249d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
It wouldn't solve the problem, because in re= ality I'm matching something like this piece of code implementing a dou= bly-linked list:

type 'a cell =3D { data : 'a;
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 next : 'a link ref;
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 last : 'a link ref;=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }

and 'a link =3D Co= ns of 'a cell | Nil

type 'a t =3D {
=A0 firstl: 'a li= nk ref;
=A0 lastl:=A0 'a link ref;
}

=A0 let pop s =3D mat= ch s with
=A0=A0=A0 | {firstl =3D {contents=3DNil}; lastl =3D _ } -> raise Empty
=A0=A0=A0 | {lastl =3D {contents=3DCons {data; _}}} when s.lastl =3D= =3D s.firstl ->
=A0=A0=A0=A0=A0=A0=A0 (* one term *)
=A0=A0=A0=A0= =A0=A0=A0 s.lastl :=3D Nil;
=A0=A0=A0=A0=A0=A0=A0 s.firstl :=3D Nil;
=A0=A0=A0=A0=A0=A0=A0 data

=A0=A0=A0 | {firstl =3D
=A0=A0=A0=A0= =A0 {contents=3DCons {data; next=3D{contents=3DCons n}; last=3D{contents=3D= Nil}}}} ->
=A0=A0=A0=A0=A0=A0=A0 n.last :=3D Nil;
=A0=A0=A0=A0=A0= =A0=A0 s.firstl :=3D Cons n;
=A0=A0=A0=A0=A0=A0=A0 data
=A0=A0=A0 | _= -> failwith "Error in pop"


See how unwieldy that is without the ability to clean it up b= y matching on refs?

-Yotam


On Thu, Oct 10, 2013 at 3:34 PM, Ashish= Agarwal <agarwal1975@gmail.com> wrote:
Would it solve your problem= to instead write:

match !x with
| y -> ...=

On Thu, Oct 10, 2013 at 3:17 PM, Yotam Bar= noy <yotambarnoy@gmail.com> wrote:
I recen= tly found out how ugly it is to pattern-match on a ref, using {contents=3D.= ..}. This should be extremely easy to fix in the parser. Can it please be p= ut into the next version of ocaml?
ie.

match x with
| ref y -> ...

-Yotam


--047d7b6d9e1cb3b97f04e868249d--