comp.lang.ada
 help / color / mirror / Atom feed
* Tagged type: how to know which specific type
@ 1995-03-18 19:43 woodruff
  1995-03-19 22:27 ` Robert Dewar
  1995-03-21 14:38 ` Tucker Taft
  0 siblings, 2 replies; 3+ messages in thread
From: woodruff @ 1995-03-18 19:43 UTC (permalink / raw)


I've been puzzling for some time about the proper use of tagged types when 
I wish to have information about what type of object I'm modeling, without 
having the object itself. 

In the olden days using a discriminated record type I would have an easy
solution because the discriminant itself was a visible type, and I could
use a value of that type to give information about a record instance.

Let me pose a problem that I think illustrates my puzzle; I've had
excellent advice from my c.l.a friends in the past and I hope that will
happen again!

Suppose my discourse is about vehicles and their fuels; I'll just talk
about the fuel here:

Type fuel is tagged <whatever>
  Type Diesel is new fuel with <whatever>
  Type Gasoline is new fuel .....

So if I want to put fuel in my car, how can I be sure I get the right
stuff?  Naively I imagine going to the pump, reading the label on the
pump, and dispensing fuel iff the pump has gasoline.  How do I ask the
pump what kind it has without dispensing (more formally, what kind of
answer will I get if I ask that question of the pump)?  I don't want to
take some fuel if it's the wrong kind, and I sure don't want to pour the
generic stuff into my car, and depend on the car to raise an exception
if it's not gasoline.

To go even further, suppose I'm in the filling station business.  The
OSHA has just made a new regulation: for every chemical I have in
inventory, I'm required to have a document - the Material Safety Data
Sheet - that carries emergency information about toxicity etc.

So now I call the official printer and ask for copies of the MSDS for
gasoline.  How do I say which MSDS I want?  It seems if I hope to use
the language to assure that my model is correct, that I must send some
gasoline to the printing plant(!)

-- 
John Woodruff
Lawrence Livermore National Lab
510 422 4661



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Tagged type: how to know which specific type
  1995-03-18 19:43 Tagged type: how to know which specific type woodruff
@ 1995-03-19 22:27 ` Robert Dewar
  1995-03-21 14:38 ` Tucker Taft
  1 sibling, 0 replies; 3+ messages in thread
From: Robert Dewar @ 1995-03-19 22:27 UTC (permalink / raw)


You can of course pass Tag values around, but you can't dispatch on them.
It is indeed true that if you want to dispatch you must send along at least
a dummy instance of the value. I have run into this in writing the file
stuff for GNAT (which is incidentally quite a nice example of the use of
tagged types -- each of the I/O modules, text_io, sequential_io etc uses
a control block derived by extension from an abstract common control block,
and then operations like open are class-wide operations, and there is a
dispatching operation for close called when files are closed automatically.

Well when I wrote open, the common code needs to call the dispatching
Allocate routine to allocate an instance of the control block. I found
that I had to declare a dummy instance of the control block to pass to the
open procedure so it knew which Allocate to call. It's a little annoying,
since at some level it would be fine to pass just the Tag, since in practice
that is a pointer to the dispatching table - oh well. It is not so terrible
to write:

    Dummy_Control_Block : Text_File_Control_Block;

    Open ( ......, Dummy_Control_Block, ....)

and I guess you have to do the same with your Gasoline and Printer example

    Fuel_Type : Gasolene;

    Print ( ...., Fuel_Type, ....)

again, not so terrible!




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Tagged type: how to know which specific type
  1995-03-18 19:43 Tagged type: how to know which specific type woodruff
  1995-03-19 22:27 ` Robert Dewar
@ 1995-03-21 14:38 ` Tucker Taft
  1 sibling, 0 replies; 3+ messages in thread
From: Tucker Taft @ 1995-03-21 14:38 UTC (permalink / raw)


woodruff@tanana.llnl.gov wrote:
: I've been puzzling for some time about the proper use of tagged types when
: I wish to have information about what type of object I'm modeling, without
: having the object itself.

: In the olden days using a discriminated record type I would have an easy
: solution because the discriminant itself was a visible type, and I could
: use a value of that type to give information about a record instance.

: Let me pose a problem that I think illustrates my puzzle; I've had
: excellent advice from my c.l.a friends in the past and I hope that will
: happen again!

: Suppose my discourse is about vehicles and their fuels; I'll just talk
: about the fuel here:

: Type fuel is tagged <whatever>
:   Type Diesel is new fuel with <whatever>
:   Type Gasoline is new fuel .....

: So if I want to put fuel in my car, how can I be sure I get the right
: stuff?  Naively I imagine going to the pump, reading the label on the
: pump, and dispensing fuel iff the pump has gasoline.  How do I ask the
: pump what kind it has without dispensing (more formally, what kind of
: answer will I get if I ask that question of the pump)?  I don't want to
: take some fuel if it's the wrong kind, and I sure don't want to pour the
: generic stuff into my car, and depend on the car to raise an exception
: if it's not gasoline.

You could have a function that returned a Tag (of type Ada.Tags.Tag),
and then compare it with the 'Tag attribute of some specific type.
E.g.:
    function Fuel_Tag(Pump : Pump_Type) return Ada.Tags.Tag;
    if Fuel_Tag(Some_Pump) /= Gasoline'Tag then
        Put_Line("No thank you.");
    else
        Put_Line("Fill 'er up!");
        Fill(My_Car, From => Some_Pump);
    end if;

: To go even further, suppose I'm in the filling station business.  The
: OSHA has just made a new regulation: for every chemical I have in
: inventory, I'm required to have a document - the Material Safety Data
: Sheet - that carries emergency information about toxicity etc.

: So now I call the official printer and ask for copies of the MSDS for
: gasoline.  How do I say which MSDS I want?  It seems if I hope to use
: the language to assure that my model is correct, that I must send some
: gasoline to the printing plant(!)

You can get a printable name for a tag using either Ada.Tags.External_Tag
or Ada.Tags.Expanded_Name.  An external tag (of type String) can be
converted back to an internal tag (of type Ada.Tags.Tag) using Internal_Tag.

See RM95 3.9(5-18) for more details on the above.

Note also that a membership test can be used to see if a particular
object belongs to a specific type or to a class of types, e.g.:

    if Some_Fuel in Gasoline'Class then ...

: John Woodruff
: Lawrence Livermore National Lab
: 510 422 4661

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1995-03-21 14:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-03-18 19:43 Tagged type: how to know which specific type woodruff
1995-03-19 22:27 ` Robert Dewar
1995-03-21 14:38 ` Tucker Taft

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