home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / computer-lang / Ada / programming / part1 / text0000.txt < prev   
Encoding:
Text File  |  1996-05-31  |  31.3 KB  |  817 lines

  1. Archive-name: computer-lang/Ada/programming/part1
  2. Comp-lang-ada-archive-name: programming/part1
  3. Posting-Frequency: monthly
  4. Last-modified: 22 May 1996
  5. Last-posted: 23 April 1996
  6.  
  7.                                Ada Programmer's
  8.                        Frequently Asked Questions (FAQ)
  9.  
  10.    IMPORTANT NOTE: No FAQ can substitute for real teaching and
  11.    documentation. There is an annotated list of Ada books in the
  12.    companion comp.lang.ada FAQ.
  13.  
  14.     Recent changes to this FAQ are listed in the first section after the table
  15.     of contents. This document is under explicit copyright.
  16.  
  17. This is part 1 of a 4-part posting; part 1 contains the table of contents.
  18. Part 2 begins with question 5.
  19. Part 3 begins with question 6.
  20. Part 4 begins with question 9.
  21. They should be the next postings in this thread.
  22.  
  23.  
  24. Introduction
  25.  
  26.    Ada is an advanced, modern programming language, designed and
  27.    standardized to support and strongly encourage widely recognized
  28.    software engineering principles: reliability, portability, modularity,
  29.    reusability, programming as a human activity, efficiency,
  30.    maintainability, information hiding, abstract data types, genericity,
  31.    concurrent programming, object-oriented programming, etc.
  32.  
  33.    All validated Ada compilers (i.e. a huge majority of the commercial
  34.    Ada compilers) have passed a controlled validation process using an
  35.    extensive validation suite. Ada is not a superset or extension of any
  36.    other language. Ada does not allow the dangerous practices or effects
  37.    of old languages, although it does provide standardized mechanisms to
  38.    interface with other languages such as Fortran, Cobol, and C.
  39.  
  40.    Ada is recognized as an excellent vehicle for education in programming
  41.    and software engineering, including for a first programming course.
  42.  
  43.    Ada is defined by an international standard (the language reference
  44.    manual, or LRM), which has been revised in 1995. Ada is taught and
  45.    used all around the world (not just in the USA). Ada is used in a very
  46.    wide range of applications: banking, medical devices,
  47.    telecommunications, air traffic control, airplanes, railroad
  48.    signalling, satellites, rockets, etc.
  49.  
  50.    The latest version of this FAQ is always accessible through WWW as
  51.    http://lglwww.epfl.ch/Ada/FAQ/programming.html#title
  52.  
  53. Maintenance
  54.  
  55.    This FAQ is maintained on an individual volunteer basis, by Magnus
  56.    Kempe (Magnus.Kempe@di.epfl.ch). [Note: This is done as a hobby, not
  57.    in my capacity as an employee at the Swiss Federal Institute of
  58.    Technology. --MK]
  59.  
  60.    The coding style used in most of the example Ada code is my own, and
  61.    you'll have to live with it (you may want to adopt it :-).
  62.  
  63.  
  64.      _________________________________________________________________
  65.  
  66.    Opinions (if any) expressed are those of the submitters and/or
  67.    maintainer.
  68.      _________________________________________________________________
  69.  
  70. Table of Contents:
  71.  
  72.      * 1: Recent changes to this FAQ
  73.  
  74.      * 2: Information about this document
  75.  
  76.      * 3: Elementary Questions
  77.           + 3.1: How do I make operations directly visible without
  78.             "use"ing a package?
  79.           + 3.2: How do I assign to an array of length 1?
  80.           + 3.3: How do I create a C-style nul-terminated string?
  81.           + 3.4: How can I create an array of strings of various length?
  82.           + 3.5: I know an exception is raised, but my program quits with
  83.             no warning. Why?
  84.           + 3.6: I have only one task in my program, but it doesn't seem
  85.             to run. Why?
  86.           + 3.7: How do I increase the stack size for a task?
  87.           + 3.8: What's the difference between a type conversion and a
  88.             qualifier?
  89.           + 3.9: How do I avoid the potential space in front of
  90.             Integer'Image?
  91.           + 3.10: Why is an exception raised when giving a default
  92.             discriminant?
  93.           + 3.11: When I want an Integer type, what's wrong with just
  94.             using the predefined type Integer or Long_Integer? Why would
  95.             I ever want to declare new Integer types?
  96.           + 3.12: Since I can always declare my own portable integer
  97.             types, why would I ever want to use the predefined type
  98.             Integer?
  99.           + 3.13: I am learning Ada. Can I experiment with a game
  100.             program?
  101.           + 3.14: How can I do a non-blocking, keystroke-at-a-time read
  102.             from the terminal?
  103.  
  104.  
  105.      * 4: Advantages of Ada
  106.           + 4.1: Why use Ada?
  107.           + 4.2: Ada seems large and complex, why is it this way?
  108.           + 4.3: Is there a contest with fame and money for good Ada
  109.             programmers?
  110.  
  111.  
  112.      * 5: Object-Oriented Programming with Ada
  113.           + 5.1: Why does Ada have "tagged types" instead of classes?
  114.           + 5.2: Variant records seem like a dead feature now. When
  115.             should I use them instead of tagged types?
  116.           + 5.3: What is meant by "interface inheritance" and how does
  117.             Ada support it?
  118.           + 5.4: How do you do multiple inheritance in Ada 9X?
  119.           + 5.5: Why are Controlled types so, well, strange?
  120.           + 5.6: What do "covariance" and "contravariance" mean, and does
  121.             Ada support either or both?
  122.           + 5.7: What is meant by upcasting/expanding and
  123.             downcasting/narrowing?
  124.           + 5.8: How does Ada do "narrowing"?
  125.           + 5.9: What is the difference between a class-wide access type
  126.             and a "general" class-wide access type?
  127.  
  128.  
  129.      * 6: Ada Numerics
  130.           + 6.1: Where can I find anonymous ftp sites for Ada math
  131.             packages? In particular where are the random number
  132.             generators?
  133.           + 6.2: How can I write portable code in Ada 83 using predefined
  134.             types like Float and Long_Float? Likewise, how can I write
  135.             portable code that uses Math functions like Sin and Log that
  136.             are defined for Float and Long_Float?
  137.           + 6.3: Is Ada any good at numerics, and where can I learn more
  138.             about it?
  139.           + 6.4: How do I get Real valued and Complex valued math
  140.             functions in Ada 95?
  141.           + 6.5: What libraries or public algorithms exist for Ada?
  142.  
  143.  
  144.      * 7: Efficiency of Ada Constructs
  145.           + 7.1: How much extra overhead do generics have?
  146.           + 7.2: How does Ada compare to other languages in efficiency of
  147.             code?
  148.  
  149.  
  150.      * 8: Advanced Programming Techniques with Ada
  151.           + 8.1: How can I redefine the assignment operation?
  152.           + 8.2: Does Ada have automatic constructors and destructors?
  153.           + 8.3: Should I stick to a one package, one type approach while
  154.             writing Ada software?
  155.           + 8.4: What is the "Beaujolais Effect"?
  156.           + 8.5: What about the "Ripple Effect"?
  157.           + 8.6: How to write an Ada program to compute when one has had
  158.             too much alcohol to legally drive?
  159.           + 8.7: Does Ada have macros?
  160.  
  161.  
  162.      * 9: Ada and Other Programming Languages
  163.           + 9.1: Where can I find programs that will translate from [some
  164.             language] to Ada?
  165.           + 9.2: How can I convert Ada 83 sources to Ada 9X?
  166.           + 9.3: I hear that Ada is slower than Fortran or C, is that
  167.             true?
  168.           + 9.4: Isn't Ada less "elegant" than Eiffel?
  169.           + 9.5: Are there any papers detailing the differences between
  170.             Ada and C++?
  171.           + 9.6: I keep hearing that Ada is a "strongly typed language",
  172.             but it seems different from what's meant in C++. Are they
  173.             different?
  174.           + 9.7: I'm told Ada does all sorts of static type checking, but
  175.             can't you get the same effect using a tool like "lint" with
  176.             C?
  177.           + 9.8: Does Ada have something like the Standard Template
  178.             Library (STL) in C++, or like the components one finds in
  179.             Smalltalk environments?
  180.           + 9.9: Where can I find the equivalent of "printf" in Ada?
  181.  
  182.  
  183.      * 10: Interfacing with Ada
  184.           + 10.1: I am writing software that used the Distributed
  185.             Interactive Simulation (DIS) interface, does an interface
  186.             exist in Ada?
  187.           + 10.2: Is there any support for Common Object Request Broker
  188.             Architecture (CORBA) for Ada 9X?
  189.  
  190.  
  191.      * 11: Finding Additional Information
  192.           + 11.1: Where can I find Ada books?
  193.           + 11.2: Are there other Ada-related FAQs?
  194.           + 11.3: What is the "HBAP WWW Server"?
  195.  
  196.  
  197.      * 12: Pretty-printing and Measuring Ada Source Code
  198.           + 12.1: Is there software that generates a pretty PostScript
  199.             file from Ada source code?
  200.           + 12.2: I use vgrind to do "pretty printing" of my source. Is
  201.             there a vgrind definition for Ada?
  202.           + 12.3: How about a source code reformatter?
  203.           + 12.4: How can I count source lines of code (SLOC)?
  204.           + 12.5: Can I measure other things?
  205.  
  206.  
  207.      * 13: Credits
  208.  
  209.      * 14: Copying this FAQ
  210.  
  211.  
  212.      _________________________________________________________________
  213.  
  214. 1: Recent changes to this FAQ
  215.  
  216.      * 960522: "aimap" located (12.3).
  217.      * 960320: comparing the efficiency of Ada and other languages (7.2)
  218.        (submitted by D. Wheeler)
  219.      * 960228: references for redefining the assignment operation (8.1)
  220.        (switched questions 8.1 and 8.2).
  221.      * 960130: another game written in Ada (3.13).
  222.      * 960123: ARA contest for good Ada (4.3).
  223.      * 950925: removed a couple of non-programming questions.
  224.      * 950828: the change logs now indicate the section/question number.
  225.      * 950819: non-blocking, keystroke-at-a-time reading (3.14).
  226.      * 950819: some URL updates: AdaIC files keep moving around.
  227.      * 950630: corrected answers on task switching (3.6) and conversion
  228.        vs. qualifier (3.8).
  229.      * 950517: "general" class-wide access type (5.9).
  230.      * 950413: variant records vs. tagged types (5.2), submitted by
  231.        K.áShillington.
  232.      * 950406: why Ada does not and should not have macros (8.7).
  233.      * 950320: more on DIS (10.1).
  234.      * 950315: Ada is good at numerics (6.3), by J.áParker.
  235.      * 950309: drinking and driving example (8.6).
  236.      * 950306: added counting SLOC (12.4) and measuring (12.5).
  237.      * 950222: fixed some typos.
  238.      * 950207: revised introduction.
  239.      * 950202: updated discussion of C++ STL and Smalltalk library (9.8).
  240.      * 950126: advantages of code sharing for generics (7.1); Pascal to
  241.        Ada tool (9.1).
  242.      * 950125: why define new integer types (3.11), and why use the
  243.        predefined Integer type (3.12), submitted by J.áParker.
  244.      * 950124: approved for posting in *.answers.
  245.      * 950116: converting Ada 83 code to Ada 9X (9.2).
  246.      * 950106: lengthy code sections extracted and put on FTP server.
  247.      * 950105: printf solution (9.9); update on exception traces and
  248.        vgrind.
  249.      * 950104: links from TOC to all questions.
  250.  
  251.  
  252.    What's important and missing:
  253.      * everything, life, and 42
  254.  
  255.  
  256.      _________________________________________________________________
  257.  
  258.  
  259. 2: Information about this document
  260.  
  261.    This file is posted monthly to comp.lang.ada, comp.answers, and
  262.    news.answers.
  263.  
  264.    This document has a home on the Home of the Brave Ada Programmers
  265.    (HBAP) WWW Server, in hypertext format, URL
  266.    http://lglwww.epfl.ch/Ada/FAQ/programming.html
  267.  
  268.    It is available --as posted in *.answers-- on rtfm.mit.edu, which
  269.    archives all FAQ files posted to *.answers; see
  270.    ftp://rtfm.mit.edu/pub/usenet-by-group/news.answers/computer-lang/Ada
  271.  
  272.    The text-only version is also available in directory
  273.    ftp://lglftp.epfl.ch/pub/Ada/FAQ
  274.  
  275.    Magnus Kempe maintains this document; it's a hobby, not a job.
  276.    Feedback (corrections, suggestions, ideas) about it is to be sent via
  277.    e-mail to Magnus.Kempe@di.epfl.ch
  278.    Thanks.
  279.  
  280.    In all cases, the most up-to-date version of the FAQ is the version
  281.    maintained on the HBAP WWW Server. Please excuse any formatting
  282.    inconsistencies in the posted version of this document, as it is
  283.    automatically generated from the on-line version.
  284.  
  285.      _________________________________________________________________
  286.  
  287.  
  288. 3: Elementary Questions
  289.  
  290.  
  291. 3.1: How do I make operations directly visible without "use"ing the package?
  292.  
  293.    In Ada 83, you can rename the operations in your scope.
  294.  
  295.      -- Say you have an integer type called Int in package Types
  296.      function "<" (Left, Right : Types.Int)
  297.        return Boolean
  298.        renames Types."<";
  299.      -- Make sure the profiles of the first and last "<" match!
  300.  
  301.  
  302.    For operators, Ada 95 introduces the "use type" clause:
  303.  
  304.      use type Types.Int; -- makes operators directly visible
  305.  
  306.  
  307. 3.2: How do I assign to an array of length 1?
  308.  
  309.    Because of ambiguity of parentheses, named notation must be used for
  310.    one-element aggregates (or, under a different angle: a positional
  311.    aggregate must have more than one component).
  312.  
  313.    See [RM9X 4.3.3(7)] as well as the syntax rule of
  314.    positional_array_aggregate in [RM9X 4.3.3]; historians see [RM83
  315.    4.3(4)].
  316.  
  317.      declare
  318.        Array_of_One : array (1..1) of Float;
  319.      begin
  320.        -- Array_of_One := (10.0);   -- Won't work, parsed as an expression
  321.                                     -- within parentheses
  322.  
  323.        Array_of_One := (1 => 10.0); -- No ambiguity here
  324.      end;
  325.  
  326.  
  327.    You can't write a one-element positional aggregate in Ada. Nor a
  328.    zero-element aggregate. The reason for this restriction is that it
  329.    would be difficult for compilers to determine whether:
  330.  
  331.      ( exp )
  332.  
  333.    is a parenthesized expression of some type, or an aggregate of an
  334.    array type. If Ada had used some other notation for aggregates (say,
  335.    "[...]"), then this problem would not exist.
  336.  
  337.    Apparently the original requirements for Ada forbade using certain
  338.    ASCII characters, like '[' and ']', because those characters were not
  339.    available on all hardware. Also, certain characters are used for
  340.    different purposes and glyphs in countries that need additional
  341.    letters not present in ASCII.
  342.  
  343.  
  344. 3.3: How do I create a C-style nul-terminated string?
  345.  
  346.    In a declaration block, append an ASCII.NUL to create a constant Ada
  347.    string.
  348.  
  349.      declare
  350.        Str_Nul : constant String := Str & ASCII.NUL;
  351.      begin
  352.        Call_Requiring_C_String (Str_Nul (Str_Nul'First)'Address);
  353.      end;
  354.  
  355. -- or --
  356.  
  357.      function Nul_Terminate (Str : String)
  358.        return String is
  359.        Str_Nul : constant String := Str & ASCII.NUL;
  360.      begin
  361.        return Str_Nul;
  362.      end Nul_Terminate;
  363.  
  364.  
  365. 3.4: How can I create an array of strings of various length?
  366.  
  367.    In Ada 83, you have to use string access types and "new" to get
  368.    "ragged" arrays:
  369.  
  370.      type String_Access is
  371.        access String;
  372.  
  373.      Strings : constant array (Positive range 1..3) of String_Access
  374.              := ( 1 => new String'("One"),
  375.                   2 => new String'("Two"),
  376.                   3 => new String'("Three")
  377.                 );
  378.  
  379.  
  380.    In Ada 95, the process is simplified by using aliased constants:
  381.  
  382.      type String_Access is
  383.        access constant String;
  384.  
  385.      One : aliased constant String := "One";
  386.      Two : aliased constant String := "Two";
  387.      Three : aliased constant String := "Three";
  388.  
  389.      Strings : constant array (Positive range <>) of String_Access
  390.              := ( 1 => One'Access,
  391.                   2 => Two'Access,
  392.                   3 => Three'Access
  393.                 );
  394.  
  395.  
  396. 3.5: I know an exception is raised, but my program quits with no warning. Why?
  397.  
  398.  
  399.    On some Ada compilers, you have to manually "with" Text_IO before
  400.    exception information is diplayed to the terminal.
  401.  
  402.    On other Ada compilers, you must set an environment variable flag in
  403.    order to cause the exception information trace to be displayed.
  404.  
  405.  
  406. 3.6: I have only one task in my program, but it doesn't seem to run. Why?
  407.  
  408.    In Ada, the main procedure is automatically designated as a task. This
  409.    task may be running forever, thus starving your other task(s), because
  410.    round-robin scheduling (time-slicing) is not required (pre-emptive
  411.    scheduling applies to tasks with different levels of priority).
  412.  
  413.    If the task in question is getting starved, it's a programmer problem,
  414.    not an Ada problem. The programmer has to use an Ada compiler that
  415.    supports pragma Time_Slice, or do the scheduling himself (by changing
  416.    the implementation of his Ada program to ensure that no task starves
  417.    another).
  418.  
  419.    One solution is to explicitly put the main task to sleep within a loop
  420.    construct in order to avoid starvation of the other task(s), as in:
  421.  
  422.      procedure Main is
  423.        task Test;
  424.        task body Test is
  425.        begin
  426.          loop
  427.            delay 1.0;
  428.            Text_IO.Put_Line ("Test");
  429.          end loop;
  430.        end Test;
  431.      begin
  432.        loop
  433.          delay 20.0;
  434.          Text_IO.Put_Line ("Sleeping then writing");
  435.        end loop;
  436.      end Main;
  437.  
  438.  
  439. 3.7: How do I increase the stack size for a task?
  440.  
  441.    Define the task as a "task type" and then use a pragma representation
  442.    clause.
  443.  
  444.      task type A_Task_Type;
  445.      for A_Task_Type'STORAGE_SIZE use 10_000;
  446.      -- 10K bytes allocated to instances of A_Task_Type
  447.      A_Task : A_Task_Type;
  448.  
  449.  
  450. 3.8: What's the difference between a type conversion and a qualifier?
  451.  
  452.    Use a qualifier (tick) to tell the compiler what type it can expect;
  453.    this is strictly a compile-time issue: a qualifier "hints" the type,
  454.    usually to remove an ambiguity. Use a conversion to tell the compiler
  455.    to convert an expression from one type to another (usually within one
  456.    derivation hierarchy); this operation may require a change of
  457.    representation at run-time (e.g. in case of a representation clause
  458.    applying exclusively to the source type).
  459.  
  460.      A : Integer := Integer'(1);  -- this is a qualifier: same as ":= 1;"
  461.      B : Integer := Integer (1);  -- this is a conversion
  462.  
  463.  
  464. 3.9: How do I avoid the potential space in front of Integer'Image?
  465.  
  466.    Use the function Trim from package Ada.Strings.Fixed (you can
  467.    actually trim strings in many other useful ways):
  468.  
  469.      function My_Image (I : Integer)
  470.        return String is
  471.      begin -- My_Image
  472.        return Ada.Strings.Fixed.Trim (Integer'Image (I), Ada.Strings.Left);
  473.      end My_Image;
  474.  
  475.      ... My_Image (12) = "12" ...
  476.  
  477.  
  478.    In Ada 83, code a function that accepts a string and strips the
  479.    leading blank:
  480.  
  481.      function Strip_Leading_Blank (Str : String)
  482.        return String is
  483.      begin -- Strip_Leading_Blank
  484.        if Str (Str'First) = ' ' then
  485.          return Str (1+Str'First .. Str'Last);
  486.        else
  487.          return Str;
  488.        end if;
  489.      end Strip_Leading_Blank;
  490.  
  491.      ...
  492.  
  493.      function My_Image (I : Integer)
  494.        return String is
  495.      begin -- My_Image
  496.        return Strip_Leading_Blank (Integer'Image (I));
  497.      end My_Image;
  498.  
  499.      ... My_Image (12) = "12" ...
  500.  
  501.  
  502. 3.10: Why is an exception raised when giving a default discriminant?
  503.  
  504.    Let's assume you would like to model varying-length strings:
  505.  
  506.      type V_String (Size : Natural := 0) is
  507.        record
  508.          S : String (1 .. Size);
  509.        end record;
  510.  
  511.  
  512.    (from Robert Dewar)
  513.  
  514.    When you give a default discriminant, then one method (I actually
  515.    think it is the preferred method) of implementation is to allocate the
  516.    maximum possible length. Since your discriminant is of type Natural,
  517.    this clearly won't work!
  518.  
  519.    GNAT may compile it, but it won't run it, and indeed I consider it a
  520.    GNAT bug (on the todo list) that no warning is issued at compile time
  521.    for this misuse.
  522.  
  523.    Some compilers, notably Alsys and RR, have at least partially "solved"
  524.    this problem by introducing hidden pointers, but this to me is an
  525.    undesirable implementation choice.
  526.  
  527.    First, it means there is hidden heap activity, which seems
  528.    undesirable. In a language where pointers are explicit, it is
  529.    generally a good idea if allocation is also explicit, and certainly
  530.    for real-time work, hidden anything is worrisome.
  531.  
  532.    Second, it is not easy to do uniformly. Alsys ends up introducing
  533.    arbitrary restrictions on the composition of such types (try making an
  534.    array of them), and RR introduces non-contiguous representations,
  535.    which are legal but troublesome.
  536.  
  537.    To "solve" the problem yourself, just declare a reasonable maximum
  538.    length, and use a subtype representing this length as the subtype of
  539.    the discriminant:
  540.  
  541.      Max_Length : constant := 200;
  542.  
  543.      subtype Index is
  544.        Natural range 0 .. Max_Length;
  545.  
  546.      type V_String (Size : Index := 0) is
  547.        record
  548.          S : String (1 .. Size);
  549.        end record;
  550.  
  551.  
  552. 3.11: When I want an Integer type, what's wrong with just using the predefined
  553. type Integer or Long_Integer? Why would I ever want to declare new Integer
  554. types?
  555.  
  556.    If you declare 2 distinct integer types, for example,
  557.  
  558.      type Data_Index        is range 1..100;
  559.      type Time_Series_Index is range 0..2**15-1;
  560.  
  561.  
  562.    then objects of type Data_Index can't be assigned (directly) to
  563.    variables of type Time_Series_Index, and vice-versa. Likewise,
  564.    variables of these 2 types can't be mixed in arithmetical expressions
  565.    (without explicit type conversions). This may seem like a source of
  566.    endless irritation, but on the contrary, good progammers use it to
  567.    improve the clarity of their code, to make it more robust, and more
  568.    portable. The first 2 examples discuss this. The third example
  569.    discusses the declaration of machine-portable 32-bit integers.
  570.    Declaring objects of type Integer can be highly non-portable, and of
  571.    course type Long_Integer may not exist on some compilers.
  572.  
  573.     Example 1.
  574.  
  575.    Suppose you declare arrays using the above indices:
  576.  
  577.      type Time_Series is array (Time_Series_Index) of Float;
  578.      type Y_Axis_Data is array (Data_Index)        of Float;
  579.  
  580.      Measurement : Time_Series;
  581.  
  582.  
  583.    Now if you mistakenly try to iterate over one array with the index of
  584.    the other, the compiler can catch the error at compile time:
  585.  
  586.      for I in Data_Index loop
  587.         Sum := Sum + Measurement(I);  -- compilation error
  588.      end loop;
  589.  
  590.  
  591.     Example 2.
  592.  
  593.    This is lifted from Tucker Taft's brief introduction to Ada 95 in the
  594.    contributed papers section of the Ada World Wide Web homepage. Here
  595.    Tucker uses the Ada 95 unsigned integers, called modular types, in the
  596.    implementation of a protected type, which defines a disk control unit.
  597.    Modular types are integer types with "and", "or" and "xor" defined, so
  598.    systems programmers are likely to use them as bit masks. Just as the
  599.    array indices of the 2 arrays defined above are never meant to be
  600.    mixed, the modular integer types used to implement the disk control
  601.    unit are never meant to be mixed. To make sure the compiler enforces
  602.    this, they are declared as distinct types:
  603.  
  604.      type Flags   is mod 2**4;  -- a 4-bit flags field
  605.      type Control is mod 2**4;  -- A 4-bit control field
  606.  
  607.      Status_Mask  : constant Flags := 2#1001#;   -- Set first and last bits.
  608.      Status_Ready : constant Flags := 2#1000#;   -- Status = Ready
  609.  
  610.      Start_Xfr    : constant Control := 2#0001#; -- Initiate xfr command
  611.  
  612.  
  613.    Now if someone attempts to apply a Flag variable where a Control
  614.    variable should be used (or vice-versa) the compiler will catch the
  615.    error. This is especially important when the code is maintained by
  616.    programmers who did not write it.
  617.  
  618.     Remarks on Examples 1 and 2.
  619.  
  620.    1. Notice that in both examples the programmer was able to state his
  621.    intentions rather forcefully in the code - intentions that otherwise
  622.    might have been expressed much less forcefully in comment statements.
  623.    Because of Ada's strong typing model, the compiler was able to catch
  624.    errors at compile-time when the programmer's intentions were violated.
  625.  
  626.  
  627.    2. Notice also that the Integer declarations in the 2 examples are
  628.    machine portable, unlike Integer and Long_Integer. A compiler will
  629.    typically map these integer types onto the most efficient base type
  630.    that is available on the target machine.
  631.  
  632.     Example 3.
  633.  
  634.    Although the examples given above are good ones, it is not necessarily
  635.    a common practice to define a large number of distinct integer types.
  636.    In many cases it is appropriate to use (say) a 32-bit integer (or a
  637.    small number of such types) and declare appropriate subtypes of it
  638.    (them). To declare a portable 32-bit integer (or more accurately, the
  639.    most efficient integer that is at least 32-bits):
  640.  
  641.    type Int_tmp is range -2**31+1 .. 2**31-1;
  642.    type Integer_32 is range Int_tmp'Base'First..Int_tmp'Base'Last;
  643.  
  644.  
  645.    A compiler may reject this declaration if no suitable base type is
  646.    available, but this is rare. What happens is this: in order to
  647.    implement Int_tmp, the compiler chooses as the base type of Int_tmp an
  648.    integer type that is available on the target machine. This base type
  649.    is usually the most efficient integer that accomodates the range of
  650.    Int_tmp, which in turn is usually the machine's 32-bit integer. (It
  651.    might even be a 64-bit integer on some machines, in which case
  652.    Integer_32'Size = 64, and Integer_32'Last = 2**63-1. Maybe we should
  653.    not call it Integer_32!)
  654.  
  655.  
  656. 3.12: Since I can always declare my own portable integer types, why would I
  657. ever want to use the predefined type Integer?
  658.  
  659.    The language itself provides some guidance here. The predefined type
  660.    Integer is used by Ada in the implementation of a number of convenient
  661.    services. The following examples describe some of these services.
  662.    Notice that in most of the following examples, it is unlikely that it
  663.    will ever matter whether or not the predefined type Integer is
  664.    16-bits, 32-bits, 48-bits, or 64-bits.
  665.  
  666.    a) The exponentiation of X (written X**N) is defined by the language
  667.    for any floating point or integer X, provided N is of type Integer. (N
  668.    should be non-negative for integer X though.)
  669.  
  670.    b) Ada's predefined String type (really just a packed unconstrained
  671.    array of characters) uses an index of subtype Positive (i.e. type
  672.    Integer).
  673.  
  674.    c) The array index in the following "short-hand" array declaration is
  675.    implicitly defined to be type Integer:
  676.  
  677.      A : array(10..40) of Float;
  678.  
  679.  
  680.    d) The loop parameter I in the following for loop is implicitly
  681.    declared type Integer:
  682.  
  683.      for I in 10..40 loop
  684.       ...
  685.      end loop;
  686.  
  687.  
  688.    This application of type Integer is the one most likely to get you
  689.    into portability trouble. If you write: "for I in 1..2**17 loop", then
  690.    you get a constraint error on compilers that make Integer 16-bits,
  691.    because 2**17 is out of range of any Ada 16-bit integer.
  692.  
  693.  
  694. 3.13: I am learning Ada. Can I experiment with a game program?
  695.  
  696.    Of course.
  697.  
  698.    The Public Ada library (FTP wuarchive.wustl.edu) has a portable Ada
  699.    Tetris program in the languages/ada/misc/games directory. It uses
  700.    tasking, keyboard input, and ANSI screen graphics. Have fun!
  701.  
  702.    There is also "program Small", a tiny text adventure program, that you
  703.    can expand; it is documented at URL
  704.    http://lglwww.epfl.ch/Ada/Tutorials/Lovelace/small.htm
  705.  
  706.  
  707. 3.14: How can I do a non-blocking, keystroke-at-a-time read from the terminal?
  708.  
  709.    Use the procedure Text_IO.Get_Immediate [RM95 A.10.7(11)].
  710.  
  711.    If you don't have an Ada 95 compiler but have a POSIX binding, there
  712.    is a package using POSIX services that provides non-blocking,
  713.    keystroke-at-a-time access to the terminal. It is available by FTP in
  714.    file ftp://lglftp.epfl.ch/pub/Ada/FAQ/inkey.ada
  715.  
  716.      _________________________________________________________________
  717.  
  718. 4: Advantages of Ada
  719.  
  720. 4.1: Why use Ada?
  721.  
  722.    Think of it like this: We're the kid on the street corner, licking
  723.    that tasty ice cream cone on a hot summer day; an impish grin
  724.    decorates our face as we consume our cool confection. Meanwhile, other
  725.    kids gather round, noticing our pleasure. It matters not a whit that
  726.    they've just had a drink, or had their fill with supper -- they now
  727.    want ice cream. We offer no lecture on how good the ice cream is, we
  728.    simply demonstrate that we are happy, and let their memories carry
  729.    them to the nearest ice cream truck.
  730.  
  731.    (Sorry, I got a little carried away --DW).
  732.  
  733.  
  734. 4.2: Ada seems large and complex, why is it this way?
  735.  
  736.    (Robert Dewar, lead designer of the GNU Ada compiler, responds):
  737.  
  738.    During the Ada 9X development process we have often had fierce
  739.    arguments over the need to simplify proposals, and I pointed out some
  740.    time ago that the idea of simplicity is heavily overloaded:
  741.      * simple to implement
  742.      * simple to describe informally
  743.      * simple to describe formally
  744.      * results in simple programs
  745.      * simple to understand and/or remember
  746.      * short to describe
  747.  
  748.  
  749.    None of these goals are quite the same, and often they severely
  750.    conflict.
  751.  
  752.    If you listen to programming language design types, especially from
  753.    universities, they often have very little experience in programming,
  754.    and especially little experience in writing large delivered,
  755.    maintained software. That doesn't mean they know nothing about
  756.    programming languages, but it does tend to mean that their view of
  757.    complexity is skewed, and in particularly concentrates on the
  758.    simplicity of the language itself, rather than on the simplicity of
  759.    resulting programs.
  760.  
  761.    A lot of the creative tension in the 9X design process arose from this
  762.    same fundamental dichotomy. The design team tended to have a high
  763.    tolerance for language complexity (partly because they were very good
  764.    at understanding language details), but had a lot of experience in
  765.    actual large scale programming, and so their idea of simplicity was
  766.    biased heavily to simplifying Ada programs. The opposite voice,
  767.    worried about the simplicity of the language itself, represented by a
  768.    section of the DR's and ISO group (who, being a larger more diverse
  769.    group tended to reflect a wider view), considered that the design team
  770.    had gone too far in this direction. If you want to get a feel for the
  771.    transitions, look at the early versions of the 9X ILS, particularly
  772.    version 1.0.
  773.  
  774.    In retrospect, I think we came up with what is at least very close the
  775.    optimal balance. Tuck can speak for himself here more clearly than I
  776.    can speak for him, but I would guess that he and the other members of
  777.    the team recognize that you have to be able to sell the resulting
  778.    design as an acceptably simple whole, and thus must step back from the
  779.    most extensive proposals, while on the other hand, the more
  780.    conservative KISS sentiments were convinced to accept more features
  781.    than they originally felt comfortable with because of convincing
  782.    programming examples and discussions of resulting programming
  783.    complexity. The third wing of opinion ("I don't care what you think,
  784.    but if we can't implement it, then it's not much use!") was also
  785.    effectively fed in from the user-implementor teams.
  786.  
  787.    Is the result too complex? Time will tell, but I think the balance is
  788.    a successful blend.
  789.  
  790.  
  791. 4.3: Is there a contest with fame and money for good Ada programmers?
  792.  
  793.    Yes, they should enter the Ada Lovelace Programming Contest sponsored
  794.    by the Ada Resource Association (ARA).
  795.  
  796.    The Ada contest seeks to recognize the most readable, original,
  797.    reusable, and clear working Ada programs. Like the Ada programming
  798.    language, the contest is named in honor of the first programmer in
  799.    history, Lady Ada Lovelace.
  800.  
  801.    Every three months, the ARA will pay US$ 750 to the best Ada code
  802.    segment submitted. Submissions must be received by the 15th (midnight)
  803.    of the "contest month" and the award will be announced at the end of
  804.    the second month. A submission is made by emailing the source code to
  805.  
  806.         ara-contest@ocsystems.com
  807.  
  808.    The first contest closed December 15th, 1995. (The next contest month
  809.    is March 1996.)
  810.  
  811.    The rules and guidelines of the contest are available from the Ada
  812.    Contest WWW Home at http://lglwww.epfl.ch/Ada/Contest/announce.html
  813.  
  814.    This contest is open to all. Sharpen your designs, code, comments, and
  815.    demos; show the world how good and unobfuscated your Ada code is, and
  816.    win the prize!
  817.