This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

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: Zheng Li Subject: Re: How do I get polymorphic partial application?
```"Till Varoquaux" <till.varoquaux@gmail.com> writes:
> 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:
Well, there are cases eta expansions won't help. somehow you must modify your
code.

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

I suppose you still want to create different instance with parametrized
counter. You can do it in this way:

# type tag_func = {func: 'a. 'a -> int * 'a} ;;
# let cnTag s = let r = ref s in {func = fun x -> incr r; !r, x} ;;

(* either explicit use tag1.func here *)
# let tag1 = cnTag 1 ;;

# tag1.func 10;;
- : int * int = (2, 10)
# tag1.func "asdf";;
- : int * string = (3, "asdf")

(* or one extra step *)
# let tag1 = cnTag 1 ;;
# let tag1 x = tag1.func x ;;

# tag1 10;;
- : int * int = (2, 10)
# tag1 "asdf";;
- : int * string = (3, "asdf")

HTH.

--
Zheng Li
http://www.pps.jussieu.fr/~li

```