Re: [racket] Unexpected error extracting value from request bindings.
On Mon, Aug 01, 2011 at 12:28:51PM -0400, Eli Barzilay wrote: > Five minutes ago, Hendrik Boom wrote: > > On Mon, Aug 01, 2011 at 01:08:27AM -0400, Neil Van Dyke wrote: > > > > > > Esoterica #2: You'll see functions like "gensym" and "gentemp" in > > > some old Lisp dialects, and they will be used mostly to get around > > > problems of non-hygienic macros in that particular dialect. I > > > don't recall seeing them used in real Racket or Scheme code, > > > however, probably because those languages have hygienic macros. > > > > I've used them extensively in programs doing theorem proving on > > formulas involving quantifiers. This had noting to do with macros, > > hygienic or not. > > That's really the same use as macros: it's the easiest way to get a > "completely fresh" value, which is frequently needed in such a world > (and can get hairy if you're restricted in a pure world). IMO > uninterned symbols are actually a little *worse* for that purpose, > because besides the freshness, they have this confusing feature of > being printed like other symbols. Yes, it's to prevent rogue variable capture, which are what hygienic macros are for, too. But it is an intnded use which is not macros. -- hendrik _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
Yesterday, Jay McCarthy wrote: > (gensym) generates something that cannot be created again in anyway. > > Even it is printed as 'g1753, that doesn't mean typing 'g1753 will > get the same thing, nor that (string->symbol "g1753") will either. > This is the whole point of (gensym). The process of converting an input string into the same symbol is called "interning", and `gensym' doesn't do that as feature. Two relevant functions here are `symbol-interned?' and `string->uninterned-symbol'. > The Web server (basicaly) runs symbol->string to create the page and > then string->symbol later to turn the HTTP request in to the symbol. > When you did that to your gensym'd symbol, you got the same thing > that came out of the HTML. If that's for some symbols that are used in the server, then maybe there should be a check using `symbol-interned?'? > As for how it works... you can think of symbols as just pointers to > table entries that contain the actual text. (gensym) basically just > uses a different table, so it's impossible for a pointer in one > table to be the same as a pointer in the other table even if the > text is the same on both table entries. `gensym' doesn't use a different table -- it just doesn't use any table at all. 11 hours ago, Neil Van Dyke wrote: > You are correct that it is effectively black magic, in that you > probably don't need to know about it right now. Only a small > percentage of Racket programmers will ever need to know about it. You just need to be aware of that `gensym' feature... > Esoterica #1: If you wanted the produced symbol maker (in the > example above, a particular "my-make-symbol" value) to be callable > from multiple threads simultaneously, then the code should be > modified to use a semaphore, so that the setting of > "last-serial-number" and the incrementing of "serial-number" happens > in a mutex block. (The "string->symbol" part could be left out of > the mutex block, to increase parallelism.) But you probably don't > want to be calling the same "my-make-symbol" from multiple threads > simultaneously, anyway, so I didn't complicate this illustration > with a semaphore. Sidenote: channels are often more convenient than a semaphore. Instead of locking a mutex, you'd run a producer in its own thread and dump the counter on a channel: (define counter (let ([ch (make-channel)]) (thread (λ () (for ([i (in-naturals)]) (channel-put ch i (λ () (channel-get ch and now each (counter) call returns a new number, safely. > Esoterica #2: You'll see functions like "gensym" and "gentemp" in > some old Lisp dialects, and they will be used mostly to get around > problems of non-hygienic macros in that particular dialect. I don't > recall seeing them used in real Racket or Scheme code, however, > probably because those languages have hygienic macros. They're still sometimes used as a reliable way to get a unique value. (Since the uniqueness is its main feature, it will stay this way even if Racket -- or the particular language you use -- ever avoids allocating duplicate strings and uses hash-consing.) > Estoterica #3: In some Lisp dialects (not Racket), interned symbols > are not garbage-collected, so, if you tried to generate unique > symbols infinite numbers of times in a run of a program, > *eventually* they might want to take up more space than can be > represented by your computer and then by the universe. (And that can lead to extremely frustrating bugs...) -- ((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay: http://barzilay.org/ Maze is Life! _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
Five minutes ago, Hendrik Boom wrote: > On Mon, Aug 01, 2011 at 01:08:27AM -0400, Neil Van Dyke wrote: > > > > Esoterica #2: You'll see functions like "gensym" and "gentemp" in > > some old Lisp dialects, and they will be used mostly to get around > > problems of non-hygienic macros in that particular dialect. I > > don't recall seeing them used in real Racket or Scheme code, > > however, probably because those languages have hygienic macros. > > I've used them extensively in programs doing theorem proving on > formulas involving quantifiers. This had noting to do with macros, > hygienic or not. That's really the same use as macros: it's the easiest way to get a "completely fresh" value, which is frequently needed in such a world (and can get hairy if you're restricted in a pure world). IMO uninterned symbols are actually a little *worse* for that purpose, because besides the freshness, they have this confusing feature of being printed like other symbols. -- ((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay: http://barzilay.org/ Maze is Life! _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
On Mon, Aug 01, 2011 at 01:08:27AM -0400, Neil Van Dyke wrote: > > Esoterica #2: You'll see functions like "gensym" and "gentemp" in > some old Lisp dialects, and they will be used mostly to get around > problems of non-hygienic macros in that particular dialect. I don't > recall seeing them used in real Racket or Scheme code, however, > probably because those languages have hygienic macros. I've used them extensively in programs doing theorem proving on formulas involving quantifiers. This had noting to do with macros, hygienic or not. > > Estoterica #3: In some Lisp dialects (not Racket), interned symbols > are not garbage-collected, so, if you tried to generate unique > symbols infinite numbers of times in a run of a program, > *eventually* they might want to take up more space than can be > represented by your computer and then by the universe. Getting them garbage-collected was essential. One thing that's useful is to have the print routine assign names to the symbols. Symbols that are never printed never need to have names. The few that actually are printed can then have shorter names. -- hendrik _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
All good points. I should also mention that formlets do this automatically, which is why you don't name the fields when using formlets. Jay On Sun, Jul 31, 2011 at 11:08 PM, Neil Van Dyke wrote: > You are correct that it is effectively black magic, in that you probably > don't need to know about it right now. Only a small percentage of Racket > programmers will ever need to know about it. > > When you look at the "reference/symbols.html" page of Racket documentation, > pretend that you don't see anything other than "symbol?", "string->symbol", > and "symbol->string". You will probably use those three procedures a lot, > and you will probably never need to use anything else on that page. > > I suggest making your own somewhat "gensym"-like procedure, tailored to your > needs. If you're, say, generating symbols that must be unique only within a > single generated Web page, write your own procedure to support doing exactly > that. In this procedure, you will naturally use "string->symbol", and > consequently procedures like "eq?", "memq", and "assq" will simply work like > you'd expect (no black magic). As an example, here's one utility procedure > you might write, which itself produces a "gensym"-like procedure that you > can use in the context of, say, generating a single Web page: > > ;; Definition of your utility procedure: > (define (make-serially-numbered-symbol-maker prefix-string) > (let ((serial-number 0)) > (lambda () > (let ((last-serial-number serial-number)) > (set! serial-number (+ 1 serial-number)) > (string->symbol (string-append prefix-string > (number->string > last-serial-number))) > > ;; Example use of that utility procedure: > (let ((my-make-symbol (make-serially-numbered-symbol-maker "foo-"))) > (list (my-make-symbol) > (my-make-symbol) > (my-make-symbol))) > ;;==> '(foo-0 foo-1 foo-2) > > If a procedure that produces new procedures seems a little too fancy-pants > right now, you can do a similar thing in a block of your code that needs the > symbols made, without the part about producing a procedure. The nice thing > here about a procedure that produces a new procedure is that it's a good way > to put this code into a reusable library, rather merely having a code > pattern that you retype or copy&paste every time you need to do a similar > thing. > > Esoterica #1: If you wanted the produced symbol maker (in the example above, > a particular "my-make-symbol" value) to be callable from multiple threads > simultaneously, then the code should be modified to use a semaphore, so that > the setting of "last-serial-number" and the incrementing of "serial-number" > happens in a mutex block. (The "string->symbol" part could be left out of > the mutex block, to increase parallelism.) But you probably don't want to > be calling the same "my-make-symbol" from multiple threads simultaneously, > anyway, so I didn't complicate this illustration with a semaphore. > > Esoterica #2: You'll see functions like "gensym" and "gentemp" in some old > Lisp dialects, and they will be used mostly to get around problems of > non-hygienic macros in that particular dialect. I don't recall seeing them > used in real Racket or Scheme code, however, probably because those > languages have hygienic macros. > > Estoterica #3: In some Lisp dialects (not Racket), interned symbols are not > garbage-collected, so, if you tried to generate unique symbols infinite > numbers of times in a run of a program, *eventually* they might want to take > up more space than can be represented by your computer and then by the > universe. > > -- > http://www.neilvandyke.org/ > _ > For list-related administrative tasks: > http://lists.racket-lang.org/listinfo/users > -- Jay McCarthy Assistant Professor / Brigham Young University http://faculty.cs.byu.edu/~jay "The glory of God is Intelligence" - D&C 93 _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
You are correct that it is effectively black magic, in that you probably don't need to know about it right now. Only a small percentage of Racket programmers will ever need to know about it. When you look at the "reference/symbols.html" page of Racket documentation, pretend that you don't see anything other than "symbol?", "string->symbol", and "symbol->string". You will probably use those three procedures a lot, and you will probably never need to use anything else on that page. I suggest making your own somewhat "gensym"-like procedure, tailored to your needs. If you're, say, generating symbols that must be unique only within a single generated Web page, write your own procedure to support doing exactly that. In this procedure, you will naturally use "string->symbol", and consequently procedures like "eq?", "memq", and "assq" will simply work like you'd expect (no black magic). As an example, here's one utility procedure you might write, which itself produces a "gensym"-like procedure that you can use in the context of, say, generating a single Web page: ;; Definition of your utility procedure: (define (make-serially-numbered-symbol-maker prefix-string) (let ((serial-number 0)) (lambda () (let ((last-serial-number serial-number)) (set! serial-number (+ 1 serial-number)) (string->symbol (string-append prefix-string (number->string last-serial-number))) ;; Example use of that utility procedure: (let ((my-make-symbol (make-serially-numbered-symbol-maker "foo-"))) (list (my-make-symbol) (my-make-symbol) (my-make-symbol))) ;;==> '(foo-0 foo-1 foo-2) If a procedure that produces new procedures seems a little too fancy-pants right now, you can do a similar thing in a block of your code that needs the symbols made, without the part about producing a procedure. The nice thing here about a procedure that produces a new procedure is that it's a good way to put this code into a reusable library, rather merely having a code pattern that you retype or copy&paste every time you need to do a similar thing. Esoterica #1: If you wanted the produced symbol maker (in the example above, a particular "my-make-symbol" value) to be callable from multiple threads simultaneously, then the code should be modified to use a semaphore, so that the setting of "last-serial-number" and the incrementing of "serial-number" happens in a mutex block. (The "string->symbol" part could be left out of the mutex block, to increase parallelism.) But you probably don't want to be calling the same "my-make-symbol" from multiple threads simultaneously, anyway, so I didn't complicate this illustration with a semaphore. Esoterica #2: You'll see functions like "gensym" and "gentemp" in some old Lisp dialects, and they will be used mostly to get around problems of non-hygienic macros in that particular dialect. I don't recall seeing them used in real Racket or Scheme code, however, probably because those languages have hygienic macros. Estoterica #3: In some Lisp dialects (not Racket), interned symbols are not garbage-collected, so, if you tried to generate unique symbols infinite numbers of times in a run of a program, *eventually* they might want to take up more space than can be represented by your computer and then by the universe. -- http://www.neilvandyke.org/ _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
(gensym) generates something that cannot be created again in anyway. Even it is printed as 'g1753, that doesn't mean typing 'g1753 will get the same thing, nor that (string->symbol "g1753") will either. This is the whole point of (gensym). The Web server (basicaly) runs symbol->string to create the page and then string->symbol later to turn the HTTP request in to the symbol. When you did that to your gensym'd symbol, you got the same thing that came out of the HTML. As for how it works... you can think of symbols as just pointers to table entries that contain the actual text. (gensym) basically just uses a different table, so it's impossible for a pointer in one table to be the same as a pointer in the other table even if the text is the same on both table entries. Jay On Sun, Jul 31, 2011 at 9:07 PM, J G Cho wrote: > That sounds like a black magic. I am almost tempted to ask how it's done. > > I imagined (gensym) would generate some unique sequence not used so > far. Why would this approach not work? > > 2011/7/31 Jay McCarthy : >> Gensym produces symbols that are not eq to any other symbols created any >> other way. The bindings library produces symbols from strings, so they >> aren't eq to the gensym'd one. >> >> Jay >> >> Sent from my iPhone >> >> On 2011/07/31, at 15:13, J G Cho wrote: >> >>> I am a bit puzzled by this error: >>> >>> >>> Exception >>> The application raised an exception with the message: >>> >>> extract-binding/single: 'q10493 not found in '((q10493 . "no")) >>> >>> >>> Context: >>> Symbol q10493 was generated by (gensym 'q) and used in >>> >>> (list (radio-input (symbol->string nom) "yes") >>> (radio-input (symbol->string nom) "no"))) >>> >>> When I try to extract it >>> >>> (define (extract-single nom) >>> (λ (bindings) >>> (extract-binding/single nom bindings))) >>> >>> it complains >>> >>> So I changed to >>> (define (extract-single nom) >>> (λ (bindings) >>> (extract-binding/single (string->symbol (symbol->string nom)) >>> bindings))) >>> >>> And it seems to work but left me scratching my head. >>> >>> Does anybody care to shed some light on this? >>> >>> jGc >>> >>> _ >>> For list-related administrative tasks: >>> http://lists.racket-lang.org/listinfo/users >> > -- Jay McCarthy Assistant Professor / Brigham Young University http://faculty.cs.byu.edu/~jay "The glory of God is Intelligence" - D&C 93 _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
That sounds like a black magic. I am almost tempted to ask how it's done. I imagined (gensym) would generate some unique sequence not used so far. Why would this approach not work? 2011/7/31 Jay McCarthy : > Gensym produces symbols that are not eq to any other symbols created any > other way. The bindings library produces symbols from strings, so they aren't > eq to the gensym'd one. > > Jay > > Sent from my iPhone > > On 2011/07/31, at 15:13, J G Cho wrote: > >> I am a bit puzzled by this error: >> >> >> Exception >> The application raised an exception with the message: >> >> extract-binding/single: 'q10493 not found in '((q10493 . "no")) >> >> >> Context: >> Symbol q10493 was generated by (gensym 'q) and used in >> >> (list (radio-input (symbol->string nom) "yes") >> (radio-input (symbol->string nom) "no"))) >> >> When I try to extract it >> >> (define (extract-single nom) >> (λ (bindings) >> (extract-binding/single nom bindings))) >> >> it complains >> >> So I changed to >> (define (extract-single nom) >> (λ (bindings) >> (extract-binding/single (string->symbol (symbol->string nom)) >> bindings))) >> >> And it seems to work but left me scratching my head. >> >> Does anybody care to shed some light on this? >> >> jGc >> >> _ >> For list-related administrative tasks: >> http://lists.racket-lang.org/listinfo/users > _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users
Re: [racket] Unexpected error extracting value from request bindings.
Gensym produces symbols that are not eq to any other symbols created any other way. The bindings library produces symbols from strings, so they aren't eq to the gensym'd one. Jay Sent from my iPhone On 2011/07/31, at 15:13, J G Cho wrote: > I am a bit puzzled by this error: > > > Exception > The application raised an exception with the message: > > extract-binding/single: 'q10493 not found in '((q10493 . "no")) > > > Context: > Symbol q10493 was generated by (gensym 'q) and used in > > (list (radio-input (symbol->string nom) "yes") > (radio-input (symbol->string nom) "no"))) > > When I try to extract it > > (define (extract-single nom) > (λ (bindings) >(extract-binding/single nom bindings))) > > it complains > > So I changed to > (define (extract-single nom) > (λ (bindings) >(extract-binding/single (string->symbol (symbol->string nom)) >bindings))) > > And it seems to work but left me scratching my head. > > Does anybody care to shed some light on this? > > jGc > > _ > For list-related administrative tasks: > http://lists.racket-lang.org/listinfo/users _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users

