comp.lang.ada
 help / color / mirror / Atom feed
* Generic formal type with 'Image
@ 2018-06-06 13:03 Alejandro R. Mosteo
  2018-06-06 14:18 ` ytomino
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Alejandro R. Mosteo @ 2018-06-06 13:03 UTC (permalink / raw)


I'm pretty sure the answer is "no", but just in case:

Is there a formal for a generic that serves for any type that has a 
predefined 'Image?

The purpose is to avoid:

generic
    type Printable is ... -- What should go here?
    with function Image (P : Printable) return String is <>;
package

and then have to pass the 'Image attribute as the Image function in all 
instantiations.

The closest thing I can think of is (<>) but that won't do for floating 
point types.

I understand this is an unusually narrow case (I need a generic for many 
numeric types, both discrete and floating, and this would save me some 
typing -- that I have already spent here anyway.)

With these issues I feel a kind of overlap/missed connection between 
attributes and interfaces.

Thanks,
Alex.


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

* Re: Generic formal type with 'Image
  2018-06-06 13:03 Generic formal type with 'Image Alejandro R. Mosteo
@ 2018-06-06 14:18 ` ytomino
  2018-06-06 15:34 ` Jeffrey R. Carter
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: ytomino @ 2018-06-06 14:18 UTC (permalink / raw)


On Wednesday, June 6, 2018 at 10:03:24 PM UTC+9, Alejandro R. Mosteo wrote:
> I'm pretty sure the answer is "no", but just in case:
> 
> Is there a formal for a generic that serves for any type that has a 
> predefined 'Image?
> 
> The purpose is to avoid:
> 
> generic
>     type Printable is ... -- What should go here?
>     with function Image (P : Printable) return String is <>;
> package
> 
> and then have to pass the 'Image attribute as the Image function in all 
> instantiations.
> 
> The closest thing I can think of is (<>) but that won't do for floating 
> point types.
> 
> I understand this is an unusually narrow case (I need a generic for many 
> numeric types, both discrete and floating, and this would save me some 
> typing -- that I have already spent here anyway.)
> 
> With these issues I feel a kind of overlap/missed connection between 
> attributes and interfaces.
> 
> Thanks,
> Alex.

If AI12-0020-1 (http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0020-1.txt?rev=1.7&raw=N) is adopted, 'Image will be able to be used in anywhere.

As the available but unrecommended solution, there is GNAT's Type_Class attribute.

 generic
    type Printable is private;
 package P is
    procedure Print (Item : Printable);
 end P;

 package body P is

    procedure Print (Item : Printable) is
    begin
       case Printable'Type_Class is
          when Type_Class_Floating_Point =>
             -- We have to convert from the formal private type to some float type, with some dirty hack.
             -- This is the case of using streams:
             Printable'Write (temporary-stream, Item);
             Set_Index (temporary-stream, 1);
             case Printable_Type'Stream_Size is
                when Float'Stream_Size =>
                   Float'Read (temporary-stream, Float_Var);
                when Long_Float'Stream_Size =>
                   Long_Float'Read (temporary-stream, Long_Float_Var);
                ...

Or, as disappointing answer, we have to write normally.

 generic
    type Printable is private;
    with function Image (P : Printable) return String is <>;
    with function "+" (Left, Right : Printable) return Printable is <>;
    -- Maybe you need "-", "*", "/", "<", "<=", Zero, One, etc
 package


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

* Re: Generic formal type with 'Image
  2018-06-06 13:03 Generic formal type with 'Image Alejandro R. Mosteo
  2018-06-06 14:18 ` ytomino
@ 2018-06-06 15:34 ` Jeffrey R. Carter
  2018-06-06 18:35 ` Alejandro R. Mosteo
  2018-06-06 18:56 ` Shark8
  3 siblings, 0 replies; 8+ messages in thread
From: Jeffrey R. Carter @ 2018-06-06 15:34 UTC (permalink / raw)


On 06/06/2018 03:03 PM, Alejandro R. Mosteo wrote:
> I'm pretty sure the answer is "no", but just in case:

You're right; as things are now, you can't do it. You have to do

generic -- Name
     type Printable is private;
     with function Image (P : Printable) return String is <>;
package Name is
    procedure Proc (P : in Printable);
    function Func (P : Printable) return Boolean;
end Name;

You can then, if you like, do things like

generic -- Name_Discrete
    type Discrete is (<>);
package Name_Discrete is
    procedure Proc (P : in Discrete);
    function Func (P : Discrete) return Boolean;
end Name_Discrete;

with Name;
package body Name_Discrete is
    package Internal is new Name
       (Printable => Discrete, Image => Discrete'Image);

    procedure Proc (P : in Discrete) renames Internal.Proc;
    function Func (P : Discrete) return Boolean renames Internal.Func;
end Name_Discrete;

-- 
Jeff Carter
"You me on the head hitted."
Never Give a Sucker an Even Break
108


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

* Re: Generic formal type with 'Image
  2018-06-06 13:03 Generic formal type with 'Image Alejandro R. Mosteo
  2018-06-06 14:18 ` ytomino
  2018-06-06 15:34 ` Jeffrey R. Carter
@ 2018-06-06 18:35 ` Alejandro R. Mosteo
  2018-06-07  8:35   ` Marius Amado-Alves
  2018-06-06 18:56 ` Shark8
  3 siblings, 1 reply; 8+ messages in thread
From: Alejandro R. Mosteo @ 2018-06-06 18:35 UTC (permalink / raw)


Thanks for the suggestions, and the link to the AI. I find these 
discussions fascinating.

Alex.

On 06/06/2018 15:03, Alejandro R. Mosteo wrote:
> I'm pretty sure the answer is "no", but just in case:
> 
> Is there a formal for a generic that serves for any type that has a 
> predefined 'Image?
> 
> The purpose is to avoid:
> 
> generic
>     type Printable is ... -- What should go here?
>     with function Image (P : Printable) return String is <>;
> package
> 
> and then have to pass the 'Image attribute as the Image function in all 
> instantiations.
> 
> The closest thing I can think of is (<>) but that won't do for floating 
> point types.
> 
> I understand this is an unusually narrow case (I need a generic for many 
> numeric types, both discrete and floating, and this would save me some 
> typing -- that I have already spent here anyway.)
> 
> With these issues I feel a kind of overlap/missed connection between 
> attributes and interfaces.
> 
> Thanks,
> Alex.

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

* Re: Generic formal type with 'Image
  2018-06-06 13:03 Generic formal type with 'Image Alejandro R. Mosteo
                   ` (2 preceding siblings ...)
  2018-06-06 18:35 ` Alejandro R. Mosteo
@ 2018-06-06 18:56 ` Shark8
  2018-06-06 20:34   ` Randy Brukardt
  3 siblings, 1 reply; 8+ messages in thread
From: Shark8 @ 2018-06-06 18:56 UTC (permalink / raw)


On Wednesday, June 6, 2018 at 7:03:24 AM UTC-6, Alejandro R. Mosteo wrote:
> I'm pretty sure the answer is "no", but just in case:
> 
> Is there a formal for a generic that serves for any type that has a 
> predefined 'Image?

Yes, there is:

  GENERIC
    Type K is (<>);
  PACKAGE Example IS ...

Will do it, as all parameters of K are discrete, and therefore K has the 'Image attribute. (As of Ada 2012 *only* discrete types have the 'Image attribute.)

> 
> The purpose is to avoid:
> 
> generic
>     type Printable is ... -- What should go here?
>     with function Image (P : Printable) return String is <>;
> package Extended_Generic is ...
> 
> and then have to pass the 'Image attribute as the Image function in all 
> instantiations.

This is the more general construct ans will work on something like:

  TYPE Vector IS ARRAY(Positive RANGE <>) of Integer;
  
  Function Image( Input : Vector ) Return String is
    Function List( Data : Vector := Input ) Return String is
    (case Data'Length is
        when 0 => '',
        when 1 => Integer'Image(Data'First),
        when others => Image(Data(Data'First..Natural'Pred(Data'Last))) &
                          Integer'Image(Data'Last)
    );
  Begin
    Return '(' & List & ')';
  End Image;

  Package X is new Extended_Generic( Vector );

this allows more complex items for your Printable parameter; you could in fact make the parameter "Type Printable(<>) is limited private" for near-maximum coverage. (You wouldn't be able to use it on incomplete types.)

> 
> The closest thing I can think of is (<>) but that won't do for floating 
> point types.

This is because floats aren't discrete.

> 
> I understand this is an unusually narrow case (I need a generic for many 
> numeric types, both discrete and floating, and this would save me some 
> typing -- that I have already spent here anyway.)
> 
> With these issues I feel a kind of overlap/missed connection between 
> attributes and interfaces.

This is true.
What you might do is make a sort of generic signature package and work from there.

GENERIC
  Type Numeric is private;
  Function Image( Input Numeric ) Return String is <>;
  Function "+" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "-" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "/" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "*" ( Left, Right : Numeric ) Return Numeric is <>;
  Function "**"( Left : Numeric; Right Natural ) Return Numeric is <>;
  -- Other operations.
PACKAGE General_Numeric IS ...

Then for your particular case use either a child of this, or a generic accepting it as a parameter.


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

* Re: Generic formal type with 'Image
  2018-06-06 18:56 ` Shark8
@ 2018-06-06 20:34   ` Randy Brukardt
  0 siblings, 0 replies; 8+ messages in thread
From: Randy Brukardt @ 2018-06-06 20:34 UTC (permalink / raw)


"Shark8" <onewingedshark@gmail.com> wrote in message 
news:0dd44c4f-99b5-426d-ba08-831cbb14a795@googlegroups.com...
...
> Will do it, as all parameters of K are discrete, and therefore K has the
>'Image attribute. (As of Ada 2012 *only* discrete types have the 'Image
> attribute.)

Ada 95 and later allows 'Image on all *scalar* types, that is including 
reals.

Ada 2020 is likely to allow 'Image on all types, and also to allow 
redefining it similarly to the way one redefines streaming. I say likely 
because that's still being worked on, and with the deadlines rapidly 
approaching, I can't guarentee anything will get finished and thus included 
unless it is already finished.

                                   Randy.




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

* Re: Generic formal type with 'Image
  2018-06-06 18:35 ` Alejandro R. Mosteo
@ 2018-06-07  8:35   ` Marius Amado-Alves
  2018-06-07 21:26     ` Randy Brukardt
  0 siblings, 1 reply; 8+ messages in thread
From: Marius Amado-Alves @ 2018-06-07  8:35 UTC (permalink / raw)


Fascinating indeed, thanks.

My gut feeling is that the assertion

   X = T'Value(T'Image(X))

should be met for all types with 'Image.

'Image for records would be really useful!

Also useful (and easy) would be an Object'Image utility (like GNAT's 'Img) for all types with 'Image.

/* Also note that 'Image for arrays (with the 'Value rule above) could create too large strings, say of 20M characters, for a 1000x1000 matrix. Programmer's responsability, I know. Ok since it's just for debugging I guess. */

Sorry if this has been discussed/decided already in the AI. (I see there that Randy supports the Value rule.)

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

* Re: Generic formal type with 'Image
  2018-06-07  8:35   ` Marius Amado-Alves
@ 2018-06-07 21:26     ` Randy Brukardt
  0 siblings, 0 replies; 8+ messages in thread
From: Randy Brukardt @ 2018-06-07 21:26 UTC (permalink / raw)


"Marius Amado-Alves" <amado.alves@gmail.com> wrote in message 
news:40793817-5a96-4802-8014-f8e92da8d47e@googlegroups.com...
> Fascinating indeed, thanks.
>
> My gut feeling is that the assertion
>
>   X = T'Value(T'Image(X))
>
> should be met for all types with 'Image.

I agree, but the plan is to not extend 'Value this time. We'll see if people 
complain.

                         Randy.


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

end of thread, other threads:[~2018-06-07 21:26 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-06 13:03 Generic formal type with 'Image Alejandro R. Mosteo
2018-06-06 14:18 ` ytomino
2018-06-06 15:34 ` Jeffrey R. Carter
2018-06-06 18:35 ` Alejandro R. Mosteo
2018-06-07  8:35   ` Marius Amado-Alves
2018-06-07 21:26     ` Randy Brukardt
2018-06-06 18:56 ` Shark8
2018-06-06 20:34   ` Randy Brukardt

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