[racket] sharing an expensive computation required in procedure and its contract?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Jul 20 14:26:51 EDT 2014

On Jul 20, 2014, at 1:56 PM, Jon Zeppieri wrote:

> I feel like this has come up before, but I can't seem to find the
> discussion in the archive. I have a procedure that needs to perform a
> fairly expensive computation which is needed both for checking the
> inputs and for producing output. I'd like to avoid computing it twice,
> and I'd also like to use contracts to check the inputs. Is there any
> reasonable way to do this?


No. The contract system is designed so that the contractual 
computations are in principle independent from the 'mutator'
computations. 


> The code is like this:
> 
> (define (do-stuff x y z)
>  (define important-value (expensive x y z))
>  ... do stuff with important-value ...)
> 
> (provide/contract
>  [do-stuff (->i [x ...] [y ...] [z (x y)
> contract-that-depends-on-important-value]) [result ...])])
> 
> 
> I could memoize the expensive computation, but that would require
> something like an LRU cache to avoid memory problems, and I'd rather
> not have to worry about that.
> 
> Alternatively, I could shift things around so that the contract is on
> the procedure that generates important-value, but then blame reporting
> would be incorrect.
> 
> Any ideas?


Define a syntactic abstraction that spans contracts and 'mutators'. 

-- Matthias


Posted on the users mailing list.