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 2E7387EEF6 for ; Wed, 3 Jun 2015 20:06:17 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of octa@polychoron.fr) identity=pra; client-ip=217.70.183.197; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="octa@polychoron.fr"; x-sender="octa@polychoron.fr"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of octa@polychoron.fr) identity=mailfrom; client-ip=217.70.183.197; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="octa@polychoron.fr"; x-sender="octa@polychoron.fr"; x-conformance=sidf_compatible Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@relay5-d.mail.gandi.net) identity=helo; client-ip=217.70.183.197; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="octa@polychoron.fr"; x-sender="postmaster@relay5-d.mail.gandi.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CRBQB2QG9VnMW3Rtlbg2RkgxioCIEvBpNghXcCgUQ8EAEBAQEBAQERAQEBAQEGDQkJIS5BBYNdAQEEIwQRQBELGAICBRYLAgIJAwIBAgFFEwgBAYgnBgMGt1ijbAwBGwSBIYR4hSqEUzoWglKBRQWUN4MWiTyFKY9DAoQegjOBAQEBAQ X-IPAS-Result: A0CRBQB2QG9VnMW3Rtlbg2RkgxioCIEvBpNghXcCgUQ8EAEBAQEBAQERAQEBAQEGDQkJIS5BBYNdAQEEIwQRQBELGAICBRYLAgIJAwIBAgFFEwgBAYgnBgMGt1ijbAwBGwSBIYR4hSqEUzoWglKBRQWUN4MWiTyFKY9DAoQegjOBAQEBAQ X-IronPort-AV: E=Sophos;i="5.13,548,1427752800"; d="scan'208";a="162668108" Received: from relay5-d.mail.gandi.net ([217.70.183.197]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 03 Jun 2015 20:06:16 +0200 Received: from mfilter26-d.gandi.net (mfilter26-d.gandi.net [217.70.178.154]) by relay5-d.mail.gandi.net (Postfix) with ESMTP id C436541C054 for ; Wed, 3 Jun 2015 20:06:16 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mfilter26-d.gandi.net Received: from relay5-d.mail.gandi.net ([217.70.183.197]) by mfilter26-d.gandi.net (mfilter26-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id CPbEN1ZA7+4c for ; Wed, 3 Jun 2015 20:06:15 +0200 (CEST) X-Originating-IP: 197.88.29.169 Received: from [192.168.1.107] (unknown [197.88.29.169]) (Authenticated sender: octa@polychoron.fr) by relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 0EBF741C087 for ; Wed, 3 Jun 2015 20:06:14 +0200 (CEST) Message-ID: <556F4214.6030101@polychoron.fr> Date: Wed, 03 Jun 2015 20:06:12 +0200 From: Octachron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: caml-list@inria.fr References: <20150602100015.E87D67EEF7@sympa.inria.fr> <556E2CE1.9040801@bioquant.uni-heidelberg.de> <556F30D9.9010109@inria.fr> <556F3C8D.4060708@ispras.ru> In-Reply-To: <556F3C8D.4060708@ispras.ru> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] pattern matching on mapped lists On 06/03/15 19:42, Mikhail Mandrykin wrote: > It seems GADT encoding mostly works here, but the absence of local > opens in patterns and redefinition of [] constructor and [_; _] > special syntax for lists currently makes the use of such encoding > inconvenient e.g.: > > module Flist = > struct > > type z = Z > > type 'a s = S of 'a > > type ('a, _) t = > | Nil : ('a, z) t > | :: : 'a * ('a, 'b) t -> ('a, 'b s) t > > let rec map : type a. _ -> (_, a) t -> (_, a) t = fun f -> > function > | Nil -> Nil > | x :: xs -> f x :: map f xs > end > > open Flist > > let a :: b :: c :: Nil = map ((+) 2) @@ 1 :: 2 :: 3 :: Nil;; (* > Can't use Flist.[a; b; c] on both sides *) > > let d :: e :: Nil = map ((^) "2 + ") @@ "1" :: "2" :: Nil;; > > let g :: Nil = map ((+) 2) @@ 1 :: Nil;; > > Printf.printf "%d = %s = %d\n%!" a d g;; > > BTW, both features are already proposed as feature requests: > https://github.com/ocaml/ocaml/pull/187 > https://github.com/ocaml/ocaml/pull/76 > > Regards, Mikhail > A possibility to lighten these constructions is to use a ppx extension to rewrite the special syntax [a;...] into a standard Cons(a,... Nil)...) construction. For instance with my experimental ppx_listlike extension (https://github.com/Octachron/ppx_listlike), these examples can be rewritten as let [%ll? [a; b; c] ] = map ((+) 2) [%ll 1; 2; 3 ];; or with more syntactic sugar let%with_ll [ d; e ] = map ((^) "2 + ") ["1"; "2" ];; let%with_ll [g] = map ((+) 2) [1];; Regards, octachron.