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 873ED7F919 for ; Fri, 20 May 2016 10:40:17 +0200 (CEST) IronPort-PHdr: 9a23:4kHwRhLwzn89VKMt3tmcpTZWNBhigK39O0sv0rFitYgUKfTxwZ3uMQTl6Ol3ixeRBMOAu6MC0LGd6vi8EUU7or+/81k6OKRWUBEEjchE1ycBO+WiTXPBEfjxciYhF95DXlI2t1uyMExSBdqsLwaK+i760zceF13FOBZvIaytQ8iJ35XxjLv5osWPKyxzxxODIppKZC2sqgvQssREyaBDEY0WjiXzn31TZu5NznlpL1/A1zz158O34YIxu38I46FppIZ8VvDxdqE8CLhZFygOMmYv5cStuwOQYxGI4y5Wd39Q2jpFGRKPpEX/X4ztvzH6u/Bn8CeAMMDwQKxyVTn0vPQjcwPhlCpSb21xy2rQkMEl1K8= Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=misterherr@freenet.de; spf=None smtp.mailfrom=misterherr@freenet.de; spf=None smtp.helo=postmaster@mout2.freenet.de Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of misterherr@freenet.de) identity=pra; client-ip=195.4.92.92; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="misterherr@freenet.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of misterherr@freenet.de) identity=mailfrom; client-ip=195.4.92.92; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="misterherr@freenet.de"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mout2.freenet.de) identity=helo; client-ip=195.4.92.92; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="misterherr@freenet.de"; x-sender="postmaster@mout2.freenet.de"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0DeAQC/zD5XgFxcBMNegmwhgX61FIZ8hhECgTM7EQEBAQEBAQEBEQEBCQ0JCSEvgi2CFQEBAQMBIwQGSwYLCxgJFgsCAgkDAgECAUUTBgIBAYgQARIJAwGyUYxmCYEsAYNRAQEBAQYBAQEBI4pyh0CCWQWYNAGXXASFW49LNoJkgTdsiAMBAQE X-IPAS-Result: A0DeAQC/zD5XgFxcBMNegmwhgX61FIZ8hhECgTM7EQEBAQEBAQEBEQEBCQ0JCSEvgi2CFQEBAQMBIwQGSwYLCxgJFgsCAgkDAgECAUUTBgIBAYgQARIJAwGyUYxmCYEsAYNRAQEBAQYBAQEBI4pyh0CCWQWYNAGXXASFW49LNoJkgTdsiAMBAQE X-IronPort-AV: E=Sophos;i="5.26,338,1459807200"; d="scan'208,217";a="219022064" Received: from mout2.freenet.de ([195.4.92.92]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 May 2016 10:40:16 +0200 Received: from [195.4.92.141] (helo=mjail1.freenet.de) by mout2.freenet.de with esmtpa (ID misterherr@freenet.de) (port 25) (Exim 4.85 #1) id 1b3fyh-0002uX-TM for caml-list@inria.fr; Fri, 20 May 2016 10:40:15 +0200 Received: from localhost ([::1]:40607 helo=mjail1.freenet.de) by mjail1.freenet.de with esmtpa (ID misterherr@freenet.de) (Exim 4.85 #1) id 1b3fyh-0007Ow-Nz for caml-list@inria.fr; Fri, 20 May 2016 10:40:15 +0200 Received: from mx10.freenet.de ([195.4.92.20]:40576) by mjail1.freenet.de with esmtpa (ID misterherr@freenet.de) (Exim 4.85 #1) id 1b3fw9-0008WQ-DZ for caml-list@inria.fr; Fri, 20 May 2016 10:37:37 +0200 Received: from x4e3295ac.dyn.telefonica.de ([78.50.149.172]:43074 helo=suse132-intel.home.loc) by mx10.freenet.de with esmtpsa (ID misterherr@freenet.de) (TLSv1.2:DHE-RSA-AES128-SHA:128) (port 465) (Exim 4.85 #1) id 1b3fw9-0003xu-5G for caml-list@inria.fr; Fri, 20 May 2016 10:37:37 +0200 To: caml-list@inria.fr References: <0F7D3B1B3C4B894D824F5B822E3E5A172CF046D5@IRSMSX102.ger.corp.intel.com> From: "Mr. Herr" Message-ID: <573ECCD0.8060106@freenet.de> Date: Fri, 20 May 2016 10:37:36 +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: <0F7D3B1B3C4B894D824F5B822E3E5A172CF046D5@IRSMSX102.ger.corp.intel.com> Content-Type: multipart/alternative; boundary="------------010208010206010001040705" X-Originated-At: 78.50.149.172!43074 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. --------------010208010206010001040705 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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 --------------010208010206010001040705 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

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

--------------010208010206010001040705--