Ohjelmointikielten periaatteet Antti-Juhani Kaijanaho 1. marraskuuta 2010
2
Sisältö 1 Johdanto 7 1.1 Ohjelmoinnista.......................... 7 1.2 Mikä on ohjelmointikieli..................... 7 1.3 Historia............................... 9 1.3.1 Kaksi ensimmäistä sukupolvea: ennen vuotta 1955. 9 1.3.2 Automaattinen ohjelmointi ja ohjelmointikielten synty: 1955 1960....................... 10 1.3.3 Baabelin torni: 1960-luku................ 11 1.3.4 Modernismin alku: 1970-luku.............. 12 1.3.5 Olio-ohjelmoinnin ja juontokielten nousu: 1980-luku 14 1.3.6 Internetin nousu: 1990-luku............... 15 1.3.7 Tietoturva ja rinnakkaisuus: 2000-luku........ 15 1.3.8 Sukupolvista....................... 15 1.4 Kielten jaotteluja......................... 16 1.5 Ohjelmointikielten suunnitteluperiaatteita........... 17 1.6 Kielen määrittely......................... 19 1.7 Toteutustekniikoista....................... 20 2 Peruskäsitteitä 23 2.1 Metakieli kohdekieli...................... 23 2.2 Staattinen dynaaminen..................... 24 2.3 Olio................................. 24 2.4 Arvo................................ 26 2.5 Tyyppi............................... 26 2.6 (Nimetty) vakio.......................... 27 2.7 Muuttuja.............................. 27 2.8 Lohkorakenne........................... 28 3 Syntaktisia kysymyksiä 31 3.1 Lausekkeet............................. 31 3.2 Formaali kielioppi......................... 35 3
4 SISÄLTÖ 4 Denotationaalinen semantiikka 39 4.1 Perusidea.............................. 39 4.2 Muuttujat............................. 41 4.3 Suoraviivaohjelmat........................ 43 4.4 Paikalliset muuttujat....................... 44 5 Kontrollivuon ohjaus 47 5.1 Ehtolausekkeet.......................... 47 5.1.1 Erillään........................... 48 5.1.2 Yhdessä.......................... 50 5.2 Peräkkäistys............................ 51 5.3 Go to................................ 52 5.4 Valintalauseet........................... 52 5.5 Toistolauseet eli silmukat..................... 55 5.5.1 Kiintopisteteoriaa..................... 57 5.5.2 Informaatiojärjestys................... 58 5.6 Vahtikomennot.......................... 59 5.7 Väitteet............................... 62 6 Lyhytaskelsemantiikka 65 6.1 Tilat................................. 65 6.2 Päättely............................... 66 6.3 While-kieli............................. 66 6.4 Epädeterminismi......................... 67 6.5 Siirräntä.............................. 68 7 Aliohjelmat 71 7.1 Kutsusekvenssit.......................... 71 7.2 Parametrinvälitysmekanismit.................. 73 7.3 Aktivaatiotietue.......................... 74 7.4 Vuorottaisrutiinit......................... 76 7.5 Aliohjelmien formalisointia................... 76 8 Lambda-kieli 79 8.1 Syntaktiset määritelmät..................... 80 8.2 Denotationaalinen semantiikka................. 82 8.3 Sievennykset............................ 82 8.4 Churchin koodaukset....................... 83 8.5 Laskujärjestykset......................... 85
SISÄLTÖ 5 9 Oliokielten erityispiirteitä 87 9.1 Olio................................. 88 9.2 Yksinkertainen kieli........................ 89 9.3 Inkrementaalinen muokkaus.................. 92 9.3.1 Prototyyppiperintä.................... 93 9.3.2 Luokkaperintä...................... 93 9.4 Tiedonpiilotus........................... 95 9.5 Moniperinnän ongelma..................... 96 9.6 Binäärimetodien ongelma.................... 99 10 Samanaikaisuus 101 10.1 Samanaikainen suoritus..................... 102 10.2 Kriittiset lohkot.......................... 104 10.3 Monitorit.............................. 106 10.4 Transaktiomuisti......................... 107 10.5 CSP................................. 107 10.6 Piilaskento............................. 110 11 Logiikkaohjelmoinnin idea 113 11.1 Logiikkakieli............................ 113 11.2 Korvaukset............................. 114 11.3 Samastus.............................. 115 11.4 Logiikkaohjelman suoritus.................... 117 11.5 Samastus parametrinvälitysmekanismina........... 119 12 Staattinen tyyppijärjestelmä 121 12.1 Tyypitetty while-kieli....................... 121 12.2 Yksinkertaisesti tyypitetty lambda-kieli............ 124 12.3 Tyyppijärjestelmän eheys.................... 128 12.3.1 Esimerkkikieli....................... 129 12.3.2 Säilymislemma...................... 131 12.3.3 Etenemislemma...................... 132 12.3.4 Eheyslause......................... 132 12.4 Tyyppipäättely: Hindley Milner................ 133 13 Tietorakenteita 137 13.1 Perustyyppejä........................... 137 13.2 Yksikkötyyppi........................... 138 13.3 Paikalliset muuttujat....................... 139 13.4 Taulukot.............................. 140 13.5 Monikot.............................. 141
6 SISÄLTÖ 13.6 Tietueet............................... 142 13.7 Variantit.............................. 143 13.8 Viitetyypit............................. 145 14 Rekursiiviset tyypit 147 14.1 Rekursio tyypitetyssä lambda-kielessä............. 147 14.2 Nimiyhtäläisyys.......................... 150 14.3 Isorekursio............................. 150 14.4 Ekvirekursio............................ 151 14.5 Rekursiivisten tyyppien seurauksia............... 153 15 Parametripolymorfismi 155 15.1 Kumitus.............................. 155 15.2 Monimuotoisuus......................... 156 15.3 Systeemi F............................. 157 15.4 Tyyppioperaattorit........................ 159 16 Alityypitys 165 16.1 Alityypitys on osittaisjärjestys.................. 166 16.2 Tietueiden alityypitys....................... 166 16.3 Funktioiden alityypitys...................... 167 16.4 Top ja Bot.............................. 168 16.5 Eksplisiittiset tyyppimuunnokset................ 168 16.6 Muuttuvaiset........................... 169 16.7 This-tyyppi............................. 170 A Matematiikan kertausta 171
Kirjallisuutta Martín ABADI ja Luca CARDELLI, 1996. A Theory of Objects. Springer. Lloyd ALLISON, 1986. A practical introduction to denotational semantics. Numero 23 teoksessa Cambridge Computer Science Texts. Cambridge University Press. Deborah J. ARMSTRONG, 2006. The quarks of object-oriented development. Communications of the ACM, 49 (2):ss. 123 128. Franz BAADER ja Wayne SNYDER, 2001. Unification theory. Teoksessa Alan ROBINSON ja Andrei VORONKOV, toim., Handbook of Automated Reasoning, osa I, ss. 445 533. Amsterdam: Elsevier. H. P. BARENDREGT, 1984. The Lambda Calculus. Its Syntax and Semantics. Numero 103 teoksessa Studies in logic and the foundation of mathematics. Amsterdam: Elsevier, revised painos. Alexandre BERGEL, Stéphane DUCASSE, Oscar NIERSTRASZ ja Roel WU- YTS, 2008. Stateful traits and their formalization. Computer Languages, Systems & Structures, 34 (2 3):ss. 83 108. http://dx.doi.org/10.1016/j. cl.2007.05.003. Grady BOOCH, 1994. Object-oriented analysis and design with applications. Addison Wesley, second painos. Gilad BRACHA, Martin ODERSKY, David STOUTAMIRE ja Philip WADLER, 1998. Making the future safe for the past: adding genericity to the Java programming language. Teoksessa OOPSLA 98: Proceedings of the 13th ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, ss. 183 200. New York, NY, USA: ACM. ISBN 1-58113- 005-8. doi:http://doi.acm.org/10.1145/286936.286957. Michael BRANDT ja Fritz HENGLEIN, 1998. Coinductive axiomatization of recursive type equality and subtyping. Fundamenta Informaticae, 33 (4):ss. 309 338. 175
176 KIRJALLISUUTTA Kim BRUCE, Luca CARDELLI, Giuseppe CASTAGNA, Jonathan EIFRIG, Scott SMITH, Valeri TRIFONOV, Gary T. LEAVENS ja Benjamin PIERCE, 1995. On binary methods. Theory and Practice of Object Systems, 1 (3). http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1. 1.132.298&rep=rep1&type=pdf. Luca CARDELLI, 2004. Type systems. Teoksessa Allen B. TUCKER, toim., CRC Handbook of Computer Science and Engineering. CRC, 2 painos. Luca CARDELLI ja Peter WEGNER, 1985. On understanding types, data abstraction, and polymorphism. ACM Computing Surveys, 17 (4). Alonzo CHURCH, 1932. A set of postulates for the foundation of logic. The Annals of Mathematics, Second Series, 33 (2):ss. 346 366. Alonzo CHURCH, 1933. A set of postulates for the foundation of logic (second paper). The Annals of Mathematics, Second Series, 34 (4):ss. 839 864. Alonzo CHURCH, 1936. An unsolvable problem of elementary number theory. American Journal of Mathematics, 58 (2):ss. 345 363. Alonzo CHURCH, 1985. The Calculi of Lambda Conversion. Princeton University Press. Haskell B. CURRY ja Robert FEYS, 1968. Combinatory Logic, osa 1. North- Holland. O.-J. DAHL, E. W. DIJKSTRA ja C. A. R. HOARE, 1972. Structured Programming. Lontoo: Academic Press. Edsger W. DIJKSTRA, 1968. Go to statement considered harmful. Communications of the ACM, 11 (3). Letter to the editor. Edsger W. DIJKSTRA, 1976. A Discipline of Programming. Englewood Cliffs, NJ: Prentice-Hall. Stéphane DUCASSE, Oscar NIERSTRASZ, Nathanael SCHÄRLI, Roel WU- YTS ja Andrew P. BLACK, 2006. Traits: A mechanism for fine-grained reuse. ACM Transactions on Programming Languages and Systems, 28 (2):ss. 331 388. http://doi.acm.org/10.1145/1119479.1119483. J. A. GOUGEN, J. W. THATCHER, E. G. WAGNER ja J. B. WRIGHT, 1977. Initial algebra semantics and continuous algebras. Journal of the ACM, 24 (1).
KIRJALLISUUTTA 177 Per Brinch HANSEN, 1972. Structured multiprogramming. Communications of the ACM, 15 (7):ss. 574 578. ISSN 0001-0782. doi:http://doi.acm.org/ 10.1145/361454.361473. Tim HARRIS, Simon MARLOW, Simon PEYTON-JONES ja Maurice HERLI- HY, 2005. Composable memory transactions. Teoksessa PPoPP 05: Proceedings of the tenth ACM SIGPLAN symposium on Principles and practice of parallel programming, ss. 48 60. New York, NY, USA: ACM. ISBN 1-59593- 080-9. doi:http://doi.acm.org/10.1145/1065944.1065952. C. A. R. HOARE, 1974. Monitors: an operating system structuring concept. Communications of the ACM, 17 (10):ss. 549 557. ISSN 0001-0782. doi: http://doi.acm.org/10.1145/355620.361161. C. A. R. HOARE, 1978. Communicating sequential processes. Communications of the ACM, 21 (8):ss. 666 677. ISSN 0001-0782. doi:http://doi.acm.org/ 10.1145/359576.359585. C. A. R. HOARE, 1989. Hints on programming language design. Teoksessa C. A. R. HOARE ja C. B. JONES, toim., Essays in Computer Science. New York: Prentice-Hall. C. A. R. HOARE, 2003. Assertions: a personal perspective. IEEE Annals of the History of Computing, 25 (2). Tony HOARE, 2004. Towards the verifying compiler. Teoksessa Olaf OWE, Stein KROGDAHL ja Tom LYCHE, toim., From Object-Orientation to Formal Methods: Essays in Memory of Ole-Johan Dahl, Lecture Notes in Computer Science, osa 2635. Springer. ISO/IEC 14977, 1996. Information lechnology Syntactic metalanguage Extended BNF. International Organization for Standardization. ISO/IEC 14977:1996(E). S. C. KLEENE, 1934. Proof by cases in formal logic. Annals of Mathematics, Second Series, 35 (3):ss. 529 544. S. C. KLEENE, 1935. A theory of positive integers in formal logic. Part II. American Journal of Mathematics, 57 (2):ss. 219 244. Donald E. KNUTH, 1974. Structured programming with go to statements. Computing Surveys, 6 (4).
178 KIRJALLISUUTTA Donald E. KNUTH, 1999. MMIXware: A RISC Computer for the Third Millennium, Lecture Notes in Computer Science, osa 1750. Berlin: Springer. Katso myös sivua http://www-cs-staff.stanford.edu/~knuth/mmixware. html. Donald E. KNUTH ja Luis TRABB PARDO, 1977. The early development of programming languages. Teoksessa J. BELZER, A. G. HOLZMAN ja A. KENT, toim., Encyclopedia of Computer Science and Technology, osa 6. New York: Dekker. Julkaistu myös kirjassa Metropolis et al. (1980). Thomas E. KURTZ, 1981. Basic. Teoksessa History of programming languages I, ss. 515 537. New York, NY, USA: ACM. ISBN 0-12-745040-8. doi: http://doi.acm.org/10.1145/800025.1198404. John MCCARTHY, 1981. History of lisp. Teoksessa History of programming languages I, ss. 173 185. New York, NY, USA: ACM. ISBN 0-12-745040-8. doi:http://doi.acm.org/10.1145/800025.1198360. N. METROPOLIS, J. HOWLETT ja Gian-Carlo ROTA, toim., 1980. A History of Computing in the Twentieth Century. New York: Academic Press. Robin MILNER, 1978. A theory of type polymorphism in programming. Journal of Computer and System Sciences, 17 (3):ss. 348 375. Robin MILNER, Joachim PARROW ja David WALKER, 1990. A Calculus of Mobile Processes, Part I. Tekninen Raportti ECS-LFCS-89-85, University of Edinburgh, Laboratory for Foundations of Computer Science. http: //www.lfcs.inf.ed.ac.uk/reports/89/ecs-lfcs-89-85/. Peter NAUR ET AL., 1963. Revised report on the algorithmic language AL- GOL 60. Communications of the ACM, 6 (1):ss. 1 17. John K. OUSTERHOUT, 1998. Scripting: Higher-level programming for the 21st century. Computer, 31 (3):ss. 23 30. Simon PEYTON JONES, toim., 2003. Haskell 98 Language and Libraries: The Revised Report. Cambridge University Press. URL http://research.microsoft.com/users/simonpj/ haskell98-revised/ Benjamin C. PIERCE, 1996. Foundational calculi for programming languages. Teoksessa Allen B. TUCKER, toim., Handbook of Computer Science and Engineering, luku 139. CRC Press. http://www.cis.upenn.edu/ ~bcpierce/papers/crchandbook.ps.
KIRJALLISUUTTA 179 Benjamin C. PIERCE, 2002. Type and Programming Languages. Cambridge, MA: MIT Press. Benjamin C. PIERCE ja David N. TURNER, 2000. Pict: A programming language based on the pi-calculus. Teoksessa Gordon PLOTKIN, Colin STIR- LING ja Mads TOFTE, toim., Proof, Language and Interaction: Essays in Honour of Robin Milner, ss. 455 494. MIT Press. http://www.cis.upenn. edu/~bcpierce/papers/pict-design.ps. G. D. PLOTKIN,???? A structural approach to operational semantics. http: //homepages.inf.ed.ac.uk/gdp/publications/sos.ps. University of Aarhus lecture notes from the early 1980s. John C. REYNOLDS, 1998. Theories of Programming Languages. Cambridge: Cambridge University Press. Hannele SALMINEN ja Jouko VÄÄNÄNEN, 1997. Johdatus logiikkaan. Jyväskylä: Gaudeamus. Jean E. SAMMET, 1981. The early history of cobol. Teoksessa History of programming languages I, ss. 199 243. New York, NY, USA: ACM. ISBN 0-12-745040-8. doi:http://doi.acm.org/10.1145/800025.1198367. Michael L. SCOTT, 2000. Programming Language Pragmatics. Morgan Kaufmann. Guy Lewis STEELE, Jr., 1977. Debunking the expensive procedure call myth or procedure call implementations considered harmful or, LAMBDA: The ultimate GOTO. Teoksessa James K. KETCHEL ET AL., toim., Proceedings of the 1977 annual conference, ss. 153 162. ACM Press. Christopher STRACHEY, 2000. Fundamental concepts in programming languages. Higher-Order and Symbolic Computation, 13:ss. 11 49. Perustuu Stracheyn vuonna 1967 pitämiin luentoihin. Antero TAIVALSAARI, 1993. A Critical View of Inheritance and Reusability in Object-oriented Programming. väitöskirja, University of Jyväskylä. A. VAN WIJNGAARDEN ET AL., toim., 1976. Revised Report on the Algorithmic Language Algol 68. Berliini: Springer.