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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 055B27EE4B for ; Thu, 10 Oct 2013 21:47:03 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of yotambarnoy@gmail.com) identity=pra; client-ip=209.85.216.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="yotambarnoy@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of yotambarnoy@gmail.com designates 209.85.216.47 as permitted sender) identity=mailfrom; client-ip=209.85.216.47; receiver=mail2-smtp-roc.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 (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qa0-f47.google.com) identity=helo; client-ip=209.85.216.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="yotambarnoy@gmail.com"; x-sender="postmaster@mail-qa0-f47.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlYCABYDV1LRVdgvm2dsb2JhbABZgz9Srk2KFIhOgR0IFg4BAQEBAQYLCwkUKIIlAQEEAScZARsSCwEDAQsGBQsDCg0hIgERAQUBChIGExIIh1kBAwkGDJt1jFWDCoQVChknAwpkiQEBBQyPNwQHhCMDmAWBL45oGCmEaiA X-IPAS-Result: AlYCABYDV1LRVdgvm2dsb2JhbABZgz9Srk2KFIhOgR0IFg4BAQEBAQYLCwkUKIIlAQEEAScZARsSCwEDAQsGBQsDCg0hIgERAQUBChIGExIIh1kBAwkGDJt1jFWDCoQVChknAwpkiQEBBQyPNwQHhCMDmAWBL45oGCmEaiA X-IronPort-AV: E=Sophos;i="4.90,1074,1371074400"; d="scan'208";a="36469451" Received: from mail-qa0-f47.google.com ([209.85.216.47]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 10 Oct 2013 21:47:00 +0200 Received: by mail-qa0-f47.google.com with SMTP id k4so5615qaq.13 for ; Thu, 10 Oct 2013 12:47:01 -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=ko3pFgK5sphIeZ6VP989cnVkGVFVLr6lkIzLh/QaMqU=; b=Qf1Zfmutc5br58gXVQTq6TncrEnSQJrlkft5v2NgNrl874E4Kq1lAofeBEPlarkYnw 7oeQRq+VjOZZMOqcpDZXLd3zr1+IM2F/NVKKVWlG6yiEWtqaA8RCGw3OsF2kcvgjsss5 bqJzrWXixfrC5pNuXMOsCgZLawstO2plAGMFUTi42AIK1Afda4TmwiNd6W6Pb6nvQQmS EuNR7H2gZ8ZSI/AgHVyOVmP8wnGb8GxNHvU6w6IAc5I+pltMXDUXeqjHVKkpoquBjcpM BL+jj1xrMv8VEz3PLK3QqwqkvXxG52nc8jaXLdQqm/PpklF7EhtzeWQEl6U+InfSRYa6 Gkjw== X-Received: by 10.229.127.74 with SMTP id f10mr13697003qcs.16.1381434421099; Thu, 10 Oct 2013 12:47:01 -0700 (PDT) MIME-Version: 1.0 Received: by 10.224.139.20 with HTTP; Thu, 10 Oct 2013 12:46:40 -0700 (PDT) In-Reply-To: References: From: Yotam Barnoy Date: Thu, 10 Oct 2013 15:46:40 -0400 Message-ID: To: David Allsopp Cc: Ocaml Mailing List Content-Type: multipart/alternative; boundary=001a1133dd4cf3428704e8684303 Subject: Re: [Caml-list] Pattern matching on refs --001a1133dd4cf3428704e8684303 Content-Type: text/plain; charset=ISO-8859-1 D'oh! I always forget about mutable fields somehow. Refs just take over in my mind and I end up putting them everywhere I need mutability. Shows how little I actually use mutability in ocaml. And the reason for the linked lists is that I need a (low-performance) queue/stack with random access. And the reason for implementing a doubly-linked list myself is that my advisor is against using any library that's not part of the ocaml distribution. Sorry for the disturbance folks. Move along! -Yotam On Thu, Oct 10, 2013 at 3:42 PM, David Allsopp wrote: > 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? > > I imagine there are those who might suggest that the ugliness of pattern > matching on refs is part of the discouragement against using them! > > > match x with > > | ref y -> ... > > I'm guessing that you're really pattern matching with refs inside tuples > or something which makes using !x impractical? That said, if you've ended > with up (foo, bar, baz) where at least one of those is a reference, why not > consider using records with mutable fields? > > While writing this, Yotam Barnoy wrote: > > 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; > > } > > Completely ignoring why you might be implementing linked lists in a > list-processing language (I'm sure there's a good reason!), why not have > > type 'a cell = {data: 'a; > next: mutable 'a link; > last: mutable 'link} > > ? > > The parser change you propose is probably not trivial - for a start, "ref" > is part of the Pervasives module, not part of the grammar, and [ref] itself > can be redefined (try [let ref x = x] in the toplevel). Putting something > into the grammar to allow pattern matching on ref like this would at best > be a grim hack. > > > David > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs --001a1133dd4cf3428704e8684303 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
D'oh! I always forget about mutable fields s= omehow. Refs just take over in my mind and I end up putting them everywhere= I need mutability. Shows how little I actually use mutability in ocaml.

And the reason for the linked lists is that I need a (low-performance) = queue/stack with random access. And the reason for implementing a doubly-li= nked list myself is that my advisor is against using any library that's= not part of the ocaml distribution.

Sorry for the disturbance folks. Move along!

-Yotam<= br>


On T= hu, Oct 10, 2013 at 3:42 PM, David Allsopp <dra-news@metastack.com> wrote:
Yotam Barnoy wrote:
> I recently 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 put into the next version of ocaml?

I imagine there are those who might suggest that the ugliness of patt= ern matching on refs is part of the discouragement against using them!

> match x with
> | ref y -> ...

I'm guessing that you're really pattern matching with refs in= side tuples or something which makes using !x impractical? That said, if yo= u've ended with up (foo, bar, baz) where at least one of those is a ref= erence, why not consider using records with mutable fields?

While writing this, Yotam Barnoy wrote:
> 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 =3D { data : 'a;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 next : 'a link ref;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 last : 'a link ref;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 }

Completely ignoring why you might be implementing linked lists in a l= ist-processing language (I'm sure there's a good reason!), why not = have

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

?

The parser change you propose is probably not trivial - for a start, "= ref" is part of the Pervasives module, not part of the grammar, and [r= ef] itself can be redefined (try [let ref x =3D x] in the toplevel). Puttin= g something into the grammar to allow pattern matching on ref like this wou= ld at best be a grim hack.


David

--
Caml-list mailing list. =A0Subscription management and archives:
ht= tps://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
<= br>
--001a1133dd4cf3428704e8684303--