caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Insufficient polymorphism in local let.
@ 2012-03-23  4:58 David Brown
  2012-03-23  5:12 ` Gabriel Scherer
  0 siblings, 1 reply; 2+ messages in thread
From: David Brown @ 2012-03-23  4:58 UTC (permalink / raw)
  To: caml-list

I'm trying to figure out how to get 'lmust' in 'get2' in the code
below to work.  It seems that it's type is being inferred as

   string -> string option -> string

instead of

   string -> 'a option -> 'a

The 'must' definition keeps it's polymorphism, as seen in 'get1'.  Is
there some way I can annotate the definition of 'lmust' so that it
will work?  Lifting 'lmust' out of 'get2' doesn't seem to help.

Thanks,
David

let must section name = function
   | Some o -> o
   | None -> 
     Printf.eprintf "Expecting %s in %s\n%!" name section;
     exit 1

type foo = { a: string; b: int }

let get1 a b =
   { a = must "get1" "a" a;
     b = must "get1" "b" b }

let get2 a b =
   (* This isn't polymorphic enough. *)
   let lmust = must "get2" in
   { a = lmust "a" a;
     b = lmust "b" b }

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

* Re: [Caml-list] Insufficient polymorphism in local let.
  2012-03-23  4:58 [Caml-list] Insufficient polymorphism in local let David Brown
@ 2012-03-23  5:12 ` Gabriel Scherer
  0 siblings, 0 replies; 2+ messages in thread
From: Gabriel Scherer @ 2012-03-23  5:12 UTC (permalink / raw)
  To: David Brown; +Cc: caml-list

This is a consequence of value restriction, with a direct workaround.
See the FAQ:

  "A function obtained through partial application is not polymorphic enough"
  http://caml.inria.fr/resources/doc/faq/core.en.html#eta-expansion

  let get1 a b =
    let must x = lmust "get2" x in
    ....

On Fri, Mar 23, 2012 at 5:58 AM, David Brown <caml@davidb.org> wrote:
> I'm trying to figure out how to get 'lmust' in 'get2' in the code
> below to work.  It seems that it's type is being inferred as
>
>  string -> string option -> string
>
> instead of
>
>  string -> 'a option -> 'a
>
> The 'must' definition keeps it's polymorphism, as seen in 'get1'.  Is
> there some way I can annotate the definition of 'lmust' so that it
> will work?  Lifting 'lmust' out of 'get2' doesn't seem to help.
>
> Thanks,
> David
>
> let must section name = function
>  | Some o -> o
>  | None ->     Printf.eprintf "Expecting %s in %s\n%!" name section;
>    exit 1
>
> type foo = { a: string; b: int }
>
> let get1 a b =
>  { a = must "get1" "a" a;
>    b = must "get1" "b" b }
>
> let get2 a b =
>  (* This isn't polymorphic enough. *)
>  let lmust = must "get2" in
>  { a = lmust "a" a;
>    b = lmust "b" b }
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


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

end of thread, other threads:[~2012-03-23  5:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-23  4:58 [Caml-list] Insufficient polymorphism in local let David Brown
2012-03-23  5:12 ` Gabriel Scherer

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