caml-list - the Caml user's mailing list
 help / Atom feed
From: "Frédéric Bour" <frederic.bour@lakaban.net>
To: "Richard W.M. Jones" <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Syntax puzzle / suggestion
Date: Fri, 9 Aug 2019 16:56:33 +0200
Message-ID: <CAMp_u=cZ++7o5LtUXGy6jQKxwKRMAbA5-bnmpM1DWOmRO5cGMw@mail.gmail.com> (raw)
In-Reply-To: <20190809142719.ff4kogz4fq6r7vfr@annexia.org>

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

Abusing list notations and GADTs to keep track of length and ensure
exhaustive matching:

module M = struct type ('a, 'b) t = [] : (_, unit) t | (::) : 'a * ('a, 'b)
t -> ('a, unit * 'b) t end;;
let rec to_list : type a b. (a, b) M.t -> a list = function | M.[] -> [] |
M.(x :: xs) -> x :: to_list xs;;
let decompose x = (to_list x, x);;

# let all_items, M.[item1; item2] = decompose M.["hello"; "world"];;
val all_items : string list = ["hello"; "world"]
val item1 : string = "hello"
val item2 : string = "world"

On Fri, Aug 9, 2019 at 4:27 PM Richard W.M. Jones <rich@annexia.org> wrote:

> Let's imagine you have a list of named things, but you also want to
> collect them up into a single list.  For example:
>
>   let item1 = "hello"
>   let item2 = "world"
>   let all_items = [ item1; item2 ]
>
>   let () =
>     printf "item1 = %s\n" item1;
>     printf "all_items = %s\n" (String.concat ", " all_items)
>
> This is fine, but there's a danger that a programmer could add item3
> but forget to add it to the "all_items" list.  (In the real world
> problem to which this applies, my items are complex and lengthy
> structures, and the "all-things-list" is well off the bottom of the
> page when you're viewing the top item).
>
> My idea to fix this was to write:
>
>   let all_items = [
>     ("hello" as item1);
>     ("world" as item2);
>   ]
>
> Actually I was slightly surprised to find this doesn't compile.  I
> guess because "as" can only be used like this in patterns, not
> expressions.  Also the scoping is wrong because the "as item1" if it
> worked probably wouldn't apply outside the list.
>
> I haven't worked out if it's possible to write this in ordinary OCaml,
> although I suppose ppx could solve it.  Any ideas if this is possible?
>
> Rich.
>

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

      parent reply index

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-09 14:27 Richard W.M. Jones
2019-08-09 14:40 ` Gabriel Scherer
2019-08-09 14:45   ` Richard W.M. Jones
2019-08-09 14:48 ` Martin Riener
2019-08-10  5:19   ` rixed
2019-08-09 14:51 ` Josh Berdine
2019-08-10 15:18   ` Oliver Bandel
2019-08-10 20:34   ` Chet Murthy
2019-08-09 14:56 ` Frédéric Bour [this message]

Reply instructions:

You may reply publically 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='CAMp_u=cZ++7o5LtUXGy6jQKxwKRMAbA5-bnmpM1DWOmRO5cGMw@mail.gmail.com' \
    --to=frederic.bour@lakaban.net \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    /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

caml-list - the Caml user's mailing list

Archives are clonable:
	git clone --mirror http://inbox.vuxu.org/caml-list
	git clone --mirror https://inbox.ocaml.org/caml-list

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.caml-list


AGPL code for this site: git clone https://public-inbox.org/ public-inbox