Hom

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.

Advertisements

2 Comments to “Hom”

  1. This is the internal hom, correct? So the contract category is cartesian-closed?

    • Yep. I’ll explain in detail in another post; I want to introduce the JavaScript code and let people get comfortable with the ideas there before I start abstracting them.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: