ebook img

Theoretical introduction to programming PDF

365 Pages·2006·1.18 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Theoretical introduction to programming

Theoretical Introduction to Programming Bruce Mills Theoretical Introduction to Programming With 29 Figures Bruce Mills,BEng,BSc,PhD British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library Library ofCongress Control Number: 2005926335 ISBN-10:1-84628-021-4 Printed on acid-free paper ISBN-13:978-1-84628-021-4 © Springer-Verlag London Limited 2006 Apart from any fair dealing for the purposes ofresearch or private study,or criticism or review, as permitted under the Copyright,Designs and Patents Act 1988,this publication may only be reproduced,stored or transmitted,in any form or by any means,with the prior permission in writing ofthe publishers,or in the case ofreprographic reproduction in accordance with the terms oflicences issued by the Copyright Licensing Agency.Enquiries concerning reproduction outside those terms should be sent to the publishers. The use ofregistered names,trademarks,etc.in this publication does not imply,even in the absence ofa specific statement,that such names are exempt from the relevant laws and regula- tions and therefore free for general use. The publisher makes no representation,express or implied,with regard to the accuracy ofthe information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Printed in the United States ofAmerica (SPI/SBA) 9 8 7 6 5 4 3 2 1 Springer Science+Business Media springeronline.com v Preface Thisbookisorganisedintoalargenumberofbrief,self-containedentries. Admittedly, there is no such thing as a self-contained entry. For exam- ple, you need some knowledge of English to understand this paragraph. But, the principleis that each entry, of one or two pages, is a conceptual whole as well as a part of a greater whole (see note 20) in the same way that a car has four whole wheels, and not eight half wheels. Some entries are intended to demonstrate a technique, or introduce an historically contingent fact such as the actual syntax of a contemporary language, or in this case, a specific issue regarding this book. Others are intended to illustrate a more eternal truth. They may be about a contemporary language, but stress a philosophical position or broadly based attitude. Both of these I have called notions. Finally, there are entries that are intended to cause the reader to dosomething other than just nodding their head as a sign of either agreement or an incipient dormant state. These are the exercises. Thedistinction canonlybearbitrary; theclassification ismerely aguide to suggest the sense in which the pages are intended. In many cases, entries that are not specifically labelled as exercises in- volve generic opportunities for self-study. As this is a book on computer programming, it is natural and strongly advised that the reader try im- plementing each concept of interest as it arises. With this in mind, I have tried hardnot to leave outpragmatic details whoseomission would leave the reader with nothing but the illusion of understanding. Nev- ertheless, actually cutting practice code makes a big difference in the ability of the programmer to use the concepts when the need arises. At the end of the book are the notes explaining short and simple issues or (paradoxically) issues that are too complex to explain in this book. If a note became too lengthy while being written it was converted into a notion or an exercise. vii Contents Preface ..........................................................v Chapter 1. The Abstract Rational Outlook ................ 1 Abstract Computation.........................................2 Rational Thought..............................................4 Human Psychology............................................6 Mythological Language........................................8 Literate Programming........................................10 Hand-Crafted Software.......................................12 Technical Programming.......................................14 Chapter 2. A Grab Bag of Computational Models.......17 Abstract and Virtual Machines...............................18 State Machines...............................................20 State Machines in Action.....................................22 Turing Machine .............................................. 26 Non-Deterministic Machines..................................30 Von Neumann Machine.......................................34 Stack Machine................................................36 Register Machine.............................................38 Analogue Machine............................................39 Cellular Automata............................................40 Unorthodox Models .......................................... 41 The Game of Life.............................................42 The Modern Desktop Computer..............................44 Aspects of Virtual Machines..................................46 Aspects of Programming......................................48 Register Indirection .......................................... 50 Pure Expression Substitution.................................52 Lists Pure and Linked........................................54 Pure String Substitution......................................56 The Face Value of Numerals..................................58 Solving Equations............................................62 Pure Unification..............................................64 Equality of Expressions.......................................66 Equational Reasoning ........................................ 68 viii Theoretical Introduction to Programming Unification Reduction ........................................70 Code Reduction..............................................74 Programming With Logic.....................................76 Negation in Logic Programming..............................78 Impure Lambda Calculus.....................................80 Pure Lambda Calculus .......................................82 Pure Lambda Arithmetic.....................................84 Pure Lambda Flow Control...................................86 S-K Combinators.............................................90 Chapter 3. Some Formal Technology.......................92 The Ellipsis Is Not a Definition...............................93 The Summation Operator....................................95 Propositional Calculus........................................97 Boolean Algebra..............................................99 Predicate Calculus .......................................... 101 Formal Mathematical Models................................102 The Formal State Machine.................................. 103 Several Types of Networks...................................105 Informal Petri Nets..........................................107 Formal Turing Machine......................................109 The Table-Driven State Machine ............................ 110 Factors of Graphs...........................................111 Products of Graphs..........................................113 Constructive Numerics......................................115 Prime Programs.............................................117 Showing that Factorial Works...............................119 Reasoning About Code......................................123 Logical Conditions .......................................... 127 Chapter 4. Limitations on Exact Knowledge.............131 Finite-State Limitations.....................................132 N log N sorting..............................................133 Russell’s Paradox............................................134 Pure Lambda Paradoxes.....................................136 Godel’s Theorem............................................138 Non-Computability..........................................140 Solving Polynomials.........................................142 ix Churche’s Thesis............................................143 Algorithmic Complexity.....................................144 P and NP...................................................146 NP completeness............................................148 Turing Test ................................................. 149 Natural Language Processing................................150 The Computable Reals......................................151 The Diagonal Argument.....................................152 Chapter 5. Some Orthodox Languages....................154 C Pointers to Functions .....................................159 Taking C on Face Value.....................................161 Functions and Other Data in C..............................163 The C Preprocessor.........................................166 C Functions are Data Again.................................167 Java Code...................................................169 Pointer Casting ............................................. 171 The Object Data Type......................................177 Manual Objects.............................................179 Inheritance and Dynamic Type..............................181 CODASYL and Objects.....................................183 Typecasting.................................................185 The Concept of Type........................................187 Type-Checking..............................................188 Subtypes and Programming.................................189 New Datatypes..............................................190 Scheme Code................................................193 Declarative and Imperative..................................195 Sorting with Pure Substitution..............................197 Fast Sorting in Haskell......................................199 Logic in Prolog..............................................201 Functions in Prolog..........................................204 Arithmetic in Prolog........................................ 205 Meta-Logic in Prolog........................................207 What Is HTML Code? ...................................... 209 Illogical markup language ................................... 211 HTML Forgive and Forget...................................212 Expanding Beyond Recognition..............................213 x Theoretical Introduction to Programming Chapter 6. Arithmetic Computation......................214 Natural Arithmetic..........................................215 Modulo Arithmetic..........................................217 Integer Arithmetic...........................................219 Rational Arithmetic.........................................221 Complex Arithmetic.........................................223 Exact Arithmetic............................................225 Showing That a Power Loop Works..........................227 When Is a Proof Not a Proof?...............................229 Real-Valued Memory........................................231 Cellular Matrix Multiplication...............................232 Chapter 7. Repetitive Computation ...................... 235 The Use of Recursion........................................236 Doing Without the While Loop..............................238 Defining the Generic While-Loop............................240 Design of the Power Function................................244 Powers by Multiplication....................................246 Computing Powers by Squaring..............................248 Language or Algorithm?.....................................250 Repetitive Program Design..................................253 Recursive Code Compilation.................................254 Functions as Data...........................................256 Lambda Expressions in Java.................................258 The Y-combinator definition.................................260 Y-combinator factorial ...................................... 263 Y-combinator Fibonacci.....................................264 Chapter 8. Temporal Interaction..........................265 Virtual Interaction..........................................266 Incorruptible Operations .................................... 268 Temporal Computing........................................270 Multi-Threaded Code ....................................... 272 Graphs of State Machines ...................................273 Direct Thread Composition..................................274 Concurrent Thread Interference ............................. 276 Control Structures...........................................278 xi Thread Point of Execution .................................. 280 The Transition Network.....................................281 High-Level Interference......................................285 Incorruptible Commands Again..............................286 Thread Interaction..........................................288 Pure String Interaction......................................292 Showing That a Parser Works...............................295 Mutual Exclusion ........................................... 296 Good Mutual Exclusion.....................................298 A Partial Mutex Protocol ................................... 299 Guarded Commands.........................................300 Blocking Commands.........................................306 Hardware Assistance ........................................307 Proving That a Protocol Works..............................308 Two Partial Exclusion Protocols.............................309 The Peterson Protocol.......................................310 The Decker Protocol ........................................ 312 Proving That a Protocol Works..............................314 Chapter 9. Container Datatypes.......................... 315 Abstract Arrays.............................................316 Pure Containers.............................................318 Generic Maps...............................................322 Showing That Infinite Lists Work............................325 Generic Lists................................................326 Computing with Infinite Lists............................... 328 Sequence Builder............................................330 Infinite Lists in Haskell......................................333 Infinite Lists in Scheme......................................334 Primitive List Recursion.....................................336 Appendices .................................................339 End notes...................................................340 Bibliography................................................351 Glossary .................................................... 353 Index ..........................................................355 Chapter 1 The Abstract Rational Outlook In which we discover that programming is about being human. That to truly master a technology we must first master ourselves. That philo- sophical esoterica will bite us on the backside if we do not pay them enough attention. We discuss the effect that eternal truth, pure sci- ence, rational thought, group behaviour, and contemporary fashion have on our daily programming activities. We discover that identification of computation is a matter of opinion, that programming is an outlook on life in general, that the task of a programmer is to add a little wisdom to the inanimate. In short, this theme contains the bulk of the material that most readers will pay scant attention to, until it is too late. You may now skip to the next theme.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.