From: John Prevost <jmp@arsdigita.com>
To: Remi VANICAT <remi.vanicat@labri.u-bordeaux.fr>
Cc: caml-list@inria.fr
Subject: Re: bottom types and threaded exits
Date: 25 Sep 2000 14:38:25 -0400 [thread overview]
Message-ID: <87em28e2qm.fsf@localhost.localdomain.> (raw)
In-Reply-To: Remi VANICAT's message of "25 Sep 2000 12:08:50 +0200"
>>>>> "rv" == Remi VANICAT <remi.vanicat@labri.u-bordeaux.fr> writes:
rv> Julian Assange <proff@iq.org> writes:
>> Pervasives.exit is of type int -> 'a
>>
>> Here we see ocaml using 'a to represent _|_. This hack is
>> presumably so type unification still works in the face of
>> potentially non-terminating computations, e.g:
>>
>> let f a = try f a with Failure _ -> exit(1)
>>
>> How can one force 'a? For instance, Thread.exit and
>>
>> let f () = while true do () done
>>
>> has a type of unit -> unit.
>>
>> One can write something such as
>>
>> let f () = while true do () done ; Pervasives.exit (1)
>>
>> But this is clearly a hack.
>From what I can tell, what you're trying to do here is just make the
result type of a non-terminating computation go to 'a. This is
actually quite easy--you just need to loop in a way that doesn't
constrain the type. while ... do ... done always returns unit (which
is good, since usually you use it in a way that terminates.
The appropriate way to do it that will get type 'a is (in your case):
let rec f () = f ()
Note that if you choose the more direct translation
let rec f () = if true then f ()
you get constrained to type unit -> unit again, since a one-branched
if returns unit for the else branch.
So, an example of a vaguely useful function would be:
let rec call_forever f x = f x; call_forever f x
which has type:
val call_forever : ('a -> 'b) -> 'a -> 'c
So, it is not so much that 'a as bottom is a hack, as that the system
only produces such types when the result type is totally
unconstrained--and that doesn't happen with things like while loops,
only with recursion--where a function always returns the result of
calling itself (or another such function.)
John.
next prev parent reply other threads:[~2000-09-26 19:16 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-09-23 14:25 Julian Assange
2000-09-24 19:14 ` Pierre Weis
2000-09-25 10:08 ` Remi VANICAT
2000-09-25 18:38 ` John Prevost [this message]
2000-09-26 9:16 ` Xavier Leroy
2000-09-30 9:12 ` Julian Assange
2000-09-30 14:25 ` Pierre Weis
2000-09-30 15:19 ` Markus Mottl
2000-09-30 14:28 ` Stefan Monnier
2000-10-02 13:11 ` Patrick M Doane
2000-10-02 16:28 ` Markus Mottl
2000-10-05 18:39 ` Pierre Weis
2000-09-25 19:56 Damien Doligez
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87em28e2qm.fsf@localhost.localdomain. \
--to=jmp@arsdigita.com \
--cc=caml-list@inria.fr \
--cc=remi.vanicat@labri.u-bordeaux.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox