Macro problem i do not understand

hello,

i have a problem that i tested both with Racket and Guile,

i have 2 macros used in one expression like this:
scheme@(guile-user)> (define i 2)
scheme@(guile-user)> {i <- i + 1}
and i got this error:
While compiling expression:
Syntax error:
unknown location: source expression failed to match any pattern in form <-

i use SRFI-105 so :
'{i <- i + 1} expand in:
(nfx i <- i + 1)

and i'm expecting nfx to be called but none of this happens:
scheme@(guile-user)> (nfx i <- i + 1)
While compiling expression:
Syntax error:
unknown location: source expression failed to match any pattern in form <-

it seems to be the <- macro and i do not understand why?

any idea?

macros are defined like this for the beginning:
;; from file assignment.scm
(define-syntax <-

(syntax-rules ()
;; special form like : (<- (bracket-apply T 3) (bracket-apply T 4))

;; one dimension array, example: {a[4] <- 7}
;; $bracket-apply$ is from SRFI 105  bracket-apply is an argument of the macro
((_ (bracket-apply container index) expr)

....

;; from file scheme-infix.scm
(define-syntax nfx
(syntax-rules ()

((_ ident opspecial term1 op term2) (cond ((or (equal? (quote opspecial) (quote <-)) (equal? (quote opspecial) (quote ←)))
  (begin

(display "nfx") (newline)
(opspecial ident (op term1 term2)))) ;; {ident <- {term1 op term2}}

...

it is in a module like this:

(define-module (Scheme+)

#:use-module (growable-vector)
#:use-module (srfi srfi-69) ;; Basic hash tables
#:use-module (srfi srfi-31) ;; rec
#:export (nfx def bracket-apply <- ← -> → <+ ⥆ +> ⥅ declare $ & condx <> ≠ ** ⇜ ⇝ repeat)
#:replace (do when unless))

(include-from-path "def.scm")
(include-from-path "array.scm")
(include-from-path "set-values-plus.scm")
(include-from-path "apply-square-brackets.scm")
(include-from-path "assignment.scm")
(include-from-path "declare.scm")
(include-from-path "condx.scm")
(include-from-path "block.scm")
(include-from-path "not-equal.scm")
(include-from-path "exponential.scm")
(include-from-path "while-do-when-unless.scm")
(include-from-path "repeat-until.scm")
(include-from-path "scheme-infix.scm")

if it can help.

i have found that the error is related with this :

(define-syntax nfx
(syntax-rules ()

((_ ident opspecial term1 op term2) (cond ((or (equal? (quote opspecial) (quote <-)) (equal? (quote opspecial) (quote ←)))
  (begin

(display "nfx") (newline)
(opspecial ident (op term1 term2)))) ;; {ident <- term1 op term2}

 ((or (equal? (quote op) (quote ->)) (equal? (quote op) (quote →))) (op term2 (opspecial ident term1))) ;; Warning: argument names of macro do not reprensent the values contained in this case

 (else (! ident (quote opspecial) term1 op term2))))

this code works:
scheme@(guile-user)> (define i 2)
scheme@(guile-user)> {i <- i + 1}
nfx
3

but if i change the last line of the else clause by removing the quote of opspecial:
(else (! ident opspecial term1 op term2))))

it will fail:

scheme@(guile-user)> {i <- i + 1}
While compiling expression:
Syntax error:
unknown location: source expression failed to match any pattern in form <-

i understand the problem is with opspecial equal in this example to the special form <- but what i do not understand is why the code is then going to the else clause as i know that previously it was on the same example evaluating the first clause:
(begin
(display "nfx") (newline)
(opspecial ident (op term1 term2)))) ;; {ident <- term1 op term2}

here the error in Racket:

#lang reader "racket/SRFI-105.rkt"
(require "../Scheme-PLUS-for-Racket/main/Scheme-PLUS-for-Racket/Scheme+.rkt")

Welcome to DrRacket, version 8.7 [cs].
Language: reader "racket/SRFI-105.rkt", with debugging; memory limit: 8192 MB.

(define i 3)
{i <- i + 1}
<-: bad syntax in: <-

Regards,
Damien

1 Like

here is an easy to test Racket compatible version of the problem (no need of SRFI-105) ,this proove it is not related to SRFI-105 implementation:

#lang racket
(define-syntax $nfx$
  (syntax-rules ()

    ((_ ident opspecial term1 op term2) (cond ((or (equal? (quote opspecial) (quote <-)) (equal? (quote opspecial) (quote ←)))
					       (begin
						 (display "$nfx$") (newline)
						 (opspecial ident (op term1 term2)))) ;; {ident <- term1 op term2}
					      
					      ((or (equal? (quote op) (quote ->)) (equal? (quote op) (quote →))) (op term2 (opspecial ident term1))) ;; Warning: argument names of macro do not reprensent the values contained in this case
					      
					      ;;(else (! (quote ident) (quote opspecial) (quote term1) (quote op) (quote term2)))))
					      (else (! ident (quote opspecial) term1 op term2))))))





(define-syntax <-
  
  (syntax-rules ()

    ((_ var expr)
     
     (begin
       ;;(display "<- : variable set!") (newline)
       (set! var expr)
       var))))

this works:

Welcome to DrRacket, version 8.7 [cs].
Language: racket, with debugging; memory limit: 8192 MB.
> 1
1
> (define i 3)
> ($nfx$ i <- i + 1)
$nfx$
4

if i do not quote opspecial in the else clause which is not used! it give an error:

#lang racket
(define-syntax $nfx$
  (syntax-rules ()

    ((_ ident opspecial term1 op term2) (cond ((or (equal? (quote opspecial) (quote <-)) (equal? (quote opspecial) (quote ←)))
					       (begin
						 (display "$nfx$") (newline)
						 (opspecial ident (op term1 term2)))) ;; {ident <- term1 op term2}
					      
					      ((or (equal? (quote op) (quote ->)) (equal? (quote op) (quote →))) (op term2 (opspecial ident term1))) ;; Warning: argument names of macro do not reprensent the values contained in this case
					      
					      ;;(else (! (quote ident) (quote opspecial) (quote term1) (quote op) (quote term2)))))
					      (else (! ident opspecial term1 op term2))))))





(define-syntax <-
  
  (syntax-rules ()

    ((_ var expr)
     
     (begin
       ;;(display "<- : variable set!") (newline)
       (set! var expr)
       var))))

error:

Welcome to DrRacket, version 8.7 [cs].
Language: racket, with debugging; memory limit: 8192 MB.
> (define i 3)
> ($nfx$ i <- i + 1)
<-: bad syntax in: <-
> 

just understood my error:

quote is a macro, so it stop the evaluation of the other macro <- and <- is expanded in the else even if this branch will not be executed and the error arise.

1 Like