Introduction to Objective Caml

Introduction to Objective Caml

Introduction to Objective Caml Jason Hickey DRAFT. DO NOT REDISTRIBUTE. Copyright © Jason Hickey, 2008. This book has been submitted for publication by Cambridge University Press. This draft may be used until the time the book appears in print. January 11, 2008 Copyright © Jason Hickey 2 Draft. Do not redistribute. Contents Preface i 1 Introduction 1 1.1 Functional and imperative languages .................. 3 1.2 Organization ............................... 3 1.3 Additional Sources of Information ................... 3 2 Simple Expressions 5 2.1 Comment convention .......................... 5 2.2 Basic expressions ............................ 5 2.2.1 unit: the singleton type .................... 6 2.2.2 int: the integers ........................ 6 2.2.3 float: the floating-point numbers ............... 7 2.2.4 char: the characters ...................... 8 2.2.5 string: character strings .................... 8 2.2.6 bool: the Boolean values .................... 9 2.3 Operator precedences .......................... 10 2.4 The OCaml type system ......................... 11 2.5 Compiling your code .......................... 12 2.6 Exercises ................................ 14 3 Variables and Functions 15 3.1 Functions ................................ 16 3.1.1 Scoping and nested functions . 18 3.1.2 Recursive functions ....................... 18 3.1.3 Higher order functions ..................... 19 3.2 Variable names ............................. 20 3.3 Labeled parameters and arguments ................... 21 3.3.1 Rules of thumb ......................... 22 3.4 Exercises ................................ 24 4 Basic pattern matching 29 4.1 Functions with matching ........................ 30 4.2 Pattern expressions ........................... 31 3 CONTENTS CONTENTS 4.3 Values of other types .......................... 31 4.4 Incomplete matches ........................... 33 4.5 Patterns are everywhere ......................... 34 4.6 Exercises ................................ 35 5 Tuples, lists, and polymorphism 37 5.1 Polymorphism .............................. 37 5.1.1 Value restriction ........................ 38 5.1.2 Other kinds of polymorphism . 39 5.2 Tuples .................................. 41 5.3 Lists ................................... 42 5.4 Tail recursion .............................. 44 5.4.1 Optimization of tail-recursion . 44 5.4.2 Lists and tail recursion ..................... 45 5.5 Exercises ................................ 47 6 Unions 49 6.1 Binary trees ............................... 51 6.2 Unbalanced binary trees ........................ 51 6.3 Unbalanced, ordered, binary trees ................... 52 6.4 Balanced red-black trees ........................ 53 6.5 Open union types (polymorphic variants) . 55 6.5.1 Type definitions for open types . 56 6.5.2 Closed union types ....................... 57 6.6 Some common built-in unions ..................... 57 6.7 Exercises ................................ 58 7 Reference cells and side-effects 61 7.1 Pure functional programming ...................... 62 7.1.1 Value restriction ........................ 64 7.2 Queues ................................. 64 7.3 Doubly-linked lists ........................... 65 7.4 Memoization .............................. 67 7.5 Graphs .................................. 69 7.6 Exercises ................................ 73 8 Records, Arrays, and String 77 8.1 Records ................................. 77 8.1.1 Functional and imperative record updates . 78 8.1.2 Field label namespace ..................... 79 8.2 Arrays .................................. 80 8.3 Strings .................................. 80 8.4 Hash tables ............................... 81 8.5 Exercises ................................ 84 Copyright © Jason Hickey 4 Draft. Do not redistribute. CONTENTS CONTENTS 9 Exceptions 87 9.1 Nested exception handlers ....................... 89 9.2 Examples of uses of exceptions ..................... 90 9.2.1 The exception Not_found ................... 90 9.2.2 Invalid_argument and Failure . 90 9.2.3 Pattern matching failure .................... 91 9.2.4 Assertions ............................ 92 9.2.5 Memory exhaustion exceptions . 92 9.3 Other uses of exceptions ........................ 93 9.3.1 Decreasing memory usage ................... 93 9.3.2 Break statements ........................ 93 9.3.3 Unwind-protect (finally) .................... 94 9.3.4 The exn type .......................... 95 9.4 Exercises ................................ 96 10 Input and Output 99 10.1 File opening and closing ........................ 99 10.2 Writing and reading values on a channel . 100 10.3 Channel manipulation . 101 10.4 String buffers .............................. 102 10.5 Formatted output with Printf . 103 10.6 Formatted input with Scanf . 105 10.7 Exercises ................................ 106 11 Files, Compilation Units, and Programs 109 11.1 Single-file programs . 109 11.1.1 Where is the main function? . 109 11.1.2 OCaml compilers . 111 11.2 Multiple files and abstraction . 111 11.2.1 Defining an interface . 112 11.2.2 Transparent type definitions . 114 11.3 Some common errors . 115 11.3.1 Interface errors . 115 11.4 Using open to expose a namespace . 117 11.4.1 A note about open . 118 11.5 Debugging a program . 118 11.6 Exercises ................................ 121 12 The OCaml Module System 125 12.1 Structures and signatures . 125 12.2 Module definitions . 127 12.2.1 Modules are not first-class . 128 12.2.2 The let module expression . 128 12.3 Recursive modules . 129 12.4 The include directive . 130 12.4.1 Using include to extend modules . 130 Copyright © Jason Hickey 5 Draft. Do not redistribute. CONTENTS CONTENTS 12.4.2 Using include to extend implementations . 130 12.5 Abstraction, friends, and module hiding . 132 12.5.1 Using include with incompatible signatures . 133 12.6 Sharing constraints . 134 12.7 Exercises ................................ 136 13 Functors 139 13.1 Sharing constraints . 141 13.2 Module sharing constraints . 141 13.3 Module re-use using functors . 143 13.4 Higher-order functors . 145 13.5 Recursive modules and functors . 145 13.6 A complete example . 146 13.7 Exercises ................................ 150 14 Objects 155 14.1 Encapsulation and polymorphism . 156 14.2 Transformations ............................. 158 14.2.1 Basis transformations . 158 14.2.2 Functional update . 159 14.3 Binary methods ............................. 160 14.4 Object factories ............................. 160 14.5 Imperative objects ............................ 161 14.6 self: referring to the current object . 163 14.7 Initializers; private methods . 164 14.8 Object types, coercions, and subtyping . 165 14.8.1 Coercions ............................ 166 14.8.2 Subtyping ............................ 168 14.9 Narrowing ................................ 170 14.9.1 Why narrowing is bad . 171 14.9.2 Implementing narrowing . 172 14.10Alternatives to objects . 172 14.11Exercises ................................ 174 15 Classes and inheritance 179 15.1 Class basics ............................... 179 15.1.1 Class types . 180 15.1.2 Parameterized classes . 181 15.1.3 Classes with let-expressions . 181 15.1.4 Type inference . 182 15.2 Inheritance ............................... 183 15.2.1 Method override . 185 15.2.2 Class types . 186 15.2.3 Class type constraints and hiding . 187 15.2.4 Classes and class types as object types . 189 15.3 Inheritance is not subtyping . 190 Copyright © Jason Hickey 6 Draft. Do not redistribute. CONTENTS CONTENTS 15.4 Modules and classes . 193 15.5 Polymorphic methods and virtual classes . 195 15.5.1 Polymorphic methods . 196 15.5.2 Virtual (abstract) classes and methods . 198 15.5.3 Terminology . 199 15.5.4 Stacks .............................. 199 15.5.5 Lists and stacks . 200 15.6 Exercises ................................ 202 16 Multiple inheritance 209 16.1 Examples of multiple inheritance . 209 16.1.1 Inheriting from multiple independent classes . 210 16.1.2 Inheriting from multiple virtual classes . 210 16.1.3 Mixins ............................. 211 16.2 Overriding and shadowing . 213 16.3 Repeated inheritance . 214 16.4 Avoiding repeated inheritance . 216 16.4.1 Is-a vs. has-a . 216 16.4.2 Mixins revisited . 218 16.5 Exercises ................................ 219 17 Polymorphic Classes 221 17.1 Polymorphic dictionaries . 221 17.1.1 Free type variables in polymorphic classes . 222 17.1.2 Instantiating a polymorphic class . 222 17.1.3 Inheriting from a polymorphic class . 223 17.2 Polymorphic class types . 224 17.2.1 Coercing polymorphic classes . 226 17.2.2 Variance annotations . 228 17.2.3 Positive and negative occurrences . 230 17.2.4 Coercing by hiding . 231 17.3 Type constraints ............................. 232 17.4 Comparing objects and modules . 234 17.4.1 Late binding . 234 17.4.2 Extending the definitions . 236 17.5 Exercises ................................ 242 Syntax 251 .1 Notation ................................. 251 .2 Terminal symbols (lexemes) . 252 .2.1 Whitespace and comments . 253 .2.2 Keywords ............................ 253 .2.3 Prefix and infix symbols . 254 .2.4 Integer literals . 254 .2.5 Floating-point literals . 255 .2.6 Character literals . 255 Copyright © Jason Hickey 7 Draft. Do not redistribute. CONTENTS CONTENTS .2.7 String literals . 255 .2.8 Identifiers ............................ 256 .2.9 Labels .............................. 256 .2.10 Miscellaneous . 256 .3 Names .................................. 256 .3.1 Simple names . 256 .3.2 Path names . 257 .4 Expressions ..............................

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    284 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us