Re: [racket-users] Macro to extract select subexpressions into other locations

2015-07-21 Thread Alexander D. Knauth
Here's a macro that does something similar:

#lang at-exp racket

(require (for-syntax syntax/parse
 racket/contract
 ))

(begin-for-syntax
  (define (get-extracted-exprs-box extracted-exprs-id)
(syntax-local-value extracted-exprs-id))
  (define (get-extracted-exprs extracted-exprs-id)
(unbox (get-extracted-exprs-box extracted-exprs-id)))
  (define (box-update! b f)
(set-box! b (f (unbox b
  (define (add-extracted-expr! extracted-exprs-id new-expr)
(box-update! (get-extracted-exprs-box extracted-exprs-id)
 (λ (lst) (append lst (list (syntax-local-introduce 
new-expr)))

(define-syntax extract-expression
  (syntax-parser
[(extract-expression expr:expr extracted-exprs-id:id)
 (add-extracted-expr! #'extracted-exprs-id #'expr)
 #'expr]))

(define-syntax with-extract-expressions
  (syntax-parser
[(with-extract-expressions stx-expr:expr ...)
 #:with [extracted-exprs macro-id:id ...]
 (generate-temporaries #'[extracted-expressions stx-expr ...])
 #'(begin
 (define-syntax extracted-exprs (box '()))
 (begin
   (define-syntax macro-id
 (lambda (stx)
   (stx-expr #'extracted-exprs)))
   (macro-id))
 ...)]))

(define (square x) (* x x))

(with-extract-expressions
 (lambda (exprs)
   (with-syntax ([exprs exprs])
 #'(module+ test
 (require rackunit)
 (check-equal? (extract-expression (square 5) exprs) 25)
 (check-equal? (extract-expression (square -5) exprs) 25
 (lambda (exprs)
   (with-syntax ([(expr ...) (get-extracted-exprs exprs)])
 #'(module+ doc
 (require scribble/manual
  scribble/eval)
 (define evaluator (make-base-eval))
 @defproc[(square [x real?]) real?]{
   Returns the square of @racket[x], the result of
   multiplying @racket[x] by itself.
   @examples[#:eval evaluator expr ...]}


On Jul 20, 2015, at 8:03 PM, Jack Firth jackhfi...@gmail.com wrote:

 I'm trying to create a way to automatically turn test cases into examples. 
 I'd like a macro that turns this:
 
 (extract-expressions
 (module+ test
   (check-equal? (extract-expression (square 5)) 25)
   (check-equal? (extract-expression (square -5)) 25))
 (module+ doc
   @defproc[(square [x real?]) real?]{
 Returns the square of @racket[x], the result of
 multiplying @racket[x] by itself.
 @examples[#:eval evaluator (include-extracted-expressions)]}))
 
 into this:
 
 (begin
  (module+ test
(check-equal? (square 5) 25))
  (module+ doc
@defproc[(square [x real?]) real?]{
  Returns the square of @racket[x], the result of
  multiplying @racket[x] by itself.
  @examples[#:eval evaluator (square 5) (square -5)]}))
 
 That's the general idea anyway. I want to be able to gather arbitrary 
 expressions inside a region and copy them into some other marked place 
 inside the expressions. How exactly do I go about doing this, and how should 
 I do it properly?
 
 -- 
 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] Macro to extract select subexpressions into other locations

2015-07-21 Thread Alexander D. Knauth
Oh. I checked that that the expressions were accumulating in the definition of 
extract-expression, but I didn't check that it expanded in the right order to 
do what you want.

So this doesn't actually work. 

On Jul 21, 2015, at 3:08 PM, Alexander D. Knauth alexan...@knauth.org wrote:

 Here's a macro that does something similar:
 
 #lang at-exp racket
 
 (require (for-syntax syntax/parse
 racket/contract
 ))
 
 (begin-for-syntax
  (define (get-extracted-exprs-box extracted-exprs-id)
(syntax-local-value extracted-exprs-id))
  (define (get-extracted-exprs extracted-exprs-id)
(unbox (get-extracted-exprs-box extracted-exprs-id)))
  (define (box-update! b f)
(set-box! b (f (unbox b
  (define (add-extracted-expr! extracted-exprs-id new-expr)
(box-update! (get-extracted-exprs-box extracted-exprs-id)
 (λ (lst) (append lst (list (syntax-local-introduce 
 new-expr)))
 
 (define-syntax extract-expression
  (syntax-parser
[(extract-expression expr:expr extracted-exprs-id:id)
 (add-extracted-expr! #'extracted-exprs-id #'expr)
 #'expr]))
 
 (define-syntax with-extract-expressions
  (syntax-parser
[(with-extract-expressions stx-expr:expr ...)
 #:with [extracted-exprs macro-id:id ...]
 (generate-temporaries #'[extracted-expressions stx-expr ...])
 #'(begin
 (define-syntax extracted-exprs (box '()))
 (begin
   (define-syntax macro-id
 (lambda (stx)
   (stx-expr #'extracted-exprs)))
   (macro-id))
 ...)]))
 
 (define (square x) (* x x))
 
 (with-extract-expressions
 (lambda (exprs)
   (with-syntax ([exprs exprs])
 #'(module+ test
 (require rackunit)
 (check-equal? (extract-expression (square 5) exprs) 25)
 (check-equal? (extract-expression (square -5) exprs) 25
 (lambda (exprs)
   (with-syntax ([(expr ...) (get-extracted-exprs exprs)])
 #'(module+ doc
 (require scribble/manual
  scribble/eval)
 (define evaluator (make-base-eval))
 @defproc[(square [x real?]) real?]{
   Returns the square of @racket[x], the result of
   multiplying @racket[x] by itself.
   @examples[#:eval evaluator expr ...]}
 
 
 On Jul 20, 2015, at 8:03 PM, Jack Firth jackhfi...@gmail.com wrote:
 
 I'm trying to create a way to automatically turn test cases into examples. 
 I'd like a macro that turns this:
 
 (extract-expressions
 (module+ test
  (check-equal? (extract-expression (square 5)) 25)
  (check-equal? (extract-expression (square -5)) 25))
 (module+ doc
  @defproc[(square [x real?]) real?]{
Returns the square of @racket[x], the result of
multiplying @racket[x] by itself.
@examples[#:eval evaluator (include-extracted-expressions)]}))
 
 into this:
 
 (begin
 (module+ test
   (check-equal? (square 5) 25))
 (module+ doc
   @defproc[(square [x real?]) real?]{
 Returns the square of @racket[x], the result of
 multiplying @racket[x] by itself.
 @examples[#:eval evaluator (square 5) (square -5)]}))
 
 That's the general idea anyway. I want to be able to gather arbitrary 
 expressions inside a region and copy them into some other marked place 
 inside the expressions. How exactly do I go about doing this, and how should 
 I do it properly?
 
 -- 
 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.

-- 
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] Macro to extract select subexpressions into other locations

2015-07-21 Thread Matthias Felleisen


Why not step back and design a notation where testing and documenting can share 
concepts instead of retro-actively extracting expressions from one place to put 
somewhere else. -- Matthias







On Jul 21, 2015, at 3:23 PM, Alexander D. Knauth alexan...@knauth.org wrote:

 Oh. I checked that that the expressions were accumulating in the definition 
 of extract-expression, but I didn't check that it expanded in the right order 
 to do what you want.
 
 So this doesn't actually work. 
 
 On Jul 21, 2015, at 3:08 PM, Alexander D. Knauth alexan...@knauth.org wrote:
 
 Here's a macro that does something similar:
 
 #lang at-exp racket
 
 (require (for-syntax syntax/parse
racket/contract
))
 
 (begin-for-syntax
 (define (get-extracted-exprs-box extracted-exprs-id)
   (syntax-local-value extracted-exprs-id))
 (define (get-extracted-exprs extracted-exprs-id)
   (unbox (get-extracted-exprs-box extracted-exprs-id)))
 (define (box-update! b f)
   (set-box! b (f (unbox b
 (define (add-extracted-expr! extracted-exprs-id new-expr)
   (box-update! (get-extracted-exprs-box extracted-exprs-id)
(λ (lst) (append lst (list (syntax-local-introduce 
 new-expr)))
 
 (define-syntax extract-expression
 (syntax-parser
   [(extract-expression expr:expr extracted-exprs-id:id)
(add-extracted-expr! #'extracted-exprs-id #'expr)
#'expr]))
 
 (define-syntax with-extract-expressions
 (syntax-parser
   [(with-extract-expressions stx-expr:expr ...)
#:with [extracted-exprs macro-id:id ...]
(generate-temporaries #'[extracted-expressions stx-expr ...])
#'(begin
(define-syntax extracted-exprs (box '()))
(begin
  (define-syntax macro-id
(lambda (stx)
  (stx-expr #'extracted-exprs)))
  (macro-id))
...)]))
 
 (define (square x) (* x x))
 
 (with-extract-expressions
 (lambda (exprs)
  (with-syntax ([exprs exprs])
#'(module+ test
(require rackunit)
(check-equal? (extract-expression (square 5) exprs) 25)
(check-equal? (extract-expression (square -5) exprs) 25
 (lambda (exprs)
  (with-syntax ([(expr ...) (get-extracted-exprs exprs)])
#'(module+ doc
(require scribble/manual
 scribble/eval)
(define evaluator (make-base-eval))
@defproc[(square [x real?]) real?]{
  Returns the square of @racket[x], the result of
  multiplying @racket[x] by itself.
  @examples[#:eval evaluator expr ...]}
 
 
 On Jul 20, 2015, at 8:03 PM, Jack Firth jackhfi...@gmail.com wrote:
 
 I'm trying to create a way to automatically turn test cases into examples. 
 I'd like a macro that turns this:
 
 (extract-expressions
 (module+ test
 (check-equal? (extract-expression (square 5)) 25)
 (check-equal? (extract-expression (square -5)) 25))
 (module+ doc
 @defproc[(square [x real?]) real?]{
   Returns the square of @racket[x], the result of
   multiplying @racket[x] by itself.
   @examples[#:eval evaluator (include-extracted-expressions)]}))
 
 into this:
 
 (begin
 (module+ test
  (check-equal? (square 5) 25))
 (module+ doc
  @defproc[(square [x real?]) real?]{
Returns the square of @racket[x], the result of
multiplying @racket[x] by itself.
@examples[#:eval evaluator (square 5) (square -5)]}))
 
 That's the general idea anyway. I want to be able to gather arbitrary 
 expressions inside a region and copy them into some other marked place 
 inside the expressions. How exactly do I go about doing this, and how 
 should I do it properly?
 
 -- 
 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.
 
 -- 
 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.


[racket-users] Macro to extract select subexpressions into other locations

2015-07-20 Thread Jack Firth
I'm trying to create a way to automatically turn test cases into examples. I'd 
like a macro that turns this:

(extract-expressions
 (module+ test
   (check-equal? (extract-expression (square 5)) 25)
   (check-equal? (extract-expression (square -5)) 25))
 (module+ doc
   @defproc[(square [x real?]) real?]{
 Returns the square of @racket[x], the result of
 multiplying @racket[x] by itself.
 @examples[#:eval evaluator (include-extracted-expressions)]}))

into this:

(begin
  (module+ test
(check-equal? (square 5) 25))
  (module+ doc
@defproc[(square [x real?]) real?]{
  Returns the square of @racket[x], the result of
  multiplying @racket[x] by itself.
  @examples[#:eval evaluator (square 5) (square -5)]}))

That's the general idea anyway. I want to be able to gather arbitrary 
expressions inside a region and copy them into some other marked place inside 
the expressions. How exactly do I go about doing this, and how should I do it 
properly?

-- 
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.