caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Strange type inference error
@ 2015-01-23 14:43 Maxence Guesdon
  2015-01-23 15:04 ` Arnaud Spiwack
  0 siblings, 1 reply; 2+ messages in thread
From: Maxence Guesdon @ 2015-01-23 14:43 UTC (permalink / raw)
  To: caml-list

Hello,

I encountered a strange typing problem and reduced it to the following
code example:

====[t.ml]====
let mk_env () = None
let list_of_string (s: string) = [s]

let apply : 'a -> 'a option -> string list -> 'a * string list =
  fun acc env l -> (acc, ["hello"])

let f =
   let tmpl = ["coucou"] in
    fun ?(env=mk_env()) ->
      fun ~x ->
        fun () -> apply () env tmpl

let g =
   let tmpl = list_of_string "coucou" in
    fun ?(env=mk_env()) ->
      fun ~x ->
        fun () -> apply () env tmpl
====
Note that f and g only differ on the way tmpl is defined.

When I compile this file with ocaml 4.02.1, I get the following error:
File "t.ml", line 14, characters 3-119:
Error: The type of this expression,
       ?env:unit option -> x:'_a -> unit -> unit * string list,
       contains type variables that cannot be generalized

Am I missing something or should I file a bug report ?

Regards,

Maxence

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

* Re: [Caml-list] Strange type inference error
  2015-01-23 14:43 [Caml-list] Strange type inference error Maxence Guesdon
@ 2015-01-23 15:04 ` Arnaud Spiwack
  0 siblings, 0 replies; 2+ messages in thread
From: Arnaud Spiwack @ 2015-01-23 15:04 UTC (permalink / raw)
  To: Maxence Guesdon; +Cc: OCaML Mailing List

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

The error message is correct in that `g` is not a value (and so the value
restriction bites you). At the end of a module, no non-generalisable
variables must be left, so Ocaml complains (you will notice, indeed, that
in a toplevel, ocaml will give you the non-generalisable type without
complaining).

I'm guessing ocaml has a relaxed view of values where `let x = <value> in
<value>` is considered a value, which would be why `f` properly generalises.

On 23 January 2015 at 15:43, Maxence Guesdon <Maxence.Guesdon@inria.fr>
wrote:

> Hello,
>
> I encountered a strange typing problem and reduced it to the following
> code example:
>
> ====[t.ml]====
> let mk_env () = None
> let list_of_string (s: string) = [s]
>
> let apply : 'a -> 'a option -> string list -> 'a * string list =
>   fun acc env l -> (acc, ["hello"])
>
> let f =
>    let tmpl = ["coucou"] in
>     fun ?(env=mk_env()) ->
>       fun ~x ->
>         fun () -> apply () env tmpl
>
> let g =
>    let tmpl = list_of_string "coucou" in
>     fun ?(env=mk_env()) ->
>       fun ~x ->
>         fun () -> apply () env tmpl
> ====
> Note that f and g only differ on the way tmpl is defined.
>
> When I compile this file with ocaml 4.02.1, I get the following error:
> File "t.ml", line 14, characters 3-119:
> Error: The type of this expression,
>        ?env:unit option -> x:'_a -> unit -> unit * string list,
>        contains type variables that cannot be generalized
>
> Am I missing something or should I file a bug report ?
>
> Regards,
>
> Maxence
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> 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: 2709 bytes --]

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

end of thread, other threads:[~2015-01-23 15:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-23 14:43 [Caml-list] Strange type inference error Maxence Guesdon
2015-01-23 15:04 ` Arnaud Spiwack

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