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 E84C37F919 for ; Fri, 20 May 2016 12:25:51 +0200 (CEST) IronPort-PHdr: 9a23:WfglUBS7+QYYGq4gtePlO01Qf9psv+yvbD5Q0YIujvd0So/mwa64ZxKN2/xhgRfzUJnB7Loc0qyN4/GmAjBLuMzJmUtBWaIPfidNsd8RkQ0kDZzNImzAB9muURYHGt9fXkRu5XCxPBsdMs//Y1rPvi/6tmZKSV3BPAZ4bt74BpTVx5zukbviqtuIP04R3nKUWvBbElaflU3prM4YgI9veO4a6yDihT92QdlQ3n5iPlmJnhzxtY+a9Z9n9DlM6bp6r5YTGfayQ6NtRrVdCHEiMnspzMztrxjKCwWVtVUGVWBD2D9SRUDl4QvgFN+ltyrhqudn2CSAJ+X4V705Xzm+qatmHky7wBwbPiI0pTmEwvd7i7hW9Uqs Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=misterherr@freenet.de; spf=None smtp.mailfrom=misterherr@freenet.de; spf=None smtp.helo=postmaster@mout3.freenet.de Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of misterherr@freenet.de) identity=pra; client-ip=195.4.92.93; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="misterherr@freenet.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of misterherr@freenet.de) identity=mailfrom; client-ip=195.4.92.93; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="misterherr@freenet.de"; x-conformance=sidf_compatible Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mout3.freenet.de) identity=helo; client-ip=195.4.92.93; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="postmaster@mout3.freenet.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0C4AACY5T5Xf11cBMNegmwhgQB+tRKEeQENgXUahXcCgTE4FAEBAQEBAQEBEQEBCQsLCSEvgi2CFQEBAQQjBAYcLxELEQQBAQEJFggDAgIJAwIBAgE0CQgTBgIBAYgQARsDAbI6jGYJgSwBg1EBAQEBAQEBAQIBAQEBAQEBAQEBARyKcoR1gkuCWQWTNYR/AZdcBIVbj0seAQGCeoE3bIgDAQEB X-IPAS-Result: A0C4AACY5T5Xf11cBMNegmwhgQB+tRKEeQENgXUahXcCgTE4FAEBAQEBAQEBEQEBCQsLCSEvgi2CFQEBAQQjBAYcLxELEQQBAQEJFggDAgIJAwIBAgE0CQgTBgIBAYgQARsDAbI6jGYJgSwBg1EBAQEBAQEBAQIBAQEBAQEBAQEBARyKcoR1gkuCWQWTNYR/AZdcBIVbj0seAQGCeoE3bIgDAQEB X-IronPort-AV: E=Sophos;i="5.26,339,1459807200"; d="scan'208,217";a="178436446" Received: from mout3.freenet.de ([195.4.92.93]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 May 2016 12:25:50 +0200 Received: from [195.4.92.140] (helo=mjail0.freenet.de) by mout3.freenet.de with esmtpa (ID misterherr@freenet.de) (port 25) (Exim 4.85 #1) id 1b3hcr-00054n-9K for caml-list@inria.fr; Fri, 20 May 2016 12:25:49 +0200 Received: from localhost ([::1]:59058 helo=mjail0.freenet.de) by mjail0.freenet.de with esmtpa (ID misterherr@freenet.de) (Exim 4.85 #1) id 1b3hcr-0000e7-4u for caml-list@inria.fr; Fri, 20 May 2016 12:25:49 +0200 Received: from mx2.freenet.de ([195.4.92.12]:58244) by mjail0.freenet.de with esmtpa (ID misterherr@freenet.de) (Exim 4.85 #1) id 1b3haC-0003e5-JV for caml-list@inria.fr; Fri, 20 May 2016 12:23:04 +0200 Received: from x4e3295ac.dyn.telefonica.de ([78.50.149.172]:43744 helo=suse132-intel.home.loc) by mx2.freenet.de with esmtpsa (ID misterherr@freenet.de) (TLSv1.2:DHE-RSA-AES128-SHA:128) (port 465) (Exim 4.85 #1) id 1b3haC-0004Az-8V for caml-list@inria.fr; Fri, 20 May 2016 12:23:04 +0200 To: caml-list@inria.fr References: <0F7D3B1B3C4B894D824F5B822E3E5A172CF046D5@IRSMSX102.ger.corp.intel.com> <573ECCD0.8060106@freenet.de> <0F7D3B1B3C4B894D824F5B822E3E5A172CF04817@IRSMSX102.ger.corp.intel.com> From: "Mr. Herr" X-Enigmail-Draft-Status: N1110 Message-ID: <573EE587.1070504@freenet.de> Date: Fri, 20 May 2016 12:23:03 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 In-Reply-To: <0F7D3B1B3C4B894D824F5B822E3E5A172CF04817@IRSMSX102.ger.corp.intel.com> Content-Type: multipart/alternative; boundary="------------020105040005050204010703" X-Originated-At: 78.50.149.172!43744 Subject: Re: [Caml-list] Syntax for several matches, each with a "when" clause, but only one result This is a multi-part message in MIME format. --------------020105040005050204010703 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit hmm, difficult to find. The title was "Suboptimal pattern specification", but I just knew this problem was discussed here or on SO within the last ?? days. /Str. On 20.05.2016 12:04, Soegtrop, Michael wrote: > > Thanks a lot for pointing me to this answer! Obviously I need to work on my search > skills … > > > > Best regards, > > > > Michael > > > > *From:*caml-list-request@inria.fr [mailto:caml-list-request@inria.fr] *On Behalf Of > *Mr. Herr > *Sent:* Friday, May 20, 2016 10:38 AM > *To:* caml-list@inria.fr > *Subject:* Re: [Caml-list] Syntax for several matches, each with a "when" clause, > but only one result > > > > > > On 20.05.2016 09:57, Soegtrop, Michael wrote: > > Dear OCaml Users, > > > > sometimes I want to do something like > > > > match expr with > > | case1 when cond1 > > | case2 when cond2 > > | case3 when cond3 -> result > > > > but this doesn’t work. I have to write > > > > match expr with > > | case1 when cond1 -> result > > | case2 when cond2 -> result > > | case3 when cond3 -> result > > > > Usually only some of the matches have a when clause. Is there a way to avoid > copying the result term (other than writing a function) ? > > > > > > I could not see a working web link to this old list message from 2016-04-07, so I > just give you a copy of the answer by > Gabriel Scherer: > > > No, indeed you have to use a local definition to avoid code > duplication in this case. > > My understanding of the design stance of pattern-matching in OCaml is > as follows: the syntax of patterns is bounded by what can be matched > efficiently. This explains why "when" has a second-class status > (first-class when cannot be matched efficiently); sometimes the user > has to pay for this rigidity. But, on the positive side, it is a > simple and clear stance, and it correlates with the availability of > good tooling, namely exhaustivity and useless-clause warnings. > > On Thu, Apr 7, 2016 at 9:36 AM, Daniel Bünzli > wrote: > > Hello, > > > > Something I run quite often is the following pattern matching > > > > match v with > > | None | Some c when sat c -> expr > > | Some … > > > > which doesn't compile and forces me to write > > > > match v with > > | None -> expr > > | Some c when sat c -> expr > > | Some … > > > > and leads to code duplication or the introduction of a definition to avoid it. > > > > Am I missing a syntax bit ? > > > > Best, > > > > Daniel > > > > Intel Deutschland GmbH > Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany > Tel: +49 89 99 8853-0, www.intel.de > Managing Directors: Christin Eisenschmid, Christian Lamprechter > Chairperson of the Supervisory Board: Nicole Lau > Registered Office: Munich > Commercial Register: Amtsgericht Muenchen HRB 186928 > --------------020105040005050204010703 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit hmm, difficult to find. The title was "Suboptimal pattern specification", but I just knew
this problem was discussed here or on SO within the last ?? days.

/Str.

On 20.05.2016 12:04, Soegtrop, Michael wrote:

Thanks a lot for pointing me to this answer! Obviously I need to work on my search skills …

 

Best regards,

 

Michael

 

From: caml-list-request@inria.fr [mailto:caml-list-request@inria.fr] On Behalf Of Mr. Herr
Sent: Friday, May 20, 2016 10:38 AM
To: caml-list@inria.fr
Subject: Re: [Caml-list] Syntax for several matches, each with a "when" clause, but only one result

 

 

On 20.05.2016 09:57, Soegtrop, Michael wrote:

Dear OCaml Users,

 

sometimes I want to do something like

 

match expr with

| case1 when cond1

| case2 when cond2

| case3 when cond3 -> result

 

but this doesn’t work. I have to write

 

match expr with

| case1 when cond1 -> result

| case2 when cond2 -> result

| case3 when cond3 -> result

 

Usually only some of the matches have a when clause. Is there a way to avoid copying the result term (other than writing a function) ?

 

 

I could not see a working web link to this old list message from 2016-04-07, so I just give you a copy of the answer by
Gabriel Scherer:


No, indeed you have to use a local definition to avoid code
duplication in this case.
 
My understanding of the design stance of pattern-matching in OCaml is
as follows: the syntax of patterns is bounded by what can be matched
efficiently. This explains why "when" has a second-class status
(first-class when cannot be matched efficiently); sometimes the user
has to pay for this rigidity. But, on the positive side, it is a
simple and clear stance, and it correlates with the availability of
good tooling, namely exhaustivity and useless-clause warnings.
 
On Thu, Apr 7, 2016 at 9:36 AM, Daniel Bünzli
<daniel.buenzli@erratique.ch> wrote:
Hello,
 
Something I run quite often is the following pattern matching
 
match v with
| None | Some c when sat c -> expr
| Some …
 
which doesn't compile and forces me to write
 
match v with
| None -> expr
| Some c when sat c -> expr
| Some …
 
and leads to code duplication or the introduction of a definition to avoid it.
 
Am I missing a syntax bit ?
 
Best,
 
Daniel

 

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


--------------020105040005050204010703--