* How do I get polymorphic partial application?
@ 2007-06-23 18:55 Till Varoquaux
2007-06-23 21:41 ` [Caml-list] " Philippe Wang
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Till Varoquaux @ 2007-06-23 18:55 UTC (permalink / raw)
To: OCaml
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
--
http://till-varoquaux.blogspot.com/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] How do I get polymorphic partial application?
2007-06-23 18:55 How do I get polymorphic partial application? Till Varoquaux
@ 2007-06-23 21:41 ` Philippe Wang
2007-06-23 21:49 ` Lukasz Stafiniak
2007-06-23 22:07 ` Zheng Li
2007-06-23 22:09 ` Zheng Li
2 siblings, 1 reply; 9+ messages in thread
From: Philippe Wang @ 2007-06-23 21:41 UTC (permalink / raw)
To: Till Varoquaux, ocaml ml
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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] How do I get polymorphic partial application?
2007-06-23 21:41 ` [Caml-list] " Philippe Wang
@ 2007-06-23 21:49 ` Lukasz Stafiniak
2007-06-23 22:09 ` Till Varoquaux
2007-06-23 22:11 ` Philippe Wang
0 siblings, 2 replies; 9+ messages in thread
From: Lukasz Stafiniak @ 2007-06-23 21:49 UTC (permalink / raw)
To: caml-list
On 6/23/07, Philippe Wang <lists@philippewang.info> wrote:
> 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:
>
> I think you should not try to "hide" that ugly side effect...
>
Could someone give an example where this "eta-thing" forbids a crash?
I don't remember any example even in Xavier Leroy papers.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How do I get polymorphic partial application?
2007-06-23 18:55 How do I get polymorphic partial application? Till Varoquaux
2007-06-23 21:41 ` [Caml-list] " Philippe Wang
@ 2007-06-23 22:07 ` Zheng Li
2007-06-23 22:09 ` Zheng Li
2 siblings, 0 replies; 9+ messages in thread
From: Zheng Li @ 2007-06-23 22:07 UTC (permalink / raw)
To: caml-list
"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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: How do I get polymorphic partial application?
2007-06-23 18:55 How do I get polymorphic partial application? Till Varoquaux
2007-06-23 21:41 ` [Caml-list] " Philippe Wang
2007-06-23 22:07 ` Zheng Li
@ 2007-06-23 22:09 ` Zheng Li
2 siblings, 0 replies; 9+ messages in thread
From: Zheng Li @ 2007-06-23 22:09 UTC (permalink / raw)
To: caml-list
"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
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] How do I get polymorphic partial application?
2007-06-23 21:49 ` Lukasz Stafiniak
@ 2007-06-23 22:09 ` Till Varoquaux
2007-06-23 22:25 ` Lukasz Stafiniak
2007-06-23 22:11 ` Philippe Wang
1 sibling, 1 reply; 9+ messages in thread
From: Till Varoquaux @ 2007-06-23 22:09 UTC (permalink / raw)
To: Lukasz Stafiniak; +Cc: caml-list
As said previously, in a HM type ineference system type variables are
generalized only in "let", that is:
let id x = x (*starting with an identity function*)
let id'= id id (*this is a partial application; it has type '_a ->'_a*)
let _=
print_string (id' "a");
print_int (id' 4)
won't work (since id' has type '_a->'_a).
Replacing the definition of id' with the eta expanded version (let id'
x = (id id) x) will however work. It is not 100% equivalent since
every application of the new id' goes twice through id whilst the old
was a reference to id....
I hope this answers your question.
Cheers,
Till
On 6/23/07, Lukasz Stafiniak <lukstafi@gmail.com> wrote:
> On 6/23/07, Philippe Wang <lists@philippewang.info> wrote:
> > 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:
> >
> > I think you should not try to "hide" that ugly side effect...
> >
> Could someone give an example where this "eta-thing" forbids a crash?
> I don't remember any example even in Xavier Leroy papers.
>
> _______________________________________________
> 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
>
--
http://till-varoquaux.blogspot.com/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] How do I get polymorphic partial application?
2007-06-23 21:49 ` Lukasz Stafiniak
2007-06-23 22:09 ` Till Varoquaux
@ 2007-06-23 22:11 ` Philippe Wang
2007-06-23 22:28 ` Lukasz Stafiniak
1 sibling, 1 reply; 9+ messages in thread
From: Philippe Wang @ 2007-06-23 22:11 UTC (permalink / raw)
To: Lukasz Stafiniak, ocaml ml
Lukasz Stafiniak wrote:
> On 6/23/07, Philippe Wang <lists@philippewang.info> wrote:
>> 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:
>>
>> I think you should not try to "hide" that ugly side effect...
>>
> Could someone give an example where this "eta-thing" forbids a crash?
> I don't remember any example even in Xavier Leroy papers.
If I understand what you mean, this could explain it :
let foo () =
let container = ref [] in
fun x -> container := x :: !container; !container
val foo : unit -> 'a -> 'a list
(* first : doesn't work *)
let plop = foo ()
let l1 = plop 42
let l2 = plop "42" (* crash -- but wouldn't crash with most *dynamic*
typing systems *)
let plop2 = fun () -> foo ()
let l1' = plop2 () 42
let l2' = plop2 () "42" (* no crash *)
but plop and plop2 don't share the same semantics...
--
Philippe Wang
mail[at]philippewang.info
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] How do I get polymorphic partial application?
2007-06-23 22:09 ` Till Varoquaux
@ 2007-06-23 22:25 ` Lukasz Stafiniak
0 siblings, 0 replies; 9+ messages in thread
From: Lukasz Stafiniak @ 2007-06-23 22:25 UTC (permalink / raw)
To: caml-list
On 6/24/07, Till Varoquaux <till.varoquaux@gmail.com> wrote:
> As said previously, in a HM type ineference system type variables are
> generalized only in "let", that is:
>
> let id x = x (*starting with an identity function*)
> let id'= id id (*this is a partial application; it has type '_a ->'_a*)
> let _=
> print_string (id' "a");
> print_int (id' 4)
>
> won't work (since id' has type '_a->'_a).
>
> Replacing the definition of id' with the eta expanded version (let id'
> x = (id id) x) will however work. It is not 100% equivalent since
> every application of the new id' goes twice through id whilst the old
> was a reference to id....
>
> I hope this answers your question.
> Cheers,
> Till
No, it is related to the so called "value restriction" made Caml-ish
way, in a pure functional (no references) HM types can be generalized.
And I restate my question: can someone give an example, where if the
top-level let-definition types were generalized, this would lead to a
crash?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] How do I get polymorphic partial application?
2007-06-23 22:11 ` Philippe Wang
@ 2007-06-23 22:28 ` Lukasz Stafiniak
0 siblings, 0 replies; 9+ messages in thread
From: Lukasz Stafiniak @ 2007-06-23 22:28 UTC (permalink / raw)
To: ocaml ml
On 6/24/07, Philippe Wang <lists@philippewang.info> wrote:
>
> If I understand what you mean, this could explain it :
>
Uuups, I skipped your answer. Thanks and sorry for the noise!
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2007-06-23 22:28 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-23 18:55 How do I get polymorphic partial application? Till Varoquaux
2007-06-23 21:41 ` [Caml-list] " Philippe Wang
2007-06-23 21:49 ` Lukasz Stafiniak
2007-06-23 22:09 ` Till Varoquaux
2007-06-23 22:25 ` Lukasz Stafiniak
2007-06-23 22:11 ` Philippe Wang
2007-06-23 22:28 ` Lukasz Stafiniak
2007-06-23 22:07 ` Zheng Li
2007-06-23 22:09 ` Zheng Li
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox