;;;; "mbe.scm" "Macro by Example" (Eugene Kohlbecker, R4RS)
;;; From: Dorai Sitaram, dorai@cs.rice.edu, 1991, 1999
;
;Permission to copy this software, to redistribute it, and to use it
;for any purpose is granted, subject to the following restrictions and
;understandings.
;
;1.  Any copy made of this software must include this copyright notice
;in full.
;
;2.  I have made no warrantee or representation that the operation of
;this software will be error-free, and I am under no obligation to
;provide any services, by way of maintenance, update, or otherwise.
;
;3.  In conjunction with products arising from the use of this
;material, there shall be no use of my name in any advertising,
;promotional, or sales literature without prior written consent in
;each case.

;;; revised Dec. 6, 1993 to R4RS syntax (if not semantics).
;;; revised Mar. 2 1994 for SLIB (jaffer@ai.mit.edu).
;;; corrections, Apr. 24, 1997.
;;; corr., Jan. 30, 1999. (mflatt@cs.rice.edu, dorai@cs.rice.edu)
;;; revised Aug. 30 1999 for STk (eg@unice.fr)
;;; revised Jan. 25 2001 for STklos (eg@unice.fr)
;;; updated Jun. 5, 2002 (eg@essi.fr)
;;;     introduced special symbols which must be rewritten in an output
;;;     pattern (see below)
;;;
;;; A vanilla implementation of hygienic macro-by-example as described
;;; by Eugene Kohlbecker and in R4RS Appendix.
;;;
;;;

#|
                        STklos Implementation Notes

This macro implementation has been slightly modified for STklos.
In particular,
  - all the function reside in the module MBE, the only binding which is
    visible from outside is DEFINE-SYNTAX (and the fake LET-SYNTAX and
    LETREC-SYNTAX).
  - function some used by the original code and function split are added
    in the MBE module
  - The macro define-syntax itself is expanded to a function call which
    parses all the clauses rather than to a cond which tests all the
    clauses. This conducts to code which is smaller than the original
    solution (particularly for syntaxes with a lot of clauses).
  - Symbols which starts with %% never appear in the macro expansion
    (i.e. they are replaced by a gensymed symbol). The example below
    illustrates the problem. Suppose we have defined

        (define-syntax swap!
           (syntax-rules ()
             ((swap! x y)
              (my-let ((tmp x)) (set! x y) (set! y tmp)))))

    the expansion of (swap! a b) would be:

        (my-let ((tmp a)) (set! a b) (set! b tmp))

    which is not hygienic. In this case, two symbols were introduced MY-LET
    and TMP (with a "let" expansion would be correct, since LET is treated
    specially by MBE. Here MY-LET must be kept as is whereas TMP must be
    replaced by a gensymed symbol. Changing TMP by %%TMP in the previous
    definition yields the following expansion:

        (my-let ((|G156| a)) (set! a b) (set! b |G156|))

    which is correct.
  - Tail patterns are handled as in SRFI-46. For example:

        (define-syntax last-two
           (syntax-rules ()
             ((last-two skip ... x y) '(x y))))

        (last-two 1 2 3 4)  ===>  (3 4)
     Tail patterns support was done by Vitaly Magerya


<doc SYNTAX define-syntax
 * (define-syntax <identifier> <transformer-spec>)
 *
 * |<Define-syntax>|  extends the top-level syntactic environment by binding
 * the |<identifier>| to the specified transformer.
 *
 * NOTE: |<transformer-spec>| should be an instance of |syntax-rules|.
 * @lisp
 * (define-syntax let*
 *   (syntax-rules ()
 *     ((let* () body1 body2 ...)
 *      (let () body1 body2 ...))
 *     ((let* ((name1 val1) (name2 val2) ...)
 *        body1 body2 ...)
 *      (let ((name1 val1))
 *        (let* (( name2 val2) ...)
 *          body1 body2 ...))))
 * @end lisp
doc>
<doc SYNTAX syntax-rules
 * (syntax-rules <literals> <syntax-rule> ...)
 *
 * |<literals>| is a list of identifiers, and each |<syntax-rule>| should be of
 * the form
 * @lisp
 * (pattern template)
 * @end lisp
 *
 * An instance of |<syntax-rules>| produces a new macro transformer by
 * specifying a sequence of hygienic rewrite rules. A use of a macro
 * whose name is associated with a transformer specified by
 * <syntax-rules> is matched against the patterns contained in the
 * <syntax-rules>, beginning with the leftmost syntax-rule. When a match is
 * found, the macro use is transcribed hygienically according to the
 * template.
 * @l
 * Each pattern begins with the name for the macro. This name is not
 * involved in the matching and is not considered a pattern variable or
 * literal identifier.
 * @l
 * NOTE: For a complete description of the Scheme pattern language,
 * refer to R5RS.
doc>
|#

(define-module MBE
  (import SCHEME)
  (export let-syntax letrec-syntax)

  (define-macro (letrec-syntax . args)
    (error 'letrec-syntax
      "cannot be used here. You must load the file \"full-syntax\" to access it:"
           (cons 'letrec-syntax args)))


;;;
;;; Scheme utilities
;;;

(define some any)               ;; because of SRFI-1

;;; Splits lst into prefix of length n and the rest,
;;; returs a cons of those two.
(define (split lst n)
  (let spl ((before '()) (after lst) (i n))
    (cond ((<= i 0) (cons (reverse before) after))
          ((null? after) (error 'split "list is too short"))
          (else (spl (cons (car after) before) (cdr after) (- i 1))))))

;;; The following definition are here to avoid compiler warnings about
;;; undefined symbol reference. They will be deleted with the new compiler
(define hyg:untag-no-tags               #f)
(define hyg:untag-vanilla               #f)
(define hyg:untag-lambda                #f)
(define hyg:untag-letrec                #f)
(define hyg:untag-named-let             #f)
(define hyg:untag-let                   #f)
(define hyg:untag-let*                  #f)
(define hyg:untag-do                    #f)
(define hyg:untag-list                  #f)
(define hyg:untag-list*                 #f)
(define hyg:untag-quasiquote            #f)
(define hyg:flatten                     #f)

(define mbe:ellipsis?                   #f)
(define mbe:split-at-ellipsis           #f)
(define mbe:get-ellipsis-nestings       #f)
(define mbe:ellipsis-sub-envs           #f)
(define mbe:contained-in?               #f)

;=============================================================================

;; reverse assq:
;;
;; (hyg:rassq 'a '((10 . b) (50 . a) (1111 . c)))
;; (50 . a)
(define hyg:rassq
  (lambda (k al)
    (let loop ((al al))
      (if (null? al) #f
        (let ((c (car al)))
          (if (eq? (cdr c) k) c
            (loop (cdr al))))))))

;; hyg:tag uses gensym to rename variables (actually, tag the ones which will be renamed).
;;
;; e is the form
;; kk ar ethe keywords
;; al is an assoc list
;;
;; returns a list with
;; CAR: the original form, rewritten so only the keywords stay with the same names,
;;      the rest is substituted.
;; CDR: is an assoc list that maps the generated symbols (G...) with their original
;;      names.
;;
;; the list al is appended to the output CDR, so the assoc list may be build gradually.
;;
;; (hyg:tag '(f a b c) '(a c)          '()) => ((G984 a G985 c) (G985 . b) (G984 . f))
;; (hyg:tag 1          '(fun x fun oh) '()) => (1)
;; (hyg:tag '(f a b c) '(a c)          '((G-something . y))) => ((G1333 a G1334 c) (G1334 . b) (G1333 . f) (G-something . y))
(define hyg:tag
  (lambda (e kk al ellipsis)
    (cond ((pair? e)
            (let* ((a-te-al (hyg:tag (car e) kk al ellipsis))
                    (d-te-al (hyg:tag (cdr e) kk (cdr a-te-al) ellipsis)))
              (cons (cons (car a-te-al) (car d-te-al))
                (cdr d-te-al))))
      ((vector? e)
        (list->vector
          (hyg:tag (vector->list e) kk al ellipsis)))
      ((symbol? e)
        (cond ((eq? e ellipsis) (cons ellipsis al))
          ((memq e kk) (cons e al))
          ((hyg:rassq e al) =>
            (lambda (c)
              (cons (car c) al)))
          (else
            (let ((te (gensym e)))
              (cons te (cons (cons te e) al))))))
      (else (cons e al)))))

;;untagging
;;
;; e is the TAGGED expression, as produced by hyg:tag
;; al is the assoc list of tags -- se hyg:tag
;; tmps is a list of gensymed symbols NOT to be substituted back.
;;
;; (hyg:untag '(G1333 a G1334 c) '((G1334 . b) (G1333 . f) (G-something . y)) '()) => (f a b c)
;; (hyg:untag '(G1333 a G1334 c) '((G1334 . b) (G1333 . f) (G-something . y)) '(G1334)) => (f a G1334 c)
(define hyg:untag
  (lambda (e al tmps)
    (if (pair? e)
      (let ((a (hyg:untag (car e) al tmps)))
        (if (list? e)
          (case a
            ((quote) (hyg:untag-no-tags e al))
            ((quasiquote) (list a (hyg:untag-quasiquote (cadr e) al tmps)))
            ((if begin)
              `(,a ,@(map (lambda (e1)
                            (hyg:untag e1 al tmps)) (cdr e))))
            ((set! define)
              `(,a ,(hyg:untag-vanilla (cadr e) al tmps)
                 ,@(map (lambda (e1)
                          (hyg:untag e1 al tmps)) (cddr e))))
            ((lambda) (hyg:untag-lambda (cadr e) (cddr e) al tmps))
            ((letrec) (hyg:untag-letrec (cadr e) (cddr e) al tmps))
            ((let)
              (let ((e2 (cadr e)))
                (if (symbol? e2)
                  (hyg:untag-named-let e2 (caddr e) (cdddr e) al tmps)
                  (hyg:untag-let e2 (cddr e) al tmps))))
            ((let*) (hyg:untag-let* (cadr e) (cddr e) al tmps))
            ((do) (hyg:untag-do (cadr e) (caddr e) (cdddr e) al tmps))
            ((case)
              `(case ,(hyg:untag-vanilla (cadr e) al tmps)
                 ,@(map
                     (lambda (c)
                       `(,(hyg:untag-vanilla (car c) al tmps)
                          ,@(hyg:untag-list (cdr c) al tmps)))
                     (cddr e))))
            ((cond)
              `(cond ,@(map
                         (lambda (c)
                           (hyg:untag-list c al tmps))
                         (cdr e))))
            (else (cons a (hyg:untag-list (cdr e) al tmps))))
          (cons a (hyg:untag-list* (cdr e) al tmps))))
      (hyg:untag-vanilla e al tmps))))

(define hyg:untag-list
  (lambda (ee al tmps)
    (map (lambda (e)
           (hyg:untag e al tmps)) ee)))

(define hyg:untag-list*
  (lambda (ee al tmps)
    (let loop ((ee ee))
      (if (pair? ee)
        (cons (hyg:untag (car ee) al tmps)
          (loop (cdr ee)))
        (hyg:untag ee al tmps)))))

(define hyg:untag-no-tags
  (lambda (e al)
    (cond ((pair? e)
            (cons (hyg:untag-no-tags (car e) al)
              (hyg:untag-no-tags (cdr e) al)))
      ((vector? e)
        (list->vector
          (hyg:untag-no-tags (vector->list e) al)))
      ((not (symbol? e)) e)
      ((assq e al) => cdr)
      (else e))))

(define hyg:untag-quasiquote
  (lambda (form al tmps)
    (let qq ((x form) (level 0))
      (cond
       ((pair? x)
        (let ((first (qq (car x) level)))
          (cond
           ((and (eq? first 'unquote) (list? x))
            (let ((rest (cdr x)))
              (if (or (not (pair? rest))
                      (not (null? (cdr rest))))
                  (error 'unquote "takes exactly one expression")
                  (if (zero? level)
                      (list 'unquote (hyg:untag (car rest) al tmps))
                      (cons first (qq rest (- level 1)))))))
           ((and (eq? first 'quasiquote) (list? x))
            (cons 'quasiquote (qq (cdr x) (+ level 1))))
           ((and (eq? first 'unquote-splicing) (list? x))
            (error 'unquote-splicing "invalid context within quasiquote"))
           ((pair? first)
            (let ((car-first (qq (car first) level)))
              (if (and (eq? car-first 'unquote-splicing)
                       (list? first))
                  (let ((rest (cdr first)))
                    (if (or (not (pair? rest))
                            (not (null? (cdr rest))))
                        (error 'unquote-splicing "takes exactly one expression")
                        (list (list 'unquote-splicing
                                    (if (zero? level)
                                        (hyg:untag (cadr rest) al tmps)
                                        (qq (cadr rest) (- level 1)))
                                    (qq (cdr x) level)))))
                  (cons (cons car-first
                              (qq (cdr first) level))
                        (qq (cdr x) level)))))
           (else
            (cons first (qq (cdr x) level))))))
       ((vector? x)
        (list->vector
         (qq (vector->list x) level)))
       (else (hyg:untag-no-tags x al))))))

(define hyg:untag-lambda
  (lambda (bvv body al tmps)
    (let ((tmps2 (append! (hyg:flatten bvv) tmps)))
      `(lambda ,bvv
         ,@(hyg:untag-list body al tmps2)))))

(define hyg:untag-letrec
  (lambda (varvals body al tmps)
    (let ((tmps (append! (map car varvals) tmps)))
      `(letrec
         ,(map
            (lambda (varval)
              `(,(car varval)
                 ,(hyg:untag (cadr varval) al tmps)))
            varvals)
         ,@(hyg:untag-list body al tmps)))))

(define hyg:untag-let
  (lambda (varvals body al tmps)
    (let ((tmps2 (append! (map car varvals) tmps)))
      `(let
         ,(map
             (lambda (varval)
               `(,(car varval)
                  ,(hyg:untag (cadr varval) al tmps)))
             varvals)
         ,@(hyg:untag-list body al tmps2)))))

(define hyg:untag-named-let
  (lambda (lname varvals body al tmps)
    (let ((tmps2 (cons lname (append! (map car varvals) tmps))))
      `(let ,lname
         ,(map
             (lambda (varval)
               `(,(car varval)
                  ,(hyg:untag (cadr varval) al tmps)))
             varvals)
         ,@(hyg:untag-list body al tmps2)))))

(define hyg:untag-let*
  (lambda (varvals body al tmps)
    (let ((tmps2 (append! (reverse! (map car varvals)) tmps)))
      `(let*
         ,(let loop ((varvals varvals)
                      (i (length varvals)))
            (if (null? varvals) '()
              (let ((varval (car varvals)))
                (cons `(,(car varval)
                         ,(hyg:untag (cadr varval)
                            al (list-tail tmps2 i)))
                  (loop (cdr varvals) (- i 1))))))
         ,@(hyg:untag-list body al tmps2)))))

(define hyg:untag-do
  (lambda (varinistps exit-test body al tmps)
    (let ((tmps2 (append! (map car varinistps) tmps)))
      `(do
         ,(map
            (lambda (varinistp)
              (let ((var (car varinistp)))
                `(,var ,@(hyg:untag-list (cdr varinistp) al
                           (cons var tmps)))))
            varinistps)
         ,(hyg:untag-list exit-test al tmps2)
         ,@(hyg:untag-list body al tmps2)))))

;; (hyg:untag-vanilla 'a '((a . b)) '(a c)) => a
;; (hyg:untag-vanilla 'a '((a . b)) '(c))   => b
(define hyg:untag-vanilla
  (lambda (e al tmps)
    (cond ((pair? e)
            (cons (hyg:untag-vanilla (car e) al tmps)
              (hyg:untag-vanilla (cdr e) al tmps)))
      ((vector? e)
        (list->vector
          (hyg:untag-vanilla (vector->list e) al tmps)))
      ((not (symbol? e)) e)
      ((memq e tmps) e)
      ((assq e al) => (lambda (v)
                        ;; This is a kludge which basically permits to
                        ;; say that a symbol which starts with "%%" must
                        ;; always be gensymed. This is used when the
                        ;; output pattern introduces a new symbol since
                        ;; original MBE code does not deal well with
                        ;; this case [eg]
                        (let ((str (symbol->string (cdr v))))
                          (if (and (> (string-length str) 2)
                                   (string=? (substring str 0 2) "%%"))
                              (car v)
                              (cdr v)))))
      (else e))))

;; a flatten procedure.
(define hyg:flatten
  (lambda (e)
    (let loop ((e e) (r '()))
      (cond ((pair? e) (loop (car e)
                             (loop (cdr e) r)))
            ((null? e) r)
            (else (cons e r))))))

;;;; End of hygiene filter.

;;; finds the leftmost index of list l where something equal to x
;;; occurs
(define mbe:position
  (lambda (x l)
    (let loop ((l l) (i 0))
      (cond ((not (pair? l)) #f)
            ((equal? (car l) x) i)
            (else (loop (cdr l) (+ i 1)))))))

;;; (mbe:append-map f l) == (apply append (map f l))

(define mbe:append-map
  (lambda (f l)
    (let loop ((l l))
      (if (null? l) '()
          (append (f (car l)) (loop (cdr l)))))))


;; split-improper-tail helps deal with improper lists when matching,
;; and is used by both mbe:matches-pattern? and mbe:get-bindings.
;; It will return two values:
;;
;; 1. The last CDR (either NIL if it's a proper list, or the improper tail)
;; 2. The original list WITHOUT the impreper tail.
;;
;; Examples:
;;
;; (split-improper-tail '())        => values '(), '()
;; (split-improper-tail '(1 2 3))   => values '(), '(1 2 3)
;; (split-improper-tail '(1 2 . 3)) => values '(3), '(1 2)
(define (split-improper-tail L)
  (let Loop ((new '())
             (old L))
    (if (pair? old)
        (Loop (cons (car old) new)
              (cdr old))
        (values old (reverse new)))))

;;; tests if expression e matches pattern p where k is the list of
;;; keywords, and 'ellipsis' is the symbol used for the ellipsis.
;;
;; Examples:
;;
;; (mbe:matches-pattern? '(f a b) '(* 3 4) '() '...)      => #t
;; (mbe:matches-pattern? '(f a x b) '(* 2 x 4) '(x) '...) => #t
;; (mbe:matches-pattern? '(f a x b) '(* 2 3 4) '(x) '...) => #f
(define mbe:matches-pattern?
  (lambda (p e k ellipsis)
      (cond ((mbe:ellipsis? p ellipsis)
             ;; p+, e+ will be the last CDRs of p, e (the improper tail of '())
             ;; then, p and e will be rebound to a similar list, WITHOUT that tail,
             ;; if it existed (see split-improper-tail
             (let-values (((p+ p) (split-improper-tail p))
                          ((e+ e) (split-improper-tail e)))
               (and (or (null? e) (pair? e))
                    (let* ((p-head (car p))
                           (p-tail (cddr p))
                           (e-head=e-tail (mbe:split-at-ellipsis e p-tail)))
                      (and e-head=e-tail
		                   (not (memq ellipsis p-tail))             ; fail on multiple ellipses
                           (mbe:matches-pattern?  p+ e+ k ellipsis) ; if improper, match tails
                           (let ((e-head (car e-head=e-tail))
                                 (e-tail (cdr e-head=e-tail)))
                             (and (every
			                       (lambda (x) (mbe:matches-pattern? p-head x k ellipsis))
                                   e-head)
			                      (mbe:matches-pattern? p-tail e-tail k ellipsis))))))))
            ((pair? p)
             (and (pair? e)
		          (mbe:matches-pattern? (car p) (car e) k ellipsis)
		          (mbe:matches-pattern? (cdr p) (cdr e) k ellipsis)))
            ((symbol? p) (if (memq p k) (eq? p e) #t))
            (else (equal? p e)))))

;;; gets the bindings of pattern variables of pattern p for
;;; expression e where k is the list of keywords, and 'ellipsis'
;;; is the symbol used for the ellipsis.
;;
;; Examples:
;;
;; (mbe:get-bindings '(f a b) '(* 3 4) '() '...)
;;   => ((f . *) (a . 3) (b . 4))
;;
;; (mbe:get-bindings'(f a x b) '(* 2 x 4) '(x) '...)
;;   => ((f . *) (a . 2) (b . 4))
;;
;; (mbe:get-bindings '(f a x b) '(* 2 3 4) '(x) '...)
;;   => ((f . *) (a . 2) (b . 4))
;;
;; (mbe:get-bindings '(f a . b) '(func 1 2 3) '() '...)
;;   =>((f . func) (a . 1) (b 2 3))
;;
;; (mbe:get-bindings '(f a ...) '(func 1 2) '() '...) ; see how lists are dealt with!
;;   => ((f . func) ((a) ((a . 1)) ((a . 2))))
;;
;; (mbe:get-bindings '(f a ...) '(func 1 2) '() ':::) ; alternative ellipsis
;;   => ((f . func) (a . 1) (... . 2))
;;
;; (mbe:get-bindings '(f a :::) '(func 1 2) '() ':::)  ; alternative ellipsis
;;   => ((f . func) ((a) ((a . 1)) ((a . 2))))
;;
;; (mbe:get-bindings '(f a b ... c) '(func 1 2 3 4 5 6) '() '...)
;;   => ((f . func) (a . 1) ((b) ((b . 2)) ((b . 3)) ((b . 4)) ((b . 5))) (c . 6))
(define mbe:get-bindings
  (lambda (p e k ellipsis)
    (cond ((mbe:ellipsis? p ellipsis)
           ;; p+, e+ will be the last CDRs of p, e (the improper tail of '())
           ;; then, p and e will be rebound to a similar list, WITHOUT that tail,
           ;; if it existed (see split-improper-tail
           (let-values (((p+ p) (split-improper-tail p))
                        ((e+ e) (split-improper-tail e)))
             (let* ((p-head (car p))
                    (p-tail (cddr p))
                    (e-head=e-tail (mbe:split-at-ellipsis e p-tail))
                    (e-head (car e-head=e-tail))
                    (e-tail (cdr e-head=e-tail)))
	           (let ((res (cons (cons (mbe:get-ellipsis-nestings p-head k ellipsis)
		                              (map (lambda (x) (mbe:get-bindings p-head x k ellipsis))
                                           e-head))
	                            (mbe:get-bindings p-tail e-tail k ellipsis))))
                 (if (null? p+)
                     res
                     (append (mbe:get-bindings p+ e+ k ellipsis) res))))))
          ((pair? p)
	       (append (mbe:get-bindings (car p) (car e) k ellipsis)
	               (mbe:get-bindings (cdr p) (cdr e) k ellipsis)))
          ((symbol? p)
           (if (memq p k) '() (list (cons p e))))
          (else '()))))

;;; expands pattern p using environment r;
;;; k is the list of keywords
(define mbe:expand-pattern
  (lambda (p r k ellipsis)
    (cond ((mbe:ellipsis? p ellipsis)
           (append (let* ((p-head (car p))
			              (nestings (mbe:get-ellipsis-nestings p-head k ellipsis))
                          (rr (mbe:ellipsis-sub-envs nestings r)))
                     (map (lambda (r1)
			                (mbe:expand-pattern p-head (append r1 r) k ellipsis))
                          rr))
	               (mbe:expand-pattern (cddr p) r k ellipsis)))
          ((pair? p)
	       (cons (mbe:expand-pattern (car p) r k ellipsis)
	             (mbe:expand-pattern (cdr p) r k ellipsis)))
          ((symbol? p)
           (if (memq p k) p
             (let ((x (assq p r)))
               (if x (cdr x) p))))
          (else p))))

;;; returns a list that nests a pattern variable as deeply as it
;;; is ellipsed
(define mbe:get-ellipsis-nestings
  (lambda (p k ellipsis)
    (let sub ((p p))
      (cond ((mbe:ellipsis? p ellipsis) (cons (sub (car p)) (sub (cddr p))))
            ((pair? p) (append (sub (car p)) (sub (cdr p))))
            ((symbol? p) (if (memq p k) '() (list p)))
            (else '())))))

;;; finds the subenvironments in r corresponding to the ellipsed
;;; variables in nestings
(define mbe:ellipsis-sub-envs
  (lambda (nestings r)
    (let ((sub-envs-list
           (let loop ((r r) (sub-envs-list '()))
             (if (null? r) (reverse! sub-envs-list)
                 (let ((c (car r)))
                   (loop (cdr r)
                   (if (mbe:contained-in? nestings (car c))
                       (cons (cdr c) sub-envs-list)
                       sub-envs-list)))))))
        (case (length sub-envs-list)
          ((0) #f)
          ((1) (car sub-envs-list))
          (else
           (let loop ((sub-envs-list sub-envs-list) (final-sub-envs '()))
             (if (some null? sub-envs-list) (reverse! final-sub-envs)
                 (loop (map cdr sub-envs-list)
                       (cons (mbe:append-map car sub-envs-list)
                             final-sub-envs)))))))))

;;; checks if nestings v and y have an intersection
(define mbe:contained-in?
  (lambda (v y)
    (if (or (symbol? v) (symbol? y)) (eq? v y)
        (some (lambda (v_i)
                        (some (lambda (y_j)
                                        (mbe:contained-in? v_i y_j))
                                      y))
                      v))))

;;; split expression e so that its second half matches with
;;; pattern p-tail
(define mbe:split-at-ellipsis
  (lambda (e p-tail)
    (let ((i (- (length e) (length p-tail))))
      (and (>= i 0) (split e i)))))

;;; tests if x is an ellipsing pattern, i.e., of the form
;;; (blah ... . blah2)
(define mbe:ellipsis?
  (lambda (x ellipsis)
    (and (pair? x) (pair? (cdr x)) (eq? (cadr x) ellipsis))))


#|

                        Original DEFINE-SYNTAX


(define-macro (define-syntax macro-name syn-rules)
  (if (or (not (pair? syn-rules))
          (not (eq? (car syn-rules) 'syntax-rules)))
    (error 'define-syntax "in ~S, bad syntax-rules ~S" macro-name syn-rules)

    (let ((keywords (cons macro-name (cadr syn-rules)))
          (clauses (cddr syn-rules)))
      `(define-macro (,macro-name . macro-arg)
         (let ((macro-arg (cons ',macro-name macro-arg))
               (keywords ',keywords))
           (cond ,@(map
                     (lambda (clause)
                       (let ((in-pattern (car clause))
                              (out-pattern (cadr clause)))
                         `((mbe:matches-pattern? ',in-pattern macro-arg
                             keywords)
                            (let ((tagged-out-pattern+alist
                                    (hyg:tag
                                      ',out-pattern
                                      (append! (hyg:flatten ',in-pattern) keywords)
                                      '())))
                              (hyg:untag
                                (mbe:expand-pattern
                                  (car tagged-out-pattern+alist)
                                  (mbe:get-bindings ',in-pattern macro-arg keywords)
                                  keywords)
                                (cdr tagged-out-pattern+alist)
                                '())))))
                     clauses)
             (else (error ',macro-name " no matching clause: ~S" ',clauses))))))))

|#


;; find-clause will find the clause to be used in order to expand a macro.
;;
;; Example:
;;
;; (define-syntax2 f
;;   (syntax-rules ()
;;     ((f a b)   (+ a b))
;;     ((f a b c) (* b c))))
;;
;; (find-clause 'f '(2 3) '() '( ((f a b)   (+ a b)) ((f a b c) (* b c))))
;; => (+ 2 3)
;;
;; (find-clause 'f '(1 2 3) '() '( ((f a b)   (+ a b)) ((f a b c) (* b c))))
;; => (* 2 3)
(define (find-clause macro-name macro-args keywords clauses ellipsis)
  (let ((macro-form (cons macro-name macro-args)))
    (let Loop ((l clauses))
      (if (null? l)
          (error macro-name "no matching clause for ~S" macro-form)
          (let ((in-pattern  (caar  l))
                (out-pattern (cadar l)))
            (if (mbe:matches-pattern? in-pattern macro-form keywords ellipsis)
                (let ((tmp (hyg:tag out-pattern
                                    (append! (hyg:flatten in-pattern) keywords)
                                    '()
                                    ellipsis)))
                  (hyg:untag (mbe:expand-pattern (car tmp)
                                                 (mbe:get-bindings in-pattern
                                                                   macro-form
                                                                   keywords
                                                                   ellipsis)
                                                 keywords
                                                 ellipsis)
                             (cdr tmp)
                             '()))
                (Loop (cdr l))))))))


;;
;; A very simple implementation of let-syntax.
;; FIXME: Need some work
;;
;; Example (with alternative ellipsis symbol '===):
;;
;; (macro-expand '(let-syntax ((f (syntax-rules === () ((f) 10)))) (f)))
;;
;; =>
;; (%let-syntax ((f (lambda args
;;                    (%find-macro-clause 'f
;;                                        args
;;                                        '(f)
;;                                        '(((f) 10)) '===))))
;;              (f))
(define-macro (let-syntax bindings . body)
  `(%let-syntax
    ,(map (lambda (x)
            (let ((macro-name (car x))
                  (syn-rules  (cadr x)))
              (let ((alt-ellipsis? (not (list? (cadr syn-rules)))))
                (let ((ellipsis (if alt-ellipsis?
                                    (cadr syn-rules)
                                    '...))
                      (keywords (if alt-ellipsis?
                                    (cons macro-name (caddr syn-rules))
                                    (cons macro-name (cadr syn-rules))))
                      (clauses  (if alt-ellipsis?
                                    (cdddr syn-rules)
                                    (cddr syn-rules))))
                  `(,macro-name (lambda args
                                  (%find-macro-clause ',macro-name
                                                      args
                                                      ',keywords
                                                      ',clauses
                                                      ',ellipsis)))))))
          bindings)
    ,@body))

)       ;;; end of module definition of MBE


(define %find-macro-clause (symbol-value 'find-clause (find-module 'MBE)))






;;MAC (define-macro (define-syntax macro-name syn-rules)
;;MAC   (if (or (not (pair? syn-rules))
;;MAC           (not (eq? (car syn-rules) 'syntax-rules)))
;;MAC     (error 'define-syntax "in `~S', bad syntax-rules ~S" macro-name syn-rules)
;;MAC
;;MAC     (let ((keywords    (cons macro-name (cadr syn-rules)))
;;MAC           (clauses     (cddr syn-rules))
;;MAC           (find-clause (symbol-value 'find-clause (find-module 'MBE))))
;;MAC       `(define-macro (,macro-name . args)
;;MAC          (%find-macro-clause ',macro-name args ',keywords ',clauses)))))


#|

                        Some macros for testing purpose

(define-syntax and
  (syntax-rules ()
    ((and) #t)
    ((and ?e) ?e)
    ((and ?e1 ?e2 ?e3 ...)
     (if ?e1 (and ?e2 ?e3 ...) #f))))

(define-syntax or
  (syntax-rules ()
    ((or) #f)
    ((or ?e) ?e)
    ((or ?e1 ?e2 ?e3 ...)
     (let ((temp ?e1))
       (if temp temp (or ?e2 ?e3 ...))))))

(define-syntax cond
    (syntax-rules (else =>)
      ((cond (else result1 result2 ...))
       (begin result1 result2 ...))
      ((cond (test => result))
       (let ((temp test))
         (if temp (result temp))))
      ((cond (test => result) clause1 clause2 ...)
       (let ((temp test))
         (if temp
             (result temp)
             (cond clause1 clause2 ...))))
      ((cond (test)) test)
      ((cond (test) clause1 clause2 ...)
       (let ((temp test))
         (if temp
             temp
             (cond clause1 clause2 ...))))
      ((cond (test result1 result2 ...))
       (if test (begin result1 result2 ...)))
      ((cond (test result1 result2 ...)
             clause1 clause2 ...)
       (if test
           (begin result1 result2 ...)
           (cond clause1 clause2 ...)))))

(define-syntax case
  (syntax-rules (else)
     ((case (key ...) clauses ...)
          (let ((atom-key (key ...)))
            (case atom-key clauses ...)))
     ((case key (else result1 result2 ...))
          (begin result1 result2 ...))
     ((case key ((atoms ...) result1 result2 ...))
          (if (memv key '(atoms ...)) (begin result1 result2 ...)))
     ((case key ((atoms ...) result1 result2 ...) clause clauses ...)
          (if (memv key '(atoms ...))
              (begin result1 result2 ...)
              (case key clause clauses ...)))))

(define-syntax and
  (syntax-rules ()
     ((and) #t)
     ((and test) test)
     ((and test1 test2 ...) (if test1 (and test2 ...) #f))))

(define-syntax or
  (syntax-rules ()
     ((or) #f)
     ((or test) test)
     ((or test1 test2 ...) (let ((x test1)) (if x x (or test2 ...))))))

(define-syntax let
  (syntax-rules ()
     ((let ((name val) ...) body1 body2 ...)
          ((lambda (name ...) body1 body2 ...) val ...))
     ((let tag ((name val) ...) body1 body2 ...)
          ((letrec ((tag (lambda (name ...) body1 body2 ...))) tag) val ...))))

(define-syntax let*
  (syntax-rules ()
     ((let* () body1 body2 ...)
          (let () body1 body2 ...))
     ((let* ((name1 val1) (name2 val2) ...) body1 body2 ...)
          (let ((name1 val1)) (let* ((name2 val2) ...) body1 body2 ...)))))


|#
