Version française
Home     About     Download     Resources     Contact us    
Browse thread
format polymorphism
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] format polymorphism
From: Christophe TROESTLER <Christophe.Troestler@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