Type-Driven Development with Idris MEAP V13
Copyright
Welcome
Brief contents
Chapter 1: Overview
1.1 What is a Type?
1.2 Introducing Type-driven Development
1.2.1 Matrix Arithmetic
1.2.2 An Automated Teller Machine
1.2.3 Concurrent Programming
1.2.4 Type, Define, Refine: The Process of Type-Driven Development
1.2.5 Dependent Types
1.2.6 Introductory Exercises
1.3 Pure Functional Programming
1.3.1 Purity and Referential Transparency
1.3.2 Side Effecting Programs
1.3.3 Partial and Total Functions
1.4 A quick tour of Idris
1.4.1 The interactive environment
1.4.2 Checking Types
1.4.3 Compiling and Running Idris Programs
1.4.4 Incomplete Definitions: Working with Holes
1.4.5 Types as a First-class Construct
1.5 Summary
Chapter 2: Getting started with Idris
2.1 Basic types
2.1.1 Numeric types and values
2.1.2 Type conversions using cast
2.1.3 Characters and strings
2.1.4 Booleans
2.2 Functions: the building blocks of Idris programs
2.2.1 Function types and definitions
2.2.2 Partially applying functions
2.2.3 Writing generic functions: variables in types
2.2.4 Writing generic functions with constrained types
2.2.5 Higher-order function types
2.2.6 Anonymous functions
2.2.7 Local definitions: let and where
2.3 Composite types
2.3.1 Tuples
2.3.2 Lists
2.3.3 Functions with lists
2.4 A complete Idris program
2.4.1 Whitespace significance: the layout rule
2.4.2 Documentation comments
2.4.3 Interactive programs
2.5 Exercises
2.6 Summary
Chapter 3: Interactive development with types
3.1 Interactive editing in Atom
3.1.1 Interactive command summary
3.1.2 Defining functions by pattern matching
3.1.3 Data types and patterns
3.2 Adding precision to types: working with vectors
3.2.1 Refining the type of allLengths
3.2.2 Type-directed search: automatic refining
3.2.3 Type, define, refine: sorting a vector
3.2.4 Exercises
3.3 Example: type-driven development of matrix functions
3.3.1 Matrix operations and their types
3.3.2 Transposing a matrix
3.3.3 Exercises
3.4 Implicit arguments: type-level variables
3.4.1 The need for implicit arguments
3.4.2 Bound and unbound implicits
3.4.3 Using implicit arguments in functions
3.5 Summary
Chapter 4: User-defined data types
4.1 Defining data types
4.1.1 Enumerations
4.1.2 Union types
4.1.3 Recursive types
4.1.4 Generic data types
4.1.5 Exercises
4.2 Defining dependent data types
4.2.1 A first example: classifying vehicles by power source
4.2.2 Defining vectors
4.2.3 Indexing vectors with bounded numbers using Fin
4.2.4 Exercises
4.3 Type-driven implementation of an interactive data store
4.3.1 Representing the store
4.3.2 Interactively maintaining state in main
4.3.3 Commands: parsing user input
4.3.4 Processing commands
4.3.5 Exercises
4.4 Summary
Chapter 5: Interactive programs: input and output processing
5.1 Interactive programming with IO
5.1.1 Evaluating and executing interactive programs
5.1.2 Actions and sequencing: the >>= operator
5.1.3 Syntactic sugar for sequencing with do notation
5.1.4 Exercises
5.2 Interactive programs and control flow
5.2.1 Producing pure values in interactive definitions
5.2.2 Pattern-matching bindings
5.2.3 Writing interactive definitions with loops
5.2.4 Exercises
5.3 Reading and validating dependent types
5.3.1 Reading a Vect from the console
5.3.2 Reading a Vect of unknown length
5.3.3 Dependent pairs
5.3.4 Validating Vect lengths
5.3.5 Exercises
5.4 Summary
Chapter 6: Programming with first-class types
6.1 Type-level functions: calculating types
6.1.1 Type synonyms: giving informative names to complex types
6.1.2 Type-level functions with pattern matching
6.1.3 Using case expressions in types
6.2 Defining functions with variable numbers of arguments
6.2.1 An addition function
6.2.2 Formatted output: a type-safe printf function
6.2.3 Exercises
6.3 Enhancing the interactive data store with schemas
6.3.1 Refining the DataStore type
6.3.2 Using a record for the DataStore
6.3.3 Correcting compilation errors using holes
6.3.4 Displaying entries in the store
6.3.5 Parsing entries according to the schema
6.3.6 Updating the schema
6.3.7 Sequencing expressions with Maybe using do notation
6.3.8 Exercises
6.4 Summary
Chapter 7: Interfaces: using constrained generic types
7.1 Generic comparisons with Eq and Ord
7.1.1 Testing for equality with Eq
7.1.2 Defining the Eq constraint using interfaces and implementations
7.1.3 Default method definitions
7.1.4 Constrained implementations
7.1.5 Constrained interfaces: defining orderings with Ord
7.1.6 Exercises
7.2 Interfaces defined in the Prelude
7.2.1 Converting to String with Show
7.2.2 Defining numeric types
7.2.3 Converting between types with Cast
7.2.4 Exercises
7.3 Interfaces parameterized by Type -> Type
7.3.1 Applying a function across a structure with Functor
7.3.2 Reducing a structure using Foldable
7.3.3 Generic do notation using Monad and Applicative
7.3.4 Exercises
7.4 Summary
Chapter 8: Equality: expressing relationships between data
8.1 Guaranteeing equivalence of data with equality types
8.1.1 Implementing exactLength, first attempt
8.1.2 Expressing equality of Nats as a type
8.1.3 Testing for equality of Nats
8.1.4 Functions as proofs: manipulating equalities
8.1.5 Implementing exactLength, second attempt
8.1.6 Equality in general: the = type
8.1.7 Exercises
8.2 Equality in practice: types and reasoning
8.2.1 Reversing a vector, first attempt
8.2.2 Type checking and evaluation
8.2.3 The rewrite construct: rewriting a type using equality
8.2.4 Delegating proofs and rewriting to holes
8.2.5 Appending vectors, revisited
8.2.6 Exercises
8.3 The empty type and decidability
8.3.1 Void: a type with no values
8.3.2 Decidability: checking properties with precision
8.3.3 DecEq: an interface for decidable equality
8.3.4 Exercises
8.4 Summary
Chapter 9: Predicates: expressing assumptions and contracts in types
9.1 Membership tests: the Elem predicate
9.1.1 Removing an element from a Vect, first attempt
9.1.2 The Elem type: guaranteeing a value is in a vector
9.1.3 Removing an element from a Vect: types as contracts
9.1.4 Auto-implicit arguments: automatically constructing proofs
9.1.5 Decidable predicates: deciding membership of a vector
9.1.6 Exercises
9.2 Expressing program state in types: a guessing game
9.2.1 Representing the game’s state
9.2.2 A top-level game function
9.2.3 A predicate for validating user input: ValidInput
9.2.4 Processing a guess
9.2.5 Deciding input validity: checking ValidInput
9.2.6 Completing the top-level game implementation
9.3 Summary
Chapter 10: Views: Extending Pattern Matching
10.1 Defining and Using Views
10.1.1 Matching the Last Item in a List
10.1.2 Building Views: Covering Functions
10.1.3 with blocks: Syntax for Extended Pattern Matching
10.1.4 Example: Reversing a List using a View
10.1.5 Example: Merge Sort
10.1.6 Exercises
10.2 Recursive Views: Termination and Efficiency
10.2.1 "Snoc" Lists: Traversing a List in Reverse
10.2.2 Recursive Views and the with Construct
10.2.3 Traversing Multiple Arguments: Nested with Blocks
10.2.4 More Traversals: Data.List.Views
10.2.5 Exercises
10.3 Data Abstraction: Hiding the Structure of Data Using Views
10.3.1 Digression: Modules in Idris
10.3.2 The Data Store, Revisited
10.3.3 Traversing the Store's Contents with a View
10.3.4 Exercises
10.4 Summary
Chapter 11: Streams and Processes: Working with Infinite Data
11.1 Streams: Generating and Processing Infinite Lists
11.1.1 First Example: Labelling Elements in a List
11.1.2 Producing an Infinite List of Numbers
11.1.3 Digression: What Does it Mean for a Function to be Total?
11.1.4 Processing Infinite Lists
11.1.5 The Stream data type
11.1.6 An Arithmetic Quiz Using Streams of Random Numbers
11.1.7 Exercises
11.2 Infinite Processes: Writing Interactive Total Programs
11.2.1 Describing Infinite Processes
11.2.2 Executing Infinite Processes
11.2.3 Executing Infinite Processes as Total Functions
11.2.4 Generating Infinite Structures using Lazy Types
11.2.5 Extending do-notation for InfIO
11.2.6 A Total Arithmetic Quiz
11.2.7 Exercise
11.3 Interactive Programs with Termination
11.3.1 Refining InfIO: Introducing Termination
11.3.2 Domain Specific Commands
11.3.3 Sequencing Commands with do-notation
11.3.4 Exercises
11.4 Summary
Chapter 12: Writing Programs with State
12.1 Working with Mutable State
12.1.1 Running Example: Tree Traversal
12.1.2 Representing Mutable State using a Pair
12.1.3 State, a Type for Describing Stateful Operations
12.1.4 Tree Traversal with State
12.1.5 Exercises
12.2 A Custom Implementation of State
12.2.1 Defining State and runState
12.2.2 Defining Functor, Applicative
and Monad Implementations for State
12.3 A Complete Program with State: Working with Records
12.3.1 Interactive Programs with State: The Arithmetic Quiz Revisited
12.3.2 Complex State: Defining Nested Records
12.3.3 Updating Record Field Values
12.3.4 Updating Record Fields by Applying Functions
12.3.5 Implementing the Quiz
12.3.6 Running Interactive and Stateful Programs: Executing the Quiz
12.3.7 Exercises
12.4 Summary
Chapter 13: State Machines: Verifying Protocols in Types
13.1 State Machines: Tracking State in Types
13.1.1 Finite State Machines: Modelling a Door
as a Type
13.1.2 Interactive Development of Sequences of Door Operations
13.1.3 Infinite States: Modelling a Vending Machine
13.1.4 A Verified Vending Machine Description
13.1.5 Exercises
13.2 Dependent Types in State: Implementing a Stack
13.2.1 Representing Stack Operations in a State Machine
13.2.2 Implementing the Stack using Vect
13.2.3 Using a Stack Interactively: A Stack-based Calculator
13.2.4 Exercises
13.3 Summary
Chapter 14: Dependent State Machines: Handling Feedback and Errors
14.1 Dealing with Errors in State Transitions
14.1.1 Refining the Door Model: Representing Failure
14.1.2 A Verified, Error Checking, Door Protocol Description
14.2 Security Properties in Types: Modelling an ATM
14.2.1 Defining States for the ATM
14.2.2 Defining a Type for the ATM
14.2.3 Simulating an ATM at the Console: Executing ATMCmd
14.2.4 Refining Preconditions using auto-implicits
14.2.5 Exercises
14.3 A Verified Guessing Game: Describing Rules in Types
14.3.1 Defining an Abstract Game State and Operations
14.3.2 Defining a Type for the Game State
14.3.3 Implementing the Game
14.3.4 Defining a Concrete Game State
14.3.5 Running the Game: Executing GameLoop
14.4 Summary
Chapter 15: Type-safe Concurrent Programming
15.1 Primitives for Concurrent Programming in Idris
15.1.1 Defining Concurrent Processes
15.1.2 The Channels Library: Primitive Message Passing
15.1.3 Problems with Channels: Type Errors and Blocking
15.2 Defining a Type for Safe Message Passing
15.2.1 Describing Message Passing Processes in a Type
15.2.2 Making Processes Total using Inf
15.2.3 Guaranteeing Responses using a State Machine and Inf
15.2.4 Generic Message Passing Processes
15.2.5 Defining a module for Process
15.2.6 Example 1: List Processing
15.2.7 Example 2: A Word Counting Process
15.3 Summary
Appendix A: Installing Idris and Editor Modes
A.1 The Idris Compiler and Environment
A.1.1 OS X
A.1.2 Windows
A.1.3 Unix-like platforms, installing from source
A.2 Editor Modes
A.2.1 Atom
A.2.2 Other Editors
Appendix B: Interactive Editing Commands
Appendix C: REPL Commands