caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] explicit polymorphic type annotation
@ 2016-03-31 12:06 William
  2016-03-31 12:26 ` octachron
  0 siblings, 1 reply; 3+ messages in thread
From: William @ 2016-03-31 12:06 UTC (permalink / raw)
  To: caml-list

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

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

* Re: [Caml-list] explicit polymorphic type annotation
  2016-03-31 12:06 [Caml-list] explicit polymorphic type annotation William
@ 2016-03-31 12:26 ` octachron
  2016-04-02 11:40   ` Goswin von Brederlow
  0 siblings, 1 reply; 3+ messages in thread
From: octachron @ 2016-03-31 12:26 UTC (permalink / raw)
  To: caml-list

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
>


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

* Re: [Caml-list] explicit polymorphic type annotation
  2016-03-31 12:26 ` octachron
@ 2016-04-02 11:40   ` Goswin von Brederlow
  0 siblings, 0 replies; 3+ messages in thread
From: Goswin von Brederlow @ 2016-04-02 11:40 UTC (permalink / raw)
  To: caml-list

On Thu, Mar 31, 2016 at 02:26:37PM +0200, octachron wrote:
> 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
> >

Or in a GADT.

MfG
	Goswin

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

end of thread, other threads:[~2016-04-02 11:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-31 12:06 [Caml-list] explicit polymorphic type annotation William
2016-03-31 12:26 ` octachron
2016-04-02 11:40   ` Goswin von Brederlow

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