Define in scheme - scheme

I was trying to run this command on drracket:
(define #t #f)
and I get the following error messege:
define: bad syntax in: #t
I want to know what's the reason for that error, and why I can do: (define + 12) and not this.
Thanks a lot!

The syntax of define is:
(define <variable> <expression>)
A variable is a special kind of identifier, and the format of identifiers is described here. As you can see from the description, #t (and more generally, anything that starts with a #) is not an identifier.

define expects an identifier as it's first argument. In this case you are supplying #t which evaluates to the boolean value true. Hence the bad syntax error message.

First argument to define must be a symbol. + is a symbol. foo is a symbol. #t is #t, not a symbol. 1 is not a symbol.

Related

More macro woes

I am still having some troubles with this concept. The key paragraph in the r7rs standard is:
"Identifiers that appear in the template but are not pattern
variables or the identifier ellipsis are inserted into the output as literal identifiers. If a literal identifier is inserted as a
free identifier then it refers to the binding of that identifier
within whose scope the instance of syntax-rules appears.
If a literal identifier is inserted as a bound identifier then
it is in effect renamed to prevent inadvertent captures of
free identifiers."
By "bound identifier" am I right that it means any argument to a lambda, a top-level define or a syntax definition ie. define-syntax, let-syntax or let-rec-syntax? (I think I could handle internal defines with a trick at compile time converting them to lambdas.)
By "free identifier" does it mean any other identifier that presumably is defined beforehand with a "bound identifier" expression?
I wonder about the output of code like this:
(define x 42)
(define-syntax double syntax-rules ()
((_) ((lambda () (+ x x)))))
(set! x 3)
(double)
Should the result be 84 or 6?
What about this:
(define x 42)
(define-syntax double syntax-rules ()
((_) ((lambda () (+ x x)))))
(define (proc)
(define x 3)
(double))
(proc)
Am I right to suppose that since define-syntax occurs at top-level, all its free references refer to top-level variables that may or may not exist at the point of definition. So to avoid collisions with local variables at the point of use, we should rename the outputted free reference, say append a '%' to the name (and disallow the user to create symbols with % in them). As well as duplicate the reference to the top-level variable, this time with the % added.
If a macro is defined in some form of nested scope (with let-syntax or let-rec-syntax) this is even trickier if it refers to scoped variables. When there is a use of the macro it will have to expand these references to their form at point of definition of the macro rather than point of use. So I'm guessing the best way is expand it naturally and scan the result for lambdas, if it finds one, rename its arguments at point of definition, as the r7rs suggests. But what about references internal to this lambda, should we change these as well? This seems obvious but was not explicitly stated in the standard.
Also I'm still not sure whether it is best to have a separate expansion phase separate from the compiler, or to interweave expanding macros with compiling code.
Thanks, and excuse me if I've missed something obviously, relatively new to this.
Steve
In your first example, properly written:
(define x 42)
(define-syntax double
(syntax-rules ()
( (_) ((lambda () (+ x x))) ) ))
(set! x 3)
(double)
the only possibility is 6 as there is only one variable called x.
In your second example, properly written:
(define x 42)
(define-syntax double
(syntax-rules ()
((_) ((lambda () (+ x x))) )))
(define (proc)
(define x 3)
(double))
(proc)
the hygienic nature of the Scheme macro system prevents capture of the unrelated local x, so the result is 84.
In general, identifiers (like your x) within syntax-rules refer to what they lexically refer to (the global x in your case). And that binding will be preserved because of hygiene. Because of hygiene you do not have to worry about unintended capture.
Thanks, I think I understand... I still wonder how in certain advanced circumstances hygiene is achieved, eg. the following:
(define (myproc x)
(let-syntax ((double (syntax-rules ()
((double) (+ x x)))))
((lambda (x) (double)) 3)))
(myproc 42)
The site comes up with 84 rather than 6. I wonder how this (correct) referential transparency is achieved just by renaming. The transformer output does not bind new variables, yet still when it expands on line 4, we have to find a way to get to the desired x rather than the most recent.
The best way I can think of is simply rename every time a lambda argument or definition shadows another, ie. keep appending %1, %2 etc... macro outputs will have their exact versions named (eg. x%1) while references to identifiers simply have their unadorned name x and the correct variable is found at compile time.
Thanks, I hope for any clarification.
Steve

Can syntax parameters be used to replace syntax?

I'm trying to use syntax parameters in order to inject new syntax where I need it to be injected. The result of this is then used in other syntax.
However, it's not working as I expect it to. Here's a minimal working example:
(require racket/stxparam)
(require (for-syntax racket/stxparam))
;; declare parameter to be replaced with code
(define-syntax-parameter placeholder
(lambda (stx)
(raise-syntax-error
(syntax-e stx)
"can only be used inside declare-many-commands")))
;; this is just to print what 'arg' looks like
(define-syntax (print-syntax stx)
(syntax-case stx ()
[(_ arg)
#'(displayln 'arg)]))
;; this is the top-level entity invoked to produce many commands
(define-syntax-rule (declare-many-commands cmds)
(begin
(let ([X 10])
(syntax-parameterize
([placeholder (make-rename-transformer #'X)])
cmds))
(let ([X 20])
(syntax-parameterize
([placeholder (make-rename-transformer #'X)])
cmds))))
(declare-many-commands
(print-syntax placeholder))
What I would like to get as result when running this is:
10
20
but what I get is:
placeholder
placeholder
EDIT:
Posted a new question to refine the problem: Injecting syntax at compile time using Racket's syntax parameters?
The problem here is that your print-syntax macro quotes its input, and inputs to macro transformers are unexpanded syntax. This means that the expansion of (print-syntax placeholder) will always be (displayln 'placeholder), and no macroexpansion ever occurs under quote, so the placeholder binding in scope is irrelevant.
If you want to use the syntax parameter, you need to actually produce a reference to the placeholder binding. In this case, you just need to remove the use of quote. You could change print-syntax to (displayln arg), but at that point, there’s really no reason for print-syntax to be a macro, since it’s equivalent to the displayln function. Just use that instead:
(declare-many-commands
(displayln placeholder))
This will print 10 and 20 as you expect.
It’s possible you really do want the quote, and I don’t understand your question. In that case, though, I think it’s difficult for me to understand what you’re getting at without some additional context.

Is there a way to check if a symbol is quoted?

I'm trying to create a macro on Emacs Lisp and I'm struggling to see if a user may pass a symbol quoted or not quoted.
Actually I need something like quote-only-if-is-not-quoted macro. Is there anything like that? I didn't found nothing about that on any Lisp dialect. Macro example:
(quote-only-if-is-not-quoted 'q) => (quote q)
(quote-only-if-is-not-quoted q) => (quote q)
Thanks in advance.
Macro arguments are unevaluated, so yes, you can check to see whether an argument is quoted and, if not, quote it. Something like this?
(defmacro quote-only-if-is-not-quoted (arg)
(if (and (consp arg)
(eq (car arg) 'quote))
arg
`(quote ,arg)))

How do I evaluate a symbol returned from a function in Scheme?

I'm refamiliarizing myself with Scheme and I've hit a problem that is probably reflecting a fundamental misunderstanding on my part.
Say I do the following in Scheme (using Guile in this case but it's the same in Chicken):
> (define x 5)
> x
5
> (string->symbol "x")
x
> (+ 5 (string->symbol "x"))
<unnamed port>:45:0: In procedure #<procedure 1b84960 at <current input>:45:0 ()>:
<unnamed port>:45:0: In procedure +: Wrong type: x
> (symbol? (string->symbol "x"))
#t
> (+ 5 x) ; here x is dereferenced to its value 5
10
> (+ 5 'x) ; here x is not dereferenced
<unnamed port>:47:0: In procedure #<procedure 1c7ba60 at <current input>:47:0 ()>:
<unnamed port>:47:0: In procedure +: Wrong type: x
I understand that string->symbol is returning a symbol, x, which is effectively quoted. However, I cannot figure out how to use the symbol returned by string->symbol in any later context. How can I have Scheme evaluate that symbol?
To give a background on why I want to do this, it's that I'm writing a C program with embedded Guile. I would like to be able to be able to access symbols defined in Guile by name from C, using for example scm_from_*_symbol or scm_string_to_symbol. The reasons these functions aren't working the way I thought they would is related to my core question above. Perhaps there's a better way to do what I want to do with Guile, but that's a different question. Right now I'm interested in the fundamental question above.
What you want is to evaluate the symbol (not to "dereference" it). I think this is what you meant:
(define x 5)
(+ 5 (eval 'x (interaction-environment)))
=> 10
Take a look at the documentation for further details.
You should read the chapter fly-evaluation of the Guile documentation.
You want eval and probably interaction-environment
I recommend reading the famous SICP and Queinnec's Lisp In Small Pieces
Symbols are not special in this sense, that is they are not easier to evaluate than ordinary strings.
Symbol is much like a string, it just doesn't have quotes around it. Well, the fundamental difference is, of course, not absence of quotes, but the fact that symbols are interned. This means that strings "x" and "x" are two different strings (although they are equal), while symbols 'x and 'x are actually the same object.

Quotation in Scheme

Following is a exercise from SICP. I couldn't figure it out on my own. Can some why help me understand?
Type following code into interpreator:
(car ''abracadabra)
And it print out 'quote'. Why?
As gimpf said, 'abracadabra = (quote abracadabra). You can verify this by typing ''abracadabra to the REPL, which will print (quote abracadabra).
Because ''abracadabra is really (quote (quote abracadabra)). In Scheme, the rule is: evaluate all the parts of the s-expression, and apply the first part to the rest of the parts.
"car" and "quote" are symbols in the below. #car and #quote are the functions they refer to.
If you take
(car (quote (quote abracadabra)))
and evaluate the parts, you get
(#car (quote abracadabra))
Then, apply the first part (the car function) to the second part (a list of two symbols).
quote
And you get just the symbol "quote".
Just remember, to figure out what happens in Scheme, evaluate the parts and apply the first to the rest. If you evaluate quote, you get the stuff inside. The only confusing part is that some primitives (number and strings) evaluate to themselves.

Resources