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
<https://docs.racket-lang.org/social-contract/index.html> package which
allows you to write it as (self-map/c integer?) instead.

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
<https://github.com/countvajhula/social-contract/issues> 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
<https://docs.racket-lang.org/megaparsack/index.html>). 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
<https://docs.racket-lang.org/social-contract/index.html#%28part._c3po%29>
and see its source here
<https://github.com/countvajhula/social-contract/tree/master/contract/social/c3po>
.

Enjoy,
-Sid

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CACQBWFnUiaRZrO6jaGtOdyr1HxF%2BXn8aSovjC_VAaMHQtuHFxQ%40mail.gmail.com.

Reply via email to