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 77C897EE4B for ; Fri, 11 Oct 2013 06:50:14 +0200 (CEST) Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of arnaud.spiwack@gmail.com) identity=pra; client-ip=209.85.212.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="arnaud.spiwack@gmail.com"; x-sender="arnaud.spiwack@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of arnaud.spiwack@gmail.com designates 209.85.212.173 as permitted sender) identity=mailfrom; client-ip=209.85.212.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="arnaud.spiwack@gmail.com"; x-sender="arnaud.spiwack@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-wi0-f173.google.com) identity=helo; client-ip=209.85.212.173; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="arnaud.spiwack@gmail.com"; x-sender="postmaster@mail-wi0-f173.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AlcCAB6CV1LRVdStm2dsb2JhbABZgz9SgymrPooUiE6BGggWDgEBAQEBBgsLCRQogiUBAQQBIwQZAS0LAQMBCwEFAwILDQ0dAgIhARIBBQEKEgYTEgcBh1oDCQYMjDiPWowCg12ERycDCollAQUMjE6CaYJ1gTkDlhyBaYEvix2DSxgpgWOCbTo X-IPAS-Result: AlcCAB6CV1LRVdStm2dsb2JhbABZgz9SgymrPooUiE6BGggWDgEBAQEBBgsLCRQogiUBAQQBIwQZAS0LAQMBCwEFAwILDQ0dAgIhARIBBQEKEgYTEgcBh1oDCQYMjDiPWowCg12ERycDCollAQUMjE6CaYJ1gTkDlhyBaYEvix2DSxgpgWOCbTo X-IronPort-AV: E=Sophos;i="4.90,1077,1371074400"; d="scan'208";a="29900918" Received: from mail-wi0-f173.google.com ([209.85.212.173]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 11 Oct 2013 06:50:13 +0200 Received: by mail-wi0-f173.google.com with SMTP id hq15so506691wib.0 for ; Thu, 10 Oct 2013 21:50:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=MPFOfHfxvr+PjDKUS3de5956x8Jvo2OMe0NtY8+No0I=; b=I1IrU6aIvXPW6EBEEaUsmNZfyVVvsIW3fTVTI07aKOMBso5kh6bEe8Gu3JaeyS5V/E yOQV6vj8ELWrawJqgRTvAGsHz6BSuJZLeQ0FLcfRL19ZePZJ2tKllXFXNlv6i6LY50wY okyAT4INJkF3S9TAQKu5qEGGzrQWMBe0U70xy99eN0KZN1cuyZ+6qrcUBFrQd/tzM6pH oQtilYtdwEG7SDDPeMznSGMH/ihGHiygtJe9awuAUnaEEEZguin5Ez0ZdEPE7ox3VfON y9gfOEBUvkC6quE4acWtHCwVhJgjraoLtiuIqypiHtzZveqNRxO9GLdc8oEzLwLnMCLx 9G5Q== X-Received: by 10.194.109.68 with SMTP id hq4mr15487208wjb.12.1381467013069; Thu, 10 Oct 2013 21:50:13 -0700 (PDT) MIME-Version: 1.0 Sender: arnaud.spiwack@gmail.com Received: by 10.216.33.193 with HTTP; Thu, 10 Oct 2013 21:49:32 -0700 (PDT) In-Reply-To: References: From: Arnaud Spiwack Date: Fri, 11 Oct 2013 06:49:32 +0200 X-Google-Sender-Auth: ErlqfUjeyFsvHuDX3FCprdDoJxk Message-ID: To: Yotam Barnoy Cc: David Allsopp , Ocaml Mailing List Content-Type: multipart/alternative; boundary=089e0102e6da9544df04e86fda09 X-Validation-by: arnaud@spiwack.net Subject: Re: [Caml-list] Pattern matching on refs --089e0102e6da9544df04e86fda09 Content-Type: text/plain; charset=UTF-8 If you need queues with random access, and do not need highly tuned performances, may I suggest you having a look at Okasaki's *Purely functional datastructures*. It has a handful of these, which do not involve assignment (they use laziness annotation, though, for good amortized performances). It would make your life better. The book version has a little more than the thesis, by the way. On 10 October 2013 21:46, Yotam Barnoy wrote: > 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 > > > --089e0102e6da9544df04e86fda09 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
If you need queues with random access, and do not need hig= hly tuned performances, may I suggest you having a look at Okasaki's Purely functional datastructures. It has a handful of these, which do = not involve assignment (they use laziness annotation, though, for good amor= tized performances). It would make your life better. The book version has a= little more than the thesis, by the way.


On 10 O= ctober 2013 21:46, Yotam Barnoy <yotambarnoy@gmail.com> = wrote:
D'oh! I alway= s 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 actua= lly 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


On Thu, Oct 1= 0, 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;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 next : 'a = link ref;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 last : 'a = link ref;
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=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;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 next: mutable= 'a link;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=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. =C2=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>

--089e0102e6da9544df04e86fda09--