Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic type system for Ada

220 views
Skip to first unread message

Victor Porton

unread,
Jan 24, 2017, 8:12:16 AM1/24/17
to
I thought on combining powers of static (like Ada) and dynamic (like Python)
languages. In two seconds I understood that this can be done by creating a
special "Dynamic" type in a static language.

Due to pure curiosity, I ask: Has anybody developed a dynamic type system
for Ada?

--
Victor Porton - http://portonvictor.org

Dmitry A. Kazakov

unread,
Jan 24, 2017, 8:44:58 AM1/24/17
to
On 24/01/2017 14:12, Victor Porton wrote:
> I thought on combining powers of static (like Ada) and dynamic (like Python)
> languages. In two seconds I understood that this can be done by creating a
> special "Dynamic" type in a static language.

There is no such thing as a dynamic type system. A dynamic type is a
value of some static type, e.g. "any" or "type type" etc. The hierarchy
must end somewhere and that is always a static type.

> Due to pure curiosity, I ask: Has anybody developed a dynamic type system
> for Ada?

AFAIK there were no proposals to add a type type to Ada. There exist
some rudimentary means, like generic constructor, but nothing beyond that.

Then before going to type types there is a lot of space in the 1st order
types for improvements that could eliminate a need in going 2nd order.
Presently Ada lacks:

1. Ad-hoc supertypes
2. Interface inheritance
3. Full multiple dispatch
4. Array, record, access, integer, float, discrete, enumeration types
interfaces to inherit from
5. Introspection

E.g. for weakly coupled systems like distributed ones, the 2nd order
types is not that important. You would not propagate primitive
operations over the network anyway. Some sort of introspection is
usually enough. In most cases 2nd order types is an overkill.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Victor Porton

unread,
Jan 24, 2017, 8:58:34 AM1/24/17
to
It seems that you didn't understood me.

Dmitry A. Kazakov wrote:
> On 24/01/2017 14:12, Victor Porton wrote:
>> I thought on combining powers of static (like Ada) and dynamic (like
>> Python) languages. In two seconds I understood that this can be done by
>> creating a special "Dynamic" type in a static language.
>
> There is no such thing as a dynamic type system. A dynamic type is a
> value of some static type, e.g. "any" or "type type" etc. The hierarchy
> must end somewhere and that is always a static type.

All I ask is just an Ada type which would be so flexible that could store
any kind of a value (just like as a variable in a dynamic language).

This type could be a variant record which could store numbers, lists/arrays,
strings, etc. (anything that can be stored in a variable in a dynamic
language).

>> Due to pure curiosity, I ask: Has anybody developed a dynamic type system
>> for Ada?
>
> AFAIK there were no proposals to add a type type to Ada. There exist
> some rudimentary means, like generic constructor, but nothing beyond that.

There is no need to "add" it to Ada. Anybody with a little programming
experience can develop a package with such a type.

I think developing such a library can be useful for Ada community.

And no, it is not a "type type".

Dmitry A. Kazakov

unread,
Jan 24, 2017, 9:26:15 AM1/24/17
to
On 24/01/2017 14:58, Victor Porton wrote:
> It seems that you didn't understood me.

You should have asked about data definition layer, not type then. Types
have operations.

> All I ask is just an Ada type which would be so flexible that could store
> any kind of a value (just like as a variable in a dynamic language).

Task value, protected object value, a value from generic instance not
yet instantiated, subprogram value?

In languages you have in mind there are types which can be used that way
and not-so-much-types which cannot.

> This type could be a variant record which could store numbers, lists/arrays,
> strings, etc. (anything that can be stored in a variable in a dynamic
> language).

Then you should have asked about a variant type of some predefined set
of scalar types and predefined class of containers. It has little use in
Ada.

As an implementation consider stream attributes. For a stream take a
memory-located stream. Yes it is implemented in Ada. It has almost no
use because Ada lacks things I listed. You cannot hang an interface on a
stream object in order to access the object stored in the stream in the
same way the original type is accessed (AKA dynamic cast, AKA
polymorphic object). For this the stream object must inherit the
interface of the type it contains, and do that ad-hoc. Variant record
types do the job (dynamic cast is performed by variant selection per
discriminant), but they are very very limited in what they can do.

Jeffrey R. Carter

unread,
Jan 24, 2017, 1:07:42 PM1/24/17
to
On 01/24/2017 02:58 PM, Victor Porton wrote:
>
> All I ask is just an Ada type which would be so flexible that could store
> any kind of a value (just like as a variable in a dynamic language).

You might want to look at type System.Storage_Elements.Storage_Array (ARM
13.7.1). You can (unchecked) convert most values to and from a Storage_Array of
the correct size. Package Ada.Storage_IO (ARM A.9) makes it easy to do so for
definite, unlimited types.

--
Jeff Carter
"It is the German who is so uncourteous to his verbs."
A Scandal in Bohemia
122

Randy Brukardt

unread,
Jan 24, 2017, 4:21:43 PM1/24/17
to
"Victor Porton" <por...@narod.ru> wrote in message
news:o67mi7$v0f$1...@gioia.aioe.org...
...
> All I ask is just an Ada type which would be so flexible that could store
> any kind of a value (just like as a variable in a dynamic language).
>
> This type could be a variant record which could store numbers,
> lists/arrays,
> strings, etc. (anything that can be stored in a variable in a dynamic
> language).

I'd suggest that it be defined as an abstract tagged type, with each of the
other kinds of things as a derived tagged type (one for holding integers,
one for holding floats, etc.). Then one could get a "variable that could
hold anything" by instantiating the Unbounded_Holder container (and that
would open uses of the other kinds of containers as well). That way, the
package wouldn't have to reinvent all of the memory management stuff that's
already in the containers. Plus, if one organized the hierarchy similar to
the chart in 3.2(12)
[http://www.ada-auth.org/standards/2xrm/html/RM-3-2.html#p12], you could
define shared operations at the appropriate levels to have some use beyond
just plain storage. (For instance, all of the numeric types would have math
this way.)

I do wonder how useful such a hierarchy would be, but I suppose someone
would have to build it to find out.

Randy.


J-P. Rosen

unread,
Jan 24, 2017, 5:02:01 PM1/24/17
to
Le 24/01/2017 à 22:21, Randy Brukardt a écrit :
> I do wonder how useful such a hierarchy would be
Presumably, you'll need that if you write an interpreter for a
dynamically typed language in Ada...

--
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

Paul Rubin

unread,
Jan 25, 2017, 12:41:45 AM1/25/17
to
Victor Porton <por...@narod.ru> writes:
> Due to pure curiosity, I ask: Has anybody developed a dynamic type system
> for Ada?

That's not so easy. You have to be able to wrap an arbitrary Ada object
in another object, along with a tag saying what the Ada type is. That
means you have to be able to encode every Ada type into a tag, and types
can be quite complicated (maybe arbitrary tree structures even). Also
the compiler might or might not have a way of telling the runtime what
type something has.

There is something like this in Haskell called Data.Dynamic, but they
had to add special features to the compiler and runtime to support it.

Dmitry A. Kazakov

unread,
Jan 25, 2017, 3:23:26 AM1/25/17
to
On 24/01/2017 22:21, Randy Brukardt wrote:

> I do wonder how useful such a hierarchy would be, but I suppose someone
> would have to build it to find out.

Not much. I did exactly this before.

The problem is that you get the "god-class" in the end. In order to be
able to re-interpret the value as a given scalar type you have to add a
primitive operation to the abstract base. E.g.

function As_Unsigned_32 (Value : Abstract_Variable)
return Unsigned_32 is abstract;
-- Raises Type_Error if not of the type

And so for each scalar type. And for arrays and records.

Otherwise you have to explicitly convert (upcast) to specific instance
derived from Abstract_Variable which is much worse.

P.S. I intended to use that on top of a stream exchange, but the
interface is so heavy that it adds no advantage to direct reading the
target object from the stream.

Georg Bauhaus

unread,
Jan 25, 2017, 3:40:30 AM1/25/17
to
Randy Brukardt <ra...@rrsoftware.com> wrote:

> if one organized the hierarchy similar to
> the chart in 3.2(12)
> [http://www.ada-auth.org/standards/2xrm/html/RM-3-2.html#p12], you could
> define shared operations at the appropriate levels to have some use beyond
> just plain storage. (For instance, all of the numeric types would have math
> this way.)

Objective-C class clusters seem to be of a similar kind, e.g. NSNumber.
The objects can then also participate in anything based on NSObject,
basically, such as being sent in a local or remote notification
or being part of a persistent object graph.
But then, Objective-C does not have so much in terms
of Ada generics, insofar as these permit specifying private
formal types, derived or not,
to have just the operations that are needed in instances.

Randy Brukardt

unread,
Jan 25, 2017, 4:55:47 PM1/25/17
to
"Dmitry A. Kazakov" <mai...@dmitry-kazakov.de> wrote in message
news:o69n9s$ogk$1...@gioia.aioe.org...
> On 24/01/2017 22:21, Randy Brukardt wrote:
>
>> I do wonder how useful such a hierarchy would be, but I suppose someone
>> would have to build it to find out.
>
> Not much. I did exactly this before.
>
> The problem is that you get the "god-class" in the end. In order to be
> able to re-interpret the value as a given scalar type you have to add a
> primitive operation to the abstract base. E.g.
>
> function As_Unsigned_32 (Value : Abstract_Variable)
> return Unsigned_32 is abstract;
> -- Raises Type_Error if not of the type
>
> And so for each scalar type. And for arrays and records.

I'd probably put such routines higher in the hierarchy (the above would
appear under "Root_Numeric", for instance), but you are correct that they
are needed (and the reverse as well, to give a way to import values, esp.
literals).

And I have no idea how to deal with most user-defined types (enumerations,
records, tasks, etc.) in such a scheme. (The interface of arrays is simple
enough that I can imagine some mechanism to deal with a subset of them.)

> Otherwise you have to explicitly convert (upcast) to specific instance
> derived from Abstract_Variable which is much worse.

That's how all of my hierarchies work. I doubt that I'd call it "much
worse"; it avoids "god-classes" and allows most of the checks to be made
statically (you can't use operations of the wrong type). It's often easy to
apply those upconverts when parameter passing, so they don't end up that
wide-spread.

But I definitely agree that this is a case where there is always going to be
a "bump under the carpet" (as Tucker Taft liked to say during Ada 9x) [you
can move the bump to different places under the carpet, but you can't get
rid of it (at least without total carpet replacement, which is where this
analogy breaks down - but I digress)]. There isn't going to be a totally
clean solution.

> P.S. I intended to use that on top of a stream exchange, but the interface
> is so heavy that it adds no advantage to direct reading the target object
> from the stream.

I suspect this sort of thing would work pretty well for a hierarchy of
numeric types, not so well if other kinds of types are included. There's
just not enough overlap of operations for it to make much sense in the
general case.

Randy.


Vincent

unread,
Jan 26, 2017, 12:53:52 PM1/26/17
to
Le mardi 24 janvier 2017 14:12:16 UTC+1, Victor Porton a écrit :
> I thought on combining powers of static (like Ada) and dynamic (like Python)
> languages. In two seconds I understood that this can be done by creating a
> special "Dynamic" type in a static language.
>
You may simply need to copy what has been done for Python objects in C. The root object has reference counting and an associated type object (or Class object in Objective-C), that references it's methods, through interfaces...

This may be very useful, for scientific computations for instance.

The challenge is to find a good integration in the Ada language. Precisely, Matlab and Python are languages, not simply libraries in C, even if there runtime is in C. See also for instance the limitations of Unbounded_Strings compared to native Ada Strings.

Regards,

Vincent



Victor Porton

unread,
Jan 27, 2017, 2:20:35 PM1/27/17
to
Paul Rubin wrote:

> Victor Porton <por...@narod.ru> writes:
>> Due to pure curiosity, I ask: Has anybody developed a dynamic type system
>> for Ada?
>
> That's not so easy. You have to be able to wrap an arbitrary Ada object
> in another object, along with a tag saying what the Ada type is. That
> means you have to be able to encode every Ada type into a tag, and types
> can be quite complicated (maybe arbitrary tree structures even). Also
> the compiler might or might not have a way of telling the runtime what
> type something has.

In my initial request there was no proposal to encode arbitrary Ada object.

> There is something like this in Haskell called Data.Dynamic, but they
> had to add special features to the compiler and runtime to support it.

Jeffrey R. Carter

unread,
Jan 28, 2017, 4:37:44 AM1/28/17
to
On 01/28/2017 02:16 AM, Dennis Lee Bieber wrote:
> On Fri, 27 Jan 2017 21:20:33 +0200, Victor Porton <por...@narod.ru>
> declaimed the following:
>>
>> In my initial request there was no proposal to encode arbitrary Ada object.
>>
> Your subject line is "dynamic type system...", so naturally we would
> consider anything in Ada that can be defined via "... type ..."

On Jan 24 he wrote

>> All I ask is just an Ada type which would be so flexible that could store
>> any kind of a value (just like as a variable in a dynamic language).

"Any kind of value" must mean "values of any type".

--
Jeff Carter
"Pray that there's intelligent life somewhere up in
space, 'cause there's bugger all down here on earth."
Monty Python's Meaning of Life
61
0 new messages