liveBook · Manning

Function type

As explained in Type-Driven Development with Idris

A function type specifies the types of a function's inputs and result.

A function type states what a function accepts and what it returns. Read `Int -> Int` as a function from `Int` to `Int`; the signature gives a plan for the definition and a check on its calls. section 2.2.1 section 5.3

double : Int -> Int           #1
double x = x + x              #2

For `double`, the signature `Int -> Int` pairs with a definition that adds its input to itself. section 2.2.1

A signature with a dependent constraint

The type `zip : Vect n a -> Vect n b -> Vect n (a, b)` expresses that both input vectors have length `n` and the result pairs corresponding elements. The type tells the caller what must hold for the function to be used safely. section 5.3

In Idris, a function is introduced with a type declaration and defined by equations. Try a definition in a file, load it in the Idris REPL, and check calls such as `double 47` or `double (double 15)`. section 2.2.1

How Type-Driven Development with Idris covers it

How deep it goes
Defines input-to-output signatures and works through a simple function and a dependent vector signature.
Examples
Uses `double : Int -> Int`, REPL evaluations, and `zip` over equal-length vectors.
What you can do after
Build an Idris function from an input-to-result signature and test its evaluations in the REPL.
How it connects
Connects function types to dependent types, generic types, and higher-order functions.

Also explained in

Compare how 5 books explain function type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Type-Driven Development with Idris. The section links point to the parts of the book it draws on.