concept datum in category idris

appears as: data, data
Type-Driven Development with Idris

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

In each of these cases, one piece of data tells you about the expected form of some other data. If you’ve programmed in C, you’ll have seen a similar idea with the printf function, where one argument is a format string that describes the number and expected types of the remaining arguments. The C type system can’t check that the format string is consistent with the arguments, so this check is often hardcoded into C compilers. In Idris, however, you can write a function similar to printf directly, by taking advantage of types as first-class constructs. You’ll see this specific example in chapter 6.

Figure 6.4. Two different data stores, with different schemas. Schema 1 requires the data to be of type (Int, String), and schema 2 requires the data to be of type (String, String, Int)

data Schema                                                         #1
SchemaType : Schema -> Type                                         #2
data DataStore : Type where
     MkData : (schema : Schema) ->                                  #3
              (size : Nat) -> 
              (items : Vect size (SchemaType schema)) ->            #4
              DataStore 

You’ve now seen several ways in which first-class types increase the expressivity of the type system, and the precision of the types we give to functions. You’ve seen how to use increased precision in types (along with holes) to help write functions, and how to write functions to calculate types. Another way you can use first-class types to increase the precision of your types, and to increase confidence in functions behaving correctly, is to write types specifically to express properties of data and the relationships between data.