home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson2.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  11.7 KB  |  335 lines

  1. <COMMENT This is the lesson file for the Lovelace Ada tutorial, lesson 2.>
  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=2>
  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: lesson2.les,v 1.7 1995/05/17 21:25:18 wheeler Exp $ >
  11.  
  12. <COMMENT A lesson is divided into 1 or more "sections".>
  13.  
  14. <SECTION NAME="Program Units">
  15. Now that we have very brief flavor of what Ada code looks like, we
  16. need to define some key terms.
  17. <P>
  18. More formally, an Ada program is composed of one or more <EM>program units</EM>.
  19. A program unit can be a:
  20. <OL>
  21. <LI>
  22. <EM>subprogram</EM>, which define executable algorithms.
  23. Procedures and functions are both subprograms.
  24. <LI>
  25. <EM>package</EM>, which defines a collection of entities.
  26. Packages are the main grouping mechanism in Ada, somewhat analogous
  27. to Modula's "module".
  28. <LI>
  29. <EM>task unit</EM>, which defines a computation that can occur in
  30. parallel with other computations.
  31. <LI>
  32. <EM>protected unit</EM>, which can coordinate data sharing
  33. between parallel computation.  This did not exist in Ada 83.
  34. <LI>
  35. <EM>generic units</EM>, which helps to make reusable components
  36. (C++'s templates are similar).
  37. </OL>
  38. <P>
  39. The latter three are advanced topics, so we will concentrate for
  40. now on packages and subprograms.
  41. <P>
  42. The <EM>package</EM> is structurally the most important kind of program unit.
  43. Most Ada programs are basically a set of a large number of packages, with
  44. one procedure used as the ``main'' procedure to start the Ada program.
  45. <P>
  46. <QUESTION Type=Multiple-Choice>
  47. In section 1-1 we defined a simple procedure called Hello.
  48. What kind of program unit was procedure Hello?
  49. <CHOICES>
  50. <CHOICE ANS=1>Subprogram
  51. <CHOICE ANS=2>Package
  52. <CHOICE ANS=3>Task Unit
  53. </CHOICES>
  54. <ANSWER ANS=1>
  55. <RESPONSES>
  56. <WHEN ANS=1>
  57. Right. A procedure is a kind of subprogram, and a subprogram is a kind of
  58. program unit.
  59. <WHEN ANS=2>
  60. No, sorry. While packages are very important to Ada, procedure Hello
  61. was not a package.
  62. <WHEN ANS=3>
  63. No, sorry.
  64. </RESPONSES>
  65.  
  66. <SECTION NAME="Declarations and Bodies">
  67. Program units (including subprograms and packages)
  68. normally consist of two parts:
  69. <UL>
  70. <LI>
  71. The <EM>declaration</EM>, which contains information that must
  72. be visible to some other program units. The declaration defines the interface
  73. for a program unit.
  74. Sometimes people refer to a declaration as a <EM>specification</EM>.
  75. They are somewhat analogous to the contents of C ``.h'' files.
  76. <LI>
  77. The <EM>body</EM>, which contains implementation details that need not
  78. be visible to other parts.
  79. They are somewhat analogous to the contents of C ``.c'' files.
  80. </UL>
  81. <P>
  82. These separate parts of program units are usually stored in separate files.
  83. This explicit distinction between declaration and body allows a program
  84. to be designed, written, and tested as a set of largely independent
  85. software components.
  86. <P>
  87. There are two special cases to help make programming easier:
  88. <OL>
  89. <LI>
  90. Separate declarations are not <EM>required</EM> for subprograms
  91. (procedures and functions). If a subprogram has a body but no declaration,
  92. the body of a subprogram can serve as its own declaration.
  93. This makes writing the `hello, world' program in lesson 1 easier -
  94. technically, that simple program is a procedure body that
  95. automatically gives its own declaration.
  96. <LI>
  97. For some packages, it's not possible to have implementation details.
  98. For example, a package declaration could be just
  99. a collection of constants (like pi and the square root of 2).
  100. In this case, the package must not have a body, since one isn't needed.
  101. This is relatively rare - most packages need
  102. both a declaration and a body.
  103. </OL>
  104. <P>
  105. <QUESTION Type=Multiple-Choice>
  106. Which part of a program unit contains the implementation details?
  107. <CHOICES>
  108. <CHOICE ANS=1>Declaration
  109. <CHOICE ANS=2>Body
  110. </CHOICES>
  111. <ANSWER ANS=2>
  112. <RESPONSES>
  113. <WHEN ANS=1>
  114. No, sorry.
  115. <WHEN ANS=2>
  116. Right. That was an easy question, but the distinction is important.
  117. </RESPONSES>
  118.  
  119. <SECTION NAME="Packages">
  120. The package is Ada's basic unit for defining a collection of logically
  121. related entities.
  122. For example, a package can be used to define a set of type declarations
  123. and their associated operations.
  124. <P>
  125. For example, all Ada compilers provide a package called <EM>Text_IO</EM>.
  126. Here's a highly simplified version of the package declaration of the
  127. package Text_IO
  128. (the lengthy complete definition is
  129. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-toc-A-10.html">part
  130. of the LRM's appendix A</A>):
  131. <P>
  132. <PRE>
  133. package Text_IO is
  134.   type File_Type is limited private;
  135.   type File_Mode is (In_File, Out_File, Append_File);
  136.   procedure Create (File : in out File_Type;
  137.                     Mode : in File_Mode := Out_File;
  138.                     Name : in String := "");
  139.   procedure Close (File : in out File_Type);
  140.   procedure Put_Line (File : in File_Type; Item : in String);
  141.   procedure Put_Line (Item : in String);
  142. end Text_IO;  
  143. </PRE>
  144. <P>
  145. The package declaration of package Text_IO defines
  146. a type called `File_Type' that represents an
  147. opened or created file. The phrase `limited private' means that there
  148. are no predefined operations on this type (more about that later).
  149. <P>
  150. Package Text_IO also defines a type called `File_Mode'; values of this type
  151. can only have one of three values (this is how enumeration types are defined
  152. in Ada).
  153. <P>
  154. The type definitions are followed by a set of subprograms that can accept
  155. values of type File_Type. Note that procedures (subprograms) can be
  156. contained inside packages.
  157. Procedure `Create' lets you create files with given names;
  158. procedure `Close' closes a file.
  159. <P>
  160. There are two procedures named `Put_Line' which write text out,
  161. but they differ
  162. in the arguments they can accept. The first takes a file and the
  163. string to be output, while the other just takes the string to be output.
  164. <P>
  165. If a package declaration includes other declarations inside it
  166. then there must be a package body somewhere that includes the bodies of
  167. the items declared.
  168. This simplified package declaration for Text_IO
  169. has procedure declarations, so somewhere else
  170. there must be a package body for Text_IO.
  171.  
  172. <QUESTION TYPE=Multiple-Choice>
  173. In this simplified package declaration for package Text_IO,
  174. how many subprograms have been defined that explicitly require a
  175. File_Type parameter?
  176. <CHOICES>
  177. <CHOICE ANS=1>One
  178. <CHOICE ANS=2>Two
  179. <CHOICE ANS=3>Three
  180. <CHOICE ANS=4>Four
  181. </CHOICES>
  182. <ANSWER ANS=3>
  183. <RESPONSES>
  184. <WHEN CORRECT>
  185. Right. They are Create, Close, and one of the Put_Line procedures.
  186. <WHEN ANS=1>
  187. No, sorry.  Keep looking.
  188. <WHEN ANS=2>
  189. No, sorry.  Keep looking.
  190. <WHEN ANS=4>
  191. This package does have explicit definitions of four subprograms,
  192. but not all of them have File_Type as a parameter.
  193. Please look again.
  194. </RESPONSES>
  195. <SECTION NAME="Compilation Units">
  196. Now that we know about program units, packages, and the
  197. difference between declarations and bodies, we can talk about
  198. <EM>compilation units</EM>.
  199. <P>
  200. A compilation unit contains either the declaration or the body of
  201. a program unit,
  202. preceded by the necessary context clause (`with' or `use' clauses).
  203. Thus a compilation unit can be a package declaration, a package body,
  204. a subprogram declaration, or a subprogram body along with its context clause.
  205. <COMMENT To be honest, some program units can't be used directly as >
  206. <COMMENT compilation units, namely tasks and protected types. >
  207. <COMMENT I haven't mentioned that here because we haven't even talked >
  208. <COMMENT about them yet, and it would just be confusing at this stage. >
  209. <P>
  210. An Ada compiler only compiles collections of one or more compilation units.
  211. That's why it's important to understand compilation units -
  212. to compile something,
  213. it must be a part of a legal compilation unit.
  214. <P>
  215. Here's a simplified <A HREF="bnf.htm">BNF syntax</A> for a compilation unit
  216. (the real definition is in
  217. <A HREF="http://lglwww.epfl.ch/Ada/LRM/9X/rm9x/rm9x-10-01.html">
  218. chapter 10 of the LRM</A>):
  219. <P>
  220. <OL>
  221. <LI>
  222. <EM>compilation_unit ::= context_clause library_item</EM>
  223. <LI>
  224. <EM>context_clause ::= {context_item}</EM>
  225. <LI>
  226. <EM>context_item ::= with_clause | use_clause</EM>
  227. <LI>
  228. <EM>with_clause ::= "with" library_unit_name { "," library_unit_name} ";"</EM>
  229. <LI>
  230. <EM>use_clause ::= "use" library_unit_name { "," library_unit_name} ";"</EM>
  231. <LI>
  232. <EM>library_item ::= package_declaration | package_body |
  233. subprogram_declaration | subprogram_body</EM>
  234. </OL>
  235. <P>
  236. Note that compilation units start with "with" and "use" clauses,
  237. followed by a program unit declaration or body.
  238. We've already seen two compilation units - the simplified
  239. package declaration for Text_IO and the procedure body Hello.
  240. <P>
  241. Although most Ada compilers permit multiple compilation units in a single
  242. file, it's usually better to put separate compilation units in
  243. separate files.  One Ada compiler (GNAT)
  244. <EM>requires</EM> different compilation units to be in different files.
  245. <P>
  246. Informally, when people say `show me X's package declaration'
  247. they really mean `show me the compilation unit that includes the
  248. package declaration of package X'.
  249. <QUESTION TYPE=Multiple-Choice>
  250. Which of the following <EM>cannot</EM> be part of a compilation unit
  251. after the context clause?
  252. <CHOICES>
  253. <CHOICE ANS=1>A package declaration
  254. <CHOICE ANS=2>A procedure declaration
  255. <CHOICE ANS=3>A procedure body
  256. <CHOICE ANS=4>A type definition
  257. </CHOICES>
  258. <ANSWER ANS=4>
  259. <RESPONSES>
  260. <WHEN CORRECT>
  261. Right.
  262. Okay, this question was a little sneaky since
  263. we haven't really discussed type definitions yet, but they clearly
  264. aren't listed in the BNF defining compilation units and the
  265. others <EM>are</EM> part of the BNF.
  266. To compile type definitions you must put them in something else
  267. (such as a package declaration); we'll discuss that more later.
  268. <WHEN ANS=1>
  269. No, sorry.
  270. <WHEN ANS=2>
  271. No, sorry.
  272. <WHEN ANS=3>
  273. No, sorry.
  274. </RESPONSES>
  275. <SECTION NAME="Review of Basic Ada Structures">
  276. Let's briefly review what we've learned so far:
  277. <OL>
  278. <LI>
  279. Logically, Ada programs are composed of a set of <EM>program units</EM>.
  280. <LI>
  281. There are different kinds of program units; the ones we've concentrated
  282. on are <EM>subprograms</EM> and <EM>packages</EM>.
  283. <LI>
  284. Subprograms define processing algorithms.
  285. Subprograms can be <EM>procedures</EM> or <EM>functions</EM>.
  286. <LI>
  287. <EM>Packages</EM> are the main Ada structuring tool used to group
  288. things together.
  289. <LI>
  290. In general, a program unit has two parts, a <EM>declaration</EM>
  291. and a <EM>body</EM>.
  292. Sometimes a declaration is also called a <EM>specification</EM>.
  293. <LI>
  294. Ada compilers compile <EM>compilation units</EM>.
  295. A compilation unit is either a program unit's declaration or body,
  296. preceded by a <EM>context clause</EM>.
  297. <LI>
  298. A <EM>context clause</EM> is a set of <EM>with clauses</EM>
  299. (that state what other program units are needed) and/or
  300. <EM>use clauses</EM> (the program units to search by default).
  301. </OL>
  302. <QUESTION TYPE=Multiple-Choice>
  303. Given what you know now, is it possible for an Ada compiler to
  304. compile a package declaration (when preceded by the appropriate
  305. context clauses), even if implementation details are needed and
  306. the package body has not been developed yet?
  307. <CHOICES>
  308. <CHOICE ANS=1>Yes
  309. <CHOICE ANS=2>No
  310. </CHOICES>
  311. <ANSWER ANS=1>
  312. <RESPONSES>
  313. <WHEN CORRECT>
  314. Right.
  315. A package declaration, when preceded by the appropriate context
  316. clauses, is a compilation unit and thus can be compiled.
  317. The Ada compiler cannot create an executable program from just the
  318. declaration alone, but it can check for interface errors.
  319. <P>
  320. This makes it easier for teams to develop software - they can
  321. develop the declarations, compile them to check their accuracy,
  322. then each of them can go off and develop the bodies for different
  323. declarations.
  324. <WHEN ANS=2>
  325. No, sorry.
  326. Here are some hints:
  327. <OL>
  328. <LI>
  329. If a package declaration has the
  330. right context clause in front of it, what does it become?
  331. <LI>
  332. What does an Ada compiler compile?
  333. </OL>
  334. </RESPONSES>
  335.