Re: [racket-users] macros in Racket repository

2021-05-09 Thread Tim Meehan
Thanks folks!


> On May 9, 2021, at 09:38, Robby Findler  wrote:
> 
> 
> Here's one way to write it in modern Racket:
> 
> #lang racket
> (require (for-syntax syntax/parse))
> 
> (module+ test (require rackunit))
> 
> (define-syntax (my-and stx)
>   (syntax-parse stx
> [(_) #'#f]
> [(_ e:expr) #'e]
> [(_ e1:expr e2:expr ...)
>  #'(if e1 (my-and e2 ...) #f)]))
> 
> (module+ test
>   (check-equal? (my-and) #f)
>   (check-equal? (my-and 1) 1)
>   (check-equal? (my-and 1 2) 2)
>   (check-equal? (my-and 1 #f 2) #f))
> 
> 
> 
>> On Sun, May 9, 2021 at 9:30 AM Jens Axel Søgaard  
>> wrote:
>> Hi Tim,
>> 
>> In this case Ryan's method leads to:
>> 
>> https://github.com/racket/racket/blob/master/racket/collects/racket/private/qq-and-or.rkt#L440
>> 
>> But in case you are wondering about the style used in that file:
>> at the point where "qq-and-or.rkt" is used, none of the usual 
>> syntax tools (such as syntax-case and syntax-parse) are available,
>> so it is written using only primitive constructs.
>> 
>> That is, that particular file does not represent the usual style of macro 
>> writing.
>> 
>> /Jens Axel
>> 
>> 
>>> Den søn. 9. maj 2021 kl. 16.13 skrev Ryan Culpepper 
>>> :
>>> Here are the three most convenient ways I know of to find that information 
>>> (which is "$RACKET/collects/racket/private/qq-and-or.rkt" in this specific 
>>> case):
>>> 
>>> If you use DrRacket, then open a file that uses `and`, right-click on an 
>>> occurrence of `and`, and choose "Open Defining File" (which changes to 
>>> "Jump to Definition (in Other File)" once DrRacket opens the file.
>>> 
>>> If you use Emacs with racket-mode, go to an occurrence of `and` and hit 
>>> "M-." (that is, hold down Meta/Alt and press the period key). You can also 
>>> use "M-x racket-visit-definition". That opens the defining module and jumps 
>>> to the definition.
>>> 
>>> If you have the `whereis` package installed, run the command `raco whereis 
>>> -b racket/base and` and it will print the path of the defining file.
>>> 
>>> Ryan
>>> 
>>> 
 On Sun, May 9, 2021 at 3:26 PM Tim Meehan  wrote:
 Where in the repository are macros like "and" and "or" defined?
 I tried searching for "and" and "or" ... but you probably know how that 
 worked out.
 
 Thanks folks!
 -- 
 You received this message because you are subscribed to the Google Groups 
 "Racket Users" group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to racket-users+unsubscr...@googlegroups.com.
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/racket-users/CACgrOxK6S8EOAGk_rPbE%2B_wMLJiSbpwMhVd4AeRL8C9%2BDW3mgg%40mail.gmail.com.
>>> 
>>> -- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to racket-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/racket-users/CANy33q%3DsLEH-ooUJxTay6pG1GNcRLZDUotNJ23L1HRTC1XqHwA%40mail.gmail.com.
>> 
>> 
>> -- 
>> -- 
>> Jens Axel Søgaard
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/CABefVgzgqRPzx6ct6LrN-TAfE18vsdqnopun0B63LmqqKxerAQ%40mail.gmail.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/5FA065BA-EFB1-424A-BDCE-7C865613EDEB%40gmail.com.


Re: [racket-users] macros in Racket repository

2021-05-09 Thread Robby Findler
Here's one way to write it in modern Racket:

#lang racket
(require (for-syntax syntax/parse))

(module+ test (require rackunit))

(define-syntax (my-and stx)
  (syntax-parse stx
[(_) #'#f]
[(_ e:expr) #'e]
[(_ e1:expr e2:expr ...)
 #'(if e1 (my-and e2 ...) #f)]))

(module+ test
  (check-equal? (my-and) #f)
  (check-equal? (my-and 1) 1)
  (check-equal? (my-and 1 2) 2)
  (check-equal? (my-and 1 #f 2) #f))



On Sun, May 9, 2021 at 9:30 AM Jens Axel Søgaard 
wrote:

> Hi Tim,
>
> In this case Ryan's method leads to:
>
>
> https://github.com/racket/racket/blob/master/racket/collects/racket/private/qq-and-or.rkt#L440
>
> But in case you are wondering about the style used in that file:
> at the point where "qq-and-or.rkt" is used, none of the usual
> syntax tools (such as syntax-case and syntax-parse) are available,
> so it is written using only primitive constructs.
>
> That is, that particular file does not represent the usual style of macro
> writing.
>
> /Jens Axel
>
>
> Den søn. 9. maj 2021 kl. 16.13 skrev Ryan Culpepper <
> rmculpepp...@gmail.com>:
>
>> Here are the three most convenient ways I know of to find that
>> information (which is "$RACKET/collects/racket/private/qq-and-or.rkt" in
>> this specific case):
>>
>> If you use DrRacket, then open a file that uses `and`, right-click on an
>> occurrence of `and`, and choose "Open Defining File" (which changes to
>> "Jump to Definition (in Other File)" once DrRacket opens the file.
>>
>> If you use Emacs with racket-mode, go to an occurrence of `and` and hit
>> "M-." (that is, hold down Meta/Alt and press the period key). You can also
>> use "M-x racket-visit-definition". That opens the defining module and jumps
>> to the definition.
>>
>> If you have the `whereis` package installed, run the command `raco
>> whereis -b racket/base and` and it will print the path of the defining file.
>>
>> Ryan
>>
>>
>> On Sun, May 9, 2021 at 3:26 PM Tim Meehan  wrote:
>>
>>> Where in the repository are macros like "and" and "or" defined?
>>> I tried searching for "and" and "or" ... but you probably know how that
>>> worked out.
>>>
>>> Thanks folks!
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to racket-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/racket-users/CACgrOxK6S8EOAGk_rPbE%2B_wMLJiSbpwMhVd4AeRL8C9%2BDW3mgg%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/racket-users/CANy33q%3DsLEH-ooUJxTay6pG1GNcRLZDUotNJ23L1HRTC1XqHwA%40mail.gmail.com
>> 
>> .
>>
>
>
> --
> --
> Jens Axel Søgaard
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CABefVgzgqRPzx6ct6LrN-TAfE18vsdqnopun0B63LmqqKxerAQ%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAL3TdOOSKK_ae-JiObMc%2B7SJDYWZg%3Depw43HiD9oa%2B2kLTro7g%40mail.gmail.com.


Re: [racket-users] macros in Racket repository

2021-05-09 Thread Jens Axel Søgaard
Hi Tim,

In this case Ryan's method leads to:

https://github.com/racket/racket/blob/master/racket/collects/racket/private/qq-and-or.rkt#L440

But in case you are wondering about the style used in that file:
at the point where "qq-and-or.rkt" is used, none of the usual
syntax tools (such as syntax-case and syntax-parse) are available,
so it is written using only primitive constructs.

That is, that particular file does not represent the usual style of macro
writing.

/Jens Axel


Den søn. 9. maj 2021 kl. 16.13 skrev Ryan Culpepper :

> Here are the three most convenient ways I know of to find that information
> (which is "$RACKET/collects/racket/private/qq-and-or.rkt" in this specific
> case):
>
> If you use DrRacket, then open a file that uses `and`, right-click on an
> occurrence of `and`, and choose "Open Defining File" (which changes to
> "Jump to Definition (in Other File)" once DrRacket opens the file.
>
> If you use Emacs with racket-mode, go to an occurrence of `and` and hit
> "M-." (that is, hold down Meta/Alt and press the period key). You can also
> use "M-x racket-visit-definition". That opens the defining module and jumps
> to the definition.
>
> If you have the `whereis` package installed, run the command `raco whereis
> -b racket/base and` and it will print the path of the defining file.
>
> Ryan
>
>
> On Sun, May 9, 2021 at 3:26 PM Tim Meehan  wrote:
>
>> Where in the repository are macros like "and" and "or" defined?
>> I tried searching for "and" and "or" ... but you probably know how that
>> worked out.
>>
>> Thanks folks!
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to racket-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/racket-users/CACgrOxK6S8EOAGk_rPbE%2B_wMLJiSbpwMhVd4AeRL8C9%2BDW3mgg%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CANy33q%3DsLEH-ooUJxTay6pG1GNcRLZDUotNJ23L1HRTC1XqHwA%40mail.gmail.com
> 
> .
>


-- 
-- 
Jens Axel Søgaard

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CABefVgzgqRPzx6ct6LrN-TAfE18vsdqnopun0B63LmqqKxerAQ%40mail.gmail.com.


Re: [racket-users] macros in Racket repository

2021-05-09 Thread Ryan Culpepper
Here are the three most convenient ways I know of to find that information
(which is "$RACKET/collects/racket/private/qq-and-or.rkt" in this specific
case):

If you use DrRacket, then open a file that uses `and`, right-click on an
occurrence of `and`, and choose "Open Defining File" (which changes to
"Jump to Definition (in Other File)" once DrRacket opens the file.

If you use Emacs with racket-mode, go to an occurrence of `and` and hit
"M-." (that is, hold down Meta/Alt and press the period key). You can also
use "M-x racket-visit-definition". That opens the defining module and jumps
to the definition.

If you have the `whereis` package installed, run the command `raco whereis
-b racket/base and` and it will print the path of the defining file.

Ryan


On Sun, May 9, 2021 at 3:26 PM Tim Meehan  wrote:

> Where in the repository are macros like "and" and "or" defined?
> I tried searching for "and" and "or" ... but you probably know how that
> worked out.
>
> Thanks folks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CACgrOxK6S8EOAGk_rPbE%2B_wMLJiSbpwMhVd4AeRL8C9%2BDW3mgg%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CANy33q%3DsLEH-ooUJxTay6pG1GNcRLZDUotNJ23L1HRTC1XqHwA%40mail.gmail.com.


[racket-users] macros in Racket repository

2021-05-09 Thread Tim Meehan
Where in the repository are macros like "and" and "or" defined?
I tried searching for "and" and "or" ... but you probably know how that
worked out.

Thanks folks!

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CACgrOxK6S8EOAGk_rPbE%2B_wMLJiSbpwMhVd4AeRL8C9%2BDW3mgg%40mail.gmail.com.


Re: [racket-users] Macros expanding to require forms

2020-11-12 Thread Veit Heller
Hello,

This is very useful information, thank you! I still have trouble 
understanding _why_ this works, though; I guess I’m a little confused as to 
how the scope-flipping influences things.

For instance, I don’t quite understand why the example from the docs works, 
but if you define the module to import in the macro (no matter whether it’s 
wrapped in `syntax-local-introduce` or not, it won’t. Example:

```
(module example racket (provide require-mod)
   (define-syntax (require-mod stx)
 #`(begin
  (module mod racket (provide hi) (define (hi) 1))
  #,(syntax-local-introduce #'(require 'mod
)
(require-mod) ; works fine, without an error
(hi) ; is still undefined
```

I’m not sure that this is supposed to work, but I’m using these to 
highlight my lack of understanding for the big picture here. What is going 
on? What am I missing?

V
On Thursday, 12 November 2020 at 13:26:48 UTC+1 sorawe...@gmail.com wrote:

> See 
> https://docs.racket-lang.org/reference/stxtrans.html#%28def._%28%28quote._~23~25kernel%29._syntax-local-introduce%29%29
>  for 
> an example program that requires a library via a macro.
>
> On Thu, Nov 12, 2020 at 3:09 AM Veit Heller  wrote:
>
>>
>> Hello,
>>
>> I’m suspecting I’m getting a little turned around by phase levels and 
>> where `require` comes in: it seems to be impossible to write a macro that 
>> expands to a valid `require` (it doesn’t throw an error, but the functions 
>> aren’t defined either). What am I missing? At what phase level does 
>> `require` do its magic? And can I tap into this magic from within a macro?
>>
>> An example to illustrate my point:
>>
>> ```
>> (define-syntax require-distributed ()
>>   ((_)
>> (require racket/place/distributed))) ; could be anything, really
>> (require-distributed) ; expands and runs fine, but no symbols :(
>> ```
>>
>> I know that this is somewhat of an odd thing to want, and I understand if 
>> it’s not possible.
>>
>> V
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Racket Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to racket-users...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/a49ab428-ae49-45ce-9d16-8a77d27f5a85n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/ae330ba0-7f89-4e2d-aa0f-6ff518dc72b7n%40googlegroups.com.


Re: [racket-users] Macros expanding to require forms

2020-11-12 Thread Sorawee Porncharoenwase
See
https://docs.racket-lang.org/reference/stxtrans.html#%28def._%28%28quote._~23~25kernel%29._syntax-local-introduce%29%29
for
an example program that requires a library via a macro.

On Thu, Nov 12, 2020 at 3:09 AM Veit Heller  wrote:

>
> Hello,
>
> I’m suspecting I’m getting a little turned around by phase levels and
> where `require` comes in: it seems to be impossible to write a macro that
> expands to a valid `require` (it doesn’t throw an error, but the functions
> aren’t defined either). What am I missing? At what phase level does
> `require` do its magic? And can I tap into this magic from within a macro?
>
> An example to illustrate my point:
>
> ```
> (define-syntax require-distributed ()
>   ((_)
> (require racket/place/distributed))) ; could be anything, really
> (require-distributed) ; expands and runs fine, but no symbols :(
> ```
>
> I know that this is somewhat of an odd thing to want, and I understand if
> it’s not possible.
>
> V
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/a49ab428-ae49-45ce-9d16-8a77d27f5a85n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegsm45NnAdw-yAqNUPKnytasXuvV0yoNQiW6cb-xu43tVw%40mail.gmail.com.


[racket-users] Macros expanding to require forms

2020-11-12 Thread Veit Heller

Hello,

I’m suspecting I’m getting a little turned around by phase levels and where 
`require` comes in: it seems to be impossible to write a macro that expands 
to a valid `require` (it doesn’t throw an error, but the functions aren’t 
defined either). What am I missing? At what phase level does `require` do 
its magic? And can I tap into this magic from within a macro?

An example to illustrate my point:

```
(define-syntax require-distributed ()
  ((_)
(require racket/place/distributed))) ; could be anything, really
(require-distributed) ; expands and runs fine, but no symbols :(
```

I know that this is somewhat of an odd thing to want, and I understand if 
it’s not possible.

V

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/a49ab428-ae49-45ce-9d16-8a77d27f5a85n%40googlegroups.com.


Re: [racket-users] [racket users] Macros sharing data?

2020-10-21 Thread Kevin Forchione


> 
> Here’s a toy example. It generates an error, but hopefully conveys the idea 
> I’m trying to express. 
> 
> #lang racket
> 
> (require (for-syntax syntax/parse)
> racket/stxparam
> racket/stxparam-exptime)
> 
> (define-syntax-parameter mval 1)
> 
> (define-syntax (foo stx)
>  (syntax-parse stx
>[(_ m:number n:number)
> #'(syntax-parameterize ([mval m])
> (bar n))]))
> 
> (define-syntax (bar stx)
>  (syntax-parse stx
>[(_ n) #'(* (syntax-parameter-value mval) n)]))
> 
> (foo 3 5)

It dawned on me that since both computations take place at runtime I can simply 
use a parameter!

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/AC61E773-7468-4A1E-916A-F8939EDA5EDC%40gmail.com.


Re: [racket-users] [racket users] Macros sharing data?

2020-10-21 Thread Kevin Forchione



> On Oct 21, 2020, at 10:33 AM, William G Hatch  wrote:
> 
> On Wed, Oct 21, 2020 at 10:07:12AM -0700, Kevin Forchione wrote:
>>  Hi guys,
>> Suppose I have a macro that computes a value and then calls another macro in 
>> its template. Is there a way to share that data with the 2nd macro without 
>> passing it as an argument?
> 
> Take a look at syntax parameters and syntax-local-value.
> 
> Syntax parameters, somewhat similar to `parameterize` for runtime
> code, allow macros to have an implicit communication channel.  The
> macro that `syntax-parameterize`es sets a value for the given
> parameter which is usually accessed by simply using the syntax
> parameter in its template somewhere.  The syntax parameter may even be
> passed in to the macro as an argument and thus end up in the template.
> This is how `this` gets its value in the class macro -- it's a syntax
> parameter.
> 
> However, if you want an inner macro to be able to inspect the value
> given to a syntax parameter, it can use `syntax-parameter-value` to
> get the current value out and do something with it.
> 
> `syntax-local-value` is another useful tool for macros to communicate.
> When you `define-syntax` an identifier, you give it a transformer
> binding.  Usually the value is a procedure (a macro transformer), but
> it can actually be other data.  One popular type of data to use is a
> struct that implements `prop:procedure`, and can thus act as a macro
> transformer as well as a storage place for data (which can be
> inspected by the struct's procedure interface or by other code).  To
> access this data, you pass the identifier (as a syntax object) that
> you `define-syntax`ed to `syntax-local-value`.
> 
> Without knowing more about what you're trying to do I'm not sure what
> more concrete advice I can give, but syntax parameters and
> `syntax-local-value` are two of the killer features that make Racket's
> macro system so powerful, so I recommend anyone who wants to use
> macros to become familiar with them.


Here’s a toy example. It generates an error, but hopefully conveys the idea I’m 
trying to express. 

#lang racket

(require (for-syntax syntax/parse)
 racket/stxparam
 racket/stxparam-exptime)

(define-syntax-parameter mval 1)

(define-syntax (foo stx)
  (syntax-parse stx
[(_ m:number n:number)
 #'(syntax-parameterize ([mval m])
 (bar n))]))

(define-syntax (bar stx)
  (syntax-parse stx
[(_ n) #'(* (syntax-parameter-value mval) n)]))

(foo 3 5)

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/0B80489F-8F0A-4171-AAD6-646B11C585F3%40gmail.com.


Re: [racket-users] [racket users] Macros sharing data?

2020-10-21 Thread Ben Greenman
You can use syntax-local-value to communicate across macros.

Here's an example:
https://docs.racket-lang.org/syntax-parse-example/index.html?q=cross-macro#(mod-path._syntax-parse-example%2Fcross-macro-communication%2Fcross-macro-communication)


On 10/21/20, Kevin Forchione  wrote:
>   Hi guys,
> Suppose I have a macro that computes a value and then calls another macro in
> its template. Is there a way to share that data with the 2nd macro without
> passing it as an argument?
>
> Thanks!
> Kevin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/5425EC92-110D-47B4-BCFB-D2B6C09BBF32%40gmail.com.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAFUu9R5i5pNGVj14NuUM7j6dJfHS3TsFTt2D6kAxLRL%2B1rymGg%40mail.gmail.com.


[racket-users] [racket users] Macros sharing data?

2020-10-21 Thread Kevin Forchione
Hi guys,
Suppose I have a macro that computes a value and then calls another macro in 
its template. Is there a way to share that data with the 2nd macro without 
passing it as an argument?

Thanks!
Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/5425EC92-110D-47B4-BCFB-D2B6C09BBF32%40gmail.com.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Kevin Forchione



> On Jun 3, 2019, at 9:42 AM, Matthias Felleisen  wrote:
> 
> 
> 
>> On Jun 3, 2019, at 12:33 PM, Kevin Forchione  wrote:
>> 
>> Oh! I see! That solves the expansio issue! Wonderful. You’ve opened up a 
>> whole new set of possibilities for me. Thanks, Matthias.
> 
> 
> I am glad I sent my solution a second time :)

Me, too. For some reason the bundling of my emails appeared only to present 
your suggestion of using syntax-parameters and perusing of docs and not the 
code! It made your follow-up comments cryptic though I didn’t realize I was 
missing something until the “OR” in your prior email. Going back through the 
bundle the earlier code was there. The “Or” solution, however, brought home to 
me that you can nest the syntax-parameterize calls. :) 

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/34F49B72-5C89-4994-9288-43DB336E2DBE%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Matthias Felleisen



> On Jun 3, 2019, at 12:33 PM, Kevin Forchione  wrote:
> 
> Oh! I see! That solves the expansio issue! Wonderful. You’ve opened up a 
> whole new set of possibilities for me. Thanks, Matthias.


I am glad I sent my solution a second time :) 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/4727251D-33B3-4C88-A99E-F13F4E417AD6%40felleisen.org.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Kevin Forchione



> On Jun 3, 2019, at 9:09 AM, Matthias Felleisen  wrote:
> 
> 
> Or, 
> 
> #lang racket
> 
> (require (for-syntax ) racket/stxparam)
> 
> (define-syntax-parameter return (syntax-rules ()))
> (define-syntax-parameter false (syntax-rules ()))
> 
> (define-syntax fn
> (syntax-rules ()
>   [(_ (arg ...) body ...)
>(λ (arg ...)
>  (let/cc fn-exit
>(syntax-parameterize ([return (syntax-rules () [(_ x) (fn-exit x)])])
>  (syntax-parameterize ([false (syntax-rules () [(_) (return #f)])])
>body ...]))
> 
> ((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
> ((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f
> 

Oh! I see! That solves the expansio issue! Wonderful. You’ve opened up a whole 
new set of possibilities for me. Thanks, Matthias.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/F77FB88D-5D84-4F67-87F5-8F53717DFF61%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Matthias Felleisen


Or, 

#lang racket

(require (for-syntax ) racket/stxparam)

(define-syntax-parameter return (syntax-rules ()))
(define-syntax-parameter false (syntax-rules ()))

(define-syntax fn
 (syntax-rules ()
   [(_ (arg ...) body ...)
(λ (arg ...)
  (let/cc fn-exit
(syntax-parameterize ([return (syntax-rules () [(_ x) (fn-exit x)])])
  (syntax-parameterize ([false (syntax-rules () [(_) (return #f)])])
body ...]))

((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/B99E2A20-68EE-46C3-B8F4-2066F3C063CD%40felleisen.org.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Matthias Felleisen
#lang racket

(require (for-syntax ) racket/stxparam)

(define-syntax-parameter return (syntax-rules ()))

(define-syntax fn
  (syntax-rules ()
[(_ (arg ...) body ...)
 (λ (arg ...)
   (let/cc fn-exit
 (syntax-parameterize ([return (syntax-rules () [(_ x) (fn-exit x)])])
   body ...)))]))

(define-syntax false
  (syntax-rules ()
[(_) (return #f)]))


((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/18038548-A78C-487B-A2A8-7F1C864903BF%40felleisen.org.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Kevin Forchione



> On Jun 3, 2019, at 8:51 AM, Matthias Felleisen  wrote:
> 
> 
> My code run your examples. Whay is missinng? 
> 
Here’s what I’ve been trying to run.

#lang racket

(require (for-syntax racket/syntax)
 racket/stxparam)

(define-syntax-parameter false
  (lambda (stx)
  (raise-syntax-error (syntax-e stx) "can only be used inside lambda.")))

(define-syntax (fn stx)
  (syntax-case stx ()
[(_ (arg ...) body0 body ...)
 (with-syntax ([return (format-id #'body0 "~a" #'return)])
   #'(λ (arg ...) (let/cc return
(syntax-parameterize ([false #'return]) body0 body 
...]))

((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
((fn (x y) (when (< x y) (false)) y) 2 30)


Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CC4AB8E6-76E4-43C3-9D2D-497F7467B858%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Matthias Felleisen


My code run your examples. Whay is missinng? 



> On Jun 3, 2019, at 11:36 AM, Kevin Forchione  wrote:
> 
> 
> 
>> On Jun 3, 2019, at 4:54 AM, Alex Knauth  wrote:
>> 
>> 
>> 
>>> On Jun 3, 2019, at 1:02 AM, Kevin Forchione  wrote:
 On Jun 2, 2019, at 7:13 PM, Matthias Felleisen  
 wrote:
> On Jun 2, 2019, at 9:41 PM, Kevin Forchione  wrote:
> 
> Hi guys,
> I’ve been working with macros and let/cc and have a situation where I 
> have the 2nd macro bound to the let/cc return of the first. No idea how 
> I’d do that, but here’s an example of what I’m attempting to do:
> 
> #lang racket
> 
> (require (for-syntax racket/syntax))
> 
> (define-syntax (fn stx)
>  (syntax-case stx ()
>[(_ (arg ...) body0 body ...)
> (with-syntax ([return (format-id #'body0 "~a" #'return)])
>   #'(λ (arg ...) (let/cc return body0 body ...)))]))
> 
> (define-syntax (false stx)
>  (syntax-case stx ()
>[(_) #'(return #f)]))
> 
> ((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
> ((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f
> 
> My thought is that the 2nd macro doesn’t know about the 1st macro except 
> perhaps through the six of compile time… so that if I had define-syntax 
> working with a lambda I’d have all the pieces available in the (fn …) but 
> not sure what tools I might use to accomplish that.
> 
> Any ideas are appreciated!
 
 This situation calls for syntax-parameters in my mind: see docs, too — 
 Matthias
>>> 
>>> Hmmm. A cursory glance at some of the examples suggests I’ll have to do 
>>> some exploring on this. One issue that it looks like I’ll have to work out 
>>> is that “false” in my example needs to translate to “(return #f)” and I’m 
>>> not sure at this stage where a syntax-parameterize statement would be 
>>> positioned in the fn macro
>> 
>> It should go around the same set of expressions that the `return` identifier 
>> would have been unhygienically bound in. For this example, that means around 
>> `body0 body ...`, or in context:
>> 
>> (λ (arg ...) (let/cc k (syntax-parameterize ([return ( #'k )]) body0 
>> body ...)))
>> 
>>> (or how it would know to use it unless it could interrogate the body list 
>>> looking for that value. That might be what I’d need to do here…
>> 
>> I'm not sure what you mean by interrogate the body, but if you're referring 
>> to the syntax-parameterize needing access to the continuation bound by 
>> let/cc, then your right. That's why in the example above, the 
>> syntax-parameterize needs to be inside of the let/cc where `k` is bound, and 
>> the new parameter value needs to refer to `k`. Is that what you meant?
> 
> That’s what I was thinking, but the transformation would have to be something 
> more like:
> 
> (let/cc return
> (syntax-parameterize ([false #'(return #f)]) body0 
> body ...]))
> 
> And that doesn’t work. I imagine that would expand into ((return #f)). 
> 
> Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/4F2802CF-7FF0-4C44-8843-97D6FC448239%40felleisen.org.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Kevin Forchione


> On Jun 3, 2019, at 4:54 AM, Alex Knauth  wrote:
> 
> 
> 
>> On Jun 3, 2019, at 1:02 AM, Kevin Forchione > > wrote:
>>> On Jun 2, 2019, at 7:13 PM, Matthias Felleisen >> > wrote:
 On Jun 2, 2019, at 9:41 PM, Kevin Forchione >>> > wrote:
 
 Hi guys,
 I’ve been working with macros and let/cc and have a situation where I have 
 the 2nd macro bound to the let/cc return of the first. No idea how I’d do 
 that, but here’s an example of what I’m attempting to do:
 
 #lang racket
 
 (require (for-syntax racket/syntax))
 
 (define-syntax (fn stx)
  (syntax-case stx ()
[(_ (arg ...) body0 body ...)
 (with-syntax ([return (format-id #'body0 "~a" #'return)])
   #'(λ (arg ...) (let/cc return body0 body ...)))]))
 
 (define-syntax (false stx)
  (syntax-case stx ()
[(_) #'(return #f)]))
 
 ((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
 ((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f
 
 My thought is that the 2nd macro doesn’t know about the 1st macro except 
 perhaps through the six of compile time… so that if I had define-syntax 
 working with a lambda I’d have all the pieces available in the (fn …) but 
 not sure what tools I might use to accomplish that.
 
 Any ideas are appreciated!
>>> 
>>> This situation calls for syntax-parameters in my mind: see docs, too — 
>>> Matthias
>> 
>> Hmmm. A cursory glance at some of the examples suggests I’ll have to do some 
>> exploring on this. One issue that it looks like I’ll have to work out is 
>> that “false” in my example needs to translate to “(return #f)” and I’m not 
>> sure at this stage where a syntax-parameterize statement would be positioned 
>> in the fn macro
> 
> It should go around the same set of expressions that the `return` identifier 
> would have been unhygienically bound in. For this example, that means around 
> `body0 body ...`, or in context:
> 
> (λ (arg ...) (let/cc k (syntax-parameterize ([return ( #'k )]) body0 
> body ...)))
> 
>> (or how it would know to use it unless it could interrogate the body list 
>> looking for that value. That might be what I’d need to do here…
> 
> I'm not sure what you mean by interrogate the body, but if you're referring 
> to the syntax-parameterize needing access to the continuation bound by 
> let/cc, then your right. That's why in the example above, the 
> syntax-parameterize needs to be inside of the let/cc where `k` is bound, and 
> the new parameter value needs to refer to `k`. Is that what you meant?

That’s what I was thinking, but the transformation would have to be something 
more like:

(let/cc return
(syntax-parameterize ([false #'(return #f)]) body0 body 
...]))

And that doesn’t work. I imagine that would expand into ((return #f)). 

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/135285AD-843C-4258-8034-EF9F59E27EB0%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-03 Thread Alex Knauth


> On Jun 3, 2019, at 1:02 AM, Kevin Forchione  wrote:
>> On Jun 2, 2019, at 7:13 PM, Matthias Felleisen > > wrote:
>>> On Jun 2, 2019, at 9:41 PM, Kevin Forchione >> > wrote:
>>> 
>>> Hi guys,
>>> I’ve been working with macros and let/cc and have a situation where I have 
>>> the 2nd macro bound to the let/cc return of the first. No idea how I’d do 
>>> that, but here’s an example of what I’m attempting to do:
>>> 
>>> #lang racket
>>> 
>>> (require (for-syntax racket/syntax))
>>> 
>>> (define-syntax (fn stx)
>>>  (syntax-case stx ()
>>>[(_ (arg ...) body0 body ...)
>>> (with-syntax ([return (format-id #'body0 "~a" #'return)])
>>>   #'(λ (arg ...) (let/cc return body0 body ...)))]))
>>> 
>>> (define-syntax (false stx)
>>>  (syntax-case stx ()
>>>[(_) #'(return #f)]))
>>> 
>>> ((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
>>> ((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f
>>> 
>>> My thought is that the 2nd macro doesn’t know about the 1st macro except 
>>> perhaps through the six of compile time… so that if I had define-syntax 
>>> working with a lambda I’d have all the pieces available in the (fn …) but 
>>> not sure what tools I might use to accomplish that.
>>> 
>>> Any ideas are appreciated!
>> 
>> This situation calls for syntax-parameters in my mind: see docs, too — 
>> Matthias
> 
> Hmmm. A cursory glance at some of the examples suggests I’ll have to do some 
> exploring on this. One issue that it looks like I’ll have to work out is that 
> “false” in my example needs to translate to “(return #f)” and I’m not sure at 
> this stage where a syntax-parameterize statement would be positioned in the 
> fn macro

It should go around the same set of expressions that the `return` identifier 
would have been unhygienically bound in. For this example, that means around 
`body0 body ...`, or in context:

(λ (arg ...) (let/cc k (syntax-parameterize ([return ( #'k )]) body0 
body ...)))

> (or how it would know to use it unless it could interrogate the body list 
> looking for that value. That might be what I’d need to do here…

I'm not sure what you mean by interrogate the body, but if you're referring to 
the syntax-parameterize needing access to the continuation bound by let/cc, 
then your right. That's why in the example above, the syntax-parameterize needs 
to be inside of the let/cc where `k` is bound, and the new parameter value 
needs to refer to `k`. Is that what you meant?

Alex Knauth

> Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/ECF4DCD1-C651-42BC-8138-5314640F2862%40knauth.org.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-02 Thread Kevin Forchione


> On Jun 2, 2019, at 7:13 PM, Matthias Felleisen  wrote:
> 
> 
> 
>> On Jun 2, 2019, at 9:41 PM, Kevin Forchione > > wrote:
>> 
>> Hi guys,
>> I’ve been working with macros and let/cc and have a situation where I have 
>> the 2nd macro bound to the let/cc return of the first. No idea how I’d do 
>> that, but here’s an example of what I’m attempting to do:
>> 
>> #lang racket
>> 
>> (require (for-syntax racket/syntax))
>> 
>> (define-syntax (fn stx)
>>  (syntax-case stx ()
>>[(_ (arg ...) body0 body ...)
>> (with-syntax ([return (format-id #'body0 "~a" #'return)])
>>   #'(λ (arg ...) (let/cc return body0 body ...)))]))
>> 
>> (define-syntax (false stx)
>>  (syntax-case stx ()
>>[(_) #'(return #f)]))
>> 
>> ((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
>> ((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f
>> 
>> My thought is that the 2nd macro doesn’t know about the 1st macro except 
>> perhaps through the six of compile time… so that if I had define-syntax 
>> working with a lambda I’d have all the pieces available in the (fn …) but 
>> not sure what tools I might use to accomplish that.
>> 
>> Any ideas are appreciated!
>  
> 
> This situation calls for syntax-parameters in my mind: see docs, too — 
> Matthias

Hmmm. A cursory glance at some of the examples suggests I’ll have to do some 
exploring on this. One issue that it looks like I’ll have to work out is that 
“false” in my example needs to translate to “(return #f)” and I’m not sure at 
this stage where a syntax-parameterize statement would be positioned in the fn 
macro (or how it would know to use it unless it could interrogate the body list 
looking for that value. That might be what I’d need to do here…

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/C4425B12-9C1F-4159-B4BA-D09C89419CB9%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros and let/cc

2019-06-02 Thread Matthias Felleisen


> On Jun 2, 2019, at 9:41 PM, Kevin Forchione  wrote:
> 
> Hi guys,
> I’ve been working with macros and let/cc and have a situation where I have 
> the 2nd macro bound to the let/cc return of the first. No idea how I’d do 
> that, but here’s an example of what I’m attempting to do:
> 
> #lang racket
> 
> (require (for-syntax racket/syntax))
> 
> (define-syntax (fn stx)
>  (syntax-case stx ()
>[(_ (arg ...) body0 body ...)
> (with-syntax ([return (format-id #'body0 "~a" #'return)])
>   #'(λ (arg ...) (let/cc return body0 body ...)))]))
> 
> (define-syntax (false stx)
>  (syntax-case stx ()
>[(_) #'(return #f)]))
> 
> ((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
> ((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f
> 
> My thought is that the 2nd macro doesn’t know about the 1st macro except 
> perhaps through the six of compile time… so that if I had define-syntax 
> working with a lambda I’d have all the pieces available in the (fn …) but not 
> sure what tools I might use to accomplish that.
> 
> Any ideas are appreciated!
 

This situation calls for syntax-parameters in my mind: see docs, too — Matthias



#lang racket

(require (for-syntax ) racket/stxparam)

(define-syntax-parameter return (syntax-rules ()))

(define-syntax fn
  (syntax-rules ()
[(_ (arg ...) body ...)
 (λ (arg ...)
   (let/cc fn-exit
 (syntax-parameterize ([return (syntax-rules () [(_ x) (fn-exit x)])])
   body ...)))]))

(define-syntax false
  (syntax-rules ()
[(_) (return #f)]))


((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/7A495B17-F49A-41BE-8DED-8EC34D4FE61F%40felleisen.org.
For more options, visit https://groups.google.com/d/optout.


[racket-users] macros and let/cc

2019-06-02 Thread Kevin Forchione
Hi guys,
I’ve been working with macros and let/cc and have a situation where I have the 
2nd macro bound to the let/cc return of the first. No idea how I’d do that, but 
here’s an example of what I’m attempting to do:

#lang racket

(require (for-syntax racket/syntax))

(define-syntax (fn stx)
  (syntax-case stx ()
[(_ (arg ...) body0 body ...)
 (with-syntax ([return (format-id #'body0 "~a" #'return)])
   #'(λ (arg ...) (let/cc return body0 body ...)))]))

(define-syntax (false stx)
  (syntax-case stx ()
[(_) #'(return #f)]))

((fn (x y) (when (< x y) (return x)) y) 2 30) ; => 2
((fn (x y) (when (< x y) (false)) y) 2 30)   ;=> #f

My thought is that the 2nd macro doesn’t know about the 1st macro except 
perhaps through the six of compile time… so that if I had define-syntax working 
with a lambda I’d have all the pieces available in the (fn …) but not sure what 
tools I might use to accomplish that.

Any ideas are appreciated!

Kevin



-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/957E342B-2DCB-4413-9577-8B9F3B1F66BD%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros crossing documentation-time and run-time phases

2019-03-21 Thread Matthew Flatt
At Mon, 18 Mar 2019 19:32:40 -0400, Philip McGrath wrote:
> On Mon, Mar 18, 2019 at 9:14 AM Matthew Flatt  wrote:
> 
> > I wonder whether the solution is an extension of `scribble/lp2` to
> > support a `require-for-chunk` form where `chunk` records any such
> > imports in its context and adds then to the stitched-together program.
> >
> >  (require-for-chunk (only-in racket/string string-join))
> >
> >  (define-syntax-parser cross-phase-macro
> >[(_ <>:id)
> > @#`begin{@(func-not-exported)
> >
> >  @chunk[<>
> > (string-join 
> >
> > would work because `chunk` would pick up the `(only-in racket/string
> > string-join)` from its context and attach it to the chunk, so that
> > `(only-in racket/string string-join)` would be added to the
> > stitched-together context.
> >
> > Maybe this would work by having `require-for-chunk` expand to something
> > like `(begin-for-syntax (register-require #'chunk ))` to register
> > in a compile-time free-id table mapping from the #'chunk identifier to
> > a list of imports. Then, the `chunk` macro would consult the table
> > using the identifier that invoked the macro. (I have not tried this to
> > be sure that it would work.)
> >
> 
> While thinking about this idea, I came up with a simpler example that
> exposes a hygiene issue in `scribble/lp2` without using `require`:
> 
> #lang scribble/lp2
> @(define-syntax-rule (chunk/define <> lhs rhs ...)
>(chunk <>
>   (define hygiene "this shouldn't cause capture")
>   (define lhs rhs ...)))
> @(chunk <*>  )
> @(chunk/define  a 1)
> @(chunk/define  b 2)
> 
> fails with the error "module: identifier already defined in: hygiene".

I can see why you might want this, but for what it's worth, I can also
see an argument against for `scribble/lp2`. If it did work, the typeset
output would be

 <*> ::=
 
 

  ::=
 (define hygiene "this shouldn't cause capture")
 (define a 1)

  ::=
 (define hygiene "this shouldn't cause capture")
 (define b 2)

which seems like nonsense. That is, there's not an extra dimension to
represent the macro-introduced scope in the textual output output, so
maybe it shouldn't be allowed.

Probably it's possible to make this work using delta introducers to
manipulate scopes, though.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros crossing documentation-time and run-time phases

2019-03-18 Thread Philip McGrath
On Mon, Mar 18, 2019 at 9:14 AM Matthew Flatt  wrote:

> I wonder whether the solution is an extension of `scribble/lp2` to
> support a `require-for-chunk` form where `chunk` records any such
> imports in its context and adds then to the stitched-together program.
>
>  (require-for-chunk (only-in racket/string string-join))
>
>  (define-syntax-parser cross-phase-macro
>[(_ <>:id)
> @#`begin{@(func-not-exported)
>
>  @chunk[<>
> (string-join 
>
> would work because `chunk` would pick up the `(only-in racket/string
> string-join)` from its context and attach it to the chunk, so that
> `(only-in racket/string string-join)` would be added to the
> stitched-together context.
>
> Maybe this would work by having `require-for-chunk` expand to something
> like `(begin-for-syntax (register-require #'chunk ))` to register
> in a compile-time free-id table mapping from the #'chunk identifier to
> a list of imports. Then, the `chunk` macro would consult the table
> using the identifier that invoked the macro. (I have not tried this to
> be sure that it would work.)
>

While thinking about this idea, I came up with a simpler example that
exposes a hygiene issue in `scribble/lp2` without using `require`:

#lang scribble/lp2
@(define-syntax-rule (chunk/define <> lhs rhs ...)
   (chunk <>
  (define hygiene "this shouldn't cause capture")
  (define lhs rhs ...)))
@(chunk <*>  )
@(chunk/define  a 1)
@(chunk/define  b 2)

fails with the error "module: identifier already defined in: hygiene".

I would have liked it to work analogously to this:

#lang racket
(define-syntax-rule (module+/define mod lhs rhs ...)
  (module+ mod
(define hygiene "this shouldn't cause capture")
(define lhs rhs ...)))
(module+/define main a 1)
(module+/define main b 2)

-Philip

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros crossing documentation-time and run-time phases

2019-03-18 Thread Matthew Flatt
Just to make sure we're on the same page, it seems like your example
fails for the same reason that

 #lang scribble/lp2
 @(require racket/string)
 @(chunk <*> string-join)

fails. That `require` turns out to do nothing, and there's no binding
of `string-join` in the "phase" of the program is that stitched
together by the chunks.

In this single-file example, the solution could be

 #lang scribble/lp2
 @(chunk  (require racket/string))
 @(chunk <*>  string-join)

but that puts `require` text in the generated document, which could be
undesirable in some context. Even if including the `require` was ok,
changing your original example to

 

 (chunk  (require (only-in racket/string string-join)))

 (define-syntax-parser cross-phase-macro
   [(_ <>:id)
@#`begin{@(func-not-exported)
 
 @chunk[<> 
(string-join
 '("string-join is not provided"
   "at runtime"
   "by #lang scribble/lp2"))]
 }])

does not work, because `chunk` binding doesn't work that way.


I wonder whether the solution is an extension of `scribble/lp2` to
support a `require-for-chunk` form where `chunk` records any such
imports in its context and adds then to the stitched-together program.

 

 (require-for-chunk (only-in racket/string string-join))

 (define-syntax-parser cross-phase-macro
   [(_ <>:id)
@#`begin{@(func-not-exported)
 
 @chunk[<>
(string-join 

would work because `chunk` would pick up the `(only-in racket/string
string-join)` from its context and attach it to the chunk, so that
`(only-in racket/string string-join)` would be added to the
stitched-together context.

Maybe this would work by having `require-for-chunk` expand to something
like `(begin-for-syntax (register-require #'chunk ))` to register
in a compile-time free-id table mapping from the #'chunk identifier to
a list of imports. Then, the `chunk` macro would consult the table
using the identifier that invoked the macro. (I have not tried this to
be sure that it would work.)


At Mon, 18 Mar 2019 02:20:50 -0400, Philip McGrath wrote:
> Racket makes it easy to write macros that mix run-time code with various
> compile-time phases, using lexical scope to refer unambiguously to the
> correct binding for each identifier, including bindings that are not
> directly exported. Macro-generating macros are a great example, in that
> they expand to code at several different phases.
> 
> When trying to write macros that interleave documentation-time and run-time
> phases, I haven't been able to find the same elegance. I'm going to
> demonstrate one of the problems I've found using `#lang scribble/lp2`, but
> I really encountered these issues while implementing the specification
> #lang I've made for Digital Ricoeur: my current implementation is based
> quite closely on `scribble/lp2`.
> 
> Imagine a library module "lib.rkt" from which you want to export a
> `cross-phase-macro`, which expands to both documentation-time and run-time
> code for your literate programs. Here's an initial attempt: (The code for
> this example is up at
> https://github.com/LiberalArtist/doc-lang-experiments/tree/1de75817866d9d782da7
> 5f1799daeb5eb26e6fe1/lp2-example1
> .)
> #lang at-exp racket/base
> 
> (provide cross-phase-macro)
> 
> (require (only-in racket/string string-join)
>  (only-in scribble/lp2 chunk)
>  syntax/parse/define
>  (for-syntax racket/base))
> 
> (define (func-not-exported)
>   "This is the result of func-not-exported.")
> 
> (define-syntax-parser cross-phase-macro
>   [(_ <>:id)
>@#`begin{@(func-not-exported)
> 
> @chunk[<>
>(string-join
> '("string-join is not provided"
>   "at runtime"
>   "by #lang scribble/lp2"))]
> }])
> 
> When you try to use `cross-phase-macro` from a literate program
> "program.scrbl":
> #lang scribble/lp2
> 
> @(require "lib.rkt")
> 
> @cross-phase-macro[<*>]
> you get an unbound identifier error complaining about the use of
> `string-join`.
> 
> The reason for the error is clear: `chunk` bodies are stitched together
> into a `doc` submodule in the `racket/base` language, and `racket/base`
> doesn't include `string-join`. In essence, `string-join` isn't bound at the
> right phase. If this were a macro-generating macro—imagine `define-syntax`
> instead of `chunk`—the solution would be equally clear: use a `for-syntax`
> require or a `begin-for-syntax` block to bind `string-join` at
> compile-time, and the use of the identifier in the syntax template will
> resolve to the right binding, even without explicitly exporting
> `string-join`.
> 
> I have yet to find a way to do the equivalent thing when mixing run-time
> and documentation-time phases, rather than run-time and compile-time(s).
> 
> Some of the objectives and implementation details of `scribble/lp2` (shared
> by my specification language) seem to make this problem harder. The
> run-time phase shouldn't have a dependency on 

[racket-users] Macros crossing documentation-time and run-time phases

2019-03-18 Thread Philip McGrath
Racket makes it easy to write macros that mix run-time code with various
compile-time phases, using lexical scope to refer unambiguously to the
correct binding for each identifier, including bindings that are not
directly exported. Macro-generating macros are a great example, in that
they expand to code at several different phases.

When trying to write macros that interleave documentation-time and run-time
phases, I haven't been able to find the same elegance. I'm going to
demonstrate one of the problems I've found using `#lang scribble/lp2`, but
I really encountered these issues while implementing the specification
#lang I've made for Digital Ricoeur: my current implementation is based
quite closely on `scribble/lp2`.

Imagine a library module "lib.rkt" from which you want to export a
`cross-phase-macro`, which expands to both documentation-time and run-time
code for your literate programs. Here's an initial attempt: (The code for
this example is up at
https://github.com/LiberalArtist/doc-lang-experiments/tree/1de75817866d9d782da75f1799daeb5eb26e6fe1/lp2-example1
.)
#lang at-exp racket/base

(provide cross-phase-macro)

(require (only-in racket/string string-join)
 (only-in scribble/lp2 chunk)
 syntax/parse/define
 (for-syntax racket/base))

(define (func-not-exported)
  "This is the result of func-not-exported.")

(define-syntax-parser cross-phase-macro
  [(_ <>:id)
   @#`begin{@(func-not-exported)

@chunk[<>
   (string-join
'("string-join is not provided"
  "at runtime"
  "by #lang scribble/lp2"))]
}])

When you try to use `cross-phase-macro` from a literate program
"program.scrbl":
#lang scribble/lp2

@(require "lib.rkt")

@cross-phase-macro[<*>]
you get an unbound identifier error complaining about the use of
`string-join`.

The reason for the error is clear: `chunk` bodies are stitched together
into a `doc` submodule in the `racket/base` language, and `racket/base`
doesn't include `string-join`. In essence, `string-join` isn't bound at the
right phase. If this were a macro-generating macro—imagine `define-syntax`
instead of `chunk`—the solution would be equally clear: use a `for-syntax`
require or a `begin-for-syntax` block to bind `string-join` at
compile-time, and the use of the identifier in the syntax template will
resolve to the right binding, even without explicitly exporting
`string-join`.

I have yet to find a way to do the equivalent thing when mixing run-time
and documentation-time phases, rather than run-time and compile-time(s).

Some of the objectives and implementation details of `scribble/lp2` (shared
by my specification language) seem to make this problem harder. The
run-time phase shouldn't have a dependency on the documentation-time phase,
which would pull in Scribble etc. Likewise, the documentation-time phase
shouldn't import the run-time phase, other than perhaps `for-label`. The
`#%module-begin` implementation uses complicated tricks to expand the
documentation-time phase before the run-time phase, even though the
documentation-time phase becomes a `module*` submodule, because it has to
discover the run-time code (uses of `chunk` or, for my language,
`begin-for-runtime`) inside of the documentation-time code. The
implementation actually expands the documentation-time phase twice, and it
uses `strip-context` in a few places, which seems like it would undo any
fancy scope manipulation that I might otherwise look to as the beginning of
a solution.

Currently, I work around this in my specification language by manually
arranging to have the language provide any bindings I need to expand to at
run-time, but this is fragile and aesthetically displeasing. Actually, I
would say the same about expanding the documentation-time phase twice (and,
in my case, with observable differences, e.g. to get `for label` requires
working thanks to an earlier post on this list
).
It seems like there must be a better way to do this, and I'm wondering if
anyone has ideas. With my specification language, in particular, I have the
freedom to completely change the implementation if I can get to a better
place.

Since much of this email has been about problems, I want to close by
reiterating that our specification #lang has been a big win for us at
Digital Ricoeur. While there are things about the implementation of the
#lang that I'd like to improve, using the #lang has reduced bugs, greatly
aided maintainability, and kept our prose documents in sync with various
layers of code. Part of my motivation for trying to make the implementation
more robust is that it has been so useful to us that I'd like to make the
core more reusable for others.


-Philip

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 

Re: [racket-users] Macros that conditionally generate code

2019-03-07 Thread Greg Hendershott
This reminds me of a similar thread on Slack yesterday.

When a macro `define`s something, it's usually better for everyone if
the identifier is supplied to the macro. It's nicer for you as the
macro writer because you don't need to think so hard about scope and
hygiene. And it's nicer for the user of the macro, not to have
identifiers injected.

It's usually OK to bend this rule, and make new identifiers based off
one supplied to the macro. For example, `(struct foo (bar))` will
define a `foo-bar` accessor. If you do something similar, using
format-id correctly to make `foo-bar` given `foo`, it should
"just-work". The format-id docs have a simple first example:
https://docs.racket-lang.org/reference/syntax-util.html#(def._((lib._racket%2Fsyntax..rkt)._format-id))

So I usually try to start on that happy, simple path.

On Wed, Mar 6, 2019 at 11:42 PM David Storrs  wrote:
>
> On Wed, Mar 6, 2019 at 11:31 PM Sorawee Porncharoenwase
>  wrote:
> >
> > Isn’t this because of hygienity in general?
>
> *headdesk*
>
> Of course.  Thanks, I should have recognized that.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that conditionally generate code

2019-03-06 Thread David Storrs
On Wed, Mar 6, 2019 at 11:31 PM Sorawee Porncharoenwase
 wrote:
>
> Isn’t this because of hygienity in general?

*headdesk*

Of course.  Thanks, I should have recognized that.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that conditionally generate code

2019-03-06 Thread Sorawee Porncharoenwase
Isn’t this because of hygienity in general? For example:

#lang racket

(define-syntax (foo stx)
  (syntax-case stx ()
[(_) #'(define doubleup 1)]))

(foo)
doubleup ; unbound id

doesn’t define doubleup.

While the below code does:

#lang racket

(define-syntax (foo stx)
  (syntax-case stx ()
[(_)
 (with-syntax ([name (datum->syntax stx 'doubleup)])
   #'(define name 1))]))

(foo)
doubleup




On Wed, Mar 6, 2019 at 7:57 PM David Storrs  wrote:

> I would like to write a macro that accepts an optional keyword
> argument.  If the argument is missing, or if it is present and has a
> true value, then some code will be generated.  Otherwise, nothing is
> done.  This was my best attempt:
>
> #lang racket
> (require (for-syntax syntax/parse))
> (define-syntax (foo stx)
>   (syntax-parse stx
> [(foo name val (~optional (~seq #:make-double? make-double?:boolean)))
>  (with-syntax ([func (if (syntax->datum #'(~? make-double? #'#t))
>  #'(define (doubleup) (* 2 val))
>  #'(begin))])
>#'func)]))
>
> (foo x 6 #:make-double? #t)
> doubleup
>
>
> No joy.  doubleup is never defined, no matter what happens with the
> keyword.  I'm not sure what I'm doing wrong -- any advice?
>
> More generally, this feels like something for which there should be a
> simpler solution.  I think that ~? isn't quite what I'm looking for,
> since the thing that I'm testing for defined/truth isn't going to be
> used in the code that will actually be generated. What should I be
> using?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Matthew Butterick

> On Sep 17, 2018, at 11:57 AM, Kevin Forchione  wrote:
> 
> In a nutshell I’m working with some hash tables whose keys are symbol and 
> whose values may be other keys or values such as identifiers, and I got a bit 
> tired of quoting all my symbols for functions and decided to use some macros 
> when working with the tables.

OK. Yes, in general you need a macro to convert something like `(id bar)` to 
`(other-id 'bar)`, because otherwise the naked `bar` is treated as an 
identifier (and therefore as usual needs a binding, or the program won't run). 
For even more notational convenience, you might also look at the `read-cdot` 
parameter and `#%dot` identifier, which can expand `table.key` to something 
like `(hash-ref table 'key)`.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Kevin Forchione


> On Sep 16, 2018, at 10:07 PM, Matthew Butterick  wrote:
> 
> 
>> On Sep 16, 2018, at 2:13 PM, Kevin Forchione > > wrote:
>> 
>> Thanks! That’s just what I wanted. Is there a way in Racket to determine if 
>> a quoted symbol has an associated procedure? 
> 
> 
> 
> #lang racket
> (require rackunit)
> 
> (define-syntax (bound-to-proc? stx)
>   (syntax-case stx ()
> [(_ 'x)
>  (and (identifier? #'x) (identifier-binding #'x))
>  #'(procedure? x)]
> [_ #'#f]))
> 
> (define bound-to-proc +)
> (define not-bound-to-proc 42)
> 
> (check-true (bound-to-proc? 'bound-to-proc))
> (check-false (bound-to-proc? 'not-bound-to-proc))

Sorry for the typo and thanks for the further explanation. Your original 
response to my query was more in line with what I’m trying to accomplish, while 
my second question was more in the realm of  curiosity. In a nutshell I’m 
working with some hash tables whose keys are symbol and whose values may be 
other keys or values such as identifiers, and I got a bit tired of quoting all 
my symbols for functions and decided to use some macros when working with the 
tables. Probably more than curiosity as I can foresee a need to retrieve a 
value from a table and either apply it, which means somehow getting at the 
binding. 

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Alexis King
> On Sep 17, 2018, at 12:21, Kevin Forchione  wrote:
> 
> That seems to be the nature of macros, and I’m not sure what the solution to 
> that paradox is, apart from perhaps building a symbol/function hash table as 
> part of a define. Presumably Racke does something like that for eva & 
> namespaces, but I’m surprised there isn’t a symbol->procedure function. 

In general, I think Philip has hit the nail on the head. I, too, thought of 
other Lisp systems when you used the wording “associated procedure” for a 
symbol, and I agree that concept makes less sense in Racket. In a more 
traditional Lisp, symbols themselves are rich values with lots of information 
attached to them, but Racket follows the Scheme approach, which treats symbols 
as nothing more than interned strings. They are just names, not bindings.

To elaborate a little more on this, it might make sense to think of the 
“associated procedure” for a symbol in Common Lisp, but in Racket, that 
information is stored in a separate place: namespaces. A Racket namespace 
represents an environment, and it maps names to values. At runtime, if you want 
to get the value bound to a particular symbol in a given namespace, you can use 
namespace-variable-value[1], but there are some caveats to that.

As mentioned above, in Common Lisp, a package-qualified symbol is precise, but 
a Racket symbol is only meaningful in context. This means that the symbol 'foo 
might be mapped to one thing in one namespace but something completely 
different in another namespace. This is a philosophically different approach to 
binding, but the practical takeaway is that you need to be really careful to 
use the *right* namespace if you want to use namespaces in Racket. A namespace 
can be created that corresponds to the top-level of a module, using 
module->namespace, or it can be an entirely independent, “free-floating” 
top-level environment created with make-base-namespace. You can acquire a 
namespace that corresponds to the surrounding lexical environment using 
define-namespace-anchor[2] and namespace-anchor->namespace[3] or 
#%variable-reference[4] and variable-reference->namespace[5].

However, beware! As Philip alludes to, namespaces only include top-level or 
module-level bindings, *not* local bindings! Racket provides no reflective 
capabilities to inspect local bindings (at least not in general; you could 
theoretically write a #lang that implements this capability yourself). These 
are opaque, and indeed, the Racket compiler does not maintain information about 
the structure of local bindings.

To summarize: Racket is a relatively static language relative to other Lisps, 
and even to other Schemes. The language is entirely {lexically, statically} 
bound. It provides limited reflective capabilities to manipulate top-level 
namespaces and inspect module-level bindings, but no more. There are definitely 
various advantages to this (e.g. local reasoning, encapsulation, optimization), 
but you may need to think differently about Racket’s binding model if coming 
from other Lisp systems.

Alexis

[1]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28def._%28%28quote._~23~25kernel%29._namespace-variable-value%29%29
[2]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-namespace-anchor%29%29
[3]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._namespace-anchor-~3enamespace%29%29
[4]: 
http://docs.racket-lang.org/reference/Locationsvariable-reference.html#%28form._%28%28quote._~23~25kernel%29._~23~25variable-reference%29%29
[5]: 
http://docs.racket-lang.org/reference/Namespaces.html#%28def._%28%28quote._~23~25kernel%29._variable-reference-~3enamespace%29%29

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Matthew Butterick

> On Sep 17, 2018, at 10:21 AM, Kevin Forchione  wrote:
> 
> That seems to be the nature of macros, and I’m not sure what the solution to 
> that paradox is, apart from perhaps building a symbol/function hash table as 
> part of a define. Presumably Racke does something like that for eva & 
> namespaces, but I’m surprised there isn’t a symbol->procedure function. 


I take a question as I find it, even if its necessity is not clear to me ;) It 
seems a teeny bit possible that you're moving against the grain of how 
identifiers & bindings usually work, and thus certain aspects seem more 
complicated than they need to. 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Philip McGrath
It might help to know a bit more context about what you're trying to do.

I think other Lisp-family languages use these terms in different ways, but
in Racket it isn't usual to talk about a symbol being bound to something. A
symbol is just a type of value (like an integer, a list, a vector, a
function, or anything else) and exists only at the run-time of some code.
Binding, on the other hand, is a compile-time notion. Functions like
`identifier-binding` operate on identifiers, which are syntax objects that
wrap symbols together with lexical context and source-location information:
in other words, the first argument to `identifier-binding` must satisfy
`identifier?`, which means it will never satisfy `symbol?`. Given renaming,
lexical scope, and other features, a symbol isn't enough information to
specify a binding, and I believe that at run-time symbolic names go away
and are replaced by some form of de Bruijn indexes. (Take that last part
with a grain of salt: I don't spend much time with low-level runtime
details.) Also, in `#lang racket`, given that there isn't a static type
system, the question of whether an identifier is bound to a procedure isn't
answerable at compile-time: Matthew's macro thus expands to false for
unbound identifiers, but expands to a `procedure?` check at runtime for all
bound identifiers.

At the same time, if you explain what you are trying to do at a
higher-level, there very well may be a way to do it that involves
implementation techniques you wouldn't think to ask about. For example, if
you want unbound identifiers to be self-quoting as symbols, the answer
would involve defining a macro named `#%top`.

Finally, I'm not sure that I understand your example:

> [define [foo symbols) (bound-to-proc? sum))
>
Ignoring the mismatched parentheses, this use of `bound-to-proc?` doesn't
match the first clause of Matthew's `syntax-case` because there is no
`quote`, and it looks like Matthew tried to implement your request to
determine "if a quoted symbol has an associated procedure".

(The implementation of `bound-to-proc?` would be more robust if
`syntax-case` were given `quote` as a literal, but that way lies madness:
what about `quasiquote`, or macros you don't know about that expand to
symbols? Using `local-expand` could help … but, again, the best thing would
be to know more about what you're ultimately trying to do.)

-Philip


On Mon, Sep 17, 2018 at 12:21 PM Kevin Forchione  wrote:

>
>
> On Sep 16, 2018, at 10:07 PM, Matthew Butterick  wrote:
>
> #lang racket
> (require rackunit)
>
> (define-syntax (bound-to-proc? stx)
>   (syntax-case stx ()
> [(_ 'x)
>  (and (identifier? #'x) (identifier-binding #'x))
>  #'(procedure? x)]
> [_ #'#f]))
>
> (define bound-to-proc +)
> (define not-bound-to-proc 42)
>
> (check-true (bound-to-proc? 'bound-to-proc))
> (check-false (bound-to-proc? 'not-bound-to-proc))
>
>
> Thanks, Matthew. This approach works as long as the macro is being used
> outside of a definition, but I suspect to build any sort of logic around it
> they’d have to be macros as well. For instance, this fails because the
> macro captures symptoms and not the value passed to the function:
>
> [define [foo symbols) (bound-to-proc? sum))
>
> That seems to be the nature of macros, and I’m not sure what the solution
> to that paradox is, apart from perhaps building a symbol/function hash
> table as part of a define. Presumably Racke does something like that for
> eva & namespaces, but I’m surprised there isn’t a symbol->procedure
> function.
>
> Kevin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-17 Thread Kevin Forchione


> On Sep 16, 2018, at 10:07 PM, Matthew Butterick  wrote:
> 
> #lang racket
> (require rackunit)
> 
> (define-syntax (bound-to-proc? stx)
>   (syntax-case stx ()
> [(_ 'x)
>  (and (identifier? #'x) (identifier-binding #'x))
>  #'(procedure? x)]
> [_ #'#f]))
> 
> (define bound-to-proc +)
> (define not-bound-to-proc 42)
> 
> (check-true (bound-to-proc? 'bound-to-proc))
> (check-false (bound-to-proc? 'not-bound-to-proc))

Thanks, Matthew. This approach works as long as the macro is being used outside 
of a definition, but I suspect to build any sort of logic around it they’d have 
to be macros as well. For instance, this fails because the macro captures 
symptoms and not the value passed to the function:

[define [foo symbols) (bound-to-proc? sum)) 

That seems to be the nature of macros, and I’m not sure what the solution to 
that paradox is, apart from perhaps building a symbol/function hash table as 
part of a define. Presumably Racke does something like that for eva & 
namespaces, but I’m surprised there isn’t a symbol->procedure function. 

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-16 Thread Matthew Butterick

> On Sep 16, 2018, at 2:13 PM, Kevin Forchione  > wrote:
> 
> Thanks! That’s just what I wanted. Is there a way in Racket to determine if a 
> quoted symbol has an associated procedure? 



#lang racket
(require rackunit)

(define-syntax (bound-to-proc? stx)
  (syntax-case stx ()
[(_ 'x)
 (and (identifier? #'x) (identifier-binding #'x))
 #'(procedure? x)]
[_ #'#f]))

(define bound-to-proc +)
(define not-bound-to-proc 42)

(check-true (bound-to-proc? 'bound-to-proc))
(check-false (bound-to-proc? 'not-bound-to-proc))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-16 Thread Kevin Forchione


> On Sep 15, 2018, at 7:27 PM, Philip McGrath  wrote:
> 
> Sure. For example:
> 
> #lang racket
> 
> (require syntax/parse/define
>  rackunit)
> 
> (define-syntax-parser foo
>   [(_ arg:id)
>#''arg]
>   [(_ arg:expr)
>#'arg])
> 
> (check-eqv? (foo 10) 10)
> (check-eq? (foo hello) 'hello)
> (check-pred procedure? (foo (λ (x) (* 2 x
> 
> -Philip
> 
> 
> On Sat, Sep 15, 2018 at 9:17 PM Kevin Forchione  > wrote:
> Hi guys,
> Is there a way to define a macro so that an argument will be quoted only when 
> it is a symbol? Something like this:
> 
> (foo 10) => 10
> (foo hello) => ‘hello
> (foo (lambda (x) (* 2 x))) => 
> etc.

Thanks! That’s just what I wanted. Is there a way in Racket to determine if a 
quoted symbol has an associated procedure? 

Kevin
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com 
> .
> For more options, visit https://groups.google.com/d/optout 
> .

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] [Racket Users] Macros and literals question

2018-09-15 Thread Philip McGrath
Sure. For example:

#lang racket

(require syntax/parse/define
 rackunit)

(define-syntax-parser foo
  [(_ arg:id)
   #''arg]
  [(_ arg:expr)
   #'arg])

(check-eqv? (foo 10) 10)
(check-eq? (foo hello) 'hello)
(check-pred procedure? (foo (λ (x) (* 2 x

-Philip


On Sat, Sep 15, 2018 at 9:17 PM Kevin Forchione  wrote:

> Hi guys,
> Is there a way to define a macro so that an argument will be quoted only
> when it is a symbol? Something like this:
>
> (foo 10) => 10
> (foo hello) => ‘hello
> (foo (lambda (x) (* 2 x))) => 
> etc.
>
> Kevin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] [Racket Users] Macros and literals question

2018-09-15 Thread Kevin Forchione
Hi guys,
Is there a way to define a macro so that an argument will be quoted only when 
it is a symbol? Something like this:

(foo 10) => 10
(foo hello) => ‘hello
(foo (lambda (x) (* 2 x))) => 
etc.

Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros

2016-10-27 Thread Alexis King
First of all, #lang racket is not Scheme, nor does it claim to be.
However, #lang r5rs does attempt to be Scheme, and it exhibits the same
behavior as #lang racket in this case, so your question is still valid.
I would argue that, in this case, Racket gets it right, and Bigloo and
Kawa are noncomformant.

In both R5RS and R7RS, the way syntax-rules matches “literals” is quite
explicit[1]:

  “[…] an input expression E matches a pattern P if and only if […]
   P is a literal identifier and E is an identifier with the same
   binding […]”

The wording “with the same binding” is important. This means literals
must not be matched by datum (that is, by their name) but by their
lexical binding. When you redefine `else`, you have created a new
binding that is distinct from the binding (or lack thereof) provided by
the Scheme standard library.

The `cond` form is listed as a “derived expression”, and an
implementation of `cond` using `syntax-rules` is provided by the
report[2]. However, it does not appear that Bigloo or Kawa properly
implement the same binding restriction mandated by the standard, so they
are nonconformant.

I also tested two other Schemes, somewhat randomly chosen. Chibi Scheme
exhibits the same incorrect behavior as Bigloo and Kawa, but Larceny has
the same behavior as Racket. It would be interesting to see what other
Schemes do, but the practical takeaway seems to be that this part of the
standard is inconsistently implemented, and you cannot depend on it if
you want to (for whatever reason) write portable Scheme.

[1]: http://docs.racket-lang.org/r5rs/r5rs-std/r5rs-Z-H-7.html#%_sec_4.3.2
[2]: http://docs.racket-lang.org/r5rs/r5rs-std/r5rs-Z-H-10.html#%_sec_7.3

> On Oct 27, 2016, at 15:46, Damien Mattei  wrote:
> 
> Hi,
> 
> why this does not work with DrRacket:
> 
> #lang racket
> (define-syntax then
>  (syntax-rules ()
>((_ ev)  ev)
>((_ ev ...) (begin ev ...
> 
> (define-syntax else
>  (syntax-rules ()
>((_ ev)  ev)
>((_ ev ...) (begin ev ...
> 
> (if #f
>(then 
> 1
> 2
> 3)
>(else
> 3
> 4
> 5))
> 
> (cond (#f 'never)
>  (else 'always))
> 
> . else: bad syntax in: else
> 
> it works with others schemes as in bigloo or kawa:
> 
> --
> Bigloo (4.1a)
> ,--^, 
> `a practical Scheme compiler'  _ ___/ /|/ 
>  
> Sam 26 sep 2015 04:59:46 CEST  ,;'( )__, ) '  
>  
> Inria -- Sophia Antipolis ;;  //   L__.   
>  
> email: big...@lists-sop.inria.fr  '   \/  '   
>  
> url: http://www-sop.inria.fr/indes/fp/Bigloo   ^   ^  
>  
> --
> 
> 
> 1:=> (define-syntax then
>  (syntax-rules ()
>((_ ev)  ev)
>((_ ev ...) (begin ev ...
> #unspecified
> 1:=> (define-syntax else
>  (syntax-rules ()
>((_ ev)  ev)
>((_ ev ...) (begin ev ...
> #unspecified
> 1:=> (if #f
>(then 
> 1
> 2
> 3)
>(else
> 3
> 4
> 5))
> 5
> 1:=> (cond (#f 'never)
>  (else 'always))
> always
> 
> Damien

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] macros

2016-10-27 Thread Damien Mattei
Hi,

why this does not work with DrRacket:

#lang racket
(define-syntax then
  (syntax-rules ()
((_ ev)  ev)
((_ ev ...) (begin ev ...

(define-syntax else
  (syntax-rules ()
((_ ev)  ev)
((_ ev ...) (begin ev ...

(if #f
(then 
 1
 2
 3)
(else
 3
 4
 5))

(cond (#f 'never)
  (else 'always))

. else: bad syntax in: else

it works with others schemes as in bigloo or kawa:

--
Bigloo (4.1a),--^, 
`a practical Scheme compiler'  _ ___/ /|/  
Sam 26 sep 2015 04:59:46 CEST  ,;'( )__, ) '   
Inria -- Sophia Antipolis ;;  //   L__.
email: big...@lists-sop.inria.fr  '   \/  '
url: http://www-sop.inria.fr/indes/fp/Bigloo   ^   ^   
--


1:=> (define-syntax then
  (syntax-rules ()
((_ ev)  ev)
((_ ev ...) (begin ev ...
#unspecified
1:=> (define-syntax else
  (syntax-rules ()
((_ ev)  ev)
((_ ev ...) (begin ev ...
#unspecified
1:=> (if #f
(then 
 1
 2
 3)
(else
 3
 4
 5))
5
1:=> (cond (#f 'never)
  (else 'always))
always

Damien

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that use with-handlers

2016-07-17 Thread Jack Firth
On Saturday, July 16, 2016 at 7:06:10 PM UTC-4, David K. Storrs wrote:
> So, if one should prefer functions, what is a good place to use
> macros?  When people talk about why LISP/Scheme/Racket are the most
> powerful languages, they inevitably mention macros and continuations.
> What is a good use case for macros?

You generally want macros for things that can't be done with functions period, 
for instance a static type checker, pattern matching, a class system with 
static method resolution, making your own language, etc.

Your use case can benefit from macros by having a function that accepts a 
procedure to test and making a macro that constructs the function from a series 
of body expressions. Consider an example of a slightly modified version of your 
throws function:

(throws "Should throw when frobnicating non-widgets"
  exn:fail:contract?
  (lambda ()
(frobnicate (make-gizmo

Notice you have to wrap the "(frobnicate (make-gizmo))" expression in a thunk 
(a function that takes no arguments) so the throws function can control when 
the test expression is evaluated. If you didn't, throws would evaluate 
"(frobnicate (make-gizmo))" as its argument before doing anything and the 
exception would be thrown immediately. If you don't want to have to write the 
wrapper lambda all the time, you can make a macro version of throws that adds 
the lambda for you:

; define-simple-macro is basically define-syntax-rule but using syntax-parse
; instead of syntax-case
(define-simple-macro (throws-macro message:expr matcher:expr body:expr ...+)
  (throws message matcher (lambda () body ...)))

; expands to a call to the throws test function
(throws-macro "Should throw when frobnicating non-widgets"
  exn:fail:contract?
  (frobnicate (make-gizmo)))

The throws-macro *at compile time* transforms the body expression it's given 
into an expression with a wrapper lambda, but *at runtime* the throws function 
does all the actual testing and logic. It's common for macro-writers to keep as 
much logic as possible out of macros and in runtime functions, as that's what 
functions are best at.

One last note - this particular use case for macros can also be achieved with 
lazy evaluation, but macros and lazy evaluation in general have very different 
non-overlapping goals and it's best not to conflate them.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread David Storrs
I see.  Okay, thanks for clarifying.

On Sat, Jul 16, 2016 at 8:05 PM, Alex Knauth  wrote:
>
>> On Jul 16, 2016, at 7:18 PM, David Storrs  wrote:
>>
>> Wow, that is a lot of problems.  Thanks for taking the time to
>> comment; this was in large part an effort to learn macros, so it's
>> helpful to get this kind of feedback.
>
>> On Sat, Jul 16, 2016 at 2:48 PM, Alex Knauth  wrote:
>
>>> A few problems with this.
>>>
>>> One, you shouldn't put extra (lambda () ...)s around the #:when conditions. 
>>> A lambda is a truthy value, since it's not false, so those #:when checks 
>>> weren't checking anything at all.
>>
>> I thought the #:when ran the predicate that it was given and reacted
>> based on the truth value of the response?
>
> The #:when takes a boolean. It works like the #:when clause in match, if 
> you've used that. Or like the condition of an if expression where the 
> then-branch keeps going in the same clause and the else-branch makes it go to 
> the next clause.
>
>> Would (string? pred)  have worked there instead?
>
> I'm not sure what you mean.
>
> #lang racket
> ;; no, I don't want a for-syntax, I'm using it at run-time, as a match-like 
> form
> (require syntax/parse)
> (syntax-parse (syntax ignored)
>   [_
>#:when (string? "hello")
>'string])
>
> This tests whether "hello" is a string. But for a pattern-variable like pred 
> it almost can work like that, but not quite.
>
> (syntax-parse (syntax (foo "hello"))
>   [(foo a)
>#:when (string? a)
>'string])
>
> This gives the error `a: pattern variable cannot be used outside of a 
> template`. A template means something like the syntax form. However even with 
> that fixed this doesn't work as expected:
>
> (syntax-parse (syntax (foo "hello"))
>   [(foo a)
>#:when (string? (syntax a)) ; writing (string? #'a) would mean the same 
> thing
>'string])
>
> The string? predicate returns false, so this fails as well. The problem is 
> that (syntax a) returns a syntax object, but you want the contents inside the 
> syntax object, because that's where the string (or otherwise) will be. To do 
> that you can use syntax-e (one-level-down) or syntax->datum (pure 
> s-expression with no syntax-object sub-expressions).
>
> (syntax-parse (syntax (foo "hello"))
>   [(foo a)
>#:when (string? (syntax-e (syntax a)))
>'string])
>
> This succeeds, producing 'string. Using syntax->datum will also work here:
>
> (syntax-parse (syntax (foo "hello"))
>   [(foo a)
>#:when (string? (syntax->datum (syntax a)))
>'string])
>
>>> Two, once you remove those lambdas, (procedure? #'pred) will always fail 
>>> because #'pred isn't the actual predicate, it's an expression, which might 
>>> eventually evaluate to predicate, or it might evaluate something else. At 
>>> compile-time, you just don't know yet. What you could do is check that it's 
>>> an identifier, with (identifier? #'pred).
>>
>> Would (procedure? (syntax->datum #'pred)) have worked there?  pred
>> either is or is not a procedure, and that should be known at compile
>> time.
>
> For simple literal data like strings, (string? (syntax->datum #'pred)) will 
> work, since literal data is known at compile-time.
>
> However procedures, although they are values, they only become values at 
> run-time. At compile-time, this pred is only an expression, so (procedure? 
> (syntax->datum #'pred)) will return false.
>
> A procedure given to you by a user in something like
> (throws (boom) exn:fail? "should trigger the proc form")
> Will not be known at compile time. (syntax pred) is an identifier, and 
> (syntax->datum (syntax pred)) will return a symbol, not a procedure.
>
> At compile-time that's all you know. It could be an identifier that happens 
> to be bound to a procedure, or it could be an identifier bound to a number, 
> or it could have no definition at all.
>
> So, for simple literal data like strings, it is known at compile-time, but 
> for more complicated things like procedures, it usually isn't.
>
> Alex Knauth
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread Alex Knauth

> On Jul 16, 2016, at 7:18 PM, David Storrs  wrote:
> 
> Wow, that is a lot of problems.  Thanks for taking the time to
> comment; this was in large part an effort to learn macros, so it's
> helpful to get this kind of feedback.

> On Sat, Jul 16, 2016 at 2:48 PM, Alex Knauth  wrote:

>> A few problems with this.
>> 
>> One, you shouldn't put extra (lambda () ...)s around the #:when conditions. 
>> A lambda is a truthy value, since it's not false, so those #:when checks 
>> weren't checking anything at all.
> 
> I thought the #:when ran the predicate that it was given and reacted
> based on the truth value of the response?  

The #:when takes a boolean. It works like the #:when clause in match, if you've 
used that. Or like the condition of an if expression where the then-branch 
keeps going in the same clause and the else-branch makes it go to the next 
clause.

> Would (string? pred)  have worked there instead?

I'm not sure what you mean.

#lang racket
;; no, I don't want a for-syntax, I'm using it at run-time, as a match-like form
(require syntax/parse)
(syntax-parse (syntax ignored)
  [_
   #:when (string? "hello")
   'string])

This tests whether "hello" is a string. But for a pattern-variable like pred it 
almost can work like that, but not quite.

(syntax-parse (syntax (foo "hello"))
  [(foo a)
   #:when (string? a)
   'string])

This gives the error `a: pattern variable cannot be used outside of a 
template`. A template means something like the syntax form. However even with 
that fixed this doesn't work as expected:

(syntax-parse (syntax (foo "hello"))
  [(foo a)
   #:when (string? (syntax a)) ; writing (string? #'a) would mean the same thing
   'string])

The string? predicate returns false, so this fails as well. The problem is that 
(syntax a) returns a syntax object, but you want the contents inside the syntax 
object, because that's where the string (or otherwise) will be. To do that you 
can use syntax-e (one-level-down) or syntax->datum (pure s-expression with no 
syntax-object sub-expressions).

(syntax-parse (syntax (foo "hello"))
  [(foo a)
   #:when (string? (syntax-e (syntax a)))
   'string])

This succeeds, producing 'string. Using syntax->datum will also work here:

(syntax-parse (syntax (foo "hello"))
  [(foo a)
   #:when (string? (syntax->datum (syntax a)))
   'string])

>> Two, once you remove those lambdas, (procedure? #'pred) will always fail 
>> because #'pred isn't the actual predicate, it's an expression, which might 
>> eventually evaluate to predicate, or it might evaluate something else. At 
>> compile-time, you just don't know yet. What you could do is check that it's 
>> an identifier, with (identifier? #'pred).
> 
> Would (procedure? (syntax->datum #'pred)) have worked there?  pred
> either is or is not a procedure, and that should be known at compile
> time.

For simple literal data like strings, (string? (syntax->datum #'pred)) will 
work, since literal data is known at compile-time. 

However procedures, although they are values, they only become values at 
run-time. At compile-time, this pred is only an expression, so (procedure? 
(syntax->datum #'pred)) will return false. 

A procedure given to you by a user in something like 
(throws (boom) exn:fail? "should trigger the proc form")
Will not be known at compile time. (syntax pred) is an identifier, and 
(syntax->datum (syntax pred)) will return a symbol, not a procedure. 

At compile-time that's all you know. It could be an identifier that happens to 
be bound to a procedure, or it could be an identifier bound to a number, or it 
could have no definition at all. 

So, for simple literal data like strings, it is known at compile-time, but for 
more complicated things like procedures, it usually isn't.

Alex Knauth

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread David Storrs
Wow, that is a lot of problems.  Thanks for taking the time to
comment; this was in large part an effort to learn macros, so it's
helpful to get this kind of feedback.


On Sat, Jul 16, 2016 at 2:48 PM, Alex Knauth  wrote:
>
>> On Jul 16, 2016, at 2:16 PM, David Storrs  wrote:
>>
>> I'm trying to write a macro to test expected exceptions.  I'd like it
>> to have the following forms:
>>
>> (throws exception-generator-function proc msg)
>> (throws exception-generator-function string msg)
>>
>> Where 'proc' would be something like exn:fail? and 'string' would be
>> matched against the message inside the exception.
>>
>> I got all the pieces of this working but not playing nice together.
>> There's three elements in the pattern for both versions, so the only
>> way to distinguish which to use is by the types of the arguments.  I
>> looked at syntax classes but made no headway on grokking that.  I saw
>> the #:when keyword for patterns and thought that would do what I
>> needed.  The following code seems like it should work, but it doesn't.
>> What am I missing?
>>
>>
>> (define-syntax (throws stx)
>>  (syntax-parse stx
>>[(_ boom pred msg)
>> #:when (lambda () (procedure? #'pred))
>> #'(with-handlers
>>([(lambda (x) #t) ;; already checked it's a proc
>>  (lambda (xcpt)
>>(println "procedure form"))])
>>boom)]
>>
>>[(_ boom pred msg)
>> #:when (lambda () (string? #'pred))
>> #'(with-handlers
>>([(lambda (x) #t) ;; already checked it's a string
>>  (println "string form")])
>>boom)]))
>>
>> (define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
>> (throws (boom) exn:fail? "should trigger the proc form")
>> (throws (boom) "PEBKAC"  "should trigger the string form")
>
> A few problems with this.
>
> One, you shouldn't put extra (lambda () ...)s around the #:when conditions. A 
> lambda is a truthy value, since it's not false, so those #:when checks 
> weren't checking anything at all.
>

I thought the #:when ran the predicate that it was given and reacted
based on the truth value of the response?  Would (string? pred)  have
worked there instead?

> Two, once you remove those lambdas, (procedure? #'pred) will always fail 
> because #'pred isn't the actual predicate, it's an expression, which might 
> eventually evaluate to predicate, or it might evaluate something else. At 
> compile-time, you just don't know yet. What you could do is check that it's 
> an identifier, with (identifier? #'pred).

Would (procedure? (syntax->datum #'pred)) have worked there?  pred
either is or is not a procedure, and that should be known at compile
time.

>
> Four, in the second clause you have (println "string form") as the handler 
> function. You probably meant to put a lambda around it like you did for the 
> first clause: (lambda (xcpt) (println "string form")).

I did, yes.  Thanks.

> Five, did you mean to actually use the pred argument in the exception 
> predicates of the with-handlers forms?
> Six, did you mean to use the msg argument for anything?

The code I sent out was stripped down to be the minimum necessary to
demonstrate the problem, so it wasn't doing what it would ultimately
have done.  Once things were working as expected then the pred and msg
would have both been used, yes.


>

>
> With the first five of these addressed, the code looks like this:
>
> #lang racket
> (require (for-syntax racket/base syntax/parse))
> (define-syntax (throws stx)
>   (syntax-parse stx
> [(_ boom pred msg)
>  #:when (identifier? #'pred)
>  #'(with-handlers
>([pred
>  (lambda (xcpt)
>(println "procedure form"))])
>  boom)]
> [(_ boom pred msg)
>  #:when (string? (syntax-e #'pred))
>  #'(with-handlers
>([(lambda (x) (string-contains? (exn-message x) pred))
>  (lambda (xcpt)
>(println "string form"))])
>  boom)]))
>
> (define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
> (throws (boom) exn:fail? "should trigger the proc form")
> (throws (boom) "PEBKAC"  "should trigger the string form")
>
> Although there is an easier, better way to check whether an argument is an 
> identifier or a string, since you're using syntax-parse. You can use the id 
> syntax class in the first clause, and the str syntax class in the second 
> clause. Then the code looks like this:
>
> #lang racket
> (require (for-syntax racket/base syntax/parse))
> (define-syntax (throws stx)
>   (syntax-parse stx
> [(_ boom pred:id msg)
>  #'(with-handlers
>([pred
>  (lambda (xcpt)
>(println "procedure form"))])
>  boom)]
> [(_ boom pred:str msg)
>  #'(with-handlers
>  

Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread David Storrs
On Sat, Jul 16, 2016 at 2:48 PM, William J. Bowman
 wrote:

> To address the immediate problem, this `procedure?` test will always return 
> `#f`, since `#'pred` is a
> syntax object and not a procedure. You need to do `(procedure? (syntax->datum 
> #'pred))`. Similarly
> reasoning applies to the `string?` test.
>

Oh, of course; I should have realized.  I was thinking of #' as the
"get the value of the syntax object" expression, but that is exactly
backwards.  It's just the shortcut for (syntax ).   Thanks!

Dave

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread David Storrs
On Sat, Jul 16, 2016 at 2:48 PM, Alexis King  wrote:
> Of course, this makes sense, given that macros operate entirely at
> compile-time. What you really want here is a function, not a macro. You
> can write a simple function that will accept a procedure or a string and
> will produce a procedure:
>[...]
>
> In fact, throws can be implemented as a function as well, [...example...]
>
> Generally, prefer functions over macros when functions will do.
> Functions are both more flexible (they can be used as values, and
> therefore can be used higher-order) and more predictable (function
> invocations have very predictable evaluation semantics, while macros
> might not).

Makes sense; thank you.

So, if one should prefer functions, what is a good place to use
macros?  When people talk about why LISP/Scheme/Racket are the most
powerful languages, they inevitably mention macros and continuations.
What is a good use case for macros?

Dave

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread Alex Knauth

> On Jul 16, 2016, at 2:16 PM, David Storrs  wrote:
> 
> I'm trying to write a macro to test expected exceptions.  I'd like it
> to have the following forms:
> 
> (throws exception-generator-function proc msg)
> (throws exception-generator-function string msg)
> 
> Where 'proc' would be something like exn:fail? and 'string' would be
> matched against the message inside the exception.
> 
> I got all the pieces of this working but not playing nice together.
> There's three elements in the pattern for both versions, so the only
> way to distinguish which to use is by the types of the arguments.  I
> looked at syntax classes but made no headway on grokking that.  I saw
> the #:when keyword for patterns and thought that would do what I
> needed.  The following code seems like it should work, but it doesn't.
> What am I missing?
> 
> 
> (define-syntax (throws stx)
>  (syntax-parse stx
>[(_ boom pred msg)
> #:when (lambda () (procedure? #'pred))
> #'(with-handlers
>([(lambda (x) #t) ;; already checked it's a proc
>  (lambda (xcpt)
>(println "procedure form"))])
>boom)]
> 
>[(_ boom pred msg)
> #:when (lambda () (string? #'pred))
> #'(with-handlers
>([(lambda (x) #t) ;; already checked it's a string
>  (println "string form")])
>boom)]))
> 
> (define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
> (throws (boom) exn:fail? "should trigger the proc form")
> (throws (boom) "PEBKAC"  "should trigger the string form")

A few problems with this.

One, you shouldn't put extra (lambda () ...)s around the #:when conditions. A 
lambda is a truthy value, since it's not false, so those #:when checks weren't 
checking anything at all.

Two, once you remove those lambdas, (procedure? #'pred) will always fail 
because #'pred isn't the actual predicate, it's an expression, which might 
eventually evaluate to predicate, or it might evaluate something else. At 
compile-time, you just don't know yet. What you could do is check that it's an 
identifier, with (identifier? #'pred).

Three, (string? #'pred) will also always fail, because #'pred is an expression. 
Now for this you can actually check that it is a string, by using (string? 
(syntax-e #'pred)).

Four, in the second clause you have (println "string form") as the handler 
function. You probably meant to put a lambda around it like you did for the 
first clause: (lambda (xcpt) (println "string form")).

Five, did you mean to actually use the pred argument in the exception 
predicates of the with-handlers forms? 

Six, did you mean to use the msg argument for anything?

With the first five of these addressed, the code looks like this:

#lang racket
(require (for-syntax racket/base syntax/parse))
(define-syntax (throws stx)
  (syntax-parse stx
[(_ boom pred msg)
 #:when (identifier? #'pred)
 #'(with-handlers
   ([pred
 (lambda (xcpt)
   (println "procedure form"))])
 boom)]
[(_ boom pred msg)
 #:when (string? (syntax-e #'pred))
 #'(with-handlers
   ([(lambda (x) (string-contains? (exn-message x) pred))
 (lambda (xcpt)
   (println "string form"))])
 boom)]))

(define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
(throws (boom) exn:fail? "should trigger the proc form")
(throws (boom) "PEBKAC"  "should trigger the string form")

Although there is an easier, better way to check whether an argument is an 
identifier or a string, since you're using syntax-parse. You can use the id 
syntax class in the first clause, and the str syntax class in the second 
clause. Then the code looks like this:

#lang racket
(require (for-syntax racket/base syntax/parse))
(define-syntax (throws stx)
  (syntax-parse stx
[(_ boom pred:id msg)
 #'(with-handlers
   ([pred
 (lambda (xcpt)
   (println "procedure form"))])
 boom)]
[(_ boom pred:str msg)
 #'(with-handlers
   ([(lambda (x) (string-contains? (exn-message x) pred))
 (lambda (xcpt)
   (println "string form"))])
 boom)]))

(define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
(throws (boom) exn:fail? "should trigger the proc form")
(throws (boom) "PEBKAC"  "should trigger the string form")




-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread 'William J. Bowman' via Racket Users
On Sat, Jul 16, 2016 at 02:16:20PM -0400, David Storrs wrote:
> I saw the #:when keyword for patterns and thought that would do what I
> needed.  The following code seems like it should work, but it doesn't.
> What am I missing?
> 
> 
> (define-syntax (throws stx)
>   (syntax-parse stx
> [(_ boom pred msg)
>  #:when (lambda () (procedure? #'pred))
To address the immediate problem, this `procedure?` test will always return 
`#f`, since `#'pred` is a
syntax object and not a procedure. You need to do `(procedure? (syntax->datum 
#'pred))`. Similarly
reasoning applies to the `string?` test.

I'm not sure your approach is ideal, but I'll let someone else comment.

--
William J. Bowman

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: PGP signature


Re: [racket-users] Macros that use with-handlers

2016-07-16 Thread Alexis King
I don’t think you want to use a macro to do this.

You could detect, at compile-time, whether or not the provided argument
is a string within the macro. The best way to do this would probably be
to use the “str” syntax class, which detects strings within a
syntax-parse pattern. However, this has a significant drawback: users
cannot provide expressions that evaluate to strings, only string
literals. For example, this will not work:

  (let ([str "some string"])
(throws (boom) str "should be treated like a string"))

Of course, this makes sense, given that macros operate entirely at
compile-time. What you really want here is a function, not a macro. You
can write a simple function that will accept a procedure or a string and
will produce a procedure:

  (define/contract (value->exn-predicate x)
(-> (or/c string? (-> any/c any/c))
(-> any/c any/c))
(if (procedure? x) x
(λ (exn)
  (and (exn? exn)
   (equal? (exn-message exn) x)

In fact, throws can be implemented as a function as well, if you just
pass the function to be invoked instead of an expression that throws an
exception itself:

  (define (throws exn-generator exn-value msg)
(with-handlers ([(value->exn-predicate exn-value)
 (λ (exn) (println "caught"))])
  (exn-generator)))

Generally, prefer functions over macros when functions will do.
Functions are both more flexible (they can be used as values, and
therefore can be used higher-order) and more predictable (function
invocations have very predictable evaluation semantics, while macros
might not).

> On Jul 16, 2016, at 11:16, David Storrs  wrote:
> 
> I'm trying to write a macro to test expected exceptions.  I'd like it
> to have the following forms:
> 
> (throws exception-generator-function proc msg)
> (throws exception-generator-function string msg)
> 
> Where 'proc' would be something like exn:fail? and 'string' would be
> matched against the message inside the exception.
> 
> I got all the pieces of this working but not playing nice together.
> There's three elements in the pattern for both versions, so the only
> way to distinguish which to use is by the types of the arguments.  I
> looked at syntax classes but made no headway on grokking that.  I saw
> the #:when keyword for patterns and thought that would do what I
> needed.  The following code seems like it should work, but it doesn't.
> What am I missing?
> 
> 
> (define-syntax (throws stx)
>  (syntax-parse stx
>[(_ boom pred msg)
> #:when (lambda () (procedure? #'pred))
> #'(with-handlers
>([(lambda (x) #t) ;; already checked it's a proc
>  (lambda (xcpt)
>(println "procedure form"))])
>boom)]
> 
>[(_ boom pred msg)
> #:when (lambda () (string? #'pred))
> #'(with-handlers
>([(lambda (x) #t) ;; already checked it's a string
>  (println "string form")])
>boom)]))
> 
> (define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
> (throws (boom) exn:fail? "should trigger the proc form")
> (throws (boom) "PEBKAC"  "should trigger the string form")

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Macros that use with-handlers

2016-07-16 Thread David Storrs
I'm trying to write a macro to test expected exceptions.  I'd like it
to have the following forms:

(throws exception-generator-function proc msg)
(throws exception-generator-function string msg)

Where 'proc' would be something like exn:fail? and 'string' would be
matched against the message inside the exception.

I got all the pieces of this working but not playing nice together.
There's three elements in the pattern for both versions, so the only
way to distinguish which to use is by the types of the arguments.  I
looked at syntax classes but made no headway on grokking that.  I saw
the #:when keyword for patterns and thought that would do what I
needed.  The following code seems like it should work, but it doesn't.
What am I missing?


(define-syntax (throws stx)
  (syntax-parse stx
[(_ boom pred msg)
 #:when (lambda () (procedure? #'pred))
 #'(with-handlers
([(lambda (x) #t) ;; already checked it's a proc
  (lambda (xcpt)
(println "procedure form"))])
boom)]

[(_ boom pred msg)
 #:when (lambda () (string? #'pred))
 #'(with-handlers
([(lambda (x) #t) ;; already checked it's a string
  (println "string form")])
boom)]))

(define (boom) (raise-argument-error 'boom "PEBKAC" 18)) ;; random choices
(throws (boom) exn:fail? "should trigger the proc form")
(throws (boom) "PEBKAC"  "should trigger the string form")

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros within expressions.

2016-04-06 Thread Alex Knauth

> On Apr 6, 2016, at 8:29 PM, Matthew Butterick  wrote:

> So you can repair your macro by matching to `y-coord` as a pattern:
> 
> ;;;
> 
> #lang racket
> (require rackunit)
> 
> (define-syntax y-coord
>   (syntax-id-rules ()
> [y-coord point-y]))
> 
> (struct point (x y))
> (define points (list (point 25 25) (point 33 54) (point 10 120)))
> 
> (check-equal? (map y-coord points) '(25 54 120))

This is something I had trouble with at first with syntax-id-rules.

One problem with this macro is that then expanding `(y-coord (point 25 25))` 
just returns the function `point-y` instead of calling `(point-y (point 25 
25))`. To do that properly, you would want to match the `(y-coord arg)` pattern 
as well.

However this doesn't work:
(define-syntax y-coord
  (syntax-id-rules ()
[y-coord point-y]
[(y-coord arg) (point-y arg)]))
Because it tries the patterns in order, and it doesn't realize that the 
identifier pattern is supposed to match only when given the exact identifier 
you want. So you want something more like this:
(define-syntax y-coord
  (syntax-id-rules ()
[(y-coord arg) (point-y arg)]
[y-coord point-y]))

But this is a common enough pattern that there's a function for that in the 
racket syntax/transformer module:

(require syntax/transformer)
(define-syntax y-coord
  (make-variable-like-transformer #'point-y))

Docs for that here:
http://docs.racket-lang.org/syntax/transformer-helpers.html#%28def._%28%28lib._syntax%2Ftransformer..rkt%29._make-variable-like-transformer%29%29

Also, this doesn't just work for identifiers like `point-y`. This can be used 
to substitute expressions as well, so it can also be used to make variable-like 
identifiers that behave like boxes, or make identifiers log something every 
time they're referenced, or ones that expand to more complicated macro calls. 
It's also a way of enforcing that a variable should be immutable.

Alex Knauth

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros within expressions.

2016-04-06 Thread Matthew Butterick
> Expanding `y-coord` by itself leads to the error.

I've tripped over this, so just to elaborate slightly.

`syntax-id-rules` will work, but your current macro:

;;;

(define-syntax y-coord
  (syntax-id-rules (map)
[(map y-coord lst) (map (lambda (p)
  (point-y p)) 
lst)]))
;;;

assumes that it will be getting a syntax pattern that looks like `(map y-coord 
lst)`. 

But it will not. The pattern it gets is simply `y-coord`. (More broadly, 
identifier macros made with `syntax-id-rules` will match only three kinds of 
patterns: the name alone, the name after an open parenthesis, or the name 
within `set!` Killer docs cross-reference: [1]) 

So you can repair your macro by matching to `y-coord` as a pattern:

;;;

#lang racket
(require rackunit)

(define-syntax y-coord
  (syntax-id-rules ()
[y-coord point-y]))

(struct point (x y))
(define points (list (point 25 25) (point 33 54) (point 10 120)))

(check-equal? (map y-coord points) '(25 54 120))

;;;


BTW the "bad syntax" error is a generic error you get when there's no pattern 
match inside a macro. Thus, it can be wise to include an `else` branch so that 
you can pinpoint the source, for instance:


;;;

(define-syntax y-coord
  (syntax-id-rules (map)
[(map y-coord lst) (map (lambda (p)
  (point-y p)) 
lst)]
[else (error 'no-pattern-matched-in-your-y-coord-macro)]))

;;;



[1] 
http://docs.racket-lang.org/guide/pattern-macros.html?q=identifier%20macro#%28part._.Identifier_.Macros%29

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros within expressions.

2016-04-06 Thread Vincent St-Amour
Adding `(define y-coord points-y)` should make the original example work
as-is.

Vincent


On Wed, 06 Apr 2016 15:45:56 -0500,
Daniel Prager wrote:
> 
> (map point-y points) gives the expected result without recourse to a
> macro.
> 
> Or is y-coord intended as a simplified example of something else?
> 
> Dan
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: FW: [racket-users] macros within expressions.

2016-04-06 Thread Benjamin Greenman
This is "bad syntax" for the same reason that `(map or '())` is "bad
syntax".
When the macro expander sees `(map y-coord points)`, it recognizes `map` as
a variable binding and then tries expanding each argument to `map`.
Expanding `y-coord` by itself leads to the error.

You could instead define `y-coord` as an identifier macro or just alias it
as a function. (As Daniel & Jos recommend.)

(define-syntax (y-coord stx) ;; Identifier macro
  (syntax-case stx ()
[y-coord:id #'point-y]))

(define y-coord point-y) ;; Function


For details on the expander:
http://docs.racket-lang.org/reference/syntax-model.html#%28part._expand-steps%29

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


FW: [racket-users] macros within expressions.

2016-04-06 Thread Jos Koot
Oops, forgot to include the users list.

  _  

From: Jos Koot [mailto:jos.k...@gmail.com] 
Sent: miércoles, 06 de abril de 2016 23:03
To: 'Richard Adler'
Subject: RE: [racket-users] macros within expressions.



You don't need a macro here. Why not simply:

(map point-y points)

May be your example is an extract where you realy need a macro.
In that case:

(define-syntax y-coord
  (syntax-id-rules ()
[(y-coord lst) (map point-y lst)]))

(y-coord points)

You can't map a macro.
You could make a macro-mapper though:

(define-syntax-rule (map-macro macro arg) (map (λ (x) (macro x)) arg))
(define-syntax-rule (y-macro arg) (point-y arg))
(map-macro y-macro points)

But in general it is wise to AVOID MACROS.

Good luck, Jos

-Original Message-
From: racket-users@googlegroups.com [ <mailto:racket-users@googlegroups.com>
mailto:racket-users@googlegroups.com]
Sent: miércoles, 06 de abril de 2016 22:27
To: Racket Users
Subject: [racket-users] macros within expressions.


I am trying to use a macro, y-coord, in the following way:

(struct point (x y))
(define points (list (point 25 25) (point 33 54) (point 10 120))
(map y-coord points)

I am expecting the result:
'(25 54 120)

and the error is "y-coord: bad syntax in: y-coord

Here is the macro:

(define-syntax y-coord
  (syntax-id-rules (map)
[(map y-coord lst) (map (lambda (p)
  (point-y p))
lst)]))

Can anyone help?
Thanks

--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to racket-users+unsubscr...@googlegroups.com.
For more options, visit  <https://groups.google.com/d/optout>
https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] macros within expressions.

2016-04-06 Thread Daniel Prager
(map point-y points) gives the expected result without recourse to a macro.

Or is y-coord intended as a simplified example of something else?

Dan

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] macros within expressions.

2016-04-06 Thread 'Richard Adler' via Racket Users

I am trying to use a macro, y-coord, in the following way:

(struct point (x y))
(define points (list (point 25 25) (point 33 54) (point 10 120))
(map y-coord points)

I am expecting the result:
'(25 54 120)

and the error is "y-coord: bad syntax in: y-coord

Here is the macro:

(define-syntax y-coord
  (syntax-id-rules (map)
[(map y-coord lst) (map (lambda (p)
  (point-y p)) 
lst)]))

Can anyone help? 
Thanks

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros outline

2016-02-05 Thread Gavin McGimpsey
Thanks, Greg!

I appreciated that approach – and ordered things pretty much the same way here.



On Thursday, February 4, 2016 at 7:17:37 PM UTC-7, Greg Hendershott wrote:
> I really like it!
> 
> Compared to FoM, you get to the point more quickly and clearly.
> 
> (FoM is closer to that genre where someone takes notes on their first
> 24 hours trying to understand a new language. (Except in my case with
> macros it felt like months not hours.))
> 
> I hope you do expand it.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros outline

2016-02-04 Thread Greg Hendershott
I really like it!

Compared to FoM, you get to the point more quickly and clearly.

(FoM is closer to that genre where someone takes notes on their first
24 hours trying to understand a new language. (Except in my case with
macros it felt like months not hours.))

I hope you do expand it.


On Wed, Feb 3, 2016 at 2:37 PM, Gavin McGimpsey  wrote:
> I've been picking up Racket here and there, and have spent the last little 
> while learning about macros. The Reference and Guide are comprehensive, of 
> course, and Greg Hendershott's Fear of Macros is an excellent introduction.
>
> I found that it took me a little to see how each of the pieces fit together 
> in the bigger picture – FoM and the Guide both take tutorial approaches that 
> start small and build upwards, but didn't make the high-level organization 
> explicit enough for me. So I wrote up this outline:
>
> http://www.gavinmcg.com/2016/02/03/racket-macros.html
>
> I'm hoping to expand it once I learn more about syntax-parse and reader 
> extensions.
>
> I'd appreciate any feedback!
>
> Gavin
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Macros outline

2016-02-03 Thread Asumu Takikawa
Hi Gavin,

On 2016-02-03 11:37:39 -0800, Gavin McGimpsey wrote:
> I'd appreciate any feedback!

This is very nice and concise! Thanks for writing it.

If you do expand it, it might be worth mentioning that the macro stepper lets
you debug macros. And if you expand with info on `syntax-parse`, you could even
drop the `syntax-case` mention since there's nothing it can do that
`syntax-parse` can't also do.

Cheers,
Asumu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Macros outline

2016-02-03 Thread Gavin McGimpsey
I've been picking up Racket here and there, and have spent the last little 
while learning about macros. The Reference and Guide are comprehensive, of 
course, and Greg Hendershott's Fear of Macros is an excellent introduction.

I found that it took me a little to see how each of the pieces fit together in 
the bigger picture – FoM and the Guide both take tutorial approaches that start 
small and build upwards, but didn't make the high-level organization explicit 
enough for me. So I wrote up this outline:

http://www.gavinmcg.com/2016/02/03/racket-macros.html

I'm hoping to expand it once I learn more about syntax-parse and reader 
extensions.

I'd appreciate any feedback!

Gavin

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.