home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson1.les < prev    next >
Encoding:
Text File  |  1995-12-07  |  16.9 KB  |  434 lines

  1. <COMMENT This is the lesson file for the Lovelace Ada tutorial, lesson 1.>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic and other WWW browsers.>
  4.  
  5. <TUTOR NAME="Lovelace">
  6. <LESSON NUMBER=1>
  7. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  8. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  9.  
  10. <COMMENT $Id: lesson1.les,v 1.10 1995/09/22 21:38:21 wheeler Exp wheeler $ >
  11.  
  12. <COMMENT A lesson is divided into 1 or more "sections".>
  13.  
  14. <SECTION NAME="What is Ada?">
  15. Ada is a computer programming language originally designed to support the
  16. construction of long-lived, highly reliable software systems.
  17. Its design emphasizes readability, avoids error-prone notation,
  18. encourages reuse and team coordination, and it is designed to be
  19. efficiently implementable.
  20. <P>
  21. A significant advantage of Ada is its reduction of debugging time.
  22. Ada tries to catch as many errors as reasonably possible, as early as possible.
  23. Many errors are caught at compile-time by Ada that aren't caught or
  24. are caught much later by other computer languages.
  25. Ada programs also catch many errors at run-time if they can't be caught
  26. at compile-time (this checking can be turned off to improve performance
  27. if desired).
  28. In addition, Ada includes a problem (exception) handling mechanism so that
  29. these problems can be dealt with at run-time.
  30.  
  31. <H2>Who uses Ada?</H2>
  32.  
  33. Ada was originally designed for the U.S. Department of Defense (DoD) for
  34. real-time embedded systems, and there's a U.S. law
  35. mandating Ada's use in DoD software development projects
  36. (with various exceptions and waiver provisions).
  37. Ada is the most commonly used language in U.S. weapons systems modernization
  38. <A HREF="doduse.htm">(more
  39. information about the DoD use of Ada is available).</A>
  40. <P>
  41. However, Ada's user base has expanded far beyond the U.S. DoD
  42. to many other areas such as
  43. large-scale information systems, distributed systems,
  44. and scientific computation.
  45. Major Ada niches include aerospace and safety-critical systems.
  46. An <A HREF="http://www.comlab.ox.ac.uk/archive/safety/lang-survey.html">informal
  47. 1994 survey</A>
  48. concluded that Ada was the most popular language for safety-critical systems.
  49. <P>
  50. People use Ada for small projects as well as large ones, since Ada's
  51. error-catching capabilities (both compile-time and run-time)
  52. significantly reduce debugging time.
  53. Also, Ada's parallel constructs can take advantage of today's more advanced
  54. operating systems (such as Microsoft's Windows NT, Windows 95, and Mach).
  55. <P>
  56. Many people use Ada when the application must run quickly.
  57. The Ada programming language was designed to be efficiently
  58. implementable, since one of its key application domains is in real-time
  59. embedded systems (where efficiency is critical).
  60. The actual efficiency of an Ada program, of course,
  61. depends on the the algorithms selected and the actual Ada compiler used.
  62. The first Ada compilers, like many other first compilers of a given language,
  63. generated inefficient code; modern Ada compilers
  64. generally generate relatively good code.
  65. Sadly, the performance of the initial Ada compilers created a myth of
  66. slow execution that is only beginning to disappear.
  67. The best test of efficiency, of course,
  68. is to benchmark a specific compiler with the
  69. type of problem you wish to solve.
  70.  
  71. <H2>Where and how is Ada standardized?</H2>
  72.  
  73. Ada was originally standardized by ANSI in 1983
  74. (ISO released an equivalent standard in 1987).
  75. Ada was recently revised to add some new capabilities;
  76. this revision is called `Ada 9X' or `Ada 95'.
  77. <!-- Its revision was actually completed in 1994, -->
  78. <!-- but due to paperwork delays at -->
  79. <!-- ISO it's technically considered a 1995 standard. -->
  80. Currently Ada compiler vendors are updating their compilers to
  81. add the new Ada 95 features.
  82. This tutorial covers the capabilities of Ada 95, but will note which
  83. features are new to Ada 95.
  84. <P>
  85. Ada is officially defined in its <EM>language reference manual</EM> (LRM).
  86. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-toc.html">The
  87. complete Ada LRM is available on-line as a hypertext document</A>.
  88. However, the LRM is not intended to be a tutorial and can be
  89. hard to understand if you're not already somewhat familiar with Ada.
  90. We will often refer to the LRM, and feel free to look at it if you're
  91. interested in more detail about a particular subject.
  92. <P>
  93. Ada was not designed by a committee.
  94. The original Ada design was the winner of a language design competition;
  95. the winning team was headed by Jean Ichbiah
  96. (Ichbiah's language was called "Green").
  97. The 1995 revision of Ada (Ada 95)
  98. was developed by a small team led by Tucker Taft.
  99. In both cases, the design underwent a public comment period where the
  100. designers responded to public comments.
  101. <P>
  102.  
  103. <H2>What exactly are Ada's capabilities?</H2>
  104. <OL>
  105. <LI>Packages (modules) of related types, objects,
  106. and operations can be defined.
  107. <LI>Packages and types can be made generic (parameterized through a template)
  108. to help create reusable components.
  109. <LI>Errors can be signalled as exceptions and handled explicitly.
  110. Many serious errors (such as computational overflow and invalid
  111. array indexes) are automatically caught and handled
  112. through this exception mechanism, improving program reliability.
  113. <LI>Tasks (multiple parallel threads of control) can be created and communicate.
  114. This is a major capability not supported in a standard way by many
  115. other languages.
  116. <LI>Data representation can be precisely controlled to support
  117. systems programming.
  118. <LI>A predefined library is included; it provides input/output (I/O),
  119. string manipulation, numeric functions,
  120. a command line interface, and a random number generator
  121. (the last two were available in Ada 83, but are standardized in Ada 95).
  122. <LI>Object-oriented programming is supported (this is a new feature of Ada 95).
  123. In fact, Ada 95 is the first standard object-oriented programming language.
  124. <LI>Interfaces to other languages (such as C, Fortran, and COBOL)
  125. are included in the language (these have been significantly
  126. enhanced in Ada 95).
  127. At least <A HREF="http://www.inmet.com/">one compiler vendor</A>
  128. generates
  129. <A HREF="http://www.yahoo.com/Computers/Languages/Java/">Java j-code</A>
  130. from Ada.
  131. </OL>
  132.  
  133. <H2>What's available for Ada?</H2>
  134. <P>
  135. There are
  136. <A HREF="http://lglwww.epfl.ch/Ada/Resources/Compilers/Compilers.html">many
  137. Ada compilers</A>, including
  138. <A HREF="http://lglwww.epfl.ch/Ada/Resources/Compilers/GNAT.html">a
  139. free Ada 95 compiler called GNAT
  140. based on the Free Software Foundation's gcc</A>.
  141. <A HREF="http://lglwww.epfl.ch/Ada/Resources/Tools/Tools.html">There
  142. are also many Ada-related tools</A>
  143. and
  144. <A HREF="http://lglwww.epfl.ch/Ada/Resources/References.html">on-line
  145. reference documents</A>.
  146. <A HREF="s14-f.htm">A later section of this tutorial
  147. provides more information about on-line Ada information sources.</A>
  148.  
  149. <QUESTION Type=Multiple-Choice>
  150. Which of the following is <EM>not</EM> an Ada capability?
  151. <CHOICES>
  152. <CHOICE ANS=1>Reduce errors and debugging time through early detection
  153. <CHOICE ANS=2>Object-oriented programming
  154. <CHOICE ANS=3>Efficiently implementable
  155. <CHOICE ANS=4>None of the above - Ada has all three of these capabilities.
  156. </CHOICES>
  157. <ANSWER ANS=4>
  158. <RESPONSES>
  159. <WHEN ANS=1>
  160. No, sorry.
  161. Error reduction is an important capability of Ada.
  162. Ada constructs have been specifically designed to help detect
  163. errors as early as possible.
  164. <WHEN ANS=2>
  165. No, sorry, that's not right.
  166. It <EM>is</EM> true that the older Ada 83 didn't completely support
  167. object-oriented (OO) programming.
  168. Ada 83 was the closest non-OO programming language to support OO programming,
  169. and so it was often called an `object-based' language.
  170. Ada 95 has enhanced Ada 83 to become a fully object-oriented programming
  171. language, including inheritance.
  172. <WHEN ANS=3>
  173. No, sorry.
  174. Ada <EM>is</EM> efficiently implemented.
  175. The very first Ada compilers, like most first compilers for a language,
  176. were not very efficient, but they came out in the early 1980s.
  177. Many of todays' Ada compilers are extremely efficient.
  178. Indeed, there's a documented case where an Ada
  179. compiler did better than assembly language programmers
  180. (see ``Ada Whips Assembly'' by Elam and Lawlis in the
  181. March 1992 issue of <EM>Crosstalk</EM>).
  182. <P>
  183. Ada compilers can generate code comparable to C, C++, and Pascal compilers.
  184. Because of the way Ada was designed, Ada compilers
  185. can sometimes generate better code because they have more information that
  186. can be used for code optimization.
  187. For example, in a large number of cases an Ada compiler can do better
  188. than a C or C++ compiler can while passing parameters, because
  189. an Ada compiler knows when aliasing is not possible.
  190. <P>
  191. Some Ada compilers, such as the GNAT and DEC VAX Ada compilers,
  192. use the same (back-end) code generators as other many other languages,
  193. and on those systems the Ada compiler's results should be at least as
  194. good as for other languages.
  195. <P>
  196. It is true that Ada run-time error-checking capabilities require some
  197. time and space, but they can be selectively or completely turned off.
  198. Since Ada compilers can often detect and optimize these away, they
  199. often do not have as much of a performance hit as you might expect even
  200. when left in.
  201. <WHEN ANS=4>
  202. Yes, that's right.
  203. </RESPONSES>
  204.  
  205. <SECTION NAME="Simple Program">
  206. Here's a simple program in Ada that simply prints a message
  207. (this is often called the <EM>hello, world!</EM> program):
  208. <P>
  209. <TEXT FONT=PRE FILE="hello.adb">
  210. <P>
  211. Here's an explanation of each line:
  212. <OL>
  213. <LI>
  214. The first line illustrates a comment; Ada comments begin with ``--''
  215. and end at the end of the line
  216. (C++ comments that begin with // work the same way).
  217. <LI>
  218. The second line illustrates a <EM>with clause</EM>, which
  219. specifies the <EM>library units</EM> (other items) that we need.
  220. This particular <EM>with clause</EM>
  221. specifies that we need <EM>Ada.Text_IO</EM>.
  222. <EM>Ada.Text_IO</EM> is a predefined
  223. library unit that provides operations to perform
  224. basic text input and output.
  225. <LI>
  226. The third line states that we're defining a new procedure
  227. named <EM>Hello</EM>. Note that in Ada there's nothing special about
  228. the name of the main program (in C and C++, it must be called <EM>main</EM>,
  229. and in Pascal, it must be specially identified as the <EM>program</EM>).
  230. <LI>
  231. The fourth line just has the keyword <EM>begin</EM>, which begins
  232. the definition of the procedure <EM>Hello</EM>.
  233. <LI>
  234. The fifth line calls Ada.Text_IO's procedure <EM>Put_Line</EM>, which prints
  235. a line to the current output (usually the screen) and then ends the
  236. current line.
  237. The basic syntax for calling a procedure is to give the library unit name,
  238. a period, the name of the procedure, and then list the parameters (if any)
  239. enclosed in parentheses (we'll see how to simplify this soon).
  240. In Ada, strings are surrounded by double quotes (the same as C and C++;
  241. Pascal uses single quotes).
  242. <LI>
  243. The last line ends the definition of the procedure.
  244. </OL>
  245. <P>
  246. Ada terminates each statement with a semicolon.
  247. This is like C and C++ and unlike standard Pascal
  248. (which uses semicolons as statement separators).
  249. <P>
  250. Just a quick note - if you're using an Ada 83 compiler
  251. instead of an Ada 95 compiler,
  252. please see the <A HREF="note83.htm">note about using Ada 83</A>
  253. before compiling this program.
  254. <P>
  255. <QUESTION Type=Multiple-Choice>
  256. What is the name of the new procedure defined above?
  257. <CHOICES>
  258. <CHOICE ANS=1>Text_IO
  259. <CHOICE ANS=2>Hello
  260. <CHOICE ANS=3>Put_Line
  261. </CHOICES>
  262. <ANSWER ANS=2>
  263. <RESPONSES>
  264. <WHEN ANS=1>
  265. No, sorry. Text_IO is the name of a predefined library
  266. unit that provides us with operations to perform
  267. basic text input and output.
  268. <WHEN ANS=2>
  269. Right, `Hello' is the name of the procedure defined.
  270. <P>
  271. If you're interested in compiling this and other sample programs
  272. with a real Ada compiler,
  273. <A HREF="how2comp.htm">an overview on how to use Ada compilers
  274. is available.</A>
  275. <WHEN ANS=3>
  276. No, sorry. Put_Line is the name of a predefined procedure
  277. that prints text.
  278. </RESPONSES>
  279.  
  280. <SECTION NAME="Use Clauses">
  281. Some programs might be very wordy if you had to always specify
  282. where a procedure is defined in order to use it.
  283. Thus, Ada provides the <EM>use clause</EM>.
  284. Whenever you use a procedure (or something else) but do not specify
  285. where it is defined, the Ada compiler will search all units
  286. listed in applicable <EM>use clause</EM>s.
  287. Use clauses follow the <EM>with clause</EM>,
  288. begin with the keyword <EM>use</EM>,
  289. and then list the library units to be searched.
  290. Here's how that first program would look with a <EM>use clause</EM>:
  291.  
  292. <PRE>
  293. -- Print a simple message to demonstrate a trivial Ada program.
  294. with Ada.Text_IO;
  295. use Ada.Text_IO;  -- use clause - automatically search Ada.Text_IO.
  296. procedure Hello is
  297. begin
  298.  Put_Line("Hello, world!"); -- Note: No longer has "Ada.Text_IO" in front.
  299. end Hello;
  300. </PRE>
  301.  
  302. <QUESTION TYPE=Multiple-Choice>
  303. If, in this new version of the program, you changed the second-to-last-line
  304. back to:
  305. <BR>
  306. Ada.Text_IO.Put_Line("Hello, world!");
  307. <BR>
  308. would the program still work?
  309.  
  310. <CHOICES>
  311. <CHOICE ANS=1>Yes
  312. <CHOICE ANS=2>No
  313. </CHOICES>
  314. <ANSWER ANS=1>
  315. <RESPONSES>
  316. <WHEN CORRECT>
  317. Right. The `use' clause simply means you don't have to include
  318. the name of the unit where `Put_Line' is defined, but you can do
  319. so if you wish to.
  320. <P>
  321. Ada includes some other mechanisms to reduce program wordiness.
  322. For example, you can "rename" packages so you can use shorter names.
  323. <P>
  324. Package "Ada.Text_IO" is used very often, so Ada predefines
  325. "Text_IO" as a shorter name for "Ada.Text_IO".
  326. <COMMENT Actually, this predefinition is more to support transition >
  327. <COMMENT from Ada 83, but this is the simplest place I can find to >
  328. <COMMENT introduce the "with"ing of just Text_IO. >
  329. <WHEN ANS=2>
  330. No, sorry.
  331. The `use clause' specifies where the Ada compiler should search,
  332. but you can always fully qualify the name of a procedure.
  333. </RESPONSES>
  334.  
  335. <SECTION NAME="Simple Variable, Integers, Parameters and Exceptions">
  336. Let's create a program to show what a simple variable and
  337. parameter-passing look like.
  338. This program will print out powers of 2, starting with 1, ``forever''.
  339. We'll call this program procedure Compute.
  340. <P>
  341. Inside this procedure
  342. create a local procedure called Double which doubles any number given to it,
  343. and a local integer variable named `X'.
  344. <P>
  345. <PRE>
  346. -- Demonstrate a trivial procedure, with another nested inside.
  347. with Ada.Text_IO, Ada.Integer_Text_IO;
  348. use Ada.Text_IO, Ada.Integer_Text_IO;
  349.  
  350. procedure Compute is
  351.  
  352.  procedure Double(Item : in out Integer) is
  353.  begin -- procedure Double.
  354.    Item := Item * 2;
  355.  end Double;
  356.  
  357.  X : Integer := 1;   -- Local variable X of type Integer.
  358.  
  359. begin -- procedure Compute
  360.  loop
  361.   Put(X);
  362.   New_Line;
  363.   Double(X);
  364.  end loop;
  365. end Compute;
  366. </PRE>
  367. <P>
  368. Note that the local variable called `X'
  369. is of type Integer with an initial value of one.
  370. Integers are used when you want to store possibly signed integers,
  371. and you don't care what the minimum and maximum range is.
  372. As we'll see later, there are other things you should do if you
  373. <EM>do</EM> care what the minimum and/or maximum range is.
  374. <P>
  375. Inside this new procedure is a local procedure called `Double', which
  376. takes a value and doubles it.
  377. A local procedure, like a local variable, can only be used inside
  378. the procedure surrounding it.
  379. This capability to nest procedures inside other procedures
  380. is useful in larger programs and is a standard
  381. capability in Pascal (though not in C or C++).
  382. <P>
  383. The phrase `in out' means that the value is both received and changed
  384. in the procedure.
  385. <P>
  386. The phrase "with .. Ada.Integer_Text_IO" permits use of
  387. a predefined Ada 95 package for performing text input and output on Integers.
  388. It includes an operation named "Put" that will print an Integer sent to it.
  389. <P>
  390. The second `begin' statement defines the Compute procedure itself.
  391. Compute has an infinite loop, which prints the current value
  392. and doubles it repeatedly.
  393. `Put' prints out the number, and `New_Line' causes the text
  394. to go to the next line.
  395. <P>
  396. Computers can't really compute an infinitely large value; sooner
  397. or later they'll run out of space to store the number.
  398. What will happen in this case?
  399. Some programming languages (notably C) simply permit garbage to be computed.
  400. Ada has a better approach: when a computation (such as doubling)
  401. cannot be performed, Ada raises an `exception'. Thus, sooner or later
  402. this program will halt with a message explaining why and where it halted.
  403. As we'll learn later, these exceptions can be caught and handled
  404. inside the program.
  405. <P>
  406. <QUESTION TYPE=Multiple-Choice>
  407. When a computation cannot be performed, what does Ada normally do?
  408. <CHOICES>
  409. <CHOICE ANS=1>Silently give the wrong answer
  410. <CHOICE ANS=2>Raise an exception
  411. <CHOICE ANS=3>Corrupt memory
  412. </CHOICES>
  413. <ANSWER ANS=2>
  414. <RESPONSES>
  415. <WHEN CORRECT>
  416. Right.
  417. We'll learn more about exceptions later, but the key idea now
  418. is that Ada normally detects errors as soon as they occur.
  419. If you're curious, the name of the exception this program will raise is
  420. "Constraint_Error".
  421. <P>
  422. Also, a quick note for GNAT Ada compiler users - currently
  423. GNAT only detects this
  424. particular error (integer overflow)
  425. at run-time if you give it the "-gnato" option.
  426. There are plans for this to eventually become the default in GNAT;
  427. see the GNAT documentation for more information.
  428.  
  429. <WHEN ANS=1>
  430. No, sorry.
  431. <WHEN ANS=3>
  432. No, certainly not.
  433. </RESPONSES>
  434.