caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alain Frisch <alain@frisch.fr>
To: Alexander Bernauer <bernauer@inf.ethz.ch>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] type of high order functions
Date: Wed, 15 Dec 2010 09:32:48 +0100	[thread overview]
Message-ID: <4D087D30.1000507@frisch.fr> (raw)
In-Reply-To: <20101214203043.GA9093@apus.ethz.ch>

On 12/14/2010 09:30 PM, Alexander Bernauer wrote:
> The code
> ---8<---
> let foo ():string  =
> 	let f: ('a ->  string) ->  'a ->  string = fun g v ->  g v
> 	in let h = string_of_int
> 	in let i = string_of_float
> 	in let x = f h 23
> 	in let y = f i 23.0
> 	in x ^ y
> --->8---
>
> leads to the following error message
>
> ---8<---
> File "test.ml", line 6, characters 14-15:
> Error: This expression has type float ->  string
>         but an expression was expected of type int ->  string
> --->8---

The scope for type variables is implicitly defined as the smallest 
surrounding structure item. In your case, the scope for 'a is the "let 
foo..." declaration, not the local declaration "let f".

There are two solution to do what you want in OCaml 3.12.

* Use the syntax introduced for polymorphic recursion:

   let f: 'a. ('a -> string) -> 'a -> string = fun g v -> g v

* Create a locally abstract type:

   let f (type a) (g : a -> string) (v : a) : string = g v




-- Alain


      parent reply	other threads:[~2010-12-15  8:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-14 20:30 Alexander Bernauer
2010-12-15  8:31 ` [Caml-list] " Florent Ouchet
2010-12-16  8:32   ` Alexander Bernauer
2010-12-16  8:46     ` bluestorm
2010-12-15  8:32 ` Alain Frisch [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4D087D30.1000507@frisch.fr \
    --to=alain@frisch.fr \
    --cc=bernauer@inf.ethz.ch \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).