liveBook · Manning

Chapter 6: Chapter 6. Programming with first-class types

What you will learn in chapter 6 of Type-Driven Development with Idris

This introduction explains first-class types in Idris and illustrates calculating types from data.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains first-class types in Idris and illustrates calculating types from data.

Key terms: first-class, types, expressions, vector, type-level functions, printf, format string, arguments

6.1 Type-level functions: calculating types

This section introduces types and expressions as part of the same language and explains why functions can calculate types.

Key terms: Vect, Nat, expressions, types, type synonyms, type-level functions, Position, schema

6.1.1 Type synonyms: giving informative names to complex types

This section introduces type synonyms as ordinary functions that give informative names to types and shows how Idris tools work with them.

Key terms: type synonym, Position, Polygon, Type, REPL, expression search, type synonyms

6.1.2 Type-level functions with pattern matching

This section uses pattern matching on a Boolean to calculate types and refine dependent function inputs and outputs.

Key terms: type-level functions, Type, case splitting, getStringOrInt, StringOrInt, refined type, dependent pattern matching, valToString, trim, cast

6.1.3 Using case expressions in types

This section develops valToString using a case expression in its type and holes to refine the definition interactively.

Key terms: case expression, type level, valToString, isInt, hole, case splitting, refine, ?argType

6.2 Defining functions with variable numbers of arguments

This section introduces using type-level functions to give functions argument lists that depend on an input value.

Key terms: type-level functions, function types, variable-length argument lists

6.2.1 An addition function

This section develops a variable-argument addition function using a computed type and refines it to work with any numeric type.

Key terms: AdderType, type-level function, Nat, numargs, accumulator, case splitting, Z, S k, hole, recursive call

6.2.2 Formatted output: a type-safe printf function

This section develops a type-safe printf whose format string determines its argument types and whose result is a String.

Key terms: printf, format string, directive, Int, String, Format, Str, Lit, Number, End

6.3 Enhancing the interactive data store with schemas

Introduces schemas as descriptions of data-store entry types and previews the planned extension of the existing store.

Key terms: schema, DataStore, String

6.3.1 Refining the DataStore type

This section refines DataStore to describe user-selected entry structures with schemas and explains how to declare operator fixity.

Key terms: DataStore, Schema, SchemaType, type-level function, fixity, precedence, infixl, infixr, infix, commenting out

6.3.2 Using a record for the DataStore

This section introduces Idris records as a way to define DataStore with automatically generated field projections.

Key terms: record declaration, constructor, projection functions, fields, DataStore, schema, items, projection

6.3.3 Correcting compilation errors using holes

This section repairs the data store program after introducing schemas, using type errors and temporary holes to guide the changes.

Key terms: type errors, holes, type-checks, MkData, schema, addToStore, addToData, getEntry, index, SchemaType

6.3.4 Displaying entries in the store

This section implements a generic display function that converts schema-typed store entries to strings by splitting on the schema.

Key terms: display, SchemaType, schema, implicit argument, case splitting, SString, SInt, show, case-split, recursively

6.3.5 Parsing entries according to the schema

This section develops a schema-directed parser that converts complete user inputs into appropriately typed values and reports failures.

Key terms: parseBySchema, SchemaType, Maybe, parseCommand, Nothing, parsePrefix, schema, remainder, SInt, SString

6.3.6 Updating the schema

This section adds user-configurable schemas to the data store and explains how schema-dependent types protect its contents.

Key terms: schema, SetSchema, Command, parseCommand, processInput, DataStore, empty store, parser, data type, types of the operations

6.3.7 Sequencing expressions with Maybe using do notation

This section explains how Maybe computations can be sequenced using bind and do notation instead of nested case expressions.

Key terms: Maybe, Nothing, Just, (>>=), maybeAdd, do notation, parseCommand

6.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Chapter 5. Interactive programs: input and output processing Chapter 7: Chapter 7. Interfaces: using constrained generic types →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.