Version française
Home     About     Download     Resources     Contact us    
Browse thread
How do I get polymorphic partial application?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Philippe Wang <lists@p...>
Subject: Re: [Caml-list] How do I get polymorphic partial application?
Till Varoquaux wrote:
> Humm... I have a small issue here: I need to get the result of the
> partial application of a polymorphic function. Since variable are
> generalized in Let it is generally advised to use eta expansions,
> (i.e transform to a total application).
> sometimes eta expansions just won't do the trick, consider:
>
> let cntTag start=
> let cnt=ref start in
> fun v -> ((incr cnt;!cnt),v)
>
> the partial application is not fully polymorphic
>
> let tag1 = cntTag 1
>
> has type
>
> val tag1 : '_a -> int * '_a = <fun>
>
> the eta expanded equivalent doesn't have the same semantic (it
> actually seems even less useful):
>
> let tag1 x= cntTag 1 x
>
> Is there an elegant solution to that problem?
> Cheers,
> Till
>
Hi,

I think you should not try to "hide" that ugly side effect...

If you do something like

let rec f a = incr x ; a
and x = ref 42          
and start s = x := s ;;

then you don't need to ask yourself how to bypass the type checker...

Still, you can hide things in a module...

module X :
sig 
  val f : 'a -> int * 'a
  val start : int -> unit
end = struct
  let rec f a = incr x ; !x, a
  and x = ref 42    
  and start s = x := s
end
(* include X *)

(only Obj.magic can do exactly what you're asking for -- except the 
"elegant" part)

-- 
Philippe Wang
   mail[at]philippewang.info