comp.lang.ada
 help / color / mirror / Atom feed
* Generics crash course
@ 2015-01-16  3:44 John Smith
  2015-01-16  5:14 ` Jeffrey Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: John Smith @ 2015-01-16  3:44 UTC (permalink / raw)


Hello,

I'm going through the wikibook about Ada.  It has this example when it comes to generics:
http://wikibook-ada.sourceforge.net/html/Algorithms__binary_search__adb.htm

What I don't understand is from lines 12 to 21.  How does this work exactly?  I mean, does this somehow "connect" the overloaded < operator to the Search procedure?

Also, on line 53, if this code is ever reached, then this means that the entire application will stop executing, yes?  The procedure won't just return to the caller.

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

* Re: Generics crash course
  2015-01-16  3:44 Generics crash course John Smith
@ 2015-01-16  5:14 ` Jeffrey Carter
  2015-01-16  5:33 ` Dirk Heinrichs
  2015-01-16  5:43 ` Niklas Holsti
  2 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2015-01-16  5:14 UTC (permalink / raw)


On 01/15/2015 08:44 PM, John Smith wrote:
> 
> What I don't understand is from lines 12 to 21.  How does this work exactly?
> I mean, does this somehow "connect" the overloaded < operator to the Search
> procedure?

You can think of a generic as a kind of template for producing actual packages
and subprograms (the example here has a generic procedure). Lines 12-21 are the
"generic formal part" that specifies the "generic formal parameters". These tell
the user what needs to be provided to use the generic. The rest of the generic
is written in terms of these parameters. The instantiation (line 73) creates an
actual procedure (in this case) from the generic that acts upon the actual
parameters in the instantiation in place of the formal parameters.

This is not a great example, since the generic is just a complication in
implementing a binary search of an array of Float indexed by Positive. A better
example might have shown searching multiple arrays of different element types,
indexed by different index types.

As an example, Search could be instantiated for

type Person is record
   ID   : Positive;
   Name : Ada.Strings.Unbounded.Unbounded_String;
   DOB  : Ada.Calendar.Time;
end record;

function "<" (Left : in Person; Right : in Person) return Boolean is
begin -- "<"
   return Left.ID < Right.ID;
end "<";

subtype PLI is Long_Integer range 1 .. Long_Integer'Last;

type Person_List is array (PLI range <>) of Person;

procedure Find is new Search
(Element_Type => Person, Index_Type => PLI, Array_Type => Person_List);

and used to find people by ID.

> Also, on line 53, if this code is ever reached, then this means that the
> entire application will stop executing, yes?  The procedure won't just return
> to the caller.

No. Line 53 is an exit statement. A plain exit statement like this
unconditionally exits the innermost enclosing loop statement. An "exit" with a
loop name can exit any enclosing loop, not just the innermost. An "exit" with a
"when" part is a conditional "exit" and only exits when its condition is true.

exit [loop-name] [when condition];

I would recommend you learn more about basic Ada (such as loop and exit
statements) before attacking more advanced topics such as generics.

-- 
Jeff Carter
"Mr. President, we must not allow a mine-shaft gap!"
Dr. Strangelove
33


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

* Re: Generics crash course
  2015-01-16  3:44 Generics crash course John Smith
  2015-01-16  5:14 ` Jeffrey Carter
@ 2015-01-16  5:33 ` Dirk Heinrichs
  2015-01-16  5:43 ` Niklas Holsti
  2 siblings, 0 replies; 5+ messages in thread
From: Dirk Heinrichs @ 2015-01-16  5:33 UTC (permalink / raw)


John Smith wrote:

> I'm going through the wikibook about Ada.  It has this example when it
> comes to generics:
> http://wikibook-ada.sourceforge.net/html/Algorithms__binary_search__adb.htm
> 
> What I don't understand is from lines 12 to 21.  How does this work
> exactly?  I mean, does this somehow "connect" the overloaded < operator to
> the Search procedure?

These lines define the generic formal parameters. The important line here, 
when it comes to "<", is 21. The box operator "<>" here means that this 
subprogram will be instantiated with one that is visible and matches the 
specification. Since the example uses Float for Element_Type (see lines 73 - 
76), its "<" function is used automatically.

See 
http://en.wikibooks.org/wiki/Ada_Programming/Generics#Generic_formal_subprograms

> Also, on line 53, if this code is ever reached, then this means that the
> entire application will stop executing, yes?  The procedure won't just
> return to the caller.

No. It will just exit the surrounding loop.

HTH...

	Dirk
-- 
Dirk Heinrichs <dirk.heinrichs@altum.de>
Tel: +49 (0)2471 209385 | Mobil: +49 (0)176 34473913
GPG Public Key CB614542 | Jabber: dirk.heinrichs@altum.de
Tox: heini@toxme.se
Sichere Internetkommunikation: http://www.retroshare.org
Privacy Handbuch: https://www.privacy-handbuch.de


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

* Re: Generics crash course
  2015-01-16  3:44 Generics crash course John Smith
  2015-01-16  5:14 ` Jeffrey Carter
  2015-01-16  5:33 ` Dirk Heinrichs
@ 2015-01-16  5:43 ` Niklas Holsti
  2015-01-16  6:27   ` Jeffrey Carter
  2 siblings, 1 reply; 5+ messages in thread
From: Niklas Holsti @ 2015-01-16  5:43 UTC (permalink / raw)


On 15-01-16 05:44 , John Smith wrote:
> Hello,
>
> I'm going through the wikibook about Ada.  It has this example when
> it comes to generics:
> http://wikibook-ada.sourceforge.net/html/Algorithms__binary_search__adb.htm
>
>  What I don't understand is from lines 12 to 21.  How does this work
> exactly?  I mean, does this somehow "connect" the overloaded <
> operator to the Search procedure?

Line 12:

    generic

says that the procedure Search is generic in that it depends on (uses)
some "generic formal parameters" which are listed and described on lines
13..21. In effect, Search is a "template" for a set of procedures which 
differ according to how these formal generic parameters are filled in 
with actual types and operations. Compare to lines 73..76 where an 
"instance" of Search called Float_Search is created, with actual types 
and operaions associated to the formals.

It is not possible to call Search, because it is generic; it is possible 
to call Float_Search, which is a normal procedure (not generic anymore). 
To "call" a generic procedure needs two steps: first instantiate (lines 
73..76), then call (line 120). Of course, Float_Search can then be 
called as many times as one likes. It is also possible to instantiate 
Search several times, but each instance will be a different 
(non-generic) procedure.

Line 13:

    type Element_Type is private;

says that Search depends on a type with the formal name Element_Type,
but that Search assumes very little about the nature of the type, 
because it is defined as "private". The actual type could be an integer, 
a float, a record, an array (of fixed size), ...

Line 14:

   type Index_Type is range <>;

says that Search also depends on a type with the formal name Index_Type, 
and that Search assumes that this type is a "discrete" type such as an 
integer type or an enumeration type.

Lines 15..16:

    type Array_Type is
       array (Index_Type range <>) of Element_Type;

say that Search depends on a type with the formal name Array_Type and 
with the property that the index is of Index_Type (but the index range 
is not fixed) and that the array contains elements of Element_Type.

Lines 17..21:

    with function "<"
       (Left  : in Element_Type;
        Right : in Element_Type)
       return Boolean
    is <>;

say that Search depends on a function with the formal name "<", with two 
(normal, not generic) parameters of type Element_Type, and which returns 
a Boolean. Line 21, "is <>", says that this formal generic parameter has 
a default association with an actual function: if Search is instantiated 
at a point where a function named "<" with the required parameter and 
result profile is directly visible, then the instantiation text does not 
have to associate an actual function with the formal "<", and the 
suitable actual "<" function is automatically used. This is why there is 
no association for the formal "<" in lines 73..76: thanks to the "is <>" 
there is an implicit association

    "<" => "<",

which tells the compiler to use whatever visible "<" function has the 
right profile, and of course this is the (predefined) "<" for Float.

Note that this has nothing directly to do with overloading. The formal 
generic function could be named Xjoids; if it has the default "is <>" 
and the instance does not associate it with an actual function, then the 
compiler would look for an actual function called Xjoids, with the right 
profile, whether or not there are overloaded forms of Xjoids.

 > Also, on line 53, if this code is ever reached, then this means that
 > the entire application will stop executing, yes?  The procedure won't
 > just return to the caller.

No, "exit" in Ada means to exit (break) from the (innermost) containing 
loop, not to stop the program. So in this example, the "exit" on line 53 
means that execution continues on line 67 (which means that Search 
returns to its caller). This "exit" statement could therefore be 
replaced by a "return" statement (but using "exit" here is better style 
IMO).

To stop the program (without calling some O/S function not defined in 
the Ada standard), one must cause the main procedure to return, or 
terminate in some way. One way is to raise an exception that propagates 
to the main procedure.

By the way, all the "return" statements in the example (lines 68, 142, 
155) are superfluous, because a subprogram returns when its "end" is 
reached.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

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

* Re: Generics crash course
  2015-01-16  5:43 ` Niklas Holsti
@ 2015-01-16  6:27   ` Jeffrey Carter
  0 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2015-01-16  6:27 UTC (permalink / raw)


On 01/15/2015 10:43 PM, Niklas Holsti wrote:
> 
>    type Element_Type is private;
> 
> says that Search depends on a type with the formal name Element_Type,
> but that Search assumes very little about the nature of the type, because it is
> defined as "private". The actual type could be an integer, a float, a record, an
> array (of fixed size), ...

It says the actual must have assignment and "=" defined.

>   type Index_Type is range <>;
> 
> says that Search also depends on a type with the formal name Index_Type, and
> that Search assumes that this type is a "discrete" type such as an integer type
> or an enumeration type.

This says the actual must be a signed integer type. A generic formal type that
can be any discrete type is "(<>)".

> By the way, all the "return" statements in the example (lines 68, 142, 155) are
> superfluous, because a subprogram returns when its "end" is reached.

A procedure returns when its "end" is reached. A function, which is also a
subprogram, raises Program_Error if it reaches its "end".

-- 
Jeff Carter
"Mr. President, we must not allow a mine-shaft gap!"
Dr. Strangelove
33


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

end of thread, other threads:[~2015-01-16  6:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-16  3:44 Generics crash course John Smith
2015-01-16  5:14 ` Jeffrey Carter
2015-01-16  5:33 ` Dirk Heinrichs
2015-01-16  5:43 ` Niklas Holsti
2015-01-16  6:27   ` Jeffrey Carter

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