Re: [racket-users] Unmap failed

2018-05-28 Thread Philip McGrath
I was able to run your code successfully, so I can't help with the specific
error message you're seeing, but there were a number of issues. In
DrRacket, with `(step 50)`, expansion reaches the memory limit I've set
before it's able to compile the program. I could run it at the command-line
(and presumable DrRacket would, too, if I gave it more RAM), but even there
it took an extraordinarily long time.

There are two other suggestions I wanted to make:

1. Other than as an experiment, there's no reason for this to be a macro
rather than a plain function. Given a far-from-optimized function step*:
(define (step* num-steps)
  (for/sum ([x (in-range num-steps)])
(expt 2 x)))
DrRacket is happy to compute `(step* 5)` even with my restrictive
memory limit in place and does so in around 800 ms, whereas running the
macro version at the command-line (which generally has better performance)
took, I think, tens of minutes—long enough that I forgot about checking on
it.

2. I noticed that in your macro implementation you were making heavy use of
`datum->syntax` rather than syntax templates and pattern variables. This
has issues for the correctness of your macro. In particular, a program like:
(let ([+ string-append])
  (step 50))
will raise an exception. This is because, when you write `(datum->syntax
stx `(+ ,@works))`, you are giving `+` the lexical context of the input
syntax, so it will be bound to whatever `+` is bound to where your macro is
used—or perhaps not bound to anything, if there isn't a binding for `+` in
scope there.

Sometimes one wants to do that sort of thing, so Racket gives you the tools
to express it, but most of the time you want the use of `+` in a macro to
mean what `+` means where the macro is defined. You can achieve that result
with low-level operations like `datum->syntax`, but doing it manually is
difficult and error-prone, so Racket provides enormously sophisticated
tools to make doing the write thing easy and natural. Even without changing
the style of your macro at all, simply using `quasisyntax` and `unsyntax`
(spelled "#`" and "#,") instead of `quasiquote` and `unquote` will fix your
problem:

#lang racket/base

(require (for-syntax racket/base))

(define-syntax (hygienic-step stx)
  (define numList (build-list (cadr (syntax->datum stx)) values))
  (define works (map (lambda (v) #`(expt 2 #,v)) numList))
  #`(+ #,@works))

(let ([+ string-append])
  (hygienic-step 50))





-Philip

On Mon, May 28, 2018 at 8:57 AM,  wrote:

> Hello, my name is Pavel and I'm new to Racket :)
>
> I'm playing with macros right now and get a strange error: "unmap failed".
>
> What I'm trying to achieve in this program: expand (step x) into (+ (expt
> 2 0) (expt 2 1) ...)
>
> For small numbers everything is OK, but I get an error for (step 100)
>
> What does this error mean and how can I fix it?
>
> Code is in attachements.
>
>
> --
> 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] Module dependencies

2018-05-28 Thread Laurent
You could also have a third file, test-a-and-b.rkt, that requires both
a.rkt and b.rkt and includes the test that depend on both files.


On Mon, May 28, 2018 at 10:08 AM Claes Wallin (韋嘉誠) 
wrote:

> (require) binds at compile time and creates a cycle, but by using
> (dynamic-require) you can get past compilation and only load the file at
> runtime, where the seemingly circular reference really isn't.
>
> --
>/c
>
> On Sun, May 27, 2018, 23:48 Matthias Felleisen 
> wrote:
>
>>
>> Modules cannot refer to each other in a cyclic fashion, including
>> submodules. — Matthias
>>
>>
>>
>> On May 27, 2018, at 8:33 AM, Brandon Irizarry 
>> wrote:
>>
>> Say I have two files, "file-a.rkt" and "file-b.rkt" that each contain a
>> submodule test, like so:
>>
>>
>> Contents of "file-a.rkt":
>>
>> #lang racket/base
>> (define (my-function) 'apple)
>> (module+ test
>> (require "file-b.rkt")
>> (other-function))
>>
>> Contents of "file-b.rkt":
>>
>> #lang racket/base
>> (define (other-function) 'orange)
>> (module+ test
>> (require "file-a.rkt")
>> (my-function))
>>
>> The require statements form a circular reference, even though running
>> file-b, along with its tests, shouldn't trigger file-a's tests.
>>
>> I've looked into compiling file-a and file-b, but that didn't work.
>>
>> - Brandon
>>
>>
>> --
>> 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.
>>
> --
> 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] Questions on functional-setter generator macro

2018-05-28 Thread Greg Hendershott
On Mon, May 28, 2018 at 9:58 AM, David Storrs  wrote:
> O *course* there is.  Lord know, why should I have an original idea
> now?  :P

The Racket package ecosystem is growing to where we need some
volunteers to form a mathic order of Lorites (like in Neal
Stephenson's novel, Anathem).

-- 
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] Questions on functional-setter generator macro

2018-05-28 Thread David Storrs
On Sat, May 26, 2018 at 3:11 PM, Alexis King  wrote:

> This isn’t a direct answer to your question, but as Matthias notes, my
> struct-update package already implements a macro that generates
> functional setters from structure definitions. Here’s a link to the
> documentation:
>

O *course* there is.  Lord know, why should I have an original idea
now?  :P



> http://docs.racket-lang.org/struct-update/index.html
>
> Of course, that isn’t especially helpful if your goal is to get better
> at authoring macros, not just generate functional setters. For that, you
> might find the (short) source code of struct-update helpful, located
> here:
>
> https://github.com/lexi-lambda/struct-update/blob/
> 8ce456cde8764ae27c348123ec9e01e76826d536/struct-update-lib/
> struct-update/main.rkt


Yes, thank you, this helps.  I hadn't thought about superclass fields.


> Admittedly, your make-functional-setter function does a bit more than
> define-struct-updaters, since it allows for a wrapper function. So I’ll
> also give some brief answers to a few of your unrelated questions.
>

I think we take a different approach -- struct-updaters always specifies a
contract of (-> struct-id? any/c struct-id) whereas make-functional-setters
offers the option to not have a contract or to specify whatever contract
you want.  I'm a firm believer in the idea that you should be allowed to
shoot yourself in the foot if you want the ability to do so.


> > On May 26, 2018, at 10:46, David Storrs 
> > wrote:
> >
> > A) Is there a way to test if a syntax class has a particular attribute
> > before trying to use it?
>
> Yes, use the attribute form. If x is an optional attribute, (attribute
> x) will be #f if the attribute was not bound and the value of the
> attribute if it was bound. If you want, you can change the default value
> to something else other than #f by providing the #:defaults option to
> ~optional.
>

Handy, thanks.


> > B) Alternatively, is there a way to create a null syntax object that
> > expands to nothing?  Not (void), not "", literally nothing.   Then I
> > could have each pattern bind all the attributes via #:with and just
> > have some of them be blank.
>
> Not in an arbitrary context. In a definition context, (begin)
> effectively expands into nothing, since begins are spliced into the
> enclosing context, but in an expression context, you can’t have
> something that expands into nothing.
>
> That said, it sounds like what you might actually want is the template
> and ?? forms from syntax/parse/experimental/template. This allows you
> to write something like this:
>
> (template (foo (?? x)))
>
> The above will be like #'(foo x) if (attribute x) is non-#f, but if it
> is #f, it will be like #'(foo). In Racket 6.12 and earlier, you must use
> the template form for ?? to work, but in Racket 7 and later, ?? will
> also work with the ordinary syntax (aka #') form, so if the word
> “experimental” spooks you, don’t worry about it too much.
>
> Alexis
>
>
This is really helpful, thank you.

-- 
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] Unmap failed

2018-05-28 Thread pavelrodzevich
Hello, my name is Pavel and I'm new to Racket :)

I'm playing with macros right now and get a strange error: "unmap failed".

What I'm trying to achieve in this program: expand (step x) into (+ (expt 2 
0) (expt 2 1) ...)

For small numbers everything is OK, but I get an error for (step 100)

What does this error mean and how can I fix it?

Code is in attachements. 


-- 
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.


macro-step.rkt
Description: Binary data


Re: [racket-users] Module dependencies

2018-05-28 Thread 韋嘉誠
(require) binds at compile time and creates a cycle, but by using
(dynamic-require) you can get past compilation and only load the file at
runtime, where the seemingly circular reference really isn't.

-- 
   /c

On Sun, May 27, 2018, 23:48 Matthias Felleisen 
wrote:

>
> Modules cannot refer to each other in a cyclic fashion, including
> submodules. — Matthias
>
>
>
> On May 27, 2018, at 8:33 AM, Brandon Irizarry 
> wrote:
>
> Say I have two files, "file-a.rkt" and "file-b.rkt" that each contain a
> submodule test, like so:
>
>
> Contents of "file-a.rkt":
>
> #lang racket/base
> (define (my-function) 'apple)
> (module+ test
> (require "file-b.rkt")
> (other-function))
>
> Contents of "file-b.rkt":
>
> #lang racket/base
> (define (other-function) 'orange)
> (module+ test
> (require "file-a.rkt")
> (my-function))
>
> The require statements form a circular reference, even though running
> file-b, along with its tests, shouldn't trigger file-a's tests.
>
> I've looked into compiling file-a and file-b, but that didn't work.
>
> - Brandon
>
>
> --
> 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.
>

-- 
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.