Types and Trust

Perhaps this used to be a well known thing in the world of the C programming language, but a way I started thinking of types are as a trust between you and the type system. At the end of the day, all data is a binary blob. Good type systems will ensure the types we tell it to enforce are enforced, generally we will trust our type checkers. The things that we have to trust, are the functions which lift binary blobs to types that we name.

Here is an example:

There could be some type called NumericString , which is a string that only contains numeric values. This could be like: "23423" or "1", but NOT 23 or 2234 or "f32" or "oiwjef". But how would we ever get a value of NumericString type?

The way this type can exist is if we write functions that can lift normal values into this type. Those fuctions are what we will be trusting. One could be fromString which has the signature String -> NumericString or getNumericString with the signature IO NumericString which gets a numeric string as input. How can we make sure that the NumericString is actually only numbers? That's what the "lifting" functions do. Those are the functions that we just have to trust that they work.

This is seen in custom libraries in Haskell when performUnsafeIO is used. That function bypasses the IO type to perform IO in a function without having to return the IO type. This is exactly what our imaginary NumericString was doing with getNumericString , just that we are able to call non-Haskell code. We could call a pure C function using performUnsafeIO , and claim it is of some type. The same trust that we had to put into NumericString would be put into that type.