Tài liệu Concepts, Techniques, and Models of Computer Programming pot

939 359 0
Tài liệu Concepts, Techniques, and Models of Computer Programming pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Concepts, Techniques, and Models of Computer Programming PETER VAN ROY 1 Universit´e catholique de Louvain (at Louvain-la-Neuve) Swedish Institute of Computer Science SEIF HARIDI 2 Royal Institute of Technology (KTH) Swedish Institute of Computer Science June 5, 2003 1 Email: pvr@info.ucl.ac.be,Web: http://www.info.ucl.ac.be/~pvr 2 Email: seif@it.kth.se,Web: http://www.it.kth.se/~seif ii Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. Contents List of Figures xvi List of Tables xxiv Preface xxvii Running the example programs xliii I Introduction 1 1 Introduction to Programming Concepts 3 1.1 Acalculator 3 1.2 Variables 4 1.3 Functions 4 1.4 Lists 6 1.5 Functionsoverlists 9 1.6 Correctness 11 1.7 Complexity 12 1.8 Lazyevaluation 13 1.9 Higher-orderprogramming 15 1.10Concurrency 16 1.11Dataflow 17 1.12State 18 1.13Objects 19 1.14Classes 20 1.15Nondeterminismandtime 21 1.16Atomicity 23 1.17Wheredowegofromhere 24 1.18Exercises 24 II General Computation Models 29 2 Declarative Computation Model 31 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. iv CONTENTS 2.1 Definingpracticalprogramminglanguages 33 2.1.1 Languagesyntax 33 2.1.2 Languagesemantics 38 2.2 Thesingle-assignmentstore 44 2.2.1 Declarativevariables 44 2.2.2 Valuestore 44 2.2.3 Valuecreation 45 2.2.4 Variableidentifiers 46 2.2.5 Valuecreationwithidentifiers 47 2.2.6 Partialvalues 47 2.2.7 Variable-variablebinding 48 2.2.8 Dataflowvariables 49 2.3 Kernellanguage 50 2.3.1 Syntax 50 2.3.2 Valuesandtypes 51 2.3.3 Basictypes 53 2.3.4 Recordsandprocedures 54 2.3.5 Basicoperations 56 2.4 Kernellanguagesemantics 57 2.4.1 Basicconcepts 57 2.4.2 Theabstractmachine 61 2.4.3 Non-suspendablestatements 64 2.4.4 Suspendablestatements 67 2.4.5 Basicconceptsrevisited 69 2.4.6 Lastcalloptimization 74 2.4.7 Activememoryandmemorymanagement 75 2.5 Fromkernellanguagetopracticallanguage 80 2.5.1 Syntacticconveniences 80 2.5.2 Functions (the fun statement) 85 2.5.3 Interactive interface (the declare statement) 88 2.6 Exceptions 91 2.6.1 Motivationandbasicconcepts 91 2.6.2 Thedeclarativemodelwithexceptions 93 2.6.3 Fullsyntax 95 2.6.4 Systemexceptions 97 2.7 Advancedtopics 98 2.7.1 Functionalprogramminglanguages 98 2.7.2 Unificationandentailment 100 2.7.3 Dynamicandstatictyping 106 2.8 Exercises 108 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. CONTENTS v 3 Declarative Programming Techniques 113 3.1 Whatisdeclarativeness? 117 3.1.1 Aclassificationofdeclarativeprogramming 117 3.1.2 Specificationlanguages 119 3.1.3 Implementing components in the declarative model . . . . 119 3.2 Iterativecomputation 120 3.2.1 Ageneralschema 120 3.2.2 Iterationwithnumbers 122 3.2.3 Usinglocalprocedures 122 3.2.4 Fromgeneralschematocontrolabstraction 125 3.3 Recursivecomputation 126 3.3.1 Growingstacksize 127 3.3.2 Substitution-based abstract machine 128 3.3.3 Converting a recursive to an iterative computation . . . . 129 3.4 Programmingwithrecursion 130 3.4.1 Typenotation 131 3.4.2 Programmingwithlists 132 3.4.3 Accumulators 142 3.4.4 Differencelists 144 3.4.5 Queues 149 3.4.6 Trees 153 3.4.7 Drawingtrees 161 3.4.8 Parsing 163 3.5 Timeandspaceefficiency 169 3.5.1 Executiontime 169 3.5.2 Memoryusage 175 3.5.3 Amortizedcomplexity 177 3.5.4 Reflectionsonperformance 178 3.6 Higher-orderprogramming 180 3.6.1 Basicoperations 180 3.6.2 Loopabstractions 186 3.6.3 Linguisticsupportforloops 190 3.6.4 Data-driventechniques 193 3.6.5 Explicitlazyevaluation 196 3.6.6 Currying 196 3.7 Abstractdatatypes 197 3.7.1 Adeclarativestack 198 3.7.2 Adeclarativedictionary 199 3.7.3 Awordfrequencyapplication 201 3.7.4 Secureabstractdatatypes 204 3.7.5 Thedeclarativemodelwithsecuretypes 205 3.7.6 Asecuredeclarativedictionary 210 3.7.7 Capabilities and security 210 3.8 Nondeclarativeneeds 213 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. vi CONTENTS 3.8.1 Text input/output with a file 213 3.8.2 Text input/output with a graphical user interface 216 3.8.3 StatelessdataI/Owithfiles 219 3.9 Programdesigninthesmall 221 3.9.1 Designmethodology 221 3.9.2 Exampleofprogramdesign 222 3.9.3 Softwarecomponents 223 3.9.4 Exampleofastandaloneprogram 228 3.10Exercises 233 4 Declarative Concurrency 237 4.1 Thedata-drivenconcurrentmodel 239 4.1.1 Basicconcepts 241 4.1.2 Semanticsofthreads 243 4.1.3 Exampleexecution 246 4.1.4 Whatisdeclarativeconcurrency? 247 4.2 Basicthreadprogrammingtechniques 251 4.2.1 Creatingthreads 251 4.2.2 Threadsandthebrowser 251 4.2.3 Dataflowcomputationwiththreads 252 4.2.4 Threadscheduling 256 4.2.5 Cooperativeandcompetitiveconcurrency 259 4.2.6 Threadoperations 260 4.3 Streams 261 4.3.1 Basicproducer/consumer 261 4.3.2 Transducersandpipelines 263 4.3.3 Managing resources and improving throughput 265 4.3.4 Streamobjects 270 4.3.5 Digitallogicsimulation 271 4.4 Usingthedeclarativeconcurrentmodeldirectly 277 4.4.1 Order-determiningconcurrency 277 4.4.2 Coroutines 279 4.4.3 Concurrentcomposition 281 4.5 Lazyexecution 283 4.5.1 Thedemand-drivenconcurrentmodel 286 4.5.2 Declarativecomputationmodels 290 4.5.3 Lazystreams 293 4.5.4 Bounded buffer 295 4.5.5 Readingafilelazily 297 4.5.6 TheHammingproblem 298 4.5.7 Lazylistoperations 299 4.5.8 Persistentqueuesandalgorithmdesign 303 4.5.9 Listcomprehensions 307 4.6 Softreal-timeprogramming 309 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. CONTENTS vii 4.6.1 Basicoperations 309 4.6.2 Ticking 311 4.7 Limitationsandextensionsofdeclarativeprogramming 314 4.7.1 Efficiency 314 4.7.2 Modularity 315 4.7.3 Nondeterminism 319 4.7.4 Therealworld 322 4.7.5 Pickingtherightmodel 323 4.7.6 Extendedmodels 323 4.7.7 Usingdifferentmodelstogether 325 4.8 TheHaskelllanguage 327 4.8.1 Computationmodel 328 4.8.2 Lazyevaluation 328 4.8.3 Currying 329 4.8.4 Polymorphictypes 330 4.8.5 Typeclasses 331 4.9 Advancedtopics 332 4.9.1 The declarative concurrent model with exceptions . . . . . 332 4.9.2 Moreonlazyexecution 334 4.9.3 Dataflowvariablesascommunicationchannels 337 4.9.4 Moreonsynchronization 339 4.9.5 Usefulnessofdataflowvariables 340 4.10Historicalnotes 343 4.11Exercises 344 5 Message-Passing Concurrency 353 5.1 Themessage-passingconcurrentmodel 354 5.1.1 Ports 354 5.1.2 Semanticsofports 355 5.2 Portobjects 357 5.2.1 The NewPortObject abstraction 358 5.2.2 Anexample 359 5.2.3 Reasoningwithportobjects 360 5.3 Simplemessageprotocols 361 5.3.1 RMI(RemoteMethodInvocation) 361 5.3.2 AsynchronousRMI 364 5.3.3 RMIwithcallback(usingthread) 364 5.3.4 RMIwithcallback(usingrecordcontinuation) 366 5.3.5 RMI with callback (using procedure continuation) . . . . . 367 5.3.6 Errorreporting 367 5.3.7 AsynchronousRMIwithcallback 368 5.3.8 Doublecallbacks 369 5.4 Programdesignforconcurrency 370 5.4.1 Programmingwithconcurrentcomponents 370 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. viii CONTENTS 5.4.2 Designmethodology 372 5.4.3 Listoperationsasconcurrencypatterns 373 5.4.4 Lift control system 374 5.4.5 Improvements to the lift control system 383 5.5 Usingthemessage-passingconcurrentmodeldirectly 385 5.5.1 Portobjectsthatshareonethread 385 5.5.2 Aconcurrentqueuewithports 387 5.5.3 Athreadabstractionwithterminationdetection 390 5.5.4 Eliminatingsequentialdependencies 393 5.6 TheErlanglanguage 394 5.6.1 Computationmodel 394 5.6.2 IntroductiontoErlangprogramming 395 5.6.3 The receive operation 398 5.7 Advancedtopics 402 5.7.1 Thenondeterministicconcurrentmodel 402 5.8 Exercises 407 6 Explicit State 413 6.1 Whatisstate? 416 6.1.1 Implicit(declarative)state 416 6.1.2 Explicitstate 417 6.2 State and system building 418 6.2.1 Systemproperties 419 6.2.2 Component-basedprogramming 420 6.2.3 Object-orientedprogramming 421 6.3 Thedeclarativemodelwithexplicitstate 421 6.3.1 Cells 422 6.3.2 Semanticsofcells 424 6.3.3 Relationtodeclarativeprogramming 425 6.3.4 Sharingandequality 426 6.4 Abstractdatatypes 427 6.4.1 EightwaystoorganizeADTs 427 6.4.2 Variationsonastack 429 6.4.3 Revocable capabilities . . 433 6.4.4 Parameterpassing 434 6.5 Statefulcollections 438 6.5.1 Indexed collections 439 6.5.2 Choosinganindexedcollection 441 6.5.3 Othercollections 442 6.6 Reasoningwithstate 444 6.6.1 Invariantassertions 444 6.6.2 Anexample 445 6.6.3 Assertions 448 6.6.4 Proofrules 449 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. CONTENTS ix 6.6.5 Normaltermination 452 6.7 Programdesigninthelarge 453 6.7.1 Designmethodology 454 6.7.2 Hierarchical system structure . . . 456 6.7.3 Maintainability 461 6.7.4 Futuredevelopments 464 6.7.5 Furtherreading 466 6.8 Casestudies 467 6.8.1 Transitiveclosure 467 6.8.2 Wordfrequencies(withstatefuldictionary) 475 6.8.3 Generatingrandomnumbers 476 6.8.4 “WordofMouth”simulation 481 6.9 Advancedtopics 484 6.9.1 Limitationsofstatefulprogramming 484 6.9.2 Memorymanagementandexternalreferences 485 6.10Exercises 487 7 Object-Oriented Programming 493 7.1 Motivations 495 7.1.1 Inheritance . . 495 7.1.2 Encapsulatedstateandinheritance 497 7.1.3 Objectsandclasses 497 7.2 ClassesascompleteADTs 498 7.2.1 Anexample 499 7.2.2 Semanticsoftheexample 500 7.2.3 Definingclasses 501 7.2.4 Initializing attributes 503 7.2.5 First-classmessages 504 7.2.6 First-class attributes 507 7.2.7 Programmingtechniques 507 7.3 ClassesasincrementalADTs 507 7.3.1 Inheritance . . 508 7.3.2 Staticanddynamicbinding 511 7.3.3 Controlling encapsulation 512 7.3.4 Forwardinganddelegation 517 7.3.5 Reflection 522 7.4 Programmingwithinheritance 524 7.4.1 Thecorrectuseofinheritance 524 7.4.2 Constructingahierarchybyfollowingthetype 528 7.4.3 Genericclasses 531 7.4.4 Multipleinheritance 533 7.4.5 Rulesofthumbformultipleinheritance 539 7.4.6 Thepurposeofclassdiagrams 539 7.4.7 Designpatterns 540 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. x CONTENTS 7.5 Relationtoothercomputationmodels 543 7.5.1 Object-basedandcomponent-basedprogramming 543 7.5.2 Higher-orderprogramming 544 7.5.3 Functional decomposition versus type decomposition . . . 547 7.5.4 Shouldeverythingbeanobject? 548 7.6 Implementing the object system . 552 7.6.1 Abstractiondiagram 552 7.6.2 Implementingclasses 554 7.6.3 Implementingobjects 555 7.6.4 Implementinginheritance 556 7.7 TheJavalanguage(sequentialpart) 556 7.7.1 Computationmodel 557 7.7.2 IntroductiontoJavaprogramming 558 7.8 Activeobjects 563 7.8.1 Anexample 564 7.8.2 The NewActive abstraction 564 7.8.3 TheFlaviusJosephusproblem 565 7.8.4 Otheractiveobjectabstractions 568 7.8.5 Eventmanagerwithactiveobjects 569 7.9 Exercises 574 8 Shared-State Concurrency 577 8.1 Theshared-stateconcurrentmodel 581 8.2 Programmingwithconcurrency 581 8.2.1 Overviewofthedifferentapproaches 581 8.2.2 Usingtheshared-statemodeldirectly 585 8.2.3 Programmingwithatomicactions 588 8.2.4 Furtherreading 589 8.3 Locks 590 8.3.1 BuildingstatefulconcurrentADTs 592 8.3.2 Tuplespaces(“Linda”) 594 8.3.3 Implementinglocks 599 8.4 Monitors 600 8.4.1 Bounded buffer 602 8.4.2 Programmingwithmonitors 605 8.4.3 Implementingmonitors 605 8.4.4 Anothersemanticsformonitors 607 8.5 Transactions 608 8.5.1 Concurrencycontrol 610 8.5.2 Asimpletransactionmanager 613 8.5.3 Transactionsoncells 616 8.5.4 Implementingtransactionsoncells 619 8.5.5 Moreontransactions 623 8.6 TheJavalanguage(concurrentpart) 625 Copyright c  2001-3 by P. Van Roy and S. Haridi. All rights reserved. [...]... used, taking account their abilities and limitations Both science and technology Programming as defined above has two essential parts: a technology and its scientific foundation The technology consists of tools, practical techniques, and standards, allowing us to do programming The science consists of a broad and deep theory with predictive power, allowing us to understand programming Ideally, the science... computation models and programs, namely the Oz language and its computation model To be precise, the computation models of this book are all carefully-chosen subsets of Oz Why did we choose Oz? The main reason is that it supports the kernel language approach well Another reason is the existence of the Mozart Programming System Panorama of computation models This book presents a broad overview of many of the... show how the judicious combined use of several computation models can help solve some of the problems of these areas Languages mentioned We mention many programming languages in the book and relate them to particular computation models For example, Java and Smalltalk are based on an object-oriented model Haskell and Standard ML are based on a functional model Prolog and Mercury are based on a logic model... computation models The models are designed not just with formal simplicity in mind (although it is important), but on the basis of how a programmer can express himself/herself and reason within the model There are many different practical computation models, with different levels of expressiveness, different programming techniques, and different ways of reasoning about them We find that each model has its domain of. .. general models For example, the concepts of lexical scoping and higher-order programming, which are usually associated with functional programming, are useful in all models This is well-known in the functional programming community Functional languages have long been extended with explicit state (e.g., Scheme [38] and Standard ML [126, 192]) and more recently with concurrency (e.g., Concurrent ML [158] and. .. of programming independent of any other domain in informatics In our experience, it divides naturally into three core topics: 1 Concepts and techniques 2 Algorithms and data structures 3 Program design and software engineering The book gives a thorough treatment of topic (1) and an introduction to (2) and (3) In which order should the topics be given? There is a strong interdependency between (1) and. .. rest of the book talks about computation models and not programming paradigms Sometimes we will use the phrase programming model This refers to what the programmer needs: the programming techniques and design principles made possible by the computation model Each computation model has its own set of techniques for programming and Copyright c 2001-3 by P Van Roy and S Haridi All rights reserved xxviii... numerous inside computer systems Modern computers are highly complex systems consisting of hardware, operating system, middleware, and application layers, each of which is based on the work of thousands of people over several decades They contain an enormous number of abstractions, working together in a highly organized manner Designing abstractions is not always easy It can be a long and painful process,... What is programming? We define programming, as a general human activity, to mean the act of extending or changing a system’s functionality Programming is a widespread activity that is done both by nonspecialists (e.g., consumers who change the settings of their alarm clock or cellular phone) and specialists (computer programmers, the audience of this book) This book focuses on the construction of software... and difficult to program with There are other, simpler forms of concurrent programming The declarative concurrency of Chapter 4 is much simpler to program with and can often be used in place of stateful concurrency (see the quote that starts Chapter 4) Stream concurrency, a simple form of declarative concurrency, has been taught in first-year courses at MIT and other institutions Another simple form of . Concepts, Techniques, and Models of Computer Programming PETER VAN ROY 1 Universit´e catholique de Louvain (at Louvain-la-Neuve) Swedish Institute of Computer. Generatingrandomnumbers 476 6.8.4 “WordofMouth”simulation 481 6.9 Advancedtopics 484 6.9.1 Limitationsofstatefulprogramming 484 6.9.2 Memorymanagementandexternalreferences

Ngày đăng: 22/02/2014, 06:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan