Google Groups Home
Help | Sign in
format polymorphism
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Christophe TROESTLER  
View profile
 More options Aug 6 2006, 7:51 pm
Newsgroups: fa.caml
From: Christophe TROESTLER <Christophe.Troest...@umh.ac.be>
Date: Sun, 06 Aug 2006 23:51:39 UTC
Local: Sun, Aug 6 2006 7:51 pm
Subject: [Caml-list] format polymorphism
Hi,

Could someone tell me why, say "%s", is of type

  'a. (string -> 'b, 'a, 'b) format

instead of

  'a 'b. (string -> 'b, 'a, 'b) format

I am asking this because if one wants to use the same format string
both for reading and printing in a given function, one needs the
latter type:

  type 'a fmt = { fmt: 'b 'c. ('a,'b,'c) format }
  fun  (s: _ fmt) -> Printf.printf s.fmt, Scanf.sscanf "string" s.fm

However, I cannot instantiate the type [_ fmt].  This is in contrast
with

  let s = ("%s": (_,_,_) format) in
  Printf.printf s, Scanf.sscanf "string" s

which works as intended.  I think this may have been discussed before;
I just can't remember the reason.  Also, is there a safe way of using
Obj.magic to make it work ?

Regards,
ChriS

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacques Garrigue  
View profile
 More options Aug 9 2006, 3:51 am
Newsgroups: fa.caml
From: Jacques Garrigue <garri...@math.nagoya-u.ac.jp>
Date: Wed, 09 Aug 2006 07:51:40 UTC
Local: Wed, Aug 9 2006 3:51 am
Subject: Re: [Caml-list] format polymorphism
From: Christophe TROESTLER <Christophe.Troest...@umh.ac.be>

> Could someone tell me why, say "%s", is of type

>   'a. (string -> 'b, 'a, 'b) format

> instead of

>   'a 'b. (string -> 'b, 'a, 'b) format

It clearly has the second type!

> I am asking this because if one wants to use the same format string
> both for reading and printing in a given function, one needs the
> latter type:

>   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.

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.

Cheers,

Jacques Garrigue

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "format polymorphism & campl4" by Christophe TROESTLER
Christophe TROESTLER  
View profile
 More options Aug 9 2006, 6:56 pm
Newsgroups: fa.caml
From: Christophe TROESTLER <Christophe.Troest...@umh.ac.be>
Date: Wed, 09 Aug 2006 22:56:52 UTC
Local: Wed, Aug 9 2006 6:56 pm
Subject: Re: [Caml-list] format polymorphism & campl4

On Wed, 09 Aug 2006, Jacques Garrigue <garri...@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?

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google