caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* type inference problem with Printf.sprintf ?
@ 2010-10-26 15:55 Emmanuel Dieul
  2010-10-26 16:04 ` [Caml-list] " Mathias Kende
  2010-10-26 21:41 ` [Caml-list] " David Allsopp
  0 siblings, 2 replies; 10+ messages in thread
From: Emmanuel Dieul @ 2010-10-26 15:55 UTC (permalink / raw)
  To: caml-list

Hi everyone.

I've just noticed a type inference problem (or am I wrong ?). Here's my example :

let format_date =
   let format_valeur = function valeur ->
     (if valeur < 10 then "0" else "") ^ (string_of_int valeur)
   in
   function date ->
     Printf.sprintf "%s/%s/%l %s:%s:%s"
       (format_valeur(date.Unix.tm_mday) : string)
       (format_valeur(date.Unix.tm_mon + 1) : string)
       (date.Unix.tm_year + 1900)
       (format_valeur(date.Unix.tm_hour) : string)
       (format_valeur(date.Unix.tm_min) : string)
       (format_valeur(date.Unix.tm_sec) : string)
;;

The "format_valeur" function has type "int -> string".
The "format_date" function has type "Unix.tm -> string".
The problem is about the Printf.sprintf function.

In fact, if I don't force the type of every date field (like 
"(format_valeur(date.Unix.tm_mday) : string)"),
the ocaml 3.12.0 compiler detects a type error.
This example

let format_date =
   let format_valeur = function valeur ->
     (if valeur < 10 then "0" else "") ^ (string_of_int valeur)
   in
   function date ->
     Printf.sprintf "%s/%s/%l %s:%s:%s"
       format_valeur(date.Unix.tm_mday)
       (format_valeur(date.Unix.tm_mon + 1) : string)
       (date.Unix.tm_year + 1900)
       (format_valeur(date.Unix.tm_hour) : string)
       (format_valeur(date.Unix.tm_min) : string)
       (format_valeur(date.Unix.tm_sec) : string)
;;

generates this error around the "%s/%s/%l %s:%s:%s" string :
Error: This expression has type
          (string -> string -> int -> string -> string -> string -> 'a -> 'b,
           unit, string, string, string, 'a -> 'b)
          format6
        but an expression was expected of type
          (string -> string -> int -> string -> string -> string -> 'a -> 'b,
           unit, string)
          format =
            (string -> string -> int -> string -> string -> string -> 'a -> 'b,
             unit, string, string, string, string)
            format6

Did I do something wrong or is there a real problem ?

Emmanuel


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] type inference problem with Printf.sprintf ?
  2010-10-26 15:55 type inference problem with Printf.sprintf ? Emmanuel Dieul
@ 2010-10-26 16:04 ` Mathias Kende
  2010-10-26 16:10   ` Emmanuel Dieul
  2010-10-26 21:41 ` [Caml-list] " David Allsopp
  1 sibling, 1 reply; 10+ messages in thread
From: Mathias Kende @ 2010-10-26 16:04 UTC (permalink / raw)
  To: Emmanuel Dieul; +Cc: caml-list

Le mardi 26 octobre 2010 à 17:55 +0200, Emmanuel Dieul a écrit :
> let format_date =
>    let format_valeur = function valeur ->
>      (if valeur < 10 then "0" else "") ^ (string_of_int valeur)
>    in
>    function date ->
>      Printf.sprintf "%s/%s/%l %s:%s:%s"
>        format_valeur(date.Unix.tm_mday)
>        (format_valeur(date.Unix.tm_mon + 1) : string)
>        (date.Unix.tm_year + 1900)
>        (format_valeur(date.Unix.tm_hour) : string)
>        (format_valeur(date.Unix.tm_min) : string)
>        (format_valeur(date.Unix.tm_sec) : string)
> ;;
> 

Here, the argument to the Printf.sprintf function are : 
"%s/%s/%l %s:%s:%s", format_valeur, date.Unix.tm_mday, ...

Just add parenthesis around (format_valeur date.Unix.tm_mday) (but
without any type annotation) to pass the _result_ of this application to
the sprintf function. You don't need parenthesis around the argument of
the function though.

Mathias


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] type inference problem with Printf.sprintf ?
  2010-10-26 16:04 ` [Caml-list] " Mathias Kende
@ 2010-10-26 16:10   ` Emmanuel Dieul
  2010-10-26 21:44     ` Arlen Cuss
  0 siblings, 1 reply; 10+ messages in thread
From: Emmanuel Dieul @ 2010-10-26 16:10 UTC (permalink / raw)
  To: Mathias Kende; +Cc: caml-list

Of course. I have to admit I've been a long time without developing in OCaml...
Thanks !

Le 26/10/2010 18:04, Mathias Kende a écrit :
> Le mardi 26 octobre 2010 à 17:55 +0200, Emmanuel Dieul a écrit :
>> let format_date =
>>     let format_valeur = function valeur ->
>>       (if valeur<  10 then "0" else "") ^ (string_of_int valeur)
>>     in
>>     function date ->
>>       Printf.sprintf "%s/%s/%l %s:%s:%s"
>>         format_valeur(date.Unix.tm_mday)
>>         (format_valeur(date.Unix.tm_mon + 1) : string)
>>         (date.Unix.tm_year + 1900)
>>         (format_valeur(date.Unix.tm_hour) : string)
>>         (format_valeur(date.Unix.tm_min) : string)
>>         (format_valeur(date.Unix.tm_sec) : string)
>> ;;
>>
> Here, the argument to the Printf.sprintf function are :
> "%s/%s/%l %s:%s:%s", format_valeur, date.Unix.tm_mday, ...
>
> Just add parenthesis around (format_valeur date.Unix.tm_mday) (but
> without any type annotation) to pass the _result_ of this application to
> the sprintf function. You don't need parenthesis around the argument of
> the function though.
>
> Mathias
>

-- 
*Emmanuel Dieul*
	122, rue Salvador Allende
boîte 25
92000 Nanterre

http://emmanuel.dieul.free.fr


^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: [Caml-list] type inference problem with Printf.sprintf ?
  2010-10-26 15:55 type inference problem with Printf.sprintf ? Emmanuel Dieul
  2010-10-26 16:04 ` [Caml-list] " Mathias Kende
@ 2010-10-26 21:41 ` David Allsopp
  1 sibling, 0 replies; 10+ messages in thread
From: David Allsopp @ 2010-10-26 21:41 UTC (permalink / raw)
  To: Emmanuel Dieul, caml-list

Emmanuel Dieul wrote:
> Hi everyone.
> 
> I've just noticed a type inference problem (or am I wrong ?). Here's my
> example :
> 
> let format_date =
>    let format_valeur = function valeur ->
>      (if valeur < 10 then "0" else "") ^ (string_of_int valeur)

Your main problem has already been answered but it's worth noting that you can eliminate this function entirely by using "%02l" in your format specification and passing the integer values from your time structure directly to give:

Printf.sprintf "%02l/%02l/%l %02l:%02l:%02l" date.Unix.tm_mday (date.Unix.tm_mon + 1) (date.Unix.tm_year + 1900) date.Unix.tm_hour date.Unix.tm_min date.Unix.tm_sec

It's worth having a complete read of the top of the Printf page in the standard library reference just to see the things it can do. 


David 


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] type inference problem with Printf.sprintf ?
  2010-10-26 16:10   ` Emmanuel Dieul
@ 2010-10-26 21:44     ` Arlen Cuss
  2010-10-27 11:30       ` Richard Jones
  0 siblings, 1 reply; 10+ messages in thread
From: Arlen Cuss @ 2010-10-26 21:44 UTC (permalink / raw)
  To: Emmanuel Dieul; +Cc: Mathias Kende, caml-list

> Le 26/10/2010 18:04, Mathias Kende a écrit :
> > Le mardi 26 octobre 2010 à 17:55 +0200, Emmanuel Dieul a écrit :
> >> let format_date =
> >>     let format_valeur = function valeur ->
> >>       (if valeur<  10 then "0" else "") ^ (string_of_int valeur)
> >>     in
> >>     function date ->
> >>       Printf.sprintf "%s/%s/%l %s:%s:%s"
> >>         format_valeur(date.Unix.tm_mday)
> >>         (format_valeur(date.Unix.tm_mon + 1) : string)
> >>         (date.Unix.tm_year + 1900)
> >>         (format_valeur(date.Unix.tm_hour) : string)
> >>         (format_valeur(date.Unix.tm_min) : string)
> >>         (format_valeur(date.Unix.tm_sec) : string)
> >> ;;

By the way, consider using this:
Printf.sprintf "%02d/%02d/%l %02d:%02d:%02d"
  date.Unix.tm_mday
  (date.Unix.tm_mon + 1)
  (date.Unix.tm_year + 1900)
  date.Unix.tm_hour
  date.Unix.tm_min
  date.Unix.tm_sec;;

.. to make format_valeur redundant. Or, you could even use CalendarLib
to do pretty printing:

# open CalendarLib;;
# Calendar.now ();;
- : CalendarLib.Calendar.t = <abstr>
# Printer.Calendar.print "%d/%m/%Y %H:%M:%S\n" (Calendar.now ());;
26/10/2010 21:43:43
- : unit = ()
# 

> >>
> > Here, the argument to the Printf.sprintf function are :
> > "%s/%s/%l %s:%s:%s", format_valeur, date.Unix.tm_mday, ...
> >
> > Just add parenthesis around (format_valeur date.Unix.tm_mday) (but
> > without any type annotation) to pass the _result_ of this application to
> > the sprintf function. You don't need parenthesis around the argument of
> > the function though.
> >
> > Mathias
> >
> 



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] type inference problem with Printf.sprintf ?
  2010-10-26 21:44     ` Arlen Cuss
@ 2010-10-27 11:30       ` Richard Jones
  2010-11-03 13:47         ` Gregory Bellier
  0 siblings, 1 reply; 10+ messages in thread
From: Richard Jones @ 2010-10-27 11:30 UTC (permalink / raw)
  To: Arlen Cuss; +Cc: Emmanuel Dieul, caml-list

On Wed, Oct 27, 2010 at 08:44:10AM +1100, Arlen Cuss wrote:
> # open CalendarLib;;
> # Calendar.now ();;
> - : CalendarLib.Calendar.t = <abstr>
> # Printer.Calendar.print "%d/%m/%Y %H:%M:%S\n" (Calendar.now ());;
> 26/10/2010 21:43:43
> - : unit = ()

Even better, use the internationally standardized format for dates:

# Printer.Calendar.print "%F %T\n" (Calendar.now ()) ;;
2010-10-27 11:28:59
- : unit = ()

http://en.wikipedia.org/wiki/ISO_8601

Rich.

-- 
Richard Jones
Red Hat


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] type inference problem with Printf.sprintf ?
  2010-10-27 11:30       ` Richard Jones
@ 2010-11-03 13:47         ` Gregory Bellier
  2010-11-03 14:43           ` Sylvain Le Gall
  0 siblings, 1 reply; 10+ messages in thread
From: Gregory Bellier @ 2010-11-03 13:47 UTC (permalink / raw)
  To: Richard Jones; +Cc: Arlen Cuss, caml-list

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

What's the point to rely on another lib while the standard lib Unix is
enough for this simple task? I don't know calendarLib, that's why I ask. But
it relies on Unix and Sys anyway so maybe it's better to just use Unix.





2010/10/27 Richard Jones <rich@annexia.org>

> On Wed, Oct 27, 2010 at 08:44:10AM +1100, Arlen Cuss wrote:
> > # open CalendarLib;;
> > # Calendar.now ();;
> > - : CalendarLib.Calendar.t = <abstr>
> > # Printer.Calendar.print "%d/%m/%Y %H:%M:%S\n" (Calendar.now ());;
> > 26/10/2010 21:43:43
> > - : unit = ()
>
> Even better, use the internationally standardized format for dates:
>
> # Printer.Calendar.print "%F %T\n" (Calendar.now ()) ;;
> 2010-10-27 11:28:59
> - : unit = ()
>
> http://en.wikipedia.org/wiki/ISO_8601
>
> Rich.
>
> --
> Richard Jones
> Red Hat
>
> _______________________________________________
> 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
>

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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: type inference problem with Printf.sprintf ?
  2010-11-03 13:47         ` Gregory Bellier
@ 2010-11-03 14:43           ` Sylvain Le Gall
  2010-11-03 15:33             ` [Caml-list] " Benedikt Grundmann
  0 siblings, 1 reply; 10+ messages in thread
From: Sylvain Le Gall @ 2010-11-03 14:43 UTC (permalink / raw)
  To: caml-list

Hello,

On 03-11-2010, Gregory Bellier <gregory.bellier@gmail.com> wrote:
>
> What's the point to rely on another lib while the standard lib Unix is
> enough for this simple task? I don't know calendarLib, that's why I ask. But
> it relies on Unix and Sys anyway so maybe it's better to just use Unix.
>

Calendar (http://calendar.forge.ocamlcore.org/) or CalendarLib helps you
to deal with a lot of details concerning date. Unix is enough, if you
just want the number of seconds between start and stop of a function. If
you want to count weeks or days, you should use Calendar.

>
> 2010/10/27 Richard Jones <rich@annexia.org>
>
>> On Wed, Oct 27, 2010 at 08:44:10AM +1100, Arlen Cuss wrote:
>> > # open CalendarLib;;
>> > # Calendar.now ();;
>> > - : CalendarLib.Calendar.t = <abstr>
>> > # Printer.Calendar.print "%d/%m/%Y %H:%M:%S\n" (Calendar.now ());;
>> > 26/10/2010 21:43:43
>> > - : unit = ()
>>
>> Even better, use the internationally standardized format for dates:
>>
>> # Printer.Calendar.print "%F %T\n" (Calendar.now ()) ;;
>> 2010-10-27 11:28:59
>> - : unit = ()
>>
>>

Regards,
Sylvain Le Gall


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: type inference problem with Printf.sprintf ?
  2010-11-03 14:43           ` Sylvain Le Gall
@ 2010-11-03 15:33             ` Benedikt Grundmann
  2010-11-03 16:23               ` Dario Teixeira
  0 siblings, 1 reply; 10+ messages in thread
From: Benedikt Grundmann @ 2010-11-03 15:33 UTC (permalink / raw)
  To: Sylvain Le Gall; +Cc: caml-list

core's (http://ocaml.janestreet.com/?q=node/13) Date.t and TZ.t
also contain lots of functions that help dealing with dates.


On 3 November 2010 14:43, Sylvain Le Gall <sylvain@le-gall.net> wrote:
> Hello,
>
> On 03-11-2010, Gregory Bellier <gregory.bellier@gmail.com> wrote:
>>
>> What's the point to rely on another lib while the standard lib Unix is
>> enough for this simple task? I don't know calendarLib, that's why I ask. But
>> it relies on Unix and Sys anyway so maybe it's better to just use Unix.
>>
>
> Calendar (http://calendar.forge.ocamlcore.org/) or CalendarLib helps you
> to deal with a lot of details concerning date. Unix is enough, if you
> just want the number of seconds between start and stop of a function. If
> you want to count weeks or days, you should use Calendar.
>
>>
>> 2010/10/27 Richard Jones <rich@annexia.org>
>>
>>> On Wed, Oct 27, 2010 at 08:44:10AM +1100, Arlen Cuss wrote:
>>> > # open CalendarLib;;
>>> > # Calendar.now ();;
>>> > - : CalendarLib.Calendar.t = <abstr>
>>> > # Printer.Calendar.print "%d/%m/%Y %H:%M:%S\n" (Calendar.now ());;
>>> > 26/10/2010 21:43:43
>>> > - : unit = ()
>>>
>>> Even better, use the internationally standardized format for dates:
>>>
>>> # Printer.Calendar.print "%F %T\n" (Calendar.now ()) ;;
>>> 2010-10-27 11:28:59
>>> - : unit = ()
>>>
>>>
>
> Regards,
> Sylvain Le Gall
>
> _______________________________________________
> 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
>



-- 
Calvin: I try to make everyone's day a little more
surreal.

(From Calvin & Hobbes)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Caml-list] Re: type inference problem with Printf.sprintf ?
  2010-11-03 15:33             ` [Caml-list] " Benedikt Grundmann
@ 2010-11-03 16:23               ` Dario Teixeira
  0 siblings, 0 replies; 10+ messages in thread
From: Dario Teixeira @ 2010-11-03 16:23 UTC (permalink / raw)
  To: Sylvain Le Gall, Benedikt Grundmann; +Cc: caml-list

Hi,

> core's (http://ocaml.janestreet.com/?q=node/13) Date.t and TZ.t
> also contain lots of functions that help dealing with dates.

One feature that Core supports and that is sorely missed in Calendar
is native handling of the timezone database present in Unix systems.
Sure, Calendar does have some rudimentary knowledge of timezones, but
I've come across situations where it's not enough, forcing me to write
C stubs to directly access the Libc timezone handling functions (which
are not pretty).

Cheers,
Dario Teixeira






^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-11-03 16:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-26 15:55 type inference problem with Printf.sprintf ? Emmanuel Dieul
2010-10-26 16:04 ` [Caml-list] " Mathias Kende
2010-10-26 16:10   ` Emmanuel Dieul
2010-10-26 21:44     ` Arlen Cuss
2010-10-27 11:30       ` Richard Jones
2010-11-03 13:47         ` Gregory Bellier
2010-11-03 14:43           ` Sylvain Le Gall
2010-11-03 15:33             ` [Caml-list] " Benedikt Grundmann
2010-11-03 16:23               ` Dario Teixeira
2010-10-26 21:41 ` [Caml-list] " David Allsopp

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