Learning Algol 68 Genie
Total Page:16
File Type:pdf, Size:1020Kb
LEARNING ALGOL 68 GENIE Algol 68 Genie 2.0.0 (September 2010) Edited by Marcel van der Veer Learning Algol 68 Genie copyright c Marcel van der Veer, 2008-2010. Algol 68 Genie copyright c Marcel van der Veer, 2001-2010. Learning Algol 68 Genie is a compilation of separate and independent documents or works, consisting of the following parts: I. Informal introduction to Algol 68, II. Programming with Algol 68 Genie, III. Example a68g programs, IV. Algol 68 Revised Report, V. Appendices Part I, II, III and V are distributed under the conditions of the GNU Free Documenta- tion License: Permission is granted to copy, distribute and / or modify the text under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. See http://www.gnu.org. Part IV is a translation of the Algol 68 Revised Report into LATEX and is therefore subject to IFIP’s condition contained in that Report: Reproduction of the Report, for any purpose, but only of the whole text, is explicitly permitted without formality. Chapter 20, "Specification of partial parametrization proposal", is not a part of the Algol 68 Revised Report, and is distributed with kind permission of the author of this proposal, C.H. Lindsey. IBM is a trademark of IBM corporation. Linux is a trademark registered to Linus Torvalds. Mac OS X is a trademark of Apple Computer. Pentium is a trademark of Intel Corporation. TEX is a trademark of the American Mathematical Society. UNIX is a registered trademark of The Open Group. Wikipedia is a trademark of the Wikimedia Foundation, Inc. Typeset in LATEX. Table of contents Preface xi I Informal introduction to Algol 68 1 1 Preliminaries 3 1.1 AbriefhistoryofAlgol68 ........................... .. 3 1.2 Terminology..................................... 6 1.3 Notationofproduction-rules. ..... 7 2 Basic concepts 11 2.1 Modesandvalues.................................. 11 2.2 Integers ....................................... 12 2.3 Identity-declarations . .... 13 2.4 Reals ......................................... 15 2.5 Formulas....................................... 18 2.6 Elementarymonadic-formulas . ... 19 2.7 Realtointegerconversion . ... 19 2.8 Elementarydyadic-formulas . ... 20 2.9 Realfunctions.................................... 22 2.10 Booleans....................................... 23 2.11 Booleanoperators................................ .. 24 2.12 Charactersandtext ............................... 24 2.13 Characteroperators .............................. .. 25 2.14 Comparisonoperators . .. 25 2.15 Programstructure ................................ 27 2.16 Variables....................................... 28 2.17 Assignation ..................................... 30 2.18 Operatorscombinedwithassignation . .... 32 2.19 Orderofevaluation................................ 32 2.20 Abbreviatedvariable-declarations . ........ 33 2.21 Readingvariables................................ .. 34 2.22 Referencestonames ............................... 35 2.23 The value NIL .................................... 36 iii TABLE OF CONTENTS 2.24 Comments...................................... 36 2.25 Pragmats....................................... 37 3 Control structures 39 3.1 Introduction..................................... 39 3.2 Theconditional-clause . ... 39 3.3 Pseudo-operators ................................. 43 3.4 Identityrelations ................................ .. 44 3.5 Theinteger-case-clause . ... 45 3.6 Theloop-clause ................................... 47 4 Stowed modes 51 4.1 Introduction..................................... 51 4.2 Rows ......................................... 51 4.3 Flexiblenames ................................... 61 4.4 Vectors,matricesandtensors . .... 65 4.5 Torrixextensions ................................. 65 4.6 Anoteonbrackets ................................. 67 4.7 Structuredmodes.................................. 67 4.8 Field-selections ................................. .. 73 4.9 Mode-declarations ................................ 74 4.10 Complexnumbers.................................. 77 4.11 BYTES — a compact [] CHAR ........................... 79 4.12 BITS — a compact [] BOOL ............................ 79 5 Routines 83 5.1 Routines ....................................... 83 5.2 Routinemodes.................................... 86 5.3 Routineswithoutparameters . .. 87 5.4 Routineswithparameters. 88 5.5 Routinesandscope ................................. 91 5.6 Declaringnewoperators............................. 92 5.7 Identificationofoperators . .... 94 5.8 Recursion ...................................... 95 5.9 Recursionanddatastructures . ... 99 5.10 Partialparametrisationandcurrying . ....... 103 6 United modes 105 6.1 United-mode-declarations . 105 6.2 Unitedmodesasarguments. 107 6.3 United-case-clauses.............................. 108 6.4 Well-formedmodes ................................. 110 6.5 Equivalenceofmodes ............................... 113 iv TABLE OF CONTENTS 7 Transput 115 7.1 Transput.......................................115 7.2 Channelsandfiles ................................. 116 7.3 Transputandscope................................. 117 7.4 Readingfiles.....................................118 7.5 Writingtofiles ...................................119 7.6 Stringterminators ................................ 121 7.7 Events ........................................121 7.8 Formattingroutines ............................... 124 7.9 Straightening .................................... 126 7.10 Default-formattransput . 126 7.11 Formattedtransput............................... 128 7.12 Binaryfiles .....................................138 7.13 Using a STRING asafile..............................138 7.14 Othertransputprocedures . 139 8 Algol 68 Genie context-free syntax 141 8.1 Introduction..................................... 141 8.2 Reservedsymbols.................................. 141 8.3 Commonproduction-rules . 144 8.4 Tags..........................................145 8.5 Contexts .......................................146 8.6 Coercions.......................................147 8.7 Particularprogram................................ 151 8.8 Clauses........................................152 8.9 Units.........................................152 8.10 Enclosed-clauses ................................ 153 8.11 Balancing ......................................156 8.12 Parallel processing in a68g ............................157 8.13 Primaries ......................................158 8.14 Secondaries ..................................... 164 8.15 Tertiaries ...................................... 164 8.16 Units.........................................165 8.17 Jumps ........................................167 8.18 Assertionsaspre-orpostconditions . ....... 169 8.19 Declarations..................................... 169 8.20 Declarers.......................................171 8.21 Pragments......................................171 8.22 Refinements..................................... 172 II Programming with Algol 68 Genie 173 9 Installing and using Algol 68 Genie 175 v TABLE OF CONTENTS 9.1 Algol68Genie....................................175 9.2 Algol68Genietransput .............................. 179 9.3 InstallingAlgol68GenieonGNU/Linux . 181 9.4 Synopsis .......................................186 9.5 a68g diagnostics .................................. 188 9.6 Options........................................191 9.7 Themonitor .....................................197 9.8 ThePreprocessor .................................. 201 9.9 Amulti-passschemetoparseAlgol68. 203 9.10 a68g sourcecodechecks .............................. 204 9.11 Limitationsandbugs.............................. 205 10 Standard-prelude and library-prelude 207 10.1 Thestandard-environ . 207 10.2 Thestandard-prelude . 208 10.3 Standardmodes................................... 208 10.4 Environmentenquiries. 210 10.5 Standardoperators............................... 213 10.6 Standardprocedures .............................. 227 10.7 Transput.......................................233 10.8 Thelibrary-prelude. 242 10.9 ALGOL68C transputprocedures . 242 10.10Drawingandplotting............................... 244 10.11Mathematicalfunctions . 249 10.12Linearalgebra.................................. 253 10.13Solutionoflinearalgebraicequations . ......... 257 10.14Fouriertransforms . 260 10.15Laplacetransform ............................... 262 10.16Constants ...................................... 263 10.17Linuxextensions ................................ 271 10.18 Regular expressions in string manipulation. .......... 276 10.19Cursessupport .................................. 279 10.20PostgreSQLsupport ............................... 280 10.21PCMSounds..................................... 289 III Example a68g programs 293 11 Example a68g programs 295 11.1 Lucasnumbers ................................... 295 11.2 Romannumbers................................... 296 11.3 HolidaysrelatedtoEaster. 297 11.4 TheSierpinskitriangle . 298 11.5 Whetstonebenchmark .............................. 299 vi TABLE OF CONTENTS 11.6 Primenumbers ................................... 301 11.7 Hilbertmatrixusingfractions . ..... 305 11.8 Mastermindcodebreaker . 307 11.9 Mandelbrotset ................................... 309 11.10Knighttraversingachessboard . 310 11.11Decisiontree...................................