concept more expressive type in category idris

appears as: more expressive type
Type-Driven Development with Idris

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

  • Refine— Before you can make progress, you’ll need to check that the length of the second vector is the same as the length of the first. As a first attempt, you might try the following:
    if len1 == len2
       then ?zipInputs_rhs1
       else ?zipInputs_rhs2
    Unfortunately, this doesn’t help. If you look at the type of ?zipInputs_rhs1, you’ll see that nothing has changed:
      len1 : Nat
      len2 : Nat
      vec2 : Vect len2 String
      vec1 : Vect len1 String
    --------------------------------------
    zipInputs_rhs1 : IO ()
    The problem is that the type of len1 == len2, Bool, tells you nothing about the meaning of the operation itself. As far as Idris is concerned, the == operation could be implemented in any way (you’ll see in chapter 7 how == can be defined) and doesn’t necessarily guarantee that len1 and len2 really are equal. Instead, you can use the following function defined in Data.Vect:
    exactLength : (len : Nat) -> (input : Vect m a) -> Maybe (Vect len a)
    This function takes a length, len, and a vector, input, of any length. If the length of the input vector turns out to be len, it returns Just input, with its type updated to Vect len a. Otherwise, it returns Nothing.

    Implementing exactLength

    To implement exactLength, you need a more expressive type than Bool for representing the result of an equality test. You’ll see how to do this in chapter 8, and we’ll discuss the limitations of Bool in general.


    Using exactLength, you can refine the definition as follows:
    zipInputs : IO ()
    zipInputs = do putStrLn "Enter first vector (blank line to end):"
                   (len1 ** vec1) <- readVect
                   putStrLn "Enter second vector (blank line to end):"
                   (len2 ** vec2) <- readVect
                   case exactLength len1 vec2 of
                        Nothing => ?zipInputs_rhs_1
                        Just vec2' => ?zipInputs_rhs_2
  • To implement exactLength, you need a more expressive type than Bool for representing the result of an equality test. You’ll see how to do this in chapter 8, and we’ll discuss the limitations of Bool in general.