Most statically-typed languages have you put all the typing information in the same place. So far, we’ve had to put the output contract at the end of the function in the return statement. We’ve seen that we can write a higher-order contract that guarantees a function will map strings to strings; here’s one that works for an arbitrary pair of contracts:
var hom = function (sourceArr, target) { var source = makeProduct(sourceArr); return function (f) { func(f); return function (varArgs) { return target(f.apply(this, source(toArray(arguments)))); }; }; };
We can use hom
to give type information up front:
var repeat = hom([str, nat32], str) (function (s, n) { return Array(n+1).join(s); });
The name “hom” comes from category theory: given any two objects A
and B
in a category, there’s a set of morphisms between them called hom(A, B)
. The higher-order contract hom
ensures that a function is an element of that set.