caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe TROESTLER <Christophe.Troestler@umh.ac.be>
To: garrigue@math.nagoya-u.ac.jp
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] format polymorphism & campl4
Date: Thu, 10 Aug 2006 00:51:12 +0200 (CEST)	[thread overview]
Message-ID: <20060810.005112.03212332.Christophe.Troestler@umh.ac.be> (raw)
In-Reply-To: <20060809.164232.15671006.garrigue@math.nagoya-u.ac.jp>

On Wed, 09 Aug 2006, Jacques Garrigue <garrigue@math.nagoya-u.ac.jp> wrote:
> 
> >   'a 'b. (string -> 'b, 'a, 'b) format
> 
> It clearly has the second type!

Thanks for answering -- I got badly confused by the error message !
(shame on me :)

> >   type 'a fmt = { fmt: 'b 'c. ('a,'b,'c) format }
> >   fun  (s: _ fmt) -> Printf.printf s.fmt, Scanf.sscanf "string" s.fm
> 
> With your definition of fmt, you are requiring 'c to be independent
> from 'a. But If you look at the type of "%s", you see that
> 'a = string -> 'c, so that {fmt="%s"} is not well-typed.

Well, yes, of course, that's obvious after you stated it!  :)

> A solution is to define it as
>   type 'a fmt = {fmt: 'b 'c. ('a -> 'b, 'c, 'b) format}
> 
> The drawback is that only allows formats taking one argument.
> One could add definitions for any defined number of arguments, but I
> don't see any generic solution.

Yeah but I really want to allow as many arguments as the user desires.
That's a bit of an annoyance for printf/scanf like schemes: you have
to know in advance in how many contexts you may want to simultaneously
use an argument of type format in order to put enough type parameters...

So I suppose my best luck to do it cleanly is to use camlp4 to make
the parameter duplication for the user.  Another approach I was
thinking about is to define a new type, say

  type ('a,'b,'c,'d) fmt

and have (for the above application)

  external pr_fmt : (_, _, 'a, 'c) fmt -> ('a, out_channel, 'c) format = "%identity"
  external sc_fmt : ('a, 'c, _, _) fmt -> ('a, Scanf.Scanning.scanbuf, 'c) format = "%identity"
  external ( !/ ) : ('a, _, 'b) format -> ('a, 'b, 'a, _) fmt = "%identity"

  let f s =
    Printf.printf (pr_fmt s), Scanf.sscanf "string" (sc_fmt s)

  f !/"%s %u"

Now, of course ( !/ ) is not completely doing what I want.  If
[pr_fmt] and [sc_fmt] are for purely internal use by the library --
thus user only seeing [/!] -- is the above safe? Is there a way using
Camlp4 to reinterpret a string in the new type (possibly prefixing it
with !/ or another chosen symbol to distinguish it) so that,
say, !/"%i %f" becomes (Obj.magic "%i ... %f": (int -> float -> 'a,
'a, int -> float -> 'b, 'b) fmt) ?

Thanks for your help,
ChriS


P.S. Out of curiosity, is there a type system with inference (say,
extending the one of OCaml) that can handle this kind of thing cleanly?


      reply	other threads:[~2006-08-09 22:51 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-06 23:33 format polymorphism Christophe TROESTLER
2006-08-09  7:42 ` [Caml-list] " Jacques Garrigue
2006-08-09 22:51   ` Christophe TROESTLER [this message]

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=20060810.005112.03212332.Christophe.Troestler@umh.ac.be \
    --to=christophe.troestler@umh.ac.be \
    --cc=caml-list@inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    /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;
as well as URLs for NNTP newsgroup(s).