On this page:
sort-kernel-syntax
kernel-syntax-bindings
kernel-syntax-references
7.1
3.3.4 mischief/kernel-syntax: Manipulating Fully Expanded Syntax

 (require mischief/kernel-syntax) package: mischief

procedure

(sort-kernel-syntax stxs    
  [#:cycles-ok? cycles-ok?])  (listof syntax?)
  stxs : (listof syntax?)
  cycles-ok? : boolean? = #false
Sorts the fully expanded definitions and expressions in stxs topologically such that definitions occur before corresponding references. The sort is stable, meaning that where possible the elements are left in the same order as they occur in stxs. When cycles-ok? is #true, mutually recursive definitions are kept in their original order with respect to each other. When cycles-ok? is #false, sort-kernel-syntax reports an error if stxs contains mutually recursive definitions.

Example:
> (sort-kernel-syntax
    (list
      #'(define-values {flatten}
          (#%plain-lambda {x}
            (#%plain-app flatten/append x '())))
      #'(define-values {flatten/append}
          (#%plain-lambda {x tail}
            (if (#%plain-app empty? x)
              tail
              (if (#%plain-app cons? x)
                (#%plain-app flatten/append (#%plain-app first x)
                  (#%plain-app flatten/append (#%plain-app rest x)
                    tail))
                (#%plain-app cons x tail)))))
      #'(#%plain-app flatten
          (#%plain-app list 2 (#%plain-app list 2 3) 4))))

'(#<syntax:eval:1:0 (define-values (flatten/append) (#%plain-lambda (x tail) (if (#%plain-app empty? x) tail (if (#%plain-app cons? x) (#%plain-app flatten/append (#%plain-app first x) (#%plain-app flatten/append (#%plain-app rest x) tail)) (#%plain-app cons x tail)))))> #<syntax:eval:1:0 (define-values (flatten) (#%plain-lambda (x) (#%plain-app flatten/append x (quote ()))))> #<syntax:eval:1:0 (#%plain-app flatten (#%plain-app list 2 (#%plain-app list 2 3) 4))>)

procedure

(kernel-syntax-bindings stx [#:among among])

  (listof identifier?)
  stx : syntax?
  among : (or/c (listof identifier?) #false) = #false
Returns the list of unique names defined by the fully expanded expression or definition stx. If among is a list, the result is restricted to names that are free-identifier=? to elements of among.

Examples:
> (kernel-syntax-bindings
    #'(define-values {flatten}
        (#%plain-lambda {x}
          (#%plain-app flatten/append x '()))))

'(#<syntax:eval:2:0 flatten>)

> (kernel-syntax-bindings
    #'(define-values {flatten/append}
        (#%plain-lambda {x tail}
          (if (#%plain-app empty? x)
            tail
            (if (#%plain-app cons? x)
              (#%plain-app flatten/append (#%plain-app first x)
                (#%plain-app flatten/append (#%plain-app rest x)
                  tail))
              (#%plain-app cons x tail)))))
    #:among (list #'sir-not-appearing-in-this-definition))

'()

> (kernel-syntax-bindings
    #'(#%plain-app flatten
        (#%plain-app list 2 (#%plain-app list 2 3) 4)))

'()

procedure

(kernel-syntax-references stx    
  [#:among among])  (listof identifier?)
  stx : syntax?
  among : (or/c (listof identifier?) #false) = #false
Returns the list of unique names referred to by the fully expanded expression or definition stx, not including built-in special forms or names defined by or locally bound within stx. If among is a list, the result is restricted to names that are free-identifier=? to elements of among.

Examples:
> (kernel-syntax-references
    #'(define-values {flatten}
        (#%plain-lambda {x}
          (#%plain-app flatten/append x '()))))

'(#<syntax:eval:5:0 flatten/append>)

> (kernel-syntax-references
    #'(define-values {flatten/append}
        (#%plain-lambda {x tail}
          (if (#%plain-app empty? x)
            tail
            (if (#%plain-app cons? x)
              (#%plain-app flatten/append (#%plain-app first x)
                (#%plain-app flatten/append (#%plain-app rest x)
                  tail))
              (#%plain-app cons x tail))))))

'(#<syntax:eval:6:0 empty?> #<syntax:eval:6:0 cons?> #<syntax:eval:6:0 first> #<syntax:eval:6:0 rest> #<syntax:eval:6:0 cons>)

> (kernel-syntax-references
    #'(#%plain-app flatten
        (#%plain-app list 2 (#%plain-app list 2 3) 4))
    #:among (list #'flatten #'flatten/append))

'(#<syntax:eval:7:0 flatten>)