From: Octachron <octa@polychoron.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] pattern matching on mapped lists
Date: Wed, 03 Jun 2015 20:06:12 +0200 [thread overview]
Message-ID: <556F4214.6030101@polychoron.fr> (raw)
In-Reply-To: <556F3C8D.4060708@ispras.ru>
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.
next prev parent reply other threads:[~2015-06-03 18:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20150602100015.E87D67EEF7@sympa.inria.fr>
2015-06-02 22:23 ` Nils Becker
2015-06-03 16:52 ` Romain Bardou
2015-06-03 17:42 ` Mikhail Mandrykin
2015-06-03 18:06 ` Octachron [this message]
2015-06-03 16:58 ` John Whitington
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=556F4214.6030101@polychoron.fr \
--to=octa@polychoron.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox