caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Warning wished
@ 2009-01-28 13:53 Julien SIGNOLES
  2009-01-28 14:07 ` Re : [Caml-list] " Matthieu Wipliez
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Julien SIGNOLES @ 2009-01-28 13:53 UTC (permalink / raw)
  To: caml-list

Hello,

Is it a bug or a well-known feature that the above program does not emit
a warning (because "f x" should have type unit in the body of "g") ?

=====
let f x = x
let g x = f x; 1
(* let _ = g 2 *)
====

Best regards,
Julien Signoles


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

* Re : [Caml-list] Warning wished
  2009-01-28 13:53 Warning wished Julien SIGNOLES
@ 2009-01-28 14:07 ` Matthieu Wipliez
  2009-01-28 14:24   ` Julien SIGNOLES
  2009-01-28 14:10 ` Dmitri Boulytchev
  2009-01-28 16:55 ` Martin Jambon
  2 siblings, 1 reply; 10+ messages in thread
From: Matthieu Wipliez @ 2009-01-28 14:07 UTC (permalink / raw)
  To: caml-list

> Hello,
> 
> Is it a bug or a well-known feature that the above program does not emit
> a warning (because "f x" should have type unit in the body of "g") ?
> 
> =====
> let f x = x
> let g x = f x; 1
> (* let _ = g 2 *)
> ====

I'm not familiar with the internals of the compiler, but what I suppose is happening is that it tries to unify the type of "f x" and "unit", and this succeeds because "f x" has type 'a, which can be unified with anything. The meaning of "a;b" seems to be let _ = a in b rather than let () = a in b.

But like I said, these are suppositions.

Cheers,
Matthieu

> 
> Best regards,
> Julien Signoles
> 
> _______________________________________________
> 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






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

* Re: [Caml-list] Warning wished
  2009-01-28 13:53 Warning wished Julien SIGNOLES
  2009-01-28 14:07 ` Re : [Caml-list] " Matthieu Wipliez
@ 2009-01-28 14:10 ` Dmitri Boulytchev
  2009-01-28 14:28   ` Julien SIGNOLES
  2009-01-28 16:55 ` Martin Jambon
  2 siblings, 1 reply; 10+ messages in thread
From: Dmitri Boulytchev @ 2009-01-28 14:10 UTC (permalink / raw)
  To: Julien SIGNOLES; +Cc: caml-list

    Hello Julien,

    no warning should be issued in this case since you have polymorphic 
function:
applying g to () you will definitely have f x of type unit :)
    Try another one:

    let f x = x+1
    let g x = f x; 1

    Now you'll get the warning since the compiler can ensure that type 
of  f x can never
be unit.

    Best regards,
    Dmitry.


> Hello,
>
> Is it a bug or a well-known feature that the above program does not emit
> a warning (because "f x" should have type unit in the body of "g") ?
>
> =====
> let f x = x
> let g x = f x; 1
> (* let _ = g 2 *)
> ====
>
> Best regards,
> Julien Signoles
>
> _______________________________________________
> 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
>
>   


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

* Re: Re : [Caml-list] Warning wished
  2009-01-28 14:07 ` Re : [Caml-list] " Matthieu Wipliez
@ 2009-01-28 14:24   ` Julien SIGNOLES
  2009-01-28 20:56     ` Nicolas Pouillard
  0 siblings, 1 reply; 10+ messages in thread
From: Julien SIGNOLES @ 2009-01-28 14:24 UTC (permalink / raw)
  To: Matthieu Wipliez; +Cc: caml-list

Le mercredi 28 janvier 2009 à 14:07 +0000, Matthieu Wipliez a écrit :
> > Hello,
> > 
> > Is it a bug or a well-known feature that the above program does not emit
> > a warning (because "f x" should have type unit in the body of "g") ?
> > 
> > =====
> > let f x = x
> > let g x = f x; 1
> > (* let _ = g 2 *)
> > ====
> 
> I'm not familiar with the internals of the compiler, but what I suppose is happening is that it tries to unify the type of "f x" and "unit", and this succeeds because "f x" has type 'a, which can be unified with anything. The meaning of "a;b" seems to be let _ = a in b rather than let () = a in b.
> 
> But like I said, these are suppositions.

Sure. However in my real case, I had an "ok" code like this :

let f x : 'a -> unit = ...
let g x =
  f x;
  x

I changed a little bit the specification of f :

let f x : 'a -> 'a = ...

I just expected that caml helps me to find all the instances where I had
to take care of this specification change. Such a thing is one of the
very good features of caml. Unfortunatly, in this case, caml did not
help me while the old implementation of "g" became wrong with the new
specification of "f" :-(.

--
Julien


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

* Re: [Caml-list] Warning wished
  2009-01-28 14:10 ` Dmitri Boulytchev
@ 2009-01-28 14:28   ` Julien SIGNOLES
  2009-01-28 14:44     ` Dmitri Boulytchev
  0 siblings, 1 reply; 10+ messages in thread
From: Julien SIGNOLES @ 2009-01-28 14:28 UTC (permalink / raw)
  To: Dmitri Boulytchev; +Cc: caml-list

Hello Dmitri,

>     no warning should be issued in this case since you have polymorphic 
> function:
> applying g to () you will definitely have f x of type unit :)

Applying g to 1 you will definitely have f x of type int and you have to
take care of the returned integer. Unfortunatly caml emits no warning in
this case (even if I understand why) :(.

--
Julien


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

* Re: [Caml-list] Warning wished
  2009-01-28 14:28   ` Julien SIGNOLES
@ 2009-01-28 14:44     ` Dmitri Boulytchev
  2009-01-28 18:29       ` Frédéric van der Plancke
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitri Boulytchev @ 2009-01-28 14:44 UTC (permalink / raw)
  To: Julien SIGNOLES; +Cc: caml-list


> Applying g to 1 you will definitely have f x of type int and you have to
> take care of the returned integer. Unfortunatly caml emits no warning in
> this case (even if I understand why) :(.
>   
    Sure :) But as far as I understand this warning means "this non-unit 
value will definitely be omitted, and this
can be possibly wrong". Under this interpretation the compiler has to 
ensure undoubtedly that the value is
always non-unit.
    BTW you may (temporarily) change your code into something like

    let f x = [x]

    of even into

    let f x = object method get = x end

    and find all the suspicious places :)

    Best regards,
    Dmitri.



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

* Re: [Caml-list] Warning wished
  2009-01-28 13:53 Warning wished Julien SIGNOLES
  2009-01-28 14:07 ` Re : [Caml-list] " Matthieu Wipliez
  2009-01-28 14:10 ` Dmitri Boulytchev
@ 2009-01-28 16:55 ` Martin Jambon
  2 siblings, 0 replies; 10+ messages in thread
From: Martin Jambon @ 2009-01-28 16:55 UTC (permalink / raw)
  To: Julien SIGNOLES; +Cc: caml-list

Julien SIGNOLES wrote:
> Hello,
> 
> Is it a bug or a well-known feature that the above program does not emit
> a warning (because "f x" should have type unit in the body of "g") ?
> 
> =====
> let f x = x
> let g x = f x; 1
> (* let _ = g 2 *)
> ====

The compiler could have a command-line switch that enforces the unit type in
sequences, i.e. it would add type annotations for you:

let f x = x
let g x = (f x : unit); 1

There's a camlp4/camlp5 syntax extension that does this (but I don't use it).


Martin

-- 
http://mjambon.com/


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

* Re: [Caml-list] Warning wished
  2009-01-28 14:44     ` Dmitri Boulytchev
@ 2009-01-28 18:29       ` Frédéric van der Plancke
  2009-01-28 19:08         ` Dmitri Boulytchev
  0 siblings, 1 reply; 10+ messages in thread
From: Frédéric van der Plancke @ 2009-01-28 18:29 UTC (permalink / raw)
  To: caml-list

Dmitri Boulytchev wrote:
>
>> Applying g to 1 you will definitely have f x of type int and you have to
>> take care of the returned integer. Unfortunatly caml emits no warning in
>> this case (even if I understand why) :(.
>>   
>    Sure :) But as far as I understand this warning means "this 
> non-unit value will definitely be omitted, and this
> can be possibly wrong". Under this interpretation the compiler has to 
> ensure undoubtedly that the value is
> always non-unit.
>    BTW you may (temporarily) change your code into something like
>
>    let f x = [x]
>
>    of even into
>
>    let f x = object method get = x end
>
>    and find all the suspicious places :)
>
>    Best regards,
>    Dmitri.
There's the question of what the compiler _does_, what the compiler 
_could_ do and what the compiler _should_ do.

I don't think "this 'a could be unit" is a good reason for skipping the 
warning. On the contrary, "this 'a will probably sometimes be a real 
value" seems a sufficient reason to add a warning. If needed, the 
programmer can easily suppress the warning by adding "ignore" or adding 
a ": unit" type constraint (whichever applies).

(Remains to see whether adding the warning to OCaml is worth the manwork.)

Frédéric


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

* Re: [Caml-list] Warning wished
  2009-01-28 18:29       ` Frédéric van der Plancke
@ 2009-01-28 19:08         ` Dmitri Boulytchev
  0 siblings, 0 replies; 10+ messages in thread
From: Dmitri Boulytchev @ 2009-01-28 19:08 UTC (permalink / raw)
  To: fvdp; +Cc: caml-list


> There's the question of what the compiler _does_, what the compiler 
> _could_ do and what the compiler _should_ do.
>
    The latter is mainly a matter of taste :) 

> I don't think "this 'a could be unit" is a good reason for skipping 
> the warning. On the contrary, "this 'a will probably sometimes be a 
> real value" seems a sufficient reason to add a warning. If needed, the 
> programmer can easily suppress the warning by adding "ignore" or 
> adding a ": unit" type constraint (whichever applies).
    I believe that compiler for statically-typed languages has to take 
all the responsibility on type information it delivers to user. So when 
it says "this
expression has type X, but here is used as type unit" it must not mean 
"well, sometimes it hasn't" :)
> (Remains to see whether adding the warning to OCaml is worth the 
> manwork.)
    IMHO it definitely doesn't deserve such a long discussion :))))

    BR,
    Dmitri.


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

* Re: Re : [Caml-list] Warning wished
  2009-01-28 14:24   ` Julien SIGNOLES
@ 2009-01-28 20:56     ` Nicolas Pouillard
  0 siblings, 0 replies; 10+ messages in thread
From: Nicolas Pouillard @ 2009-01-28 20:56 UTC (permalink / raw)
  To: Julien SIGNOLES; +Cc: Matthieu Wipliez, caml-list

Excerpts from Julien SIGNOLES's message of Wed Jan 28 15:24:14 +0100 2009:
> Le mercredi 28 janvier 2009 à 14:07 +0000, Matthieu Wipliez a écrit :
> > > Hello,
> > > 
> > > Is it a bug or a well-known feature that the above program does not emit
> > > a warning (because "f x" should have type unit in the body of "g") ?
> > > 
> > > =====
> > > let f x = x
> > > let g x = f x; 1
> > > (* let _ = g 2 *)
> > > ====
> > 
> > I'm not familiar with the internals of the compiler, but what I suppose is happening is that it tries to unify the type of "f x" and "unit", and this succeeds because "f x" has type 'a, which can be unified with anything. The meaning of "a;b" seems to be let _ = a in b rather than let () = a in b.
> > 
> > But like I said, these are suppositions.
> 
> Sure. However in my real case, I had an "ok" code like this :
> 
> let f x : 'a -> unit = ...
> let g x =
>   f x;
>   x
> 
> I changed a little bit the specification of f :
> 
> let f x : 'a -> 'a = ...
> 
> I just expected that caml helps me to find all the instances where I had
> to take care of this specification change. Such a thing is one of the
> very good features of caml. Unfortunatly, in this case, caml did not
> help me while the old implementation of "g" became wrong with the new
> specification of "f" :-(.

That's because (let f x : 'a -> 'a = ...) is not a specification for a
polymorphic function, this is rather an existential quantification than a
universal one.

They are some ugly tricks to ensure a polymorphic function, like this one:

module M : sig
  val f : t -> 'a -> 'a
end = struct
  let f x = ...
end
include M

Best regards,

-- 
Nicolas Pouillard


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

end of thread, other threads:[~2009-01-28 20:57 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-28 13:53 Warning wished Julien SIGNOLES
2009-01-28 14:07 ` Re : [Caml-list] " Matthieu Wipliez
2009-01-28 14:24   ` Julien SIGNOLES
2009-01-28 20:56     ` Nicolas Pouillard
2009-01-28 14:10 ` Dmitri Boulytchev
2009-01-28 14:28   ` Julien SIGNOLES
2009-01-28 14:44     ` Dmitri Boulytchev
2009-01-28 18:29       ` Frédéric van der Plancke
2009-01-28 19:08         ` Dmitri Boulytchev
2009-01-28 16:55 ` Martin Jambon

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