|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
A strange typing error with polymorphic variantsHi, I've come across a very strange error, and I'm not sure if it is
a bug or a feature. The following code : type 'a p = R of 'a t | E of float and 'a t = { mutable p : 'a p; c : 'a } let f = let x = sqrt(2.0) in fun () -> { c = `A; p = E 0.0 } generates the error : The type of this expression, unit -> _[> `A ] t, contains type variables that cannot be generalized but if I change the x definition to "let x = 2.0 in" then it works. Another solution is to add a dummy parameter "let f ?(dummy=())" this works too. When I say that it works, I mean that the resulting type of f is val f : unit -> [> `A ] t I've seen that in the dev version of ocaml this error has been removed but the type of f is still "val f : unit -> _[> `A] t" which is not the thing that I want. Is that an expected behavior ? Marc _______________________________________________ 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 |
|
|
Re: A strange typing error with polymorphic variantsOn Tue, Oct 27, 2009 at 3:28 AM, Marc de Falco <marc@...> wrote:
> Hi, I've come across a very strange error, and I'm not sure if it is > a bug or a feature. > > The following code : > type 'a p = R of 'a t | E of float > and 'a t = { mutable p : 'a p; c : 'a } > let f = > let x = sqrt(2.0) in > fun () -> { c = `A; p = E 0.0 } > > generates the error : > The type of this expression, unit -> _[> `A ] t, > contains type variables that cannot be generalized > > but if I change the x definition to "let x = 2.0 in" then it works. > > Another solution is to add a dummy parameter "let f ?(dummy=())" this works > too. I think this is just the value restriction. The type of f is generalized only if the right hand side is a value (rather than an expression needing some computation); in your examples the one that fails is not a value, the others are. It looks like there is a relaxation to allow let bindings which are themselves values. Jake _______________________________________________ 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 |
|
|
Re: A strange typing error with polymorphic variantsLe 27 oct. 09 à 19:24, Jake Donham a écrit : > On Tue, Oct 27, 2009 at 3:28 AM, Marc de Falco <marc@...> > wrote: >> The following code : >> type 'a p = R of 'a t | E of float >> and 'a t = { mutable p : 'a p; c : 'a } >> let f = >> let x = sqrt(2.0) in >> fun () -> { c = `A; p = E 0.0 } >> >> generates the error : >> The type of this expression, unit -> _[> `A ] t, >> contains type variables that cannot be generalized >> >> but if I change the x definition to "let x = 2.0 in" then it works. > > I think this is just the value restriction. The type of f is > generalized only if the right hand side is a value (rather than an > expression needing some computation); in your examples the one that > fails is not a value, the others are. It looks like there is a > relaxation to allow let bindings which are themselves values. With the -dlambda option, the "sqrt(2.0)" version gives: (let (f/92 (let (x/93 (caml_sqrt_float 2.0)) (function param/94 (makemutable 0 [1: 0.0] 65a)))) whereas the "2.0" version gives: (let (f/96 (let (x/97 2.0) (function param/98 (makemutable 0 [1: 0.0] 65a)))) i.e. this last version is inlined. I thought the yping was done before (??) V. _______________________________________________ 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 |
|
|
Re: A strange typing error with polymorphic variantsOops, I pushed "send" button too early.
Le 27 oct. 09 à 19:38, Vincent Aravantinos a écrit : > Le 27 oct. 09 à 19:24, Jake Donham a écrit : > >> On Tue, Oct 27, 2009 at 3:28 AM, Marc de Falco <marc@...> >> wrote: >>> The following code : >>> type 'a p = R of 'a t | E of float >>> and 'a t = { mutable p : 'a p; c : 'a } >>> let f = >>> let x = sqrt(2.0) in >>> fun () -> { c = `A; p = E 0.0 } >>> >>> generates the error : >>> The type of this expression, unit -> _[> `A ] t, >>> contains type variables that cannot be generalized >>> >>> but if I change the x definition to "let x = 2.0 in" then it works. >> >> I think this is just the value restriction. The type of f is >> generalized only if the right hand side is a value (rather than an >> expression needing some computation); in your examples the one that >> fails is not a value, the others are. It looks like there is a >> relaxation to allow let bindings which are themselves values. > > With the -dlambda option, the "sqrt(2.0)" version gives: > (let > (f/92 > (let (x/93 (caml_sqrt_float 2.0)) > (function param/94 (makemutable 0 [1: 0.0] 65a)))) > > whereas the "2.0" version gives: > (let (f/96 (let (x/97 2.0) (function param/98 (makemutable 0 [1: > 0.0] 65a)))) > > i.e. this last version is inlined. Do you think this can give a hint? V. _______________________________________________ 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 |
| Free embeddable forum powered by Nabble | Forum Help |