[plt-scheme] define/contract/provide ?

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Sun Mar 21 15:41:32 EDT 2010

On Sunday, March 21, 2010, Greg Hendershott <greghendershott at gmail.com> wrote:
> Thanks Robby and Noel.
>
> So if I understand correctly, define/contract would enforce calls to
> the function even from inside the module: define/contract is
> intra-module (function boundary) and provide/contract is inter-module
> (module boundary).  Is that correct?

Right.

> If so, the only practical difference I can see would be performance:
> If say the module code trusts itself and doesn't want to pay the price
> for runtime checking. Is that correct, or is there more nuance to
> contracts that I'm not yet understanding?

That is true but it also affects the granularity of the blame.
Finer-grained contract boundaries potentially cost more but give more
accurate blame.

> Also in general are contracts a way to avoid littering functions with
> explicit runtime checks like "(unless (some-type-predicate? arg)
> (error))", but effectively are just that behind the scenes?

They are like that with advantgages: they are more compact than than
the corresponding checks, they provide you with an interface
specification and  they get the blame assignment right when something
does go wrong.

Robby

> On Sun, Mar 21, 2010 at 2:29 PM, Greg Hendershott
> <greghendershott at gmail.com> wrote:
>> Thanks Robby and Noel.
>>
>> So if I understand correctly, define/contract would enforce calls to
>> the function even from inside the module: define/contract is
>> intra-module (function boundary) and provide/contract is inter-module
>> (module boundary).  Is that correct?
>>
>> If so, the only practical difference I can see would be performance:
>> If say the module code trusts itself and doesn't want to pay the price
>> for runtime checking. Is that correct, or is there more nuance to
>> contracts that I'm not yet understanding?
>>
>> Also in general are contracts a way to avoid littering functions with
>> explicit runtime checks like "(unless (some-type-predicate? arg)
>> (error))", but effectively are just that behind the scenes?
>>
>> On Sun, Mar 21, 2010 at 8:52 AM, Robby Findler
>> <robby at eecs.northwestern.edu> wrote:
>>> Matthew answered the macro issues, but there is a difference between
>>> contracts and types that seems to be tripping you up. Specifically, a
>>> contract always comes with a boundary. The contract governs how the
>>> code inside the boundary interacts with the code outside. This is
>>> unlike types that express facts about the program. For
>>> provide/contract (for example), the boundary is the module; the
>>> contracts govern the values that flow into and out of the module (via
>>> the functions defined in the module).
>>>
>>> So, that's something to keep in mind when you are defining your macro:
>>> where is the inside and where is the outside for your contracts?
>>>
>>> Robby
>>>
>>> On Sat, Mar 20, 2010 at 4:35 PM, Greg Hendershott
>>> <greghendershott at gmail.com> wrote:
>>>> Wrapping my brain around modules and contracts. Coming from C/C++/C# mindset.
>>>>
>>>> Have successfully used define and provide/contract in a module
>>>>
>>>> Just noticed define/contract. I'm thinking, hey, this is how my C-ish
>>>> brain works. Define the function and its types/contract in one swell
>>>> foop.
>>>>
>>>> Small annoyance is now I have to type provide by itself.
>>>>
>>>> So I'm wondering, why isn't there a define/contract/provide?  Isn't
>>>> doing all three a common scenario when making a module?  Or am I
>>>> carrying baggage from my C background?
>>>>
>>>>
>>>> On a related note, I risked further brain burn by attempting to
>>>> provide (sorry) this myself with define-syntax.
>>>>
>>>> Attempt 1 was this:
>>>>
>>>>  (define-syntax-rule (define/contract/provide (id args ...) (contract
>>>> ...) body-expr ...)
>>>>    (begin
>>>>      (define/contract (id args ...) (contract ...) body-expr ...)
>>>>      (provide id)))
>>>>
>>>> But doesn't work because provide doesn't work inside (begin). No error
>>>> making the module, but user of it doesn't see the function since
>>>> provide inside begin (I think?).
>>>>
>>>> Attempt 2 was this:
>>>>
>>>>  (define-syntax-rule (define/contract/provide (id args ...) (contract
>>>> ...) body-expr ...)
>>>>    (define/contract (id args ...) (contract ...) body-expr ...)
>>>>    (provide id))
>>>>
>>>> But that complains:
>>>>  define-syntax-rule: too many templates in: ((provide id))
>>>>
>>>>
>>>> Thanks in advance for any help!
>>>>
>>>> Greg
>>>> _________________________________________________
>>>>  For list-related administrative tasks:
>>>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>>>
>>>
>>
>


Posted on the users mailing list.