Caml is a general-purpose, powerful, high-level programming language with a large emphasis on speed and efficiency. A dialect of the ML programming language, it supports functional, imperative, and object-oriented programming styles. Caml has been developed and distributed by INRIA, a French research institute, since 1985.
The OCaml system is the main implementation of the Caml language. It has a very strong type-checking system, offers a powerful module system, automatic memory management, first-class functions, and adds a full-fledged object-oriented layer. OCaml includes a native-code compiler supporting numerous architectures, for high performance; a bytecode compiler, for increased portability; and an interactive loop, for experimentation and rapid development. OCaml’s integrated object system allows object-oriented programming without sacrificing the benefits of functional programming, parametric polymorphism, and type inference. The language is mature, producing efficient code and comes with a large set of general purpose as well as domain-specific libraries.
OCaml is often used for teaching programming, and by large corporations. OCaml benefits from a whole range of new tools and libraries, including OPAM (package manager), optimizing compilers, and development tools such as TypeRex and Merlin.
OCaml was written in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, and Didier Rémy at INRIA in France.
1. Real World OCaml by Yaron Minsky, Anil Madhavapeddy, Jason Hickey
Real World OCaml introduces the reader to OCaml, an industrial-strength programming language designed for expressiveness, safety, and speed. Through the book’s many examples, the reader learns how OCaml stands out as a tool for writing fast, succinct, and readable code. The book offers a clear guide to what you need to know to use OCaml effectively in the real world.
The book starts with a gentle introduction to OCaml. Part 2 of the book (“tools and techniques”) demonstrates how to perform practical tasks such as: parse command-lines, read and write JSON formatted data and handle concurrent I/O; while part 3 dives into low-level detail including: interfacing to C and understanding the GCC and compiler toolchain. It’s aimed at programmers who have some experience with conventional programming languages, but not specifically with statically typed functional programming.
- A Guided Tour – gives an overview of OCaml by walking through a series of small examples that cover most of the major features of the language.
- Variables and Functions – covers OCaml’s approach to variables and functions in some detail, starting with the basics of how to define a variable, and ending with the intricacies of functions with labeled and optional arguments.
- Lists and Patterns – goes into more detail about these two common elements of programming.
- Files, Modules, and Programs – shows the reader how to build an OCaml program from a collection of files, as well as the basics of working with modules and module signatures.
- Records – in depth treatment covering the details of how records work, as well as advice on how to use them effectively in software designs.
- Variants – explores one of the most useful features of OCaml.
- Error Handling – discusses some of the different approaches in OCaml to handling errors, and give some advice on how to design interfaces that make error handling easier.
- Imperative Programming – walks the reader through OCaml’s imperative features, and help use them to their fullest.
- Functors – functions from modules to modules which can be used to solve a variety of code-structuring problems.
- First-Class Modules – ordinary values that can be created from and converted back to regular modules.
- Objects – introduces the reader to OCaml objects and subtyping.
- Classes – introduces the reader to classes and inheritance.
- Maps and Hash Tables – a map is an immutable tree-based data structure where most operations take time logarithmic in the size of the map, whereas a hash table is a mutable data structure where most operations have constant time complexity. This chapter describes both of these data structures in detail and provides advice as to how to choose between them.
- Command-Line Parsing – construct basic and grouped command-line interfaces, build simple equivalents to the cryptographic md5 and shasum utilities, and shows how functional combinators can be used to declare complex command-line interfaces in a type-safe and elegant way.
- Handling JSON Data – introduces the reader to a couple of new techniques.
- Parsing with OCamilex and Menhir – OCamilex, replaces lex, and ocamlyacc and menhir, which replace yacc. This chapter explores these tools and the implementation of a parser for the JSON serialization format.
- Data Serialization with S-Expressions – goes into s-expressions in more depth.
- Concurrent Programming with Async – covers the Async library, which offers a hybrid model that aims to provide the best of both worlds.
- Foreign Function Interface – show how to call routines in C libraries directly from OCaml code, how to build higher-level abstractions in OCaml from the low-level C bindings, and work through some full examples for binding a terminal interface and UNIX date/time functions.
- Memory Representation of Values – describes the runtime format of individual OCaml variables.
- Understanding the Garbage Collector.
- The Compiler Frontend: Parsing and Type Checking – compilation pipeline and what each stage represents, source preprocessing via Camlp4 and the intermediate forms, and type-checking process, including module resolution.
- The Compiler Backend: Bytecode and Native code – untyped intermediate lambda code where pattern matching is optimized, bytecode ocamlc compiler and ocamlrun interpreter, and native code ocamlopt code generator, and debugging and profiling native code.
The online HTML version of the book is available under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
2. Unix system programming in OCaml by Xavier Leroy and Didier Rémy
Unix system programming in OCaml is an introductory course on Unix system programming, with an emphasis on communications between processes. The main novelty of this work is the use of the OCaml language, a dialect of the ML language, instead of the C language that is customary in systems programming. This gives an unusual perspective on systems programming and on the ML language.
This document describes only the programmatic interface to the Unix system. It presents neither its implementation, neither its internal architecture.
- Generalities – modules Sys and Unix, interface with the calling program, error handling, and library functions.
- Files – including opening a file, reading and writing, positioning, locks on files, complete examples: file copy, recursive copy of files, and Tape ARchive.
- Processes – creation of processes, awaiting the termination of a process, launching a program, complete examples: the command leave, a mini-shell.
- Signals – includes how to use signals, using signals, how to mask signals, signals and system calls, problems with signals.
- Classical inter-process communication: pipes – pipes, named pipes, descriptor redirectors, input/out multiplexing, complete examples: parallel sieve of Eratosthenes, composing N commands.
- Modern communication: sockets – sockets, socket creation, addresses, disconnecting sockets, establishing a service, tuning sockets, high-level primitives, complete examples: the universal client, the universal server, HTTP requests.
- Threads – creation and termination of threads, waiting, synchronization among threads: locks, conditions, event-based synchronous.
Unix System Programming in OCaml is distributed under a Creative Commons by-nc-sa license.
3. How to Think Like a (Functional) Programmer by Allen Downey and Nicholas Monje
How to Think Like a Computer Scientist is an introductory programming textbook based on the OCaml language which teaches the reader to think like a computer scientist.
It’s a modified version of Think Python by Allen Downey.
The book is intended for newcomers to programming and also those who know some programming but want to learn programming in the function-oriented paradigm, or those who simply want to learn OCaml.
- The way of the program.
- Variables and Expressions – includes values and types, variables, expressions, string operations, and debugging.
- Functions – includes function calls, math functions, composition, adding new functions, and more.
- Program Flow – includes coverage of Boolean expressions, logical operators, chained conditionals.
- Recursive Functions – recursion, infinite recursion, mutually recursive functions, tail-end recursion, and debugging.
- Algorithms – square roots, algorithms, and debugging.
- Strings – string.length, substrings, string traversal, searching, string comparison, and debugging.
- Lists – list operations, list iteration, mapping and folding, list sorting, lists and recursion, and debugging.
- Case Study: Regular Expressions.
- Putting the O in OCaml, Part 1: Imperative programming.
- Arrays – making arrays, array operations, array iteration, mapping, and folding, array sorting, and array traversal.
- Hashtables – includes folding and hashtables, reverse lookup, memos and more.
- Tuples – includes enumerated types, and aggregate types.
- Records and Custom Data Structures.
- Putting the O in OCaml, Part 2: Objects and Classes.
- Case study: data structure selection.
Permission is granted to copy, distribute, and/or modify this book under the terms of the GNU Free Documentation License, Version 1.1 or any later version.
All books in this series:
|Free Programming Books|
|Java||General-purpose, concurrent, class-based, object-oriented, high-level language|
|C||General-purpose, procedural, portable, high-level language|
|Python||General-purpose, structured, powerful language|
|C++||General-purpose, portable, free-form, multi-paradigm language|
|C#||Combines the power and flexibility of C++ with the simplicity of Visual Basic|
|PHP||PHP has been at the helm of the web for many years|
|HTML||HyperText Markup Language|
|SQL||Access and manipulate data held in a relational database management system|
|Ruby||General purpose, scripting, structured, flexible, fully object-oriented language|
|Assembly||As close to writing machine code without writing in pure hexadecimal|
|Swift||Powerful and intuitive general-purpose programming language|
|Groovy||Powerful, optionally typed and dynamic language|
|Go||Compiled, statically typed programming language|
|Pascal||Imperative and procedural language designed in the late 1960s|
|Perl||High-level, general-purpose, interpreted, scripting, dynamic language|
|R||De facto standard among statisticians and data analysts|
|COBOL||Common Business-Oriented Language|
|Scala||Modern, object-functional, multi-paradigm, Java-based language|
|Fortran||The first high-level language, using the first compiler|
|Scratch||Visual programming language designed for 8-16 year-old children|
|Lua||Designed as an embeddable scripting language|
|Logo||Dialect of Lisp that features interactivity, modularity, extensibility|
|Rust||Ideal for systems, embedded, and other performance critical code|
|Lisp||Unique features - excellent to study programming constructs|
|Ada||ALGOL-like programming language, extended from Pascal and other languages|
|Haskell||Standardized, general-purpose, polymorphically, statically typed language|
|Scheme||A general-purpose, functional language descended from Lisp and Algol|
|Prolog||A general purpose, declarative, logic programming language|
|Forth||Imperative stack-based programming language|
|Clojure||Dialect of the Lisp programming language|
|Julia||High-level, high-performance language for technical computing|
|Awk||Versatile language designed for pattern scanning and processing language|
|BASIC||Beginner’s All-purpose Symbolic Instruction Code|
|Erlang||General-purpose, concurrent, declarative, functional language|
|VimL||Powerful scripting language of the Vim editor|
|OCaml||The main implementation of the Caml language|
|ECMAScript||Best known as the language embedded in web browsers|
|Bash||Shell and command language; popular both as a shell and a scripting language|
|LaTeX||Professional document preparation system and document markup language|
|TeX||Markup and programming language - create professional quality typeset text|
|Arduino||Inexpensive, flexible, open source microcontroller platform|
|Elixir||Relatively new functional language running on the Erlang virtual machine|
|F#||Uses functional, imperative, and object-oriented programming methods|
|Tcl||Dynamic language based on concepts of Lisp, C, and Unix shells|
|Factor||Dynamic stack-based programming language|
|Eiffel||Object-oriented language designed by Bertrand Meyer|
|Agda||Dependently typed functional language based on intuitionistic Type Theory|
|Icon||Wide variety of features for processing and presenting symbolic data|
|XML||Rules for defining semantic tags describing structure ad meaning|
|Vala||Object-oriented language, syntactically similar to C#|
|Standard ML||General-purpose functional language characterized as "Lisp with types"|
|D||General-purpose systems programming language with a C-like syntax|
|Dart||Client-optimized language for fast apps on multiple platforms|
|Markdown||Plain text formatting syntax designed to be easy-to-read and easy-to-write|
|Kotlin||More modern version of Java|
|Objective-C||Object-oriented language that adds Smalltalk-style messaging to C|
|VHDL||Hardware description language used in electronic design automation|
|J||Array programming language based primarily on APL|
|LabVIEW||Designed to enable domain experts to build power systems quickly|