Fellow Scheming Racketeers,
When you've written a function that takes an integer and returns another one, you may write a contract for it as (-> integer? integer?)
. I want to tell you about the social-contract
package which allows you to write it as (self-map/c integer?)
Why would the latter form be preferable? It isn't much less to type. But, as we'll see, it is composable and it exploits the phrase structure of a contract specification. Consider:(-> (-> integer? integer? integer?) (-> integer? integer? integer?)))
With social-contract, this would be expressed as:(self-map/c (binary-composition/c integer?))
With a little familiarity, this tells you a lot, and saves you the trouble of parsing the low level contract specification in order to understand what this function does.
And how about this:(-> (-> any/c boolean?) sequence? sequence?)
This becomes simply:filter/c
Who decides what "self map," "composition," and "filter" mean?
We all do! In principle. The contracts available right now correspond to well-known mathematical or programming ideas, but they could be anything at all that we find to be common and useful. The "social" idea here is that, through issues raised and discussed
on the source repo, we collectively agree on the forms and variations of useful high level contracts.But wouldn't it be tedious to learn the social contract forms?
On the contrary, it just might be fun. The package ships with C3PO, a handy contract migration assistant that already knows both the Racket contract DSL as well as the social contract forms, so you can provide contracts you've already written and it will translate them into high-level social contract representations. This can be useful for learning the new forms in an interactive way, and can also greatly reduce the time it would take to migrate any existing contracts you may have.
Incidentally, C3PO is a "reverse compiler" implemented using parser combinators (megaparsack
). It is "reverse" in that it translates low-level contract specifications into high-level ones, and may be a curiosity in its own right. You can learn more about it here
and see its source here