Re: [racket-users] Apropos contracts: simple predicates and switching on and off

2017-05-04 Thread Dupéron Georges
Le vendredi 5 mai 2017 02:30:50 UTC+2, Ben Greenman a écrit :
> With a `define/contract-out` macro?
> 
> But I'd rather not have a macro like this in the contract library.
> I prefer reading code with all the "provide" statements at the top of the 
> file.

Since provide transformers are executed in two passes (the second one being 
after the expansion of the rest of the file, IIRC), I thought about writing 
define/contract-out so that it saves the contract, but does not implicitly 
provide the identifier.

That way, if the identifier is provided, the "saved" contract is attached to it.

Types in Typed Racket work a bit like this: the type is defined or inferred 
alongside the function definition, but is also provided. The implementation 
mechanism is a bit more complex though (it relies on a submodule to store the 
types).

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Apropos contracts: simple predicates and switching on and off

2017-05-04 Thread Ben Greenman
On Thu, May 4, 2017 at 8:23 PM, Dupéron Georges  wrote:

> In these cases, instead of turning off the contract checks altogether, it
> would be nice to have a way to disable the define/contract and turn it into
> a contract-out, using a single parameter (perhaps a per-file syntax
> parameter?).
>
> I'm not sure what would be the best syntax and implementation strategy for
> this, though.
>

With a `define/contract-out` macro?

But I'd rather not have a macro like this in the contract library.
I prefer reading code with all the "provide" statements at the top of the
file.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Apropos contracts: simple predicates and switching on and off

2017-05-04 Thread Dupéron Georges
Le jeudi 4 mai 2017 03:00:10 UTC+2, Daniel Prager a écrit :
> On the subject of turning down contracts for performance and then being 
> bitten, believe me, I hear you: my preference is to have the maximum checking 
> I can afford. But when I'm really writing stringent post-conditions (or 
> invariants) they can get *really* expensive, negatively impacting on user 
> experience, which takes us into the land of pragmatic trade-offs. 
> 
> If I don't have a way to turn them off I'm either not going to write them at 
> all or comment them out, and then regret not using them on smaller test 
> scenarios.

On this topic, I have sometimes written recursive functions with expensive 
contracts (when recursing down a list, a contract on the whole list will have a 
total cost of O(n²)).

Using (provide (contract-out …)) is of course the right solution for the final 
version, but during debugging, the extra checks at each recursion step can help 
pinpoint bugs more easily.

In these cases, instead of turning off the contract checks altogether, it would 
be nice to have a way to disable the define/contract and turn it into a 
contract-out, using a single parameter (perhaps a per-file syntax parameter?).

I'm not sure what would be the best syntax and implementation strategy for 
this, though.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Self evaluating Racket Interpreter

2017-05-04 Thread circularballs
I forgot to mention that for some reason, using the builtin foldl doesn't work 
but if I define it myself separately, then it works. 

  (define (foldl proc init lst)
(cond
  ((null? lst) init)
  (else (foldl proc (proc (car lst) init) (cdr lst)

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: Self evaluating Racket Interpreter

2017-05-04 Thread circularballs
Thanks Matthias. I should have read the error message more carefully. 

For anyone who is interested, it has nothing to do with cons or mcons or 
quotes. There was a mistake in interpreter.rkt.

If you'd like to try finding it yourself, then stop reading.
...
...
...
...
...
...
...
...
...
...
Here's the fix. In interpreter.rkt, under the definition of myeval, in the case 
of a let expression, it should have been:
  [(eq? (car expr) 'let)
   (eval-sequence (cddr expr)
  (extend-env
   (map first (second expr))
   (eval-args (map second (second expr)) env)
   env))]




-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Apropos contracts: simple predicates and switching on and off

2017-05-04 Thread Matthias Felleisen

Please read 

 The Contract Guide at 
 https://docs.racket-lang.org/guide/contracts.html?q=contracts

It has many examples and gradually builds intuition. The ideal PR would be to 
specific chapters there. 




> On May 4, 2017, at 12:38 AM, Daniel Prager  wrote:
> 
> Using Ben's left-pad example as a model I get the following re-write of the 
> real-sqrt contract
> 
>   (([x real?])
>#:pre/name (x)
>"non-negative argument expected"
>(>= x 0)
>. ->i .
>[result (x) real?]
>#:post/name (x result)
>"the sqrt of zero is zero"
>(implies (= x 0) (= result 0))
>#:post/name (x result)
>"the sqrt of a positive number is positive"
>(implies (> x 0) (> result 0))
>#:post/name (x result)
>"result * result = x (to within error)"
>(implies (> x 0) (<= (abs (- x (* result result))) 0.0001)))
> 
> Other than a bit of repetition — multiple uses of #:post/name — this is 
> pretty darn close to what I was after.
> 
> Like most people, when faced with something complex and a bit daunting — in 
> this case the ->i contract combinator — I benefit from concrete examples. 
> 
> If enough people encourage me to "make a pull request" once I'm more familiar 
> I'll propose some more examples myself.
> 
> * * *
> 
> My off-hand proposal would be to permit a variation on #:pre/name, 
> #:post/name and friends to allow multiple clauses. For example:
> 
>  #:post/name (x result)
>["sqrt of zero is zero" (implies (= x 0) (= result 0))]
>["sqrt of a positive number is positive" (implies (> x 0) (> result 0))]
>["result * result = x (to within error)" 
> (implies (> x 0) (<= (abs (- x (* result result))) 0.0001))]
> 
> 
> Next stop ... boundaries.
> 
> 
> Thanks again
> 
> Dan
> 
> 
> On Wed, May 3, 2017 at 1:57 PM, Ben Greenman  
> wrote:
> 
> 1. To practice with dependent contracts, I made a "full" spec for the 
> JavaScript left-pad function.
> https://www.npmjs.com/package/left-pad
> 
> The exercise was fun, I learned a lot, and I think my solution is "as 
> readable as possible" :) .
> https://github.com/bennn/racket-left-pad
> 
> 
> -- 
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] break-thread + thread-wait can't be handled

2017-05-04 Thread Eric Griffis
On Wednesday, May 3, 2017 at 3:04:20 PM UTC-7, Ryan Culpepper wrote:
> On 5/3/17 10:41 PM, Eric Griffis wrote:
> > Hello,
> >
> > I'm having trouble catching "terminate break" exceptions when combining 
> > break-thread with thread-wait.
> >
> > MWE 1:
> >
> >   (with-handlers ([exn:break:terminate? writeln])
> > (let ([t (thread (lambda () (thread-wait (current-thread])
> >   (break-thread t 'terminate)
> >   (thread-wait t)))
> 
> Threads do not inherit exception handlers. You need to move the 
> `with-handlers` to the new thread:
> 
>(let ([t (thread (lambda ()
>   (with-handlers ([exn:break:terminate? writeln])
> (thread-wait (current-thread)])
>  (break-thread t 'terminate)
>  (thread-wait t))
> 
> Except that isn't quite right either, because , the main thread might 
> (very likely) send the break to `t` before `t` is ready to catch it. So 
> we need some additional synchronization:
> 
>(define t-ready (make-semaphore 0))
>(let ([t (thread (lambda ()
>   (with-handlers ([exn:break:terminate? writeln])
> (semaphore-post t-ready)
> (thread-wait (current-thread)])
>  (semaphore-wait t-ready)
>  (break-thread t 'terminate)
>  (thread-wait t))
> 
> That version should reliably print out the break exception.
> 
> Ryan

Got it. I had with-handlers inside the thread at first, but got the same 
result. A simple test showed clearly that the thread was not ready to catch the 
break, hence my attempt to extract with-handlers. The semaphore did the trick, 
though.

The big lesson (for me) here is that exceptions inside a thread do not "bubble 
up" to the parent. This was not obvious (to me) from the docs.

My code is now behaving predictably. Thanks, Ryan.

Eric

-- 
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.
For more options, visit https://groups.google.com/d/optout.