concept type - check in category idris

appears as: type-checks, type-check
Type-Driven Development with Idris

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

Testing a concurrent program is difficult because, unlike a purely sequential program, there’s no guarantee about the order in which operations from different processes will execute. Even if two processes are correctly coordinated when you run a test once, there’s no guarantee they’ll be correctly coordinated when you next run the test. On the other hand, if you can express the coordination between processes in types, you can be sure that a concurrent program that type-checks has properly coordinated processes.

  • 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
  • While following this type-driven approach, you always have a file that type-checks as far as possible. Here, rather than filling the hole completely, you’ve written a small part of it with a new hole, and checked that what you have type-checks before proceeding.