caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: octachron <octa@polychoron.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] explicit polymorphic type annotation
Date: Thu, 31 Mar 2016 14:26:37 +0200	[thread overview]
Message-ID: <56FD177D.2020300@polychoron.fr> (raw)
In-Reply-To: <56FD12CC.20509@libertysurf.fr>

Hi,

In order to pass a polymorphic function to your rank-2 function f, you 
need to encapsulate it
eitheir in a record:

type poly = { f: 'a. 'a -> 'a }
let func = {f = (fun x -> x) }
let f func = func.f 1, func.f 2.

or in an object

let func = object method f:'a.'a->'a = fun x -> x end
let f (func:<f:'a. 'a -> 'a>) = func#f 1, func#f 2.;;

There is a section on polymorphic method in the manual here:
http://caml.inria.fr/pub/docs/manual-ocaml/objectexamples.html#sec34
and explicit polymorphic type for record field are briefly mentioned at 
the end of
http://caml.inria.fr/pub/docs/manual-ocaml/coreexamples.html#sec12.

Regards,
octachron


Le 03/31/16 14:06, William a écrit :
> Hi,
>
> I don't know how to handle this, and can not find how to do it in the 
> manual. I would like "func" to be polymorphic :
>
> let f func =
>   Printf.printf "%i\n%!" (func (1,2));
>   Printf.printf "%f\n%!" (func (1.,2.));;
> f fst;;
> f snd;;
>
> =>
> Characters 85-87:
> Printf.printf "%f\n%!" (func (1.,2.));;
>                               ^^
> Error: This expression has type float but an expression was expected 
> of type int
>
>
> let f (func:('a. 'a * 'a -> 'a)) =
>   Printf.printf "%i\n%!" (func (1,2));
>   Printf.printf "%f\n%!" (func (1.,2.));;
> f fst;;
> f snd;;
>
> =>
> Characters 15-16:
> let f (func:('a. 'a * 'a -> 'a)) =
>                ^
> Error: Syntax error: type expected.
>
>
>
> Could someone show me how to handle this ?
>
> Also, I did not find any relevant chapter in ocaml.org or ocaml 
> reference manual to explain how to do. Any pointers ?
>
> Best regards
>


  reply	other threads:[~2016-03-31 12:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-31 12:06 William
2016-03-31 12:26 ` octachron [this message]
2016-04-02 11:40   ` Goswin von Brederlow

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=56FD177D.2020300@polychoron.fr \
    --to=octa@polychoron.fr \
    --cc=caml-list@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).