liveBook · Manning

Type-level function

As explained in Type-Driven Development with Idris

A function whose input and output are evaluated or interpreted in type positions to determine types.

A type-level function calculates a type from an input. In Idris, it is an ordinary function returning Type, used where a type is expected. This lets a type reflect information such as a Boolean flag, a format description, or a schema. A type synonym, which gives an existing type a more meaningful name, is a special case. section 6.1 section 6.1.2

Listing 6.2. A function that calculates a type from a Bool (TypeFuns.idr)
StringOrInt : Bool -> Type
StringOrInt False = String
StringOrInt True = Int

StringOrInt maps False to String and True to Int using ordinary pattern matching. section 6.1.2

The declaration getStringOrInt : (isInt : Bool) -> StringOrInt isInt makes the result type depend on the flag. Case-splitting the implementation on isInt refines the expected result to String in the False branch and Int in the True branch. Inspecting the resulting holes gives a practical guide to completing each branch. section 6.1.2

getStringOrInt : (isInt : Bool) -> StringOrInt isInt
getStringOrInt False = "Ninety four"
getStringOrInt True = 94

The completed branches return values matching the types calculated for their respective flags. section 6.1.2

A calculated type can itself be a function type. To build an adder that accepts a specified number of additional integers, first calculate its result type from that number. Zero requires an Int; each successor adds another Int argument. section 6.2 section 6.2.1

AdderType : (numargs : Nat) -> Type
AdderType Z = Int                                        #1
AdderType (S k) = (next : Int) -> AdderType k            #2

AdderType recursively constructs the function type. The matching adder implementation returns its accumulator at zero and otherwise accepts another integer. section 6.2.1

The same idea makes formatted output type-safe. A Format description distinguishes numeric directives, string directives, literal text, and the end of the format. PrintfType adds an Int or String argument for each directive and ends in String. For example, "%s number %d" requires a String and then an Int. This version focuses on %s and %d rather than the full range of C printf directives. section 6.2.2

PrintfType : Format -> Type     
PrintfType (Number fmt) = (i : Int) -> PrintfType fmt            #1
PrintfType (Str fmt) = (str : String) -> PrintfType fmt          #2
PrintfType (Lit str fmt) = PrintfType fmt                        #3
PrintfType End = String                                          #4

PrintfType walks the format description: literals add no argument, while directives add an argument of the appropriate type. section 6.2.2

A schema can similarly describe data before entries are added. SchemaType translates string and integer descriptions into concrete types and combines them into pairs; a store can then use the calculated type for its items. In message-passing processes, an interface can instead calculate a response type from each request, tying the expected response to the message sent. section 6.3.1 section 15.2.4

How Type-Driven Development with Idris covers it

How deep it goes
Works through pattern matching, recursive function-type construction, and schema translation, using case splits and hole types to refine implementations.
Examples
Uses a Boolean-selected result, variable-argument adder, formatted output, schema-driven data store, and request-dependent process responses.
What you can do after
Builds type-level functions from scratch to calculate dependent result types and variable argument lists in Idris.
How it connects
Connects type synonyms, dependent types, pattern matching, data schemas, and message-passing interfaces.

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.