Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type "while true do ... done" specially #6833

Closed
vicuna opened this issue Apr 7, 2015 · 7 comments
Closed

Type "while true do ... done" specially #6833

vicuna opened this issue Apr 7, 2015 · 7 comments

Comments

@vicuna
Copy link

vicuna commented Apr 7, 2015

Original bug ID: 6833
Reporter: @johnwhitington
Status: acknowledged (set by @damiendoligez on 2015-06-15T15:22:02Z)
Resolution: open
Priority: normal
Severity: feature
Version: 4.02.1
Category: typing
Tags: patch
Monitored by: @diml

Bug description

It is common to (ab)use "assert false" in the following context, to avoid the, even uglier, construction of a thing-of-the-right-type (here, we could use the empty string ""):

let read_lines ch =
let b = Buffer.create 1024 in
try
while true do
Buffer.add_string b (input_line ch);
Buffer.add_string b "\n"
done;
assert false
with
End_of_file -> Buffer.contents b

Could this not be solved by special-casing the typing of the while construct in some fashion, so that in exactly the case "while true do ... done", the phrase has the same type as "assert false"? Then we could just have:

let read_lines ch =
let b = Buffer.create 1024 in
try
while true do
Buffer.add_string b (input_line ch);
Buffer.add_string b "\n"
done
with
End_of_file -> Buffer.contents b

Obviously the first example would still need to type-check, for backwards-compatibility with existing code.

File attachments

@vicuna
Copy link
Author

vicuna commented Apr 7, 2015

Comment author: @yallop

I like this idea. It fits well with the special typing of assert false. It'll break code that's over-zealous with -warn-error, but I don't think that's really anything to be concerned about.

I've attached a (trivial) patch implementing it.

@vicuna
Copy link
Author

vicuna commented Apr 21, 2015

Comment author: @damiendoligez

The special typing of assert false is not something to be proud of... I think a proper solution would be to introduce a new construct for infinite loops, but of course it'll be hard to get consensus on the syntax.

@vicuna
Copy link
Author

vicuna commented Apr 21, 2015

Comment author: @alainfrisch

I think a proper solution would be to introduce a new construct for infinite loops, but of course it'll be hard to get consensus on the syntax.

Here is a proposal:

let rec loop () =
begin end;
loop ()
in
loop ()

:-)

Bonus: type-checking already works as expected.

@vicuna
Copy link
Author

vicuna commented Apr 21, 2015

Comment author: @johnwhitington

The special typing of assert false is not something to be proud of...

With this proposal, "assert false" will be used less often, by fewer people. We replace the wart with a smaller wart, no?

I think a proper solution would be to introduce a new construct for infinite loops, but of course it'll be hard to get consensus on the syntax.

I suppose one could use an alternative syntax like "while _ do ... done", but then one can still write "while true do ... done", so we have two ways to do the same thing.

One disadvantage is that this proposal makes 'true' seem rather like a keyword, when it is really just a name for a value, albeit predefined.

@vicuna
Copy link
Author

vicuna commented Apr 21, 2015

Comment author: @alainfrisch

when it is really just a name for a value, albeit predefined

Technically, false is a constructor which can be used in a user-defined type:

type t = false of int | Foo | Bar;;
let x : t = false 42;;
assert false;; (* well typed, thanks to type-based selection of constructors )
let x : bool = false (
idem *)

@vicuna
Copy link
Author

vicuna commented Jun 15, 2015

Comment author: @damiendoligez

I suppose one could use an alternative syntax like "while _ do ... done", but then one can still write "while true do ... done", so we have two ways to do the same thing.

We already have an infinite number of ways to do the same thing:

while true && true do ... done
while true || true do ... done
while true || true || true do ... done

etc.

I rather like "while _ do ... done", or if you want to get fancy we could have "for ever do ... done".

@github-actions
Copy link

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant