comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: Why use C++?
Date: Sat, 13 Aug 2011 11:37:04 +0200
Date: 2011-08-13T11:37:04+02:00	[thread overview]
Message-ID: <4e4645c0$0$7629$9b4e6d93@newsspool1.arcor-online.net> (raw)
In-Reply-To: <j2407a$3qv$1@dont-email.me>

On 12.08.11 21:51, Jed wrote:

>> The general idea is that you start from the problem
>> without giving thought to representation.  Typically,
>> without loss of efficiency.  Compiler makers know a fair
>> bit about good choice of representation.  This means:
>> Describe precisely what matters to your program, on
>> any machine, without reference to any representation.
>> Then, and only then, and only if necessary, throw in your
>> knowledge of representation.
>
> That doesn't work when you do IO.


Specifying representation when needed, and separate  from
the type declaration, works perfectly well for I/O.
For I/O in particular!

Short summary:

Declaring a type for computation does *not* require specification
of representation.  However, should it become necessary to
specify a representation of objects of a type, the programmer
should be able to do so, but *not* by forcing the representation
into every existing type.


(Incidentally, bit-oriented, abstracting types (bit fields
in C, std::bitset in C++, bit arrays in D, or packed arrays of
Booleans in a host of Pascalish languages) don't make a
language 4GL.)

Examples of how omitting (implied) representation from type
declaration works well with I/O.

1. (I/O As-is) Suppose I/O means writing to a particular address.
There is an output port to write to.  It has CHAR_BIT bits,
and CHAR_BIT == 8.  You want to write an object of type long
which happens to consists of four octets.  One approach is to
write a function that shifts bits into a char object and then
pushes this object out the port.

Using the fundamental type system of C++, you need some size
computations, at compile time, or by the programmer, so that
the program will perform the right number of shifts, for a
given implementation.  With C++ and long, long[], etc, there
are idioms. They typically use the sizeof operator, and division.

The very same technique is available for objects declared of a
type 1e0 .. 1e5 that does not mention any other type's name!
The programmer may leave it to the compiler to choose a
representation for objects of type 1e0 .. 1e5 that is
best on the target hardware for computation.  For I/O, the
programmer again uses the type system and inquires of the type
how many octets there are in the type.  But!  He does not have
to name a type for the "computational objects", such as
declaring them to be "long", he just lists the values that
should be stored in objects of the type, and exactly these, no
more, no less.

In pseudo code,

    type T is exactly 1e0 .. 1e5.
    T obj.   // representation left to the compiler

    // compute, compute, compute, ...  then,

    for k in [0, octet_size(T)) do
      x <- shift_octet_from (obj@k),
      write_octet(x, port_address)
    done.

Note the declaration of T does not mention the name of any
existing type.

In case range types are boring, here is another bare metal type:

    type T is exactly 1e0 .. 1e5 if T % 2 == 0.

Very low level, fairly easy to compute, nothing like 4GL,
and no mention of representation.


2. (Conversion) Suppose I/O means writing a number of structured
objects to a stream.  A program uses many structured objects
in its inner loops, requiring high speed representations. For I/O,
though, a different representation would be better, or is required.

For the computation, simply declare the structure and let the
compiler choose an efficient layout (type sizes, alignment).

    type R is record      struct R {
      x, y, z : S;         S x, y, z;
      t : TS;              TS t;
    end record;          };

In an I/O module, though, specify the layout as needed for I/O.
Doing so will establish order and size of components. It all
happens in the type system, the compiler does it, and the programmer
does not have to write conversion functions:

    -- ... I/O interface ...
private
    -- users of public type R need not be affected

    type R_for_IO is new R;  -- same logical data as R

    for R_for_IO use record
       --  nothing at storage unit 0, padding

       Z at 1 range 0 .. 7;
       Y at 2 range 0 .. 7;
       X at 3 range 0 .. 7;

       -- 2 storage units padding, again

       T at 6 range 2 .. 13;
    end record;


All that now needs to be done when writing objects to a stream
is to apply a type conversion to the object to be output.
Just an ordinary type conversion.  Since the functions of the
I/O interface deal with all this, the type conversion is never
seen by users of "computational type" R.





  parent reply	other threads:[~2011-08-13  9:37 UTC|newest]

Thread overview: 87+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <fb9f787c-af06-427a-82b6-b0684e8dcbc5@s2g2000vby.googlegroups.com>
     [not found] ` <j1kaj8$dge$1@adenine.netfront.net>
     [not found]   ` <1e292299-2cbe-4443-86f3-b19b8af50fff@c29g2000yqd.googlegroups.com>
     [not found]     ` <j1tha5$11q5$1@adenine.netfront.net>
     [not found]       ` <1fd0cc9b-859d-428e-b68a-11e34de84225@gz10g2000vbb.googlegroups.com>
2011-08-10 19:05         ` Why use C++? Niklas Holsti
2011-08-10 22:37           ` Randy Brukardt
2011-08-10 22:49             ` Ludovic Brenta
2011-08-12  4:54               ` Randy Brukardt
2011-08-11  7:54             ` Dmitry A. Kazakov
2011-08-11  8:20               ` Jed
2011-08-11  9:13                 ` Dmitry A. Kazakov
2011-08-11 10:57                   ` Jed
2011-08-11 11:43                     ` Georg Bauhaus
2011-08-12  5:07                       ` Jed
2011-08-11 13:11                     ` Nomen Nescio
2011-08-11 15:11                       ` Paul
2011-08-12  5:15                       ` Jed
2011-08-12 21:39                         ` Fritz Wuehler
2011-08-14  6:52                           ` Jed
2011-08-14  8:13                             ` Nomen Nescio
2011-08-11 15:09                     ` Dmitry A. Kazakov
2011-08-12  5:03                       ` Jed
2011-08-12  8:32                         ` Georg Bauhaus
2011-08-12 13:15                           ` Hyman Rosen
2011-08-12 22:09                             ` Randy Brukardt
2011-08-12 15:14                           ` Jed
2011-08-12 17:20                             ` Georg Bauhaus
2011-08-12 19:51                               ` Jed
2011-08-12 21:22                                 ` Ludovic Brenta
2011-08-14  7:00                                   ` Jed
2011-08-16 13:06                                     ` Ludovic Brenta
2011-08-13  9:37                                 ` Georg Bauhaus [this message]
2011-08-14  5:22                                   ` Jed
2011-08-13 10:27                                 ` Georg Bauhaus
2011-08-14  5:35                                   ` Jed
2011-08-14 20:13                                     ` Georg Bauhaus
2011-08-15 11:38                                     ` Georg Bauhaus
2011-08-13 11:02                                 ` Georg Bauhaus
2011-08-14  5:56                                   ` Jed
2011-08-12  9:21                         ` Dmitry A. Kazakov
2011-08-12 13:26                           ` Jed
2011-08-12 14:30                             ` Dmitry A. Kazakov
2011-08-12 19:06                               ` Jed
2011-08-12 20:05                                 ` Dmitry A. Kazakov
2011-08-13  7:53                                   ` Jed
2011-08-13  9:15                                     ` Dmitry A. Kazakov
2011-08-13  9:29                                       ` Ian Collins
2011-08-13  9:52                                         ` Dmitry A. Kazakov
2011-08-13 11:10                                           ` Ian Collins
2011-08-13 11:46                                             ` Georg Bauhaus
2011-08-13 20:30                                               ` Ian Collins
2011-08-13 11:54                                             ` Brian Drummond
2011-08-13 13:12                                               ` Simon Wright
2011-08-14 11:01                                                 ` Brian Drummond
2011-08-14  4:54                                             ` Jed
2011-08-14  4:35                                           ` Jed
2011-08-14  6:46                                             ` Dmitry A. Kazakov
2011-08-14  4:49                                           ` Jed
2011-08-14  6:51                                             ` Dmitry A. Kazakov
2011-08-14  4:29                                       ` Jed
2011-08-14  7:29                                         ` Dmitry A. Kazakov
2011-08-16  8:18                                       ` Nick Keighley
2011-08-16  8:47                                         ` Dmitry A. Kazakov
2011-08-16  9:52                                           ` Nick Keighley
2011-08-16 10:39                                             ` Dmitry A. Kazakov
2011-08-16 10:23                                           ` Georg Bauhaus
2011-08-16 10:58                                             ` Dmitry A. Kazakov
2011-08-16 11:44                                               ` Georg Bauhaus
2011-08-16 14:51                                               ` Bill Findlay
2011-08-16 19:13                                                 ` Dmitry A. Kazakov
2011-08-16 19:23                                                   ` Bill Findlay
2011-08-12 11:48                 ` Stuart Redmann
2011-08-12 13:12                   ` Vinzent Hoefler
2011-08-12 15:50                     ` Stuart Redmann
2011-08-12 17:02                       ` Bill Findlay
2011-08-15 12:59                       ` Vinzent Hoefler
2011-08-12  5:02               ` Randy Brukardt
2011-08-12  5:16                 ` Robert Wessel
2011-08-12 16:39                   ` Adam Beneschan
2011-08-12  5:24                 ` Jed
2011-08-12  6:51                   ` Paavo Helde
2011-08-12  7:41                     ` Georg Bauhaus
2011-08-12 15:50                   ` Fritz Wuehler
2011-08-12 19:59                     ` Jed
2011-08-13  8:06                     ` Stephen Leake
2011-08-12  9:40                 ` Dmitry A. Kazakov
2011-08-12  9:45                   ` Ludovic Brenta
2011-08-12 10:48                     ` Georg Bauhaus
2011-08-12 15:56                       ` Ludovic Brenta
2011-08-13  8:08                   ` Stephen Leake
2011-08-18 13:39               ` Louisa
replies disabled

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