Chapter 2. Getting started with Idris
This chapter introduces Idris through familiar foundations, emphasizing that before tackling dependent types, it is worth understanding the basic types, functions, and program structure that every Idris programmer uses. It shows how to explore the language interactively in the REPL, how to inspect types, and how to rely on the Prelude, Idris’s always-available standard library. The chapter also demonstrates that many everyday programming tasks can already be expressed cleanly in Idris, even without advanced type features.
The central ideas are Idris’s core data types and how they behave in a strongly typed functional setting. Numeric values, characters, strings, booleans, tuples, and lists are presented alongside their common operations, with special attention to explicit type handling through the and cast. The text stresses that function application is written with whitespace, that operators are ordinary functions, and that generic programming is possible by using variables in types plus constraints such as Num, Eq, and Ord. It also explains partial application, higher-order functions, anonymous functions, and local definitions with let and where, building up the expressive power needed for real programs.
The chapter closes by showing how these pieces fit into complete Idris programs. It explains modules, namespaces, exports, indentation-based layout, comments, and documentation comments, then combines them in a worked example that computes and prints the average word length of user input. Along the way, it highlights that Idris checks totality, supports interactive execution through main and repl, and encourages a style where types guide design from the start. The result is a practical foundation for writing small, structured Idris programs before moving on to the more distinctive dependent-type features later in the book.
Summary
- The Prelude defines a number of basic types and functions and is imported automatically by all Idris programs.
- Idris provides basic numeric types, Int, Integer, Nat, and Double, as well as a Boolean type, Bool, a character type, Char, and a string type, String.
- Values can be converted between compatible types using the cast function, and can be given explicit types with the the function.
- Tuples are fixed-size collections where each element can be a different type.
- Lists are variable size collections where each element has the same type.
- Function types have one or more input types and one output type.
- Function types can be generic, meaning that they can contain variables. These variables can be constrained to allow a smaller set of types.
- Higher-order functions are functions in which one of the arguments is itself a function.
- Functions consist of a required type declaration and a definition. Function definitions are equations defining rewrite rules to be used during evaluation.
- Whitespace is significant in Idris programs. Each definition in a block must begin in exactly the same column.
- Function documentation can be accessed at the REPL with the :doc command.
- Idris programs can be divided into separate source files called modules.
- The entry point to an Idris program is the main function, which must have type IO (), and be defined in the module Main. Simple interactive programs can be written by applying the repl function from main.
FAQ
What topics are covered in Chapter 2, “Getting started with Idris”?
Chapter 2 introduces Idris basics: built-in types and functions, defining functions, using local definitions, working with tuples and lists, and structuring complete Idris programs with modules and amain function.What are the main built-in numeric types in Idris?
Idris includesInt for fixed-width signed integers, Integer for unbounded signed integers, Nat for unbounded unsigned integers, and Double for double-precision floating-point numbers.How does Idris handle numeric literals and type inference?
Numeric literals are often inferred from context. By default, a plain number like333 is treated as an Integer unless another type is required. A literal like 333.0 is a Double because of the decimal point.What is the purpose of the in Idris?
the is used to make a type explicit when Idris could infer more than one possible type. For example, the Int (6 + 3 * 12) forces the expression to be treated as an Int.Why is cast used in Idris?
cast converts a value from one primitive type to another when a valid conversion exists. It is commonly used to make values compatible in expressions that require matching types, such as converting an Integer to a Double before division.How are functions defined in Idris?
A function definition starts with a type declaration and is then defined using equations. For example,double : Int -> Int followed by double x = x + x. Type declarations are required in Idris.What is partial application in Idris?
Partial application means supplying only some of a function’s arguments and getting back a new function waiting for the rest. For example,add 2 has type Int -> Int and adds 2 to its input.How are strings and lists different in Idris?
Strings are primitive values of typeString, while lists are composite values written with square brackets and must contain elements of the same type. Strings support functions like reverse, substr, and ++; lists support ::, map, filter, and length.What is the role of let and where?
let introduces local variable bindings inside an expression, while where introduces local function definitions attached to a larger definition. Both help break programs into smaller, clearer pieces.How does Idris organize complete programs using modules and main?
A file can begin with a module declaration to create a namespace, and exported definitions are made available to other modules with export. A complete executable program defines main : IO () in the Main module, which serves as the entry point.
Type-Driven Development with Idris ebook for free