caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* function specialization
@ 2005-11-15 11:45 Vsevolod Fedorov
  2005-11-15 12:57 ` [Caml-list] " Alessandro Baretta
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Vsevolod Fedorov @ 2005-11-15 11:45 UTC (permalink / raw)
  To: caml-list

Hello!

I have code like following:
----

type 'a wrap = ('a -> unit) -> 'a -> unit

let wrap fn arg =
  fn arg

let fn1 (v: int)    = ()
let fn2 (v: string) = ()

let use (wrap: 'a wrap) =
  wrap fn1 1;
  wrap fn2 ""
  (*   ^^^ This expression has type string -> unit but is here used with
type int -> unit *)

------
and get this error from compiler.
Is there any way to use 'wrap' function for both cases: fn1 and fn2?
Or is there some 'standard' workarounds/solutions for this problem exists?

Thanks
Seva



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

* Re: [Caml-list] function specialization
  2005-11-15 11:45 function specialization Vsevolod Fedorov
@ 2005-11-15 12:57 ` Alessandro Baretta
  2005-11-15 13:00 ` Jørgen Hermanrud Fjeld
  2005-11-15 13:11 ` skaller
  2 siblings, 0 replies; 4+ messages in thread
From: Alessandro Baretta @ 2005-11-15 12:57 UTC (permalink / raw)
  To: Vsevolod Fedorov; +Cc: caml-list

Vsevolod Fedorov wrote:
> Hello!
> 
> I have code like following:
> ----
> 
> type 'a wrap = ('a -> unit) -> 'a -> unit

type wrap = { action : 'a. ('a -> unit) -> 'a -> unit }

> let wrap fn arg =
>   fn arg

let wrap = { action = fun fn arg -> fn arg }

> let fn1 (v: int)    = ()
> let fn2 (v: string) = ()
> 
> let use (wrap: 'a wrap) =
>   wrap fn1 1;
>   wrap fn2 ""
>   (*   ^^^ This expression has type string -> unit but is here used with
> type int -> unit *)

let use (wrap : wrap) =
   wrap.action fn1 1;
   wrap.action fn2 ""

Alex



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

* Re: [Caml-list] function specialization
  2005-11-15 11:45 function specialization Vsevolod Fedorov
  2005-11-15 12:57 ` [Caml-list] " Alessandro Baretta
@ 2005-11-15 13:00 ` Jørgen Hermanrud Fjeld
  2005-11-15 13:11 ` skaller
  2 siblings, 0 replies; 4+ messages in thread
From: Jørgen Hermanrud Fjeld @ 2005-11-15 13:00 UTC (permalink / raw)
  To: caml-list

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

From the top of my head, with objects (and classes) you can create 
a method that is polymorphic for each invocation "http://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#ss:polymorphic-methods".
For instance:
(**)
class type wrapper = object method wrap : 'a.('a->unit)->'a->unit end;;

let wrapper:wrapper = object 
    method wrap : 'a.('a->unit)->'a->unit = 
	fun fn arg -> fn arg 
end ;;


let fn1 (v: int)    = ()
let fn2 (v: string) = ()

let use = wrapper#wrap fn1 1 ; wrapper#wrap fn2 "" ;;
let use (wrapper:wrapper) = wrapper#wrap fn1 1; wrapper#wrap fn2 "" ;;

use wrapper ;;
(**)


On Tue, Nov 15, 2005 at 02:45:40PM +0300, Vsevolod Fedorov wrote:
> Hello!
> 
> I have code like following:
> ----
> 
> type 'a wrap = ('a -> unit) -> 'a -> unit
> 
> let wrap fn arg =
>   fn arg
> 
> let fn1 (v: int)    = ()
> let fn2 (v: string) = ()
> 
> let use (wrap: 'a wrap) =
>   wrap fn1 1;
>   wrap fn2 ""
>   (*   ^^^ This expression has type string -> unit but is here used with
> type int -> unit *)
> 
> ------
> and get this error from compiler.
> Is there any way to use 'wrap' function for both cases: fn1 and fn2?
> Or is there some 'standard' workarounds/solutions for this problem exists?
> 
> Thanks
> Seva
> 
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 

-- 
Sincerely | Homepage:
Jørgen    | http://www.hex.no/jhf
          | Public GPG key:
          | http://www.hex.no/jhf/key.txt

Truly great madness can not be achieved without significant intelligence.
		-- Henrik Tikkanen


[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: [Caml-list] function specialization
  2005-11-15 11:45 function specialization Vsevolod Fedorov
  2005-11-15 12:57 ` [Caml-list] " Alessandro Baretta
  2005-11-15 13:00 ` Jørgen Hermanrud Fjeld
@ 2005-11-15 13:11 ` skaller
  2 siblings, 0 replies; 4+ messages in thread
From: skaller @ 2005-11-15 13:11 UTC (permalink / raw)
  To: Vsevolod Fedorov; +Cc: caml-list

On Tue, 2005-11-15 at 14:45 +0300, Vsevolod Fedorov wrote:
> Hello!

>   (*   ^^^ This expression has type string -> unit but is here used with
> type int -> unit *)

Here is a simpler case:

let use (f: 'a -> unit) = f 1; f ""

The appearance of a 'free' type variable in the function
signature implicitly implies the quantification:

For all 'a . let use (f: 'a -> unit) =  f 1; f ""

which says, for each type 'a, there is a function 'use' ..

Inside a particular instance of use, 'a is fixed.

In Ocaml this design bug has been fixed for record and object fields:

type t = { f: 'a . 'a -> unit }
let use (wrap: t) = wrap.f 1; wrap.f ""

Here the quantifier is explicit and binds only the
type of the field f; and the record type t is entirely 
monomorphic.

Note that the first interpretation is necessary
in the first case, by considering:

let f (x:int) = ()
use f;

we simply cannot have the inner

f ""

type correctly. In the second case, you cannot
instantiate the field with that f, because it
is the wrong type -- the field requires the
function to be polymorphic.

With any luck this design bug will eventually be
properly fixed, so you can write:

let use (f: 'a . 'a -> unit) =  f 1; f ""

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


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

end of thread, other threads:[~2005-11-15 13:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-15 11:45 function specialization Vsevolod Fedorov
2005-11-15 12:57 ` [Caml-list] " Alessandro Baretta
2005-11-15 13:00 ` Jørgen Hermanrud Fjeld
2005-11-15 13:11 ` skaller

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