caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Pattern matching on refs
@ 2013-10-10 19:17 Yotam Barnoy
  2013-10-10 19:34 ` Ashish Agarwal
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Yotam Barnoy @ 2013-10-10 19:17 UTC (permalink / raw)
  To: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 231 bytes --]

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

[-- Attachment #2: Type: text/html, Size: 324 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Pattern matching on refs
  2013-10-10 19:17 [Caml-list] Pattern matching on refs Yotam Barnoy
@ 2013-10-10 19:34 ` Ashish Agarwal
  2013-10-10 19:37   ` Yotam Barnoy
  2013-10-10 19:42 ` David Allsopp
  2013-10-10 20:23 ` Jacques Le Normand
  2 siblings, 1 reply; 8+ messages in thread
From: Ashish Agarwal @ 2013-10-10 19:34 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 401 bytes --]

Would it solve your problem to instead write:

match !x with
| y -> ...


On Thu, Oct 10, 2013 at 3:17 PM, Yotam Barnoy <yotambarnoy@gmail.com> 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
>

[-- Attachment #2: Type: text/html, Size: 937 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Pattern matching on refs
  2013-10-10 19:34 ` Ashish Agarwal
@ 2013-10-10 19:37   ` Yotam Barnoy
  0 siblings, 0 replies; 8+ messages in thread
From: Yotam Barnoy @ 2013-10-10 19:37 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1387 bytes --]

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 <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 Barnoy <yotambarnoy@gmail.com>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
>>
>
>

[-- Attachment #2: Type: text/html, Size: 2440 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [Caml-list] Pattern matching on refs
  2013-10-10 19:17 [Caml-list] Pattern matching on refs Yotam Barnoy
  2013-10-10 19:34 ` Ashish Agarwal
@ 2013-10-10 19:42 ` David Allsopp
  2013-10-10 19:46   ` Yotam Barnoy
  2013-10-10 20:23 ` Jacques Le Normand
  2 siblings, 1 reply; 8+ messages in thread
From: David Allsopp @ 2013-10-10 19:42 UTC (permalink / raw)
  To: Ocaml Mailing List

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 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Pattern matching on refs
  2013-10-10 19:42 ` David Allsopp
@ 2013-10-10 19:46   ` Yotam Barnoy
  2013-10-11  4:49     ` Arnaud Spiwack
  0 siblings, 1 reply; 8+ messages in thread
From: Yotam Barnoy @ 2013-10-10 19:46 UTC (permalink / raw)
  To: David Allsopp; +Cc: Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 2425 bytes --]

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 <dra-news@metastack.com>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

[-- Attachment #2: Type: text/html, Size: 3380 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Pattern matching on refs
  2013-10-10 19:17 [Caml-list] Pattern matching on refs Yotam Barnoy
  2013-10-10 19:34 ` Ashish Agarwal
  2013-10-10 19:42 ` David Allsopp
@ 2013-10-10 20:23 ` Jacques Le Normand
  2 siblings, 0 replies; 8+ messages in thread
From: Jacques Le Normand @ 2013-10-10 20:23 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: caml

[-- Attachment #1: Type: text/plain, Size: 438 bytes --]

I like this syntactic sugar. Simple, easy to implement and useful;
especially when dealing with other people's code.
On Oct 10, 2013 3:18 PM, "Yotam Barnoy" <yotambarnoy@gmail.com> 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
>

[-- Attachment #2: Type: text/html, Size: 770 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Pattern matching on refs
  2013-10-10 19:46   ` Yotam Barnoy
@ 2013-10-11  4:49     ` Arnaud Spiwack
  2013-10-11  6:10       ` Francois Berenger
  0 siblings, 1 reply; 8+ messages in thread
From: Arnaud Spiwack @ 2013-10-11  4:49 UTC (permalink / raw)
  To: Yotam Barnoy; +Cc: David Allsopp, Ocaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 2970 bytes --]

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 <yotambarnoy@gmail.com> 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 <dra-news@metastack.com>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
>
>
>

[-- Attachment #2: Type: text/html, Size: 4331 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] Pattern matching on refs
  2013-10-11  4:49     ` Arnaud Spiwack
@ 2013-10-11  6:10       ` Francois Berenger
  0 siblings, 0 replies; 8+ messages in thread
From: Francois Berenger @ 2013-10-11  6:10 UTC (permalink / raw)
  To: caml-list

On 10/11/2013 01:49 PM, Arnaud Spiwack wrote:
> 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/.

There are several OCaml implementations available.
I know this one (and have seen others here and there also):

https://bitbucket.org/mmottl/pure-fun

 > 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 <yotambarnoy@gmail.com
> <mailto:yotambarnoy@gmail.com>> 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
>     <dra-news@metastack.com <mailto:dra-news@metastack.com>> 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
>
>
>


-- 
Best regards,
Francois Berenger.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-10-11  6:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-10 19:17 [Caml-list] Pattern matching on refs Yotam Barnoy
2013-10-10 19:34 ` Ashish Agarwal
2013-10-10 19:37   ` Yotam Barnoy
2013-10-10 19:42 ` David Allsopp
2013-10-10 19:46   ` Yotam Barnoy
2013-10-11  4:49     ` Arnaud Spiwack
2013-10-11  6:10       ` Francois Berenger
2013-10-10 20:23 ` Jacques Le Normand

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).