concept definition in category idris

appears as: definition, definition, definitions, The definitions, definitions, The definition, The definition
Type-Driven Development with Idris

This is an excerpt from Manning's book Type-Driven Development with Idris.

Type-driven development is a style of programming in which we write types first and use those types to guide the definition of functions. The overall process is to write the necessary data types, and then, for each function, do the following:

It’s also a good idea to check whether Idris believes the definition is total.

  • Define— You now need to check x < y and act on the result. One way to do this is with an if...then...else construct:
    insert : Ord elem =>
             (x : elem) -> (xsSorted : Vect k elem) -> Vect (S k) elem
    insert x [] = [x]
    insert x (y :: xs) = if x < y then x :: y :: xs
                                  else y :: insert x xs
    Alternatively, you can use interactive editing to give more structure to the definition, and insert a case construct to match on an intermediate result. Press Ctrl-Alt-M with the cursor over ?insert_rhs_2. This introduces a new case expression with a placeholder for the value to be inspected (so the function won’t type-check yet):
    insert : Ord elem =>
             (x : elem) -> (xsSorted : Vect k elem) -> Vect (S k) elem
    insert x [] = [x]
    insert x (y :: xs) = case _ of
                              case_val => ?insert_rhs_2
    The _ stands for an expression you need to provide in order for the function to type-check successfully. You’ll need to fill in the _ with the expression you want to match:
    insert : Ord elem =>
             (x : elem) -> (xsSorted : Vect k elem) -> Vect (S k) elem
    insert x [] = [x]
    insert x (y :: xs) = case x < y of
                              case_val => ?insert_rhs_2
  • Once the definition is complete, you can test it at the REPL, like this: