Concepts, Techniques, and Models of Computer Programming - Chapter 0 doc

43 234 0
Concepts, Techniques, and Models of Computer Programming - Chapter 0 doc

Đ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. [...]... 647 6 50 652 653 654 655 656 656 657 6 60 6 60 661 662 663 663 664 665 667 668 669 671 673 674 676 681 684 Copyright c 200 1-3 by P Van Roy and S Haridi All rights reserved xii CONTENTS III Specialized Computation Models 687 10 Graphical User Interface Programming 10. 1 Basic concepts 10. 2 Using the declarative/procedural approach 10. 2.1 Basic user interface elements 10. 2.2... 694 696 697 698 699 699 700 703 707 712 712 713 716 718 7 20 7 20 722 723 725 726 726 728 729 7 30 7 30 732 734 737 738 738 7 40 742 743 744 745 747 748 CONTENTS xiii 11.8.4 Active fault tolerance 11.9 Security 11.10Building applications 11. 10. 1 Centralized first, distributed later 11. 10. 2 Handling partial failure 11. 10. 3 Distributed components... 8 50 8 50 851 851 851 852 Bibliography 853 Index 869 Copyright c 200 1-3 by P Van Roy and S Haridi All rights reserved xvi Copyright c 200 1-3 by P Van Roy and S Haridi All rights reserved List of Figures 1.1 1.2 1.3 1.4 1.5 Taking apart the list [5 6 7 8] Calculating the fifth row of Pascal’s triangle A simple example of dataflow execution All possible executions of. .. 693 694 695 695 696 697 698 700 701 703 704 705 705 707 707 7 10 711 11.1 A simple taxonomy of distributed systems 11.2 The distributed computation model 11.3 Process-oriented view of the distribution model 11.4 Distributed locking 11.5 The advantages of asynchronous objects with dataflow 11.6 Graph notation for a distributed... Screen shot of the word frequency application Standalone dictionary library (file Dict.oz) Standalone word frequency application (file WordApp.oz) Component dependencies for the word frequency application 124 124 125 1 40 141 156 157 159 1 60 1 60 162 162 164 165 181 186 186 187 188 189 1 90 199 201 202 203 208 217 228 229 2 30 231 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4. 10 4.11 4.12... interface 10. 2.3 Declarative geometry 10. 2.4 Declarative resize behavior 10. 2.5 Dynamic behavior of widgets 10. 3 Case studies 10. 3.1 A simple progress monitor 10. 3.2 A simple calendar widget 10. 3.3 Automatic generation of a user interface 10. 3.4 A context-sensitive clock 10. 4 Implementing the GUI tool 10. 5 Exercises... all-solution search engine Solve 762 765 768 7 70 775 779 12.1 12.2 12.3 12.4 12.5 12.6 Copyright c 200 1-3 by P Van Roy and S Haridi All rights reserved LIST OF FIGURES 13.1 The kernel language with shared-state concurrency 787 B.1 Graph representation of the infinite list C1=a|b|C1 832 C.1 The ternary operator “ :=” 8 40 Copyright c 200 1-3 ... 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 200 1-3 by P Van Roy and S Haridi All rights reserved xxviii... 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... 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 application This book explains many of these models, how they are related, how to program in them, and how to combine them to greatest advantage Copyright c 200 1-3 by P Van Roy and S Haridi All rights reserved xxxiv PREFACE . 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. Advancedtopics 98 2.7.1 Functionalprogramminglanguages 98 2.7.2 Unificationandentailment 100 2.7.3 Dynamicandstatictyping 106 2.8 Exercises 108 Copyright c  200 1-3 by P. Van Roy and S. Haridi. All rights. Lazylistoperations 299 4.5.8 Persistentqueuesandalgorithmdesign 303 4.5.9 Listcomprehensions 307 4.6 Softreal-timeprogramming 309 Copyright c  200 1-3 by P. Van Roy and S. Haridi. All rights reserved. CONTENTS

Ngày đăng: 14/08/2014, 10:22

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

Tài liệu liên quan