I want a macro that create a new identifier like
(new-name first second) => first-second
that could be used to define new toplevel bindings
(define-syntax define-generic
(syntax-rules ()
((define-generic (name a b ...))
(begin
(define (new-name name data) 15) ; <= create a new binding
(define name (lambda (a b ...)
(add (new-name name-data) 7)))))) ; <= use new identifier
If i set! the value of the "new-name" binding, then it should affect the newly created procedure.
There was a discussion on Reddit on this just a few days back. Might be worthwhile studying the implementation posted for more details - http://www.reddit.com/r/scheme/comments/f54dk/i_wrote_an_hygienic_definemacro_that_can_capture/
You can't do it in a pure R5RS. Fortunately, most of the popular Scheme implementations provides a proper macro system besides that limited R5RS hygienic stuff:
(define-macro (new-name a b) (string->symbol (string-append (symbol->string a) "-" (symbol->string b))))
Related
I remember I read somewhere it is not a macro and is built into the core language. Something like that, I am not sure, because I can no longer remember from where I read it. So is struct a macro in Racket or not? If not, why is it built into the core language?
A macro; struct.rkthas
(define-syntax (struct stx)
(define (config-has-name? config)
(cond
[(syntax? config) (config-has-name? (syntax-e config))]
[(pair? config) (or (eq? (syntax-e (car config)) '#:constructor-name)
(eq? (syntax-e (car config)) '#:extra-constructor-name)
(config-has-name? (cdr config)))]
[else #f]))
(with-syntax ([orig stx])
(syntax-case stx ()
[(_ id super-id fields . config)
(and (identifier? #'id)
(identifier? #'super-id))
(if (not (config-has-name? #'config))
(syntax/loc stx
(define-struct/derived orig (id super-id) fields #:constructor-name id . config))
(syntax/loc stx
(define-struct/derived orig (id super-id) fields . config)))]
[(_ id fields . config)
(identifier? #'id)
(if (not (config-has-name? #'config))
(syntax/loc stx
(define-struct/derived orig id fields #:constructor-name id . config))
(syntax/loc stx
(define-struct/derived orig id fields . config)))]
[(_ id . rest)
(identifier? #'id)
(syntax/loc stx
(define-struct/derived orig id . rest))]
[(_ thing . _)
(raise-syntax-error #f
"expected an identifier for the structure type name"
stx
#'thing)]))))
In Racket IDE, you can use the Open Defining File function to locate the source code (if available).
It looks like I misunderstood the question, when I answered before. So here's an answer to the question that was meant:
Structs are built-in and primitive; they underpin the implementation. In fact, circa 2007, Matthew Flatt commented that in PLT Scheme (as Racket was known then), in a sense everything is a struct:
> At Thu, 31 May 2007 16:45:25 -0700, YC wrote:
> Out of curiosity - does PLT scheme actually use struct as the fundamental
> compound type, i.e. implement closure/etc on top of struct.
The way I think about it, everything is a struct, but some things use a
special-case representation because they're important enough. (The
extreme case is a fixnum).
But an equally valid answer would be: no, not all compound types use
the same representation as values from a struct constructor.
-- Source.
Start of the thread.
In addition to usepla's great answer, I wanted to add:
In the Racket documentation, the "blue box" has a phrase in the top right corner such as procedure or syntax. For struct it says syntax.
If you think about what struct does, among other things it defines named functions derived from the name of the struct. So (struct foo (a b)) will define a foo? predicate and accessors foo-a, foo-b. A plain function can't define new named things like this, so, it must be a macro.
Reading through the implementation code in define-struct.rkt, if you want to do the same thing manually, the following code is a much simplified version of what it is doing.
(define-syntax (struct stx)
;
; Function that creates compound names using syntax objects
(define (make-name id . parts)
(datum->syntax
id
(string->symbol
(apply string-append
(map (lambda (p)
(if (syntax? p)
(symbol->string (syntax-e p))
p))
parts)))
id))
;
(syntax-case stx ()
;
; parse the input and extract the name and variable
; this version uses only one variable for simplicity (3)
[(_ id avar)
;
; guard to ensure we have an identifier
(identifier? #'id)
;
; Create the names (1)
(let ((? (make-name #'id #'id "?"))
(v (make-name #'id #'id "-" #'avar)))
; Generate code to define the various functions associated with
; the new struct (2)
#`(begin
(define id (lambda (vx) (list id vx)))
(define #,? (lambda (x) (eq? (car x) id)))
(define #,v (lambda (x) (second x)))))]
))
1) We have to create the names we will define: but we need to use syntax objects to do so
2) We generate code that will define all of the functions associated with the new object in the global namespace
3) In the real version, most of the code deals with the properties that can be used a struct definition. The real version also needs to handle arbitrary numbers of variables and alternative forms, defaults etc...
I like to build a REPL with my own datatypes, but I don't like to write all the same pattern functions over and over again.
So this is a nut, which bothers me.
I got my own set of primitive datatypes (define primitives '("mytrue" "myfalse" "mynumber" ...))
Also I have (define primitiveTesters (list "mytrue?" "myfalse?" "mynumber?" ... )
The problem now is, I just want to apply (map) or a macro to get the datatype? procedurces, which basically just checks if the car of record (mynumber . ( . )) exists.
So something similar like (mynumber? (car (mynumber.(1.))) => #t in the end. But for this I need (define mynumber? (lambda (...)(...))
My define-batching macro looks like this, but I just have no luck to infuse the <variable>.
(define-syntax define-batching
(syntax-rules ()
((_ value expr)(define value expr))
((_ value) value)
((_ value1 value2 ...) (begin (define value1 expr) (define-batching test2...)))
))
So have I reached a dead end of scheme ?
I've seen something similar, I think in Emacs Lisp.
What I am looking for in the end is:
(define checker '(audi? volkswagen? mercedes?))
(define datatype '(audi volkswagen mercedes))
(map define-checker checker datatype )
or
(define-checker (car checker) (car datatype))
If I understood the question right, you need a macro
to define your own type checkers?
Here is one way to do it:
(define-syntax define-checker
(syntax-rules ()
[(define-checker name tag)
(define (name object)
(and (list? object)
(not (null? object))
(eq? (car object) 'tag)))]))
(define-checker my-car? car)
(my-car? '(car audi black)) ; evaluates to #t
(my-car? '(truck ford pink)) ; evaluates to #f
Addendum:
If you write
(define checker '(audi? volkswagen? mercedes?))
(define datatype '(audi volkswagen mercedes))
the values will become available at runtime.
Therefore you need to a different approach.
You could for example write:
(define-checker+datatype (audi? audi) (volkswagen? volkswagen?))
Here is the code:
(define-syntax define-checker
(syntax-rules ()
[(define-checker name tag)
(define (name object)
(and (list? object)
(not (null? object))
(eq? (car object) 'tag)))]))
(define-syntax define-checkers+datatype
(syntax-rules ()
[(define-checkers+datatype (name tag) ...)
(begin
(define-checker name tag)
...)]))
(define-checkers+datatype (audi? audi) (wv? wv))
(audi? '(audi black))
define-syntax is hygienic, that means it cannot influence on parent environment, that means it cannot define symbols in it.
You may try to use er-, ir- macro-transformers which allow you to explicit renames symbols.
keywords to google in you scheme documentation are 'er-macro-transformet' and 'ir-macro-transformer'
In order to teach myself more advanced macros in racket, I set about creating a macro to increment a field in a mutable struct:
(increment! instance name field)
=>
(set-name-field instance (get-name-field instance))
I produced a macro that works, and decided it would be useful to share between multiple modules. Unfortunately, since the structure mutators are not in the scope of the module defining the macro, an expansion error occurs.
The following is a contrived example demonstrating the problem. I would like to know:
Did I write the macro code in idiomatic-racket style? Is this the correct approach?
How can I control the expansion of the macro, so that it operates in the presence of identifiers not found in it's original context?
Thanks.
#lang racket/load
(module util racket
(define-syntax increment!
(lambda (stx)
(syntax-case stx ()
[(increment! s sn fn i)
(with-syntax
([set! (string->symbol
(format "set-~a-~a!" (syntax-e #'sn) (syntax-e #'fn)))]
[get (string->symbol
(format "~a-~a" (syntax-e #'sn) (syntax-e #'fn)))])
#'(set! s (+ i (get s))))]
;; default increment of 1
[(increment! s sn fn) #'(increment! s sn fn 1)])))
(provide increment!)
)
(module bank racket
(require 'util)
(struct money (dollars pounds euros) #:mutable #:transparent)
(let ([m (money 0 50 20)])
(increment! m money pounds 100)
(increment! m money dollars)
m)
)
(require 'bank)
Results in
expand: unbound identifier in module in: set-money-pounds!
You can't do just that. The problem is that you're generating symbols with the correct names, but you just return the symbols as-is, which means that with-syntax gives them some default (and wrong) lexical context. Instead, you should use datum->syntax and give it the correct context.
Below is a revision of your code that works as you expect. To see more about it, see my recent blog post on the subject of unhygienic macros.
However, this is not a robust solution. What happens when the setters and getters have different names? A more robust solution would be to use the struct name and extract the right information from it (at syntax time, in the macro) -- see the manual for details on this. It's also good to ask questions about it on the mailing list, since there might be a better way to get what you want, or a better solution if you're looking for some dot-notation-like functionality.
#lang racket/load
(module util racket
(define-syntax increment!
(lambda (stx)
(syntax-case stx ()
[(increment! s sn fn i)
(let ([id (lambda (fmt)
(let ([str (format fmt (syntax-e #'sn) (syntax-e #'fn))])
(datum->syntax #'sn (string->symbol str))))])
(with-syntax ([set! (id "set-~a-~a!")]
[get (id "~a-~a")])
#'(set! s (+ i (get s)))))]
;; default increment of 1
[(increment! s sn fn) #'(increment! s sn fn 1)])))
(provide increment!))
(module bank racket
(require 'util)
(struct money (dollars pounds euros) #:mutable #:transparent)
(let ([m (money 0 50 20)])
(increment! m money pounds 100)
(increment! m money dollars)
m))
(require 'bank)
I am trying to write a wrapper for define, that stores the values passed to it. I've been approaching it in baby steps (being new to Lisp in general, and even newer to Scheme) but have run into a wall.
In Racket, I'm starting with:
> (require (lib "defmacro.ss"))
> (define-macro (mydefine thing definition)
`(define ,thing ,definition))
> (mydefine a 9)
> a
9
Okay, that works. Time to do something in the macro, prior to returning the s-exprs:
> (define-macro (mydefine thing definition)
(display "This works")
`(define ,thing ,definition))
> (mydefine a "bob")
This works
> a
"bob"
Nice. But I can't for the life of me get it to set a global variable instead of displaying something:
> (define *myglobal* null)
> (define-macro (mydefine thing definition)
(set! *myglobal* "This does not")
`(define ,thing ,definition))
> (mydefine a ":-(")
set!: cannot set identifier before its definition: *myglobal*
Any suggestions on how to accomplish this would be greatly appreciated.
I suspect that I'm trying to swim against the current here, either by fiddling with globals from a macro in Scheme, or by using define-macro instead of learning the Scheme-specific syntax for macro creation.
You're running against Racket's phase separation -- which means that each phase (the runtime and the compile-time) operate in different worlds. As Vijay notes, one way to solve this is to do what you want at runtime, but that will probably not be what you need in the long run. The thing is that trying these things usually means that you will want to store some syntactic information at the compile-time level. For example, say that you want to store the names of all of your defined names, to be used in a second macro that will print them all out. You would do this as follows (I'm using sane macros here, define-macro is a legacy hack that shouldn't be used for real work, you can look these things up in the guide, and then in the reference):
#lang racket
(define-for-syntax defined-names '())
(define-syntax (mydefine stx)
(syntax-case stx ()
[(_ name value)
(identifier? #'name)
(begin (set! defined-names (cons #'name defined-names))
#'(define name value))]
;; provide the same syntactic sugar that `define' does
[(_ (name . args) . body)
#'(mydefine name (lambda args . body))]))
Note that defined-names is defined at the syntax level, which means that normal runtime code cannot refer to it. In fact, you can have it bound to a different value at the runtime level, since the two bindings are distinct. Now that that's done, you can write the macro that uses it -- even though defined-names is inaccessible at the runtime, it is a plain binding at the syntax level, so:
(define-syntax (show-definitions stx)
(syntax-case stx ()
[(_) (with-syntax ([(name ...) (reverse defined-names)])
#'(begin (printf "The global values are:\n")
(for ([sym (in-list '(name ...))]
[val (in-list (list name ...))])
(printf " ~s = ~s\n" sym val))))]))
The statement (set! *myglobal* "This does not") is executed in the transformer environment, not the normal environment. So it's not able to find *myglobal. We need to get both the expressions executed in the environment where *myglobal* is defined.
Here is one solution:
(define *defined-values* null)
(define-macro (mydefine thing definition)
`(begin
(set! *defined-values* (cons ,definition *defined-values*))
(define ,thing ,`(car *defined-values*))))
> (mydefine a 10)
> (mydefine b (+ 20 30))
> a
10
> b
50
> *defined-values*
(50 10)
> (define i 10)
> (mydefine a (begin (set! i (add1 i)) i)) ;; makes sure that `definition`
;; is not evaluated twice.
> a
11
If the Scheme implementation does not provide define-macro but has define-syntax, mydefine could be defined as:
(define-syntax mydefine
(syntax-rules ()
((_ thing definition)
(begin
(set! *defined-values* (cons definition *defined-values*))
(define thing (car *defined-values*))))))
I am trying to write a macro that defines a special class of data structure with associated functions.
I know this is possible; it is done multiple times in the core language itself.
As a specific example, how would I define the define-struct macro in Scheme itself. It needs to create make-struct, struct-<<field>>, etc functions.
I tried doing this using define, however, this only defines the function in the macro's lexical scope.
How can I actually define a function in a macro?
The key for an answer is datum->syntax. The basic idea is that you want to take some random data and turn it into a syntax -- in this case, turn a symbol into an identifier. An identifier is basically a symbol with some lexical information that (very roughly) indicates how it is bound. Using datum->syntax you can do exactly that: it expects an existing piece of syntax which is where it copies the binding from, and a datum (a symbol here) which is the value that is contained in the syntax wrapper.
Here's an example that demonstrates a define-struct-like tool using this:
#lang scheme
;; implements a defstruct-like macro that uses association lists
(define-syntax (defstruct-lite stx)
(syntax-case stx ()
[(defstruct-lite name field ...)
(let ([make-id
(lambda (template . ids)
(let ([str (apply format template (map syntax->datum ids))])
(datum->syntax stx (string->symbol str))))])
(with-syntax ([make-name (make-id "make-~a" #'name)]
[name? (make-id "~a?" #'name)]
[(arg ...) (generate-temporaries #'(field ...))]
[(name-field ...)
(map (lambda (f) (make-id "~a-~a" #'name f))
(syntax->list #'(field ...)))])
#'(begin
(define (make-name arg ...) (list 'name (cons 'field arg) ...))
(define (name? x) (and (pair? x) (eq? 'name (car x))))
(define (name-field x)
(and (name? x) (cdr (assq 'field (cdr x)))))
...)))]))
And here's an example of using it:
(defstruct-lite point x y)
(point-y (make-point 1 2))