In fact 'assert false' is explicitely replaced by 'raise Assert_failure(...)' by the parser.
See the documentation about that in the "Language extensions" section.

With any other argument than false, assert is considered to have type bool -> unit



Esther Baruk


On Wed, Aug 31, 2011 at 1:40 AM, Philippe Wang <mail@philippewang.info> wrote:
On Wed, Aug 31, 2011 at 12:31 AM, John Carr <jfc@mit.edu> wrote:
> # let f () = assert false; 0;;
> Warning 21: this statement never returns (or has an unsound type.)
> val f : unit -> int = <fun>

> Assert behaves like a function with type bool -> 'a.  The return type is
> an unconstrained type, a type variable not mentioned on the left hand
> side of the arrow.

Actually, assert is more like a function with type bool -> unit.
Indeed, (assert false) is a special case where it is "so obvious that
it'll raise an exception that it's made type 'a".

Notably, if you write
assert(1=2)
it's not "obvious enough" for the compiler to assume it will raise an
exception.
(Which is normal, by the way, because it's generally an undecidable
problem: in general, it's not possible to know that the evaluation of
assert's argument will return a value, especially when there is a
function application such as in "1=2".)

So,
in
let f () = assert false
f : unit -> 'a
let f () = assert (1=2)
f : unit -> unit

And while
let f () = assert false; 0;;
raises a warning,
let f () = assert (1=2); 0;;
won't raise any.

Somehow, the choice of discriminating (assert false) is not so perfect...

Cheers,

--
Philippe Wang


N.B. Sorry for double-replying.
I wonder if "reply-to: caml-list@inria.fr" field could be added
automatically to headers of all mails received by the list...


--
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs