comp.lang.ada
 help / color / mirror / Atom feed
* How can I qualify the predefined logical operations
@ 1998-07-16  0:00 hoyngj
  1998-07-16  0:00 ` hoyngj
                   ` (6 more replies)
  0 siblings, 7 replies; 10+ messages in thread
From: hoyngj @ 1998-07-16  0:00 UTC (permalink / raw)


I have the following problem, when trying to implement a function "and".

 package Some_Package is
   type Private_Type is private;
   function "And" (Left, Right: Private_Type) return Private_Type ;
 private
   type Private_Type is array (0 .. 1023) of Boolean;
 end Some_Package;


-- I want to implement Some_Package."And" using the predefined logical
-- operation "And" for any one-dimensional array type whose components
-- are of a boolean type.
 package body Some_Package is
   function "And" (Left, Right: Private_Type) return Private_Type   is
   begin
     return Left and Right;  -- ERROR: AND is here Some_Package."AND",
                             -- endless recursive
   end "and";
 end Some_Package;

To fix the error in the implementation above, I need to qualify the
predefined "And" operation:


 package body Some_Package is
   function "And" (Left, Right: Private_Type) return Private_Type is
   begin
     return Standard."And" (Left, Right);  -- ERROR: not possible
   end "and";
 end Some_Package;

How is it possible to qualify the predefined logical operations to
distinguish between Some_Package."and" and the predefined "and"?

Thanks in advance

--
Juergen Hoyng,  RIO 62             |   email : Juergen.Hoyng@ri.dasa.de
DASA/RI                            |   fax   : +49 421 539 4529
Postfach 286156                    |   voice : +49 421 539 5348
D-28361 Bremen                     |   Germany







-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
  1998-07-16  0:00 ` hoyngj
  1998-07-16  0:00 ` dennison
@ 1998-07-16  0:00 ` dennison
  1998-07-16  0:00 ` Tucker Taft
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: dennison @ 1998-07-16  0:00 UTC (permalink / raw)


In article <6okcnl$n93$1@nnrp1.dejanews.com>,
  hoyngj@my-dejanews.com wrote:
> I have the following problem, when trying to implement a function "and".
>
> -- I want to implement Some_Package."And" using the predefined logical
> -- operation "And" for any one-dimensional array type whose components
> -- are of a boolean type.

Uhhh, there's no need to do this. Its already there.

Excerpt from LRM 3.6.2 (15)  (Note 47):
The predefined operations of an array type include ... For a one-dimensional
array type, ... if the component type is boolean, the predefined logical
operators are also included.

Try it out. It works.

T.E.D.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
                   ` (3 preceding siblings ...)
  1998-07-16  0:00 ` Tucker Taft
@ 1998-07-16  0:00 ` Anonymous
  1998-07-17  0:00 ` Andreas Kalla
  1998-07-25  0:00 ` Matthew Heaney
  6 siblings, 0 replies; 10+ messages in thread
From: Anonymous @ 1998-07-16  0:00 UTC (permalink / raw)


On Thu, 16 Jul 1998 08:13:42 GMT, hoyngj@my-dejanews.com wrote:

> I have the following problem, when trying to implement a function "and".
> 
>  package Some_Package is
>    type Private_Type is private;
>    function "And" (Left, Right: Private_Type) return Private_Type ;
>  private
>    type Private_Type is array (0 .. 1023) of Boolean;
>  end Some_Package;
> 
> 
> -- I want to implement Some_Package."And" using the predefined logical
> -- operation "And" for any one-dimensional array type whose components
> -- are of a boolean type.
> ...

This is a well-known situation. Maybe we should put it in the FAQ (pause
while I check the FAQ)? OK, I just glanced at the FAQs at
www.adahome.com and see nothing on this subject, so I'll continue.

The problem is that predefined "and" for Private_Type is not defined in
Standard, but in Some_Package by the full declaration of Private_Type,
where it is already hidden by the explicit declaration of "and".

There are two common solutions to this problem, neither of which is
entirely acceptable to everyone. The first is to wrap your type in a
record:

   type Boolean_List is array (Natural range <>) of Boolean;
   type Private_Type is record
      Value : Boolean_List (0 .. 1_023) := (others => False);
   end record;

This approach has the advantage of allowing you to ensure that all
objects of the type are initialized, which is why I use it frequently.
Your function would be

   function "and" (Left : Private_Type; Right : Private_Type)
   return Private_Type is
   begin -- "and"
      return Private_Type'(Value => Left.Value and Right.Value);
   end "and";

although some degenerates would write

   return (Left.Value and Right.Value);

The second solution is to make your type a derived type:

   type Boolean_List is array (Natural range 0 .. 1_023) of Boolean;
   type Private_Type is new Boolean_List;

Now "and" is only redefined for Private_Type, not for Boolean_List, so
you can write

   function "and" (Left : Private_Type; Right : Private_Type)
   return Private_Type is
   begin -- "and"
      return
         Private_Type (Boolean_List (Left) and Boolean_List (Right) );
   end "and";

Good luck!

Jeff Carter  PGP:1024/440FBE21
My real e-mail address: ( carter @ innocon . com )
"Your mother was a hamster and your father smelt of elderberries."
Monty Python & the Holy Grail

Posted with Spam Hater - see
http://www.compulink.co.uk/~net-services/spam/





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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
                   ` (2 preceding siblings ...)
  1998-07-16  0:00 ` dennison
@ 1998-07-16  0:00 ` Tucker Taft
  1998-07-16  0:00 ` Anonymous
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: Tucker Taft @ 1998-07-16  0:00 UTC (permalink / raw)


hoyngj@my-dejanews.com wrote:

: I have the following problem, when trying to implement a function "and".

:  package Some_Package is
:    type Private_Type is private;
:    function "And" (Left, Right: Private_Type) return Private_Type ;
:  private
:    type Private_Type is array (0 .. 1023) of Boolean;
:  end Some_Package;


: -- I want to implement Some_Package."And" using the predefined logical
: -- operation "And" for any one-dimensional array type whose components
: -- are of a boolean type.
:  package body Some_Package is
:    function "And" (Left, Right: Private_Type) return Private_Type   is
:    begin
:      return Left and Right;  -- ERROR: AND is here Some_Package."AND",
:                              -- endless recursive
:    end "and";
:  end Some_Package;

: To fix the error in the implementation above, I need to qualify the
: predefined "And" operation:

The predefine "and" is declared at the point of the array type definition,
i.e. immediately inside "Some_Package."  However, you have a user-defined
"and" with the same profile, so the predefined "and" is hidden by
the user-defined one.

:  package body Some_Package is
:    function "And" (Left, Right: Private_Type) return Private_Type is
:    begin
:      return Standard."And" (Left, Right);  -- ERROR: not possible

This should not work (though it may work in some versions of GNAT),
because the predefined "and" is *not* declared in package Standard.

:    end "and";
:  end Some_Package;

: How is it possible to qualify the predefined logical operations to
: distinguish between Some_Package."and" and the predefined "and"?

You can't if they are both defined in the same scope.  Here is
a fix for your problem, involving converting to another array type
whose "and" has a different profile from the user-defined one:

    function "and" (Left, Right : Private_Type) return Private_Type is
       type Local_Array is array(0..1023) of Boolean;
       -- "and" for Local_Array implicitly declared here
    begin
       return Private_Type(Local_Array(Left) and Local_Array(Right));
    end "and";

: Thanks in advance

: --
: Juergen Hoyng,  RIO 62             |   email : Juergen.Hoyng@ri.dasa.de
: DASA/RI                            |   fax   : +49 421 539 4529
: Postfach 286156                    |   voice : +49 421 539 5348
: D-28361 Bremen                     |   Germany

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
@ 1998-07-16  0:00 ` hoyngj
  1998-07-17  0:00   ` Robert Dewar
  1998-07-16  0:00 ` dennison
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 10+ messages in thread
From: hoyngj @ 1998-07-16  0:00 UTC (permalink / raw)


Thanks for your answers. But I am still a little confused.
Look to the little Ada progam:

procedure And_Test is
   A, B, C : Boolean := True;
   type Boolean_Array is array (0 .. 31) of Boolean;
   X,Y,Z : Boolean_Array := (others => False);
begin
  C := Standard."And" (A,B);
  Z := Standard."And" (X,Y);
end And_Test;

When compiling this program with the Verdix and the Alsys compiler, I
got the following error messages:

Compiler: VADS 6.2.3(c)
OS : Solaris 2.5.1 (Sparc)

   1:procedure And_Test is
   2:   A, B, C : Boolean := True;
   3:   type Boolean_Array is array (0 .. 31) of Boolean;
   4:   X,Y,Z : Boolean_Array := (others => False);
   5:begin
   6:  C := Standard."And" (A,B);
   7:  Z := Standard."And" (X,Y);
A ----------^
A:error: RM 4.5.1: no operator visible for boolean_array "and" boolean_array
   8:end And_Test;

Compiler: Alsys Ada V5.5.4
OS : HP_UX 10.20


        7   Z := Standard."And" (X,Y);
                                 1
 1   **IDE There is a type inconsistency in this expression.

         ======================== More Information ========================
         -> Different interpretations exist for "AND":
            - STANDARD."AND"
                Its profile is: (STANDARD.BOOLEAN; STANDARD.BOOLEAN) return
         STANDARD.BOOLEAN
            - STANDARD."AND"
                Its profile is: (Any boolean array; Any boolean array) return
         Any boolean array
            - STANDARD."AND"
                Its profile is: (Any boolean type; Any boolean type) return
         Any boolean type
         -> But the expression has the following type(s):
            - AND_TEST.BOOLEAN_ARRAY at line 3, an array type of STANDARD.
         BOOLEAN
         ==================================================================


Both compiler say line 6 is o.k. but not line 7. I am totally confused about
the error message of the Alsys compiler saying there is a STANDARD."AND"
for "any boolean array". But why does Alsys generate an error message for
line 7.
And why is the predefined logical operation "and" for boolean values defined
in STANDARD in contradiction to the predefined logical operation "AND" for
boolean arrays?


-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
  1998-07-16  0:00 ` hoyngj
@ 1998-07-16  0:00 ` dennison
  1998-07-16  0:00 ` dennison
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: dennison @ 1998-07-16  0:00 UTC (permalink / raw)


Whoops. OK I see what you are getting at.

In article <6okcnl$n93$1@nnrp1.dejanews.com>,
  hoyngj@my-dejanews.com wrote:
> I have the following problem, when trying to implement a function "and".
...
>    type Private_Type is array (0 .. 1023) of Boolean;
...
>    function "And" (Left, Right: Private_Type) return Private_Type is
>    begin
>      return Standard."And" (Left, Right);  -- ERROR: not possible
>    end "and";

The best way I can see to get around this problem is the following:


 package Some_Package is
   type Private_Type is private;
   function "And" (Left, Right: Private_Type) return Private_Type ;
 private
   type And_Type is array (0 .. 1023) of Boolean;
   type Private_Type is new And_Type;
 end Some_Package;

 package body Some_Package is
   function "And" (Left, Right: Private_Type) return Private_Type   is   begin
     return Private_Type(And_Type(Left) and And_Type(Right));
   end "and";
end Some_Package;

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
                   ` (4 preceding siblings ...)
  1998-07-16  0:00 ` Anonymous
@ 1998-07-17  0:00 ` Andreas Kalla
  1998-07-19  0:00   ` Mats Weber
  1998-07-25  0:00 ` Matthew Heaney
  6 siblings, 1 reply; 10+ messages in thread
From: Andreas Kalla @ 1998-07-17  0:00 UTC (permalink / raw)


Hi!

hoyngj@my-dejanews.com wrote:

> To fix the error in the implementation above, I need to qualify the
> predefined "And" operation:
> 
>  package body Some_Package is
>    function "And" (Left, Right: Private_Type) return Private_Type is
>    begin
>      return Standard."And" (Left, Right);  -- ERROR: not possible
>    end "and";
>  end Some_Package;
> 
> How is it possible to qualify the predefined logical operations to
> distinguish between Some_Package."and" and the predefined "and"?
> 
> Thanks in advance
> 

What about this solution:

   function "And" (Left, Right: Private_Type) return Private_Type   is

     Ret_Value : Private_Type := ( others => false ) ;

   begin

        for i in Private_Type'range
        loop
            Ret_Value ( i ) := Left ( i ) and Right ( i ) ;
        end loop ;

        return Ret_Value ;

   end ;


Andreas Kalla
-- __  __
  (_ )( _) mailto:kalla@vossnet.de      or
    \\//   mailto:kalla@asrv01.atlas.de
   _//\\_  I'm Online? look at http://wwp.mirabilis.com/10343510
  (__)(__) http://pages.vossnet.de/kalla




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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 ` hoyngj
@ 1998-07-17  0:00   ` Robert Dewar
  0 siblings, 0 replies; 10+ messages in thread
From: Robert Dewar @ 1998-07-17  0:00 UTC (permalink / raw)


Tuck said

<<:  package body Some_Package is
:    function "And" (Left, Right: Private_Type) return Private_Type is
:    begin
:      return Standard."And" (Left, Right);  -- ERROR: not possible

This should not work (though it may work in some versions of GNAT),
because the predefined "and" is *not* declared in package Standard.
>>


Just so no confusion arises from the above, it is most certainly the
case that the current version of GNAT rejects the above illegal program:

     1.   package body Some_Package is
     2.     function "And" (Left, Right: Private_Type) return Private_Type is
     3.     begin
     4.       return Standard."And" (Left, Right);  -- ERROR: not possible
                             |
        >>> ""And"" not declared in "Standard"

     5.     end "And";
     6.   end Some_Package;





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

* Re: How can I qualify the predefined logical operations
  1998-07-17  0:00 ` Andreas Kalla
@ 1998-07-19  0:00   ` Mats Weber
  0 siblings, 0 replies; 10+ messages in thread
From: Mats Weber @ 1998-07-19  0:00 UTC (permalink / raw)


Andreas Kalla wrote:

> What about this solution:
> 
>    function "And" (Left, Right: Private_Type) return Private_Type   is
> 
>      Ret_Value : Private_Type := ( others => false ) ;
> 
>    begin
> 
>         for i in Private_Type'range
>         loop
>             Ret_Value ( i ) := Left ( i ) and Right ( i ) ;
>         end loop ;
> 
>         return Ret_Value ;
> 
>    end ;

This is pretty bad because many compilers won't be able to optimize away the
loop. Using the predefined "and" gives you much better chances in terms of
efficiency, especially if the array type is packed.






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

* Re: How can I qualify the predefined logical operations
  1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
                   ` (5 preceding siblings ...)
  1998-07-17  0:00 ` Andreas Kalla
@ 1998-07-25  0:00 ` Matthew Heaney
  6 siblings, 0 replies; 10+ messages in thread
From: Matthew Heaney @ 1998-07-25  0:00 UTC (permalink / raw)


hoyngj@my-dejanews.com writes:

> I have the following problem, when trying to implement a function "and".
> 
>  package Some_Package is
>    type Private_Type is private;
>    function "And" (Left, Right: Private_Type) return Private_Type ;
>  private
>    type Private_Type is array (0 .. 1023) of Boolean;
>  end Some_Package;
> 
> 
> -- I want to implement Some_Package."And" using the predefined logical
> -- operation "And" for any one-dimensional array type whose components
> -- are of a boolean type.
>  package body Some_Package is
>    function "And" (Left, Right: Private_Type) return Private_Type   is
>    begin
>      return Left and Right;  -- ERROR: AND is here Some_Package."AND",
>                              -- endless recursive
>    end "and";
>  end Some_Package;

I like to do this:

private

   type Rep is array (0 .. 1023) of Boolean;

   type Private_Type is new Private_Type_Rep;

end Some_Package;

   function "and" (L, R : Private_Type) return Private_Type is
   begin
      return Rep (L) and Rep (R);
   end;


The problem you're having is that the public and private views of the
type share a namespace.  This is sometimes a pain, but you can get
around it simply by just declaring separate types for the public and
private views, and explicitly converting between them in the body.

Another technique is to implement the full view of the type as a record:

private

   type Boolean_Array is array (0 .. 1023) of Boolean;

   type Private_Type is
      record
         Values : Boolean_Array;
      end record;

end;

   function "and" (L, R : Private_Type) return Private_Type is
   begin
      return L.Values and R.Values;
   end;

This is another way to create separate namespaces for the public and
private views of a type.

> To fix the error in the implementation above, I need to qualify the
> predefined "And" operation:
> 
> 
>  package body Some_Package is
>    function "And" (Left, Right: Private_Type) return Private_Type is
>    begin
>      return Standard."And" (Left, Right);  -- ERROR: not possible
>    end "and";
>  end Some_Package;
> 
> How is it possible to qualify the predefined logical operations to
> distinguish between Some_Package."and" and the predefined "and"?

No.  The operator "and" is implicitly declared immediately after the
declaration of the full view of the type.  As if you did this:

private

   type Private_Type is array (0 .. 1023) of Boolean;

   function "and" (L, R : Private_Type) return Boolean;

end;


In times like this (operations for the public type have the same
signiture as predefined operations of the private type), it's best to
use either type derivation or a record type to implement the full
view of the type.





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

end of thread, other threads:[~1998-07-25  0:00 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-07-16  0:00 How can I qualify the predefined logical operations hoyngj
1998-07-16  0:00 ` hoyngj
1998-07-17  0:00   ` Robert Dewar
1998-07-16  0:00 ` dennison
1998-07-16  0:00 ` dennison
1998-07-16  0:00 ` Tucker Taft
1998-07-16  0:00 ` Anonymous
1998-07-17  0:00 ` Andreas Kalla
1998-07-19  0:00   ` Mats Weber
1998-07-25  0:00 ` Matthew Heaney

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