comp.lang.ada
 help / color / mirror / Atom feed
From: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Subject: Re: Exceptions as objects (was Re: What is wrong with OO ?)
Date: 1997/02/09
Date: 1997-02-09T00:00:00+00:00	[thread overview]
Message-ID: <5djrvm$crh@mulga.cs.mu.OZ.AU> (raw)
In-Reply-To: 5de57h$dm3$1@goanna.cs.rmit.edu.au


ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe) writes:

>I wrote:
>>>In fact, the programmer can _not_ implement dynamic scoping easily by
>>>hand in Ada and thanks to the arcane scope rules of C++ (from time to
>>>time I reread the draft C++ standard trying to find out what the scope
>>>rules _are_, does _anyone_ know?) it is even less true there.
>
>fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson) replied fliply:
>>To answer your parenthetical question, the scope rules are defined in
>>sections 3.3 [basic.scope] and 3.4 [basic.lookup] of the draft.
>
>Get real!

OK, that _was_ a bit flip.  You're right that the scope rules of C++
can be rather arcane.

>fjh proposes the following sketch of implementing a dynamically
>scoped variable in C++:
>
>>	#include "dynamic_scope.h"
>>	
>>	dynamically_scoped<int> x;
>
>>	void foo() {
>>		rebind<x> local_x;	// or something like that
>>	}
>
>Close, but not quite.  (This approach is also applicable in Ada 95.)
>
>What's wrong with it?  x has the wrong type.

You can give `x' an implicit conversion to type "reference to int".

	operator int & ();

Now, due to C++'s arcane overloading rules ;-), this is not
exactly the same in all circumstances, but it usually does
the trick.  Sometimes you may have to insert a few additional
explicit conversions.

(My compiler seems to think that you need an assignment operator
as well.  Maybe it's right.)

>In languages that have them, dynamically scoped variables follow different
>_scope_ rules from other variables, but there is nothing special about
>their _contents_.  It would be necessary to do something like
>
>	int x;
>
>	void foo() {
>	    rebind< &x > preserved_x;
>	    ...
>	}

You could do that too, and it would work fine in the case of `int'.
However, for other types that has some disadvantages.
It won't work if type of `x' doesn't allow copying.
Even if `x' can be copied, you will most likely have to do a deep copy,
which may be slow.  That's why I went for the approach with
the implicit conversion-to-reference instead.

>An "implementation" of
>dynamically scoped variables which depends on giving them the wrong
>type (your approach) _does_ impose a heavy burden on the programmer,
>because then every reference that would have been a reference to x
>has to become x.contents() or whatever you want to call it.

No, that's not correct, because you can use an implicit conversion.

>>>It is also likely to be
>>>seriously less efficient than a reasonable mechanism implemented by the
>>>compiler.
>
>>I'm not convinced.
>
>Why not.  Do you actually know any C++ compilers which make your approach
>run as fast as a good Lisp system (Harlequin, Franz, CMUCL)?  The obvious
>mechanism is essentially a Prolog-style trail.

Well, with the implementation I was thinking of, block entry and block
exit are likely to quite cheap.  The main cost of my C++ implementation
is that you have an extra indirection for each access to the variable.
But I think it will be at least competitive.

	template <class T> class dynamically_scoped;

	template <class T, dynamically_scoped<T> & ds_var> class rebind;

	template <class T>
	class dynamically_scoped {
		template <dynamically_scoped<T> & ds_var>
		friend class rebind<T, ds_var>;
		T* current_var;
		T original_var;
	public:
		dynamically_scoped() {
			current_var = &original_var;
		}
		dynamically_scoped<T>& operator = (const T &val) {
			*current_var = val;
			return *this;
		}
		operator T& () {
			return *current_var;
		}
	};

	template <class T, dynamically_scoped<T> & ds_var>
	class rebind {
		T new_var;
		T* prev_var;
	public:
		rebind() {
			prev_var = ds_var.current_var;
			ds_var.current_var = &new_var;
		}
		~rebind() {
			ds_var.current_var = prev_var;
		}
	private:
		// prevent copying
		rebind(const rebind<T, ds_var>&);
		void operator =(const rebind<T, ds_var>&);
	};

If you're willing to assume that the variable can be cheaply copied,
you can do better.  Here's an alternate implementation that has
performance characteristics that are more similar to your suggested
implementation:

	template <class T> class dynamically_scoped;

	template <class T, dynamically_scoped<T> & ds_var> class rebind;

	template <class T>
	class dynamically_scoped {
		template <dynamically_scoped<T> & ds_var>
		friend class rebind<T, ds_var>;

		T current_value;
	public:
		dynamically_scoped<T>& operator = (const T & x) {
			current_value = x;
			return *this;
		}
		operator T& () {
			return current_value;
		}
	};

	template <class T, dynamically_scoped<T> & ds_var>
	class rebind {
		T old_value;
	public:
		rebind() {
			old_value = ds_var.current_value;
		}
		~rebind() {
			ds_var.current_value = old_value;
		}
	private:
		// prevent copying
		rebind(const rebind<T, ds_var>&);
		void operator =(const rebind<T, ds_var>&);
	};

>Block entry:
>	for each dynamic variable x
>	    push contents of x onto Trail
>	push block descriptor onto Trail
>
>Block exit:
>	discard block descriptor from Trail
>	for each dynamic variable x in reverse order
>	    pop contents of x from Trail

With the second implementation above, block entry and exit are
in fact slightly cheaper:

  Block entry:
	for each dynamic variable x
	   copy (not push) contents of x onto stack

  Block exit:
	for each dynamic variable x
	   copy (not push) contents of x from stack

I tried a simple test of dynamically_scoped<int>.  My compiler (SGI
C++) generated one instruction to put the address of x in a register,
and then a load/store pair for block entry and a load/store pair for
block exit.

By storing the trail on the stack, you save having to increment the
trail pointer (the stack pointer increment is usually free, since you
normally have to increment it anyway).  You also reduce the danger
of cache collisions, although that's probably not a noticable effect.
Furthermore, unlike say Prolog, you pay the cost of trailing and
untrailing only when you're actually using it (you don't have
any untrail loops).  The downside is that you can't get tail recursion
optimization.

Most modern Unix C++ implementations use separate code address tables for
exception handling, so there's no need to push any descriptors.
(PC implementations are another matter, though -- they do normally
push and pop stuff.  I think that's mostly Microsoft's fault.)

>There are three things to be said about this:
>
>(1) it is hard to see how to do _substantially_ better than this.

Yes, but as shown above you can do slightly better, and in any case all
you really need is to not do substantially worse.

>(2) the exception unwinder uses block descriptors, it is hard to
>    see how such a _global_ mechanism can be efficiently simulated
>    by _local_ classes.

Well, the local C++ classes are making use of the underlying C++
exception handling mechanism, which could use a global block descriptor
stack or separate code address tables or some other global mechanism.

>(3) Lisp systems enforce certain _global_ restrictions on the use
>    of dynamic binding.  fjh's mechanism can simulate to some degree
>    what Lisp _allows_ but not what it _forbids_.

Could you be more specific about the restrictions that Lisp systems
enforce?

You can enforce quite a bit in C++.

>None of this should be taken as endorsing the addition of dynamic binding
>to any specific language.

Yes, and the fact that you *can* implement dynamically scoped variables
in C++ does not mean that you necessarily *should* ;-)

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.




  parent reply	other threads:[~1997-02-09  0:00 UTC|newest]

Thread overview: 260+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-01-01  0:00 What is wrong with OO ? Ell
1997-01-01  0:00 ` Jon S Anthony
1997-01-02  0:00   ` Robert C. Martin
1997-01-03  0:00     ` Eirik Mangseth
1997-01-03  0:00       ` What is wrong with OO O X g Greg Comeau
1997-01-04  0:00         ` OO, C++, and something much better! John (Max) Skaller
1997-01-04  0:00           ` vlad
1997-01-05  0:00           ` Mike Anderson
1997-01-06  0:00           ` Stanley Allen
1997-01-09  0:00             ` Jon S Anthony
1997-01-06  0:00           ` Chris Brand
1997-01-08  0:00     ` Robert I. Eachus
1997-01-09  0:00     ` Bertrand Meyer
1997-01-27  0:00       ` Richard Riehle
1997-01-10  0:00     ` Robb Nebbe
1997-01-10  0:00     ` Robert I. Eachus
1997-01-03  0:00   ` What is wrong with OO ? Jon S Anthony
1997-01-03  0:00   ` Matt Austern
1997-01-04  0:00   ` OO, C++, and something much better! Pieter Schoenmakers
1997-01-04  0:00   ` What is wrong with OO ? Valerie Torres
1997-01-06  0:00     ` Bart Samwel
1997-01-08  0:00       ` Robert Dewar
1997-01-09  0:00         ` Fergus Henderson
1997-01-09  0:00           ` Robert Dewar
1997-01-10  0:00             ` Fergus Henderson
1997-01-10  0:00               ` Robert Dewar
1997-01-09  0:00           ` Richie Bielak
1997-01-10  0:00             ` Fergus Henderson
1997-01-09  0:00         ` Ken Garlington
1997-01-09  0:00         ` Simon Willcocks
1997-01-09  0:00           ` Robert Dewar
1997-01-09  0:00         ` William Clodius
1997-01-09  0:00         ` Bertrand Meyer
1997-01-09  0:00           ` Robert Dewar
1997-01-10  0:00             ` Fergus Henderson
1997-01-10  0:00               ` Robert Dewar
1997-01-12  0:00                 ` Fergus Henderson
1997-01-09  0:00           ` Jay Martin
1997-01-09  0:00             ` Robert Dewar
1997-01-10  0:00               ` Fergus Henderson
1997-01-10  0:00                 ` Robert Dewar
1997-01-12  0:00                   ` Fergus Henderson
1997-01-12  0:00                     ` Robert Dewar
1997-01-10  0:00                 ` Ken Garlington
1997-01-10  0:00               ` Jay Martin
1997-01-12  0:00                 ` Robert Dewar
1997-01-15  0:00                   ` Laurent Gasser
1997-01-15  0:00                     ` Jay Martin
1997-01-15  0:00                     ` Jonas Nygren
1997-01-17  0:00                       ` Tom Bushell
1997-01-17  0:00                         ` Michael Malak
1997-01-17  0:00                           ` Kent Budge, sandia, 
1997-01-17  0:00                         ` Eirik Mangseth
1997-01-10  0:00               ` Jay Martin
1997-01-10  0:00                 ` Joe Buck
1997-01-11  0:00                   ` Jay Martin
1997-01-12  0:00             ` Slavik Zorin
1997-01-09  0:00           ` Ken Garlington
1997-01-11  0:00           ` Piercarlo Grandi
1997-01-12  0:00             ` Thierry Goubier
1997-01-14  0:00               ` Piercarlo Grandi
1997-01-14  0:00             ` Vos nom et pr�nom
1997-01-16  0:00               ` Mark Woodruff
1997-01-17  0:00               ` Piercarlo Grandi
1997-01-10  0:00         ` Bart Samwel
1997-01-10  0:00           ` Michael Malak
1997-01-10  0:00             ` Bart Samwel
1997-01-12  0:00               ` Fergus Henderson
1997-01-10  0:00           ` Robert Dewar
1997-01-09  0:00       ` Bjarne Stroustrup
1997-01-11  0:00         ` Robert Dewar
1997-01-15  0:00           ` Bjarne Stroustrup
1997-01-19  0:00             ` Jay Martin
1997-01-27  0:00               ` Robert C. Martin
1997-01-30  0:00                 ` Damon Feldman
1997-01-20  0:00             ` Richard A. O'Keefe
1997-01-21  0:00               ` John W. Sarkela
1997-01-23  0:00               ` Piercarlo Grandi
1997-01-23  0:00             ` Bertrand Meyer
1997-01-24  0:00               ` language marketing question, was " Tom Moran
     [not found]                 ` <5cpdh8$mau@news.csus.edu>
     [not found]                   ` <5cs3un$14b4@uni.library.ucla.edu>
     [not found]                     ` <rmartin-3101971753460001@pool15-027.wwa.com>
     [not found]                       ` <5cu5ig$10fk@uni.library.ucla.edu>
     [not found]                         ` <5dao0p$gri@decius.ultra.net>
     [not found]                           ` <yf3n2th488t.fsf@sabi.demon.co.uk>
1997-02-11  0:00                             ` Worse is better, was: Language marketing question Bob Haugen
1997-02-11  0:00                               ` Donald M. MacQueen
1997-02-11  0:00                                 ` Lee Willis
     [not found]                         ` <yf3rait49b9.fsf@sabi.demon.co.uk>
     [not found]                           ` <01bc183b$fd091820$1544db03@gecmf-pc-eddjab.gecmf.capital.ge.com>
1997-02-13  0:00                             ` language marketing question, was Re: What is wrong with OO ? Robert Dewar
1997-01-25  0:00               ` Damon Feldman
1997-01-26  0:00             ` Sean Case
1997-01-26  0:00               ` William Grosso
1997-01-28  0:00                 ` Paul Keister
1997-01-28  0:00               ` Dann Corbit
1997-01-12  0:00         ` Matt Telles
1997-01-15  0:00           ` Bjarne Stroustrup
1997-01-19  0:00             ` Matthew Heaney
1997-01-12  0:00         ` Fergus Henderson
1997-01-13  0:00           ` Bart Samwel
1997-01-14  0:00         ` Vos nom et pr�nom
1997-01-16  0:00           ` Patrick Doyle
1997-01-16  0:00             ` Risto Lankinen
1997-01-16  0:00               ` Patrick Doyle
1997-01-16  0:00                 ` Risto Lankinen
1997-01-18  0:00                 ` Robert C. Martin
     [not found]           ` <01bc0269$3fd55b20$ca61e426@DCorbit.solutionsiq.com>
1997-02-10  0:00             ` richard
1997-02-10  0:00               ` Robert Dewar
1997-02-10  0:00               ` Nick Leaton
     [not found]                 ` <3303A993.759E@pratique.fr>
1997-02-21  0:00                   ` Nick Leaton
1997-02-21  0:00                   ` Nick Leaton
1997-02-22  0:00                     ` Fergus Henderson
1997-01-20  0:00         ` David Emery
     [not found]       ` <dewar.852772995@mer <dewar.852833957@merv>
1997-01-10  0:00         ` Simon Willcocks
1997-01-10  0:00           ` Robert Dewar
1997-01-10  0:00             ` Marky Mark
1997-01-10  0:00               ` Robert Dewar
1997-01-12  0:00                 ` Fergus Henderson
1997-01-12  0:00                   ` Josh Stern
1997-01-12  0:00             ` Martin ELLISON
1997-01-14  0:00               ` Piercarlo Grandi
1997-01-17  0:00     ` Lawrence G. Mayka
1997-01-19  0:00       ` Piercarlo Grandi
1997-01-19  0:00         ` Exceptions as objects (was Re: What is wrong with OO ?) Patrick Doyle
1997-01-25  0:00           ` Piercarlo Grandi
1997-01-25  0:00             ` Ronald E Jeffries
1997-01-25  0:00               ` Robert A Duff
1997-01-28  0:00                 ` Piercarlo Grandi
1997-01-29  0:00                   ` John W. Sarkela
     [not found]                     ` <yf33evav882.fsf@sabi.demon.co.uk>
     [not found]                       ` <32FA58AD.2D96@enfish.com>
1997-02-12  0:00                         ` Piercarlo Grandi
1997-01-29  0:00                   ` Robert A Duff
1997-01-30  0:00                   ` Lawrence G. Mayka
1997-01-30  0:00                     ` Robert Dewar
1997-01-29  0:00                 ` John (Max) Skaller
1997-01-25  0:00               ` Bryan Dollery
1997-01-28  0:00               ` Piercarlo Grandi
1997-01-25  0:00             ` Paul Kyzivat
1997-01-25  0:00             ` Robert A Duff
1997-01-25  0:00               ` Robert A Duff
1997-01-28  0:00                 ` Piercarlo Grandi
1997-01-28  0:00                 ` Piercarlo Grandi
     [not found]             ` <5cu43v$jkn@nntpa.cb.lucent.com>
     [not found]               ` <yf3ybd14qn4.fsf@sabi.demon.co.uk>
     [not found]                 ` <5de797$1ksa@uni.library.ucla.edu>
1997-02-07  0:00                   ` Piercarlo Grandi
1997-02-08  0:00                     ` Jay Martin
     [not found]               ` <5d93d3$nhs$1@goanna.cs.rmit.edu.au>
1997-02-07  0:00                 ` Robert A Duff
     [not found]                 ` <5dds5b$gcs@mulga.cs.mu.OZ.AU>
     [not found]                   ` <01bc14ab$3ce476e0$752d54c7@vbykov.hip.cam.org>
     [not found]                     ` <5def36$rjd@mulga.cs.mu.OZ.AU>
1997-02-07  0:00                       ` Vassili Bykov
1997-02-07  0:00                         ` Bill Gooch
1997-02-07  0:00                           ` Vassili Bykov
1997-02-08  0:00                         ` Fergus Henderson
1997-02-08  0:00                           ` Piercarlo Grandi
1997-02-08  0:00                           ` Piercarlo Grandi
1997-02-11  0:00                           ` Vassili Bykov
1997-02-07  0:00                     ` Robert A Duff
1997-02-10  0:00                       ` Vassili Bykov
1997-02-07  0:00                     ` Patrick Doyle
     [not found]                   ` <5de57h$dm3$1@goanna.cs.rmit.edu.au>
1997-02-09  0:00                     ` Fergus Henderson [this message]
1997-01-06  0:00   ` OO, C++, and something much better! Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-01-06  0:00     ` Michael F Brenner
1997-01-06  0:00       ` Tucker Taft
1997-01-07  0:00         ` Robert Dewar
1997-01-07  0:00       ` Ada Generics (was Re: OO, C++, and something much better!) Bob Gilbert
1997-01-07  0:00         ` Robert Dewar
1997-01-08  0:00       ` OO, C++, and something much better! Robert Dewar
1997-01-07  0:00     ` Andrew Koenig
1997-01-10  0:00       ` Norman H. Cohen
1997-01-07  0:00     ` Jay Martin
1997-01-08  0:00       ` Ken Garlington
1997-01-08  0:00         ` Robert Dewar
1997-01-08  0:00           ` Robert Dewar
1997-01-09  0:00           ` Ted Dennison
1997-01-12  0:00     ` Richard Riehle
1997-01-07  0:00   ` Stanley Allen
1997-01-07  0:00     ` Robert Dewar
1997-01-07  0:00       ` Bertrand Meyer
1997-01-08  0:00         ` Matthew Heaney
1997-01-08  0:00         ` Ken Garlington
1997-01-10  0:00       ` Keith Thompson
1997-01-10  0:00         ` Robert Dewar
1997-01-10  0:00           ` Robert Dewar
1997-01-15  0:00           ` Richard Kenner
1997-01-15  0:00             ` Fergus Henderson
1997-01-20  0:00             ` Andrew Koenig
1997-01-25  0:00             ` Robert Dewar
1997-01-15  0:00         ` Ole-Hjalmar Kristensen FOU.TD/DELAB
1997-01-07  0:00   ` What is wrong with OO ? Jon S Anthony
1997-01-11  0:00     ` Bjarne Stroustrup
1997-01-21  0:00       ` rharlos*cybercomm.net
1997-02-10  0:00       ` richard
1997-02-10  0:00         ` Charles A. Jolley
1997-02-11  0:00           ` Robert Dewar
1997-02-12  0:00             ` C++ Class Loc Minh Phan Van
1997-02-12  0:00               ` Kevin J. Hopps
1997-02-12  0:00               ` John Kapson [C]
1997-02-12  0:00               ` William Ying
1997-02-12  0:00               ` Ron Smith
1997-02-12  0:00               ` Paul Kenneth Egell-Johnsen
1997-02-12  0:00               ` Keith P. Boruff
1997-02-12  0:00                 ` Vlastimil Adamovsky
1997-03-03  0:00                 ` C++ Class [ not a good answer ] Luis Espinal
1997-03-06  0:00                   ` Tom Plunket
1997-02-12  0:00               ` C++ Class Jason Shankel
1997-02-12  0:00               ` Kent Tong
1997-02-12  0:00               ` Aaron J Margosis
1997-02-13  0:00               ` Lee, Shih Hao
1997-02-17  0:00             ` What is wrong with OO ? Sam Inala
1997-02-17  0:00               ` Robert Dewar
1997-02-15  0:00           ` Piercarlo Grandi
1997-02-11  0:00         ` Vlastimil Adamovsky
1997-01-07  0:00   ` Jon S Anthony
1997-01-08  0:00   ` OO, C++, and something much better! Jon S Anthony
1997-01-08  0:00     ` Mark A Biggar
1997-01-09  0:00       ` Don Harrison
1997-01-10  0:00         ` Roy Phillips
1997-01-27  0:00           ` Nick Leaton
1997-01-28  0:00             ` matthew mclellan
1997-01-09  0:00     ` Don Harrison
1997-01-09  0:00       ` Robert Dewar
1997-01-10  0:00         ` Bart Samwel
1997-01-10  0:00           ` Robert Dewar
1997-01-10  0:00         ` Ken Garlington
1997-01-10  0:00         ` Norman H. Cohen
1997-01-13  0:00         ` Don Harrison
1997-01-13  0:00           ` Robert Dewar
1997-01-14  0:00             ` Don Harrison
1997-01-13  0:00           ` Ken Garlington
1997-01-13  0:00             ` Robert Dewar
1997-01-15  0:00               ` Ken Garlington
1997-01-17  0:00                 ` Keith Thompson
1997-01-16  0:00               ` Keith Thompson
1997-01-16  0:00                 ` Ken Garlington
1997-01-13  0:00             ` Norman H. Cohen
1997-01-14  0:00             ` Michael F Brenner
1997-01-14  0:00             ` Don Harrison
1997-01-10  0:00       ` Jon S Anthony
1997-01-13  0:00         ` Don Harrison
1997-01-13  0:00           ` Don Harrison
1997-01-14  0:00             ` Jeff Carter
1997-01-15  0:00               ` Don Harrison
1997-01-17  0:00                 ` Norman H. Cohen
1997-01-17  0:00                 ` Jon S Anthony
1997-01-18  0:00             ` Patrick Doyle
1997-01-20  0:00             ` Jon S Anthony
1997-01-13  0:00           ` Robert I. Eachus
1997-01-15  0:00             ` Don Harrison
1997-01-08  0:00   ` David Emery
1997-01-09  0:00     ` Don Harrison
1997-01-10  0:00       ` Marky Mark
1997-01-14  0:00         ` Don Harrison
1997-01-08  0:00   ` Kohler Markus
1997-01-10  0:00   ` Matt Austern
1997-01-10  0:00   ` What is wrong with OO ? Pieter Schoenmakers
1997-01-12  0:00     ` Fergus Henderson
1997-01-10  0:00   ` Robert I. Eachus
1997-01-12  0:00     ` Piercarlo Grandi
1997-01-10  0:00   ` OO, C++, and something much better! Jon S Anthony
1997-01-12  0:00   ` What is wrong with OO ? Chris Morgan
1997-01-11  0:00     ` Robert Dewar
1997-01-12  0:00   ` Chris Morgan
1997-01-13  0:00   ` Chris Morgan
1997-01-13  0:00   ` Pieter Schoenmakers
1997-01-13  0:00     ` Fergus Henderson
1997-01-13  0:00   ` ak
1997-01-23  0:00   ` Bertrand Meyer
1997-01-26  0:00     ` Piercarlo Grandi
1997-01-23  0:00   ` Ulrich Windl
1997-01-23  0:00   ` Chris Bitmead
1997-01-08  0:00 ` Richard A. O'Keefe
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox