Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "OCaml List" <caml-list@inria.fr>
Subject: [Caml-list] More existential escapes (or possibly first class polymorphism)
Date: Fri, 20 Mar 2015 16:29:45 -0000	[thread overview]
Message-ID: <002a01d0632b$13cb79b0$3b626d10$@metastack.com> (raw)

Daniel's recent thread inspired to revisit a recent problem I'd had but had
given up on, with a most inelegant solution. Although what I have no is much
less inelegant, I'm wondering if it can be improved further by a trick or
some such which I can't spot.

I have a GADT containing keys where the type parameter denotes the value
type (my actual use case has many more constructors):

type _ token = Block : int -> string token
             | Role : string -> unit token

and I then want to be able to store these in a structure where I can look
them up by token value. So I define:

type binding = B : ('a token * 'a) -> token

to store a binding and:

type (_, _) eq = Eq : ('a, 'a) eq

let test_key (type r) (type s) (r : r token) (s : s token) : (r, s) eq
option =
  match (r, s) with
    (Role r, Role s) when r = s ->
      Some Eq
  | (Block r, Block s) when r = s ->
      Some Eq
  | _ ->
      None

and so I can define [find] in a manner very like Daniel's in
https://sympa.inria.fr/sympa/arc/caml-list/2015-03/msg00109.html

Given that this application is for a specific set of keys, rather than for a
universal type, I'd quite like to define [iter], which is where I've hit a
bit of inelegance:

let iter f script =
  let rec iter = function
    binding::script ->
      f binding;
      iter script
  | [] ->
      ()
  in
    iter script

but this means that the function passed needs to match on type binding,
which I'd prefer to be hidden:

let f = function
  B(Role role, ()) ->
    Printf.printf "Role %s\n%!" role
| B(Block n, code) ->
    Printf.printf "Block %d\n%!" n code

Is there a way to write [iter] such that it has signature [('a token -> 'a
-> unit) -> binding list -> unit]?

My closest attempt so far is to start with:

let iter f script =
  let rec iter = function
    B(key, binding)::script ->
      f key binding;
      iter script
  | [] ->
      ()
  in
    iter script

which obviously doesn't work because the type of [key] and [binding] in
[iter] escape their scope. Now, if my understanding is correct, the problem
here isn't so much that the existentials escape their scope, but that [f] is
monomorphic. So, if I define a record type:

type f = {f : 'a . 'a token -> 'a -> unit}

then I can alter the definition of iter to allow a polymorphic function to
be passed

let iter {f} script =
  ...

and I can get to a version of [iter] where the functions don't need to know
about the internals of type binding, but they do need to be passed wrapped
up as {f: foo}.

Is there some other wizardry on offer which can allow the polymorphic nature
of [f] to be inferred?


David


             reply	other threads:[~2015-03-20 16:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-20 16:29 David Allsopp [this message]
2015-03-20 16:43 ` Jeremy Yallop
2015-03-22 11:20   ` David Allsopp
2015-03-22 14:29     ` Gabriel Scherer

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='002a01d0632b$13cb79b0$3b626d10$@metastack.com' \
    --to=dra-news@metastack.com \
    --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