caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@ozemail.com.au>
To: Brian Hurt <bhurt@spnz.org>
Cc: Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] GC and file descriptors
Date: 19 Nov 2003 02:12:03 +1100	[thread overview]
Message-ID: <1069168323.18363.83.camel@pelican> (raw)
In-Reply-To: <Pine.LNX.4.44.0311171422420.5009-100000@localhost.localdomain>

On Tue, 2003-11-18 at 08:20, Brian Hurt wrote:
> On 18 Nov 2003, skaller wrote:
> 

> > But it is irrelevant, because programmers do not intend to write
> > arbitrary code. They intend to write comprehensible code, whose
> > correctness is therefore necessarily easy to prove.
> 
> What they intend to do and what they do do are often completely different.  
> It is a rare programmer who *intentionally* adds bugs to his code.  Yet 
> bugs still happen.

Of course. But my point is that a lot of the arguments
I have seen that 'we cannot do that because the general
problem is NP-complete' are irrelevant.

> But my point here is that determining the *exact* time that an object 
> becomes garbage can be arbitrarily complex.  Most of the time, I agree, it 
> won't be.  

The point is though: when it matters, the onus is on
the programmer to make sure the design is able to 
determine an 'exact enough' time, using whatever
tools the language may provide.

In this case the problem is *necessarily* solvable
(the assumption being the programmer checked that it
could be done before starting).

So we don't need something that determines when
*arbitrary* objects can't be refered to synchronously,
we need something that will do that job only in
cases that are simple enough to base a design
on it.

An obvious candidate is a pure stack protocol
(automatic store) and another is a hierarchical 
system (ref count).

When there are circularities .. due to the complexity
it would be unwise to count on synchronous finalisation,
since it isn't clear when that is (by definition it
is rather hard to determine ..)

> Once you allow for the fact that finalizers/destructors may not happen in 
> a defined order or at defined times, why not go whole-hog?  

Hard question. One job a language is often required to do
is emulate the behaviour of a program written in another
language .. that's not always easy to do when the object,
type, allocation or execution models are different.


> In the most intractible cases, neither the compiler nor the programmer may 
> be able to determine when the last reference is released.  But even in the 
> simple cases, the programmer may have the intent to release the resources 
> and simply doesn't.  It's called a bug.  

I agree. Bugs happen. Perhaps I can give an example:
in Java, variables must be initialised. Unlike Ocaml though,
they do not have to be initialised at the point of
declaration. Java requires instead 'manifest initialisation':
its a compromise between initialisation at the point
of declaration like Ocaml, or arbitrary initialisation
(onus on programmer) as in C++.

The language picked a constraint that is easy to check
for both the compiler and for humans which is more
flexible than the Ocaml model, but still assures
freedom from unitialised variable errors. It still
can't handle complex cases, which can be done in C++,
but they're rarely needed because if they seem
to be needed there is a good chance the design
is flawed.

> > 
> > However, two things spring to mind. First: if close is a primitive
> > operation, we need a high level way of not only tracking
> > what to close, but also of doing that tracking efficiently.
> > 
> > Secondly, since the resource is represented in memory,
> > and much of the time the dependencies which are *not*
> > represented in memory could be, using the code
> > of the garbage collector (and not just the same algorithm),
> > makes some sense.
> 
> Why special-case close?  

I wan't intending to, just picked an example to talk about.

> It's what we've been talking about, but it could 
> just as easily be "release a mutex", etc.

Agree.


> > You are making an incorrect assumption here. You're assuming
> > that finalisers only release resources. Consider again
> > the case where the final action in generating a document
> > is to create a table of contents (know nnow that all chapters
> > are seen).

> I wouldn't put that into a destructor.  DON'T USE DESTRUCTORS FOR 
> ENFORCING THE ORDER OF OPERATIONS. 

Normally I wouldn't. But here there was no choice.
You have to understand first that Interscript is a library
with a driver program that looks like this:

	read a line
	if at end of file, delete user space else
	if the line starts with @ execute it
	otherwise if we're in tangle mode 
		write to current source file
	otheriwse we have to be in document mode
		write to current document
	repeat


Now, the user code looks like this:

	@h = tangler('src/x.ml')
	@select(h)
	print_endline "I'm an ocaml program"
	<EOF>

Do you see the problem? The open file lives
in USER space. The language syntax does not
require the user close open files, because
that would leave a dangling reference.

But the engine has no idea which things
are files that need closing, and which
are documents that need tables of contents.

The ONLY way to finalise
each object correctly is in the destructor
method (__del__ method). I could add a 
'finalise' method to each object and call it
but that would not help -- the __del__
method is precisely that anyhow,
and it gets invoked automatically so it
relieves me of the task of finding
all the objects.

You can see that the problem arises
from a design decision not to require
the USER to close files.

> If it matters when the buffers are flushed, manually flush the buffers.  

See above. It isn't always possible: the program doesn't
always know which files are open. This is quite typical
in object oriented programs -- the program doesn't know
what kinds of objects exist, there is no master
that can call the 'finalise' method.

In C++ code, particularly GUI code, callbacks often
lead to suicide 'delete this' simply because there
is no one around to delete an object.


> > 
> > On thing is for sure .. I *hate* seeing
> > "program terminated with uncaught "Not_found" exception"
> > because I do hundreds of Hashtbl.find calls....

> There is a religous debate between returning 'a and throwing an exception 
> if the item isn't found, or returning 'a option.  

I don't think it is religious. There is a genuine problem here.
I do not know how to state it exactly, but I'll try.

Checking error codes and poping the stack is not only
tedious and obscures the normal control flow,
it does so to such an extent in certain cases
as to be totally and unequivocably out of the question.
For example in math calculations you simply cannot
afford to check every single function call either
before or after invocation:

	x = (a + b/c ^ d)

for example would turn into many lines of spagetti.
I will say:

	"the error detection is too localised"

for this kind of code. On the other hand the
uncaught exception of dynamic EH clearly
shows that that mechanism leads to

	"the error detection is too global"

What alternatives are there? 

One is to  have exception specifications on functions,
but that is known not to work very well. The first
difficulty is that once you have them, they must
become part of the type system. They then 'snowball'
through the whole program (in the same way 'const'
does to C programs).

It isn't possible to deduce what exceptions can
be thrown when functions are passed as arguments
to functions, so this pollution of the type system
would also be manifest in code in the form
of annotations .. basically higher order functions
would be screwed completely by this.

So exception specifications are out for
'engineering' reasons.

Another alternative is static exception handling.
I tried that in Felix. It is very good for a
wide class of problems. Static EH implies
block structured code with the handler visible
from the throw point... its really a structured goto.

This solves many cases where we really wanted
'alternate control flow constructs' rather
than error handling, but not all. And it doesn't
work where a more dynamic transmission of
error notifications is required.

What else? Continuations? Can monads be used?
We really do need a mechanism with better
locality (easily control scope).


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2003-11-18 16:12 UTC|newest]

Thread overview: 95+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-13  0:50 Dustin Sallings
2003-11-13  1:18 ` David Fox
2003-11-13  4:09   ` Dustin Sallings
2003-11-14 13:42     ` Damien Doligez
2003-11-14 14:57       ` Christophe Raffalli
2003-11-14 20:24         ` Dmitry Bely
2003-11-14 20:54           ` Eric Dahlman
2003-11-14 22:21             ` Brian Hurt
2003-11-14 21:36               ` John J Lee
2003-11-14 21:48           ` Brian Hurt
2003-11-15  1:47             ` Dmitry Bely
2003-11-15  2:25           ` Max Kirillov
2003-11-15  2:49             ` Mike Furr
2003-11-16  4:09               ` [Caml-list] Bugs from ignoring errors from close (was Re: GC and file..) Tim Freeman
2003-11-15  2:58             ` [Caml-list] GC and file descriptors David Brown
2003-11-17 14:19         ` Damien Doligez
2003-11-17 18:18           ` skaller
2003-11-14 18:35       ` Dustin Sallings
2003-11-15 14:16         ` skaller
2003-11-15 15:56           ` Ville-Pertti Keinonen
2003-11-15 17:30             ` skaller
2003-11-15 20:31               ` Martin Berger
2003-11-16 19:19               ` Brian Hurt
2003-11-17 18:15                 ` skaller
2003-11-17 19:26                   ` Aleksey Nogin
2003-11-18 13:49                     ` skaller
2003-11-18 17:51                       ` Dustin Sallings
2003-11-18 20:17                       ` Aleksey Nogin
2003-11-20  7:36                         ` Florian Hars
2003-11-17 21:20                   ` Brian Hurt
2003-11-17 23:02                     ` John J Lee
2003-11-18 12:05                     ` Ville-Pertti Keinonen
2003-11-18 15:19                       ` skaller
2003-11-18 18:10                         ` John J Lee
2003-11-18 17:55                           ` skaller
2003-11-18 20:02                         ` Ville-Pertti Keinonen
2003-11-18 21:20                           ` John J Lee
2003-11-19 12:25                           ` skaller
2003-11-19 13:55                             ` Ville-Pertti Keinonen
2003-11-19 14:26                               ` Samuel Lacas
2003-11-19 14:47                               ` skaller
2003-11-18 15:28                       ` skaller
2003-11-18 18:00                       ` John J Lee
2003-11-18 22:28                       ` Brian Hurt
2003-11-18 23:07                         ` John J Lee
2003-11-18 23:22                         ` Benjamin Geer
2003-11-19  1:49                         ` Martin Berger
2003-11-19  3:57                           ` Dustin Sallings
2003-11-19 13:35                           ` skaller
2003-11-19 13:00                         ` skaller
2003-11-19 13:02                         ` skaller
2003-11-19 17:36                           ` Brian Hurt
2003-11-20  5:14                             ` skaller
2003-11-20  7:37                               ` David Brown
2003-11-18 15:12                     ` skaller [this message]
2003-11-18 16:49                       ` Martin Berger
2003-11-18 17:46                         ` skaller
2003-11-19  1:33                           ` Martin Berger
2003-11-19  3:19                             ` Design by Contract, was " Brian Hurt
2003-11-19  2:57                               ` Jacques Carette
2003-11-19 13:27                             ` skaller
2003-11-19 14:41                               ` Martin Berger
2003-11-19 16:54                             ` Richard Jones
2003-11-19 17:18                               ` Damien Doligez
2003-11-19 21:45                                 ` Richard Jones
2003-11-19 23:09                                   ` Benjamin Geer
2003-11-20  0:50                                     ` Nicolas Cannasse
2003-11-20  9:42                                       ` Benjamin Geer
2003-11-19 18:03                               ` Martin Berger
2003-11-18 18:26                         ` Benjamin Geer
2003-11-18 19:24                           ` Xavier Leroy
2003-11-18 23:49                             ` Benjamin Geer
2003-11-19  1:36                             ` Martin Berger
2003-11-19  2:28                               ` Nicolas Cannasse
2003-11-19  3:26                               ` Brian Hurt
2003-11-19 11:44                                 ` Martin Berger
2003-11-19 17:29                                   ` Brian Hurt
2003-11-20  5:17                                     ` skaller
2003-11-20 16:13                                       ` Brian Hurt
2003-11-19 13:33                               ` skaller
2003-11-19 17:01                                 ` Richard Jones
2003-11-22  2:39                                   ` [Caml-list] AutoMLI (Was: GC and file descriptors) Jim
2003-11-19 17:43                                 ` [Caml-list] GC and file descriptors Brian Hurt
2003-11-20  5:05                                   ` skaller
2003-11-19  1:33                           ` Martin Berger
2003-11-19  2:47                             ` Benjamin Geer
2003-11-18 22:23                       ` Brian Hurt
2003-11-19 13:00                         ` skaller
2003-11-17 22:37                   ` OCaml popularity [was: Re: [Caml-list] GC and file...] John J Lee
2003-11-18  1:02                   ` [Caml-list] Re: GC and file descriptors Jed Davis
2003-11-13  1:19 ` [Caml-list] " Nicolas George
     [not found] ` <87smkstkhg.fsf@igloo.phubuh.org>
     [not found]   ` <347A7A46-1612-11D8-8F93-000393CFE6B8@spy.net>
2003-11-13 20:18     ` Mikael Brockman
     [not found] <20031118232227.GA8437@swordfish>
     [not found] ` <Pine.LNX.4.44.0311182039440.5009-100000@localhost.localdomain>
2003-11-20  6:35   ` Matt Gushee
2003-11-21 16:44     ` skaller
2003-11-21 22:17 Gregory Morrisett

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=1069168323.18363.83.camel@pelican \
    --to=skaller@ozemail.com.au \
    --cc=bhurt@spnz.org \
    --cc=caml-list@inria.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;
as well as URLs for NNTP newsgroup(s).