Re: [racket-users] How to handle circular 'requires'

2018-05-14 Thread Matthew Butterick
To add to your pile of options, because you said "certain error conditions": 
perhaps you might `raise` an exception in the db code that gets caught by the 
network code (using `with-handlers`). In this way the dependency only runs one 
direction (network module imports db module) but the db module can still 
propagate messages to the network module (by going "backward" through the 
existing calling chain with an exception, rather than "forward" by calling into 
the db module). 

Here is a toy example of a `db` module that keeps kicking back an exception to 
a `network` module until it gets zero, no circularity needed:

#lang racket

(module db racket
  (provide f)
  (define (f val)
(if (zero? val)
'finally-a-zero!
(raise val

(module network racket
  (require (submod ".." db))
  (let loop ([maybe-err-int #f])
(with-handlers ([integer? loop])
  (when maybe-err-int
(displayln (number->string maybe-err-int)))
  (f (random 10)

(require 'network)


> On May 14, 2018, at 10:28 AM, David Storrs  wrote:
> 
> This worked fine until now, but I've gotten to a point where they're
> circular -- the network code needs to receive the chunk and then
> forward it to the DB code, but if certain error conditions come up
> then the DB code needs to tell the network code to re-request the
> data.
> 
> There's various ways I could work around this (simplest being to put
> all the functions in one file), but I'm wondering if there's a
> recommended way?  C would solve this with a .h file.  Perl would solve
> it with function prototypes (or simply be able to sort it out without
> intervention).  What is the preferred Racket way, or am I simply not
> thinking about it correctly?

-- 
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] How to handle circular 'requires'

2018-05-14 Thread David Storrs
On Mon, May 14, 2018 at 10:07 PM, Greg Hendershott
 wrote:
> David you wrote "both" so I just wanted to point out I read three ways:

Good point; I misspoke.  (Mistyped?)

> 4. It doesn't sound like this is your case, but: If there are many
> function calls where you pass the extra parameter, and that's
> annoying? Keep in mind you can use a `make-parameter` parameter. (You
> can even do both, e.g. there's an optional argument, the default value
> of which is the current parameter value.) Of course, that requires
> some new .rkt file where you define and provide the parameter, and the
> other files need to require _that_.  Whether that's a bug or a
> feature, depends.

Good thought.  I'm using make-parameter is several places already, so
expanding it for this could be useful.


> Speaking of parameters: Sometimes it can make sense for an app to have
> a config.rkt where parameters are defined. Similarly-named environment
> variables are read on startup. For example, as one part of a so-called
> 12 factor app. But this is orthogonal to what folks were discussing
> above. For instance you're not going to read Racket function values
> from the environment. :)

We have one for our database parameters, so it would be easy enough to
expand.  Thanks for the idea.

-- 
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] How to handle circular 'requires'

2018-05-14 Thread Greg Hendershott
David you wrote "both" so I just wanted to point out I read three ways:

1. Units
2. Callbacks (pass the function as a parameter).
3. Lazy-requires

In a simple case (maybe yours?) I'd suggest trying 2 (callbacks)
first, and see how that works for you in practice.

Also:

4. It doesn't sound like this is your case, but: If there are many
function calls where you pass the extra parameter, and that's
annoying? Keep in mind you can use a `make-parameter` parameter. (You
can even do both, e.g. there's an optional argument, the default value
of which is the current parameter value.) Of course, that requires
some new .rkt file where you define and provide the parameter, and the
other files need to require _that_.  Whether that's a bug or a
feature, depends.

~~~

Speaking of parameters: Sometimes it can make sense for an app to have
a config.rkt where parameters are defined. Similarly-named environment
variables are read on startup. For example, as one part of a so-called
12 factor app. But this is orthogonal to what folks were discussing
above. For instance you're not going to read Racket function values
from the environment. :)

-- 
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] How to handle circular 'requires'

2018-05-14 Thread David Storrs
Brilliant.  I knew there had to be a clean way and both of these sound
good.  Thanks.

Dave

On Mon, May 14, 2018 at 4:56 PM, Alexis King  wrote:
> In addition to what Matthias says, you can also sometimes break these
> kinds of cycles using lazy-require, which defers the requiring the other
> module until it is first needed. This is simpler than using units and
> provides stronger guarantees than using callbacks, but it is a bit more
> ad-hoc than both of them, and it only works when the lazily-loaded
> module isn’t needed as part of module initialization (that is, the
> function isn’t called from the top level of a module). When it works,
> though, it can be a less invasive solution to the problem.
>
> Alexis
>
>> On May 14, 2018, at 4:28 PM, David Storrs  wrote:
>>
>> My application retrieves chunks of files from the network and writes
>> them to disk, as well as writing certain data about them to the
>> database (e.g. where they are on disk and where they came from on the
>> network).
>>
>> I've split these functions into separate files:
>>
>> app/network/chunks.rkt
>> app/lib/db/chunks.rkt
>>
>> This worked fine until now, but I've gotten to a point where they're
>> circular -- the network code needs to receive the chunk and then
>> forward it to the DB code, but if certain error conditions come up
>> then the DB code needs to tell the network code to re-request the
>> data.
>>
>> There's various ways I could work around this (simplest being to put
>> all the functions in one file), but I'm wondering if there's a
>> recommended way?  C would solve this with a .h file.  Perl would solve
>> it with function prototypes (or simply be able to sort it out without
>> intervention).  What is the preferred Racket way, or am I simply not
>> thinking about it correctly?
>

-- 
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] How to handle circular 'requires'

2018-05-14 Thread Alexis King
In addition to what Matthias says, you can also sometimes break these
kinds of cycles using lazy-require, which defers the requiring the other
module until it is first needed. This is simpler than using units and
provides stronger guarantees than using callbacks, but it is a bit more
ad-hoc than both of them, and it only works when the lazily-loaded
module isn’t needed as part of module initialization (that is, the
function isn’t called from the top level of a module). When it works,
though, it can be a less invasive solution to the problem.

Alexis

> On May 14, 2018, at 4:28 PM, David Storrs  wrote:
> 
> My application retrieves chunks of files from the network and writes
> them to disk, as well as writing certain data about them to the
> database (e.g. where they are on disk and where they came from on the
> network).
> 
> I've split these functions into separate files:
> 
> app/network/chunks.rkt
> app/lib/db/chunks.rkt
> 
> This worked fine until now, but I've gotten to a point where they're
> circular -- the network code needs to receive the chunk and then
> forward it to the DB code, but if certain error conditions come up
> then the DB code needs to tell the network code to re-request the
> data.
> 
> There's various ways I could work around this (simplest being to put
> all the functions in one file), but I'm wondering if there's a
> recommended way?  C would solve this with a .h file.  Perl would solve
> it with function prototypes (or simply be able to sort it out without
> intervention).  What is the preferred Racket way, or am I simply not
> thinking about it correctly?

-- 
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] How to handle circular 'requires'

2018-05-14 Thread Matthias Felleisen

> On May 14, 2018, at 4:28 PM, David Storrs  wrote:
> 
> My application retrieves chunks of files from the network and writes
> them to disk, as well as writing certain data about them to the
> database (e.g. where they are on disk and where they came from on the
> network).
> 
> I've split these functions into separate files:
> 
> app/network/chunks.rkt
> app/lib/db/chunks.rkt
> 
> This worked fine until now, but I've gotten to a point where they're
> circular -- the network code needs to receive the chunk and then
> forward it to the DB code, but if certain error conditions come up
> then the DB code needs to tell the network code to re-request the
> data.
> 
> There's various ways I could work around this (simplest being to put
> all the functions in one file), but I'm wondering if there's a
> recommended way?  C would solve this with a .h file.  Perl would solve
> it with function prototypes (or simply be able to sort it out without
> intervention).  What is the preferred Racket way, or am I simply not
> thinking about it correctly?



You may wish to consider Units, the second module system. 
It was designed for

— mutually referential references among the pieces
— dynamic loading (they are first-class values)

and the example in the first paper about Units comes with an 
example about DBs and Display units that refer to each other
so that they can deal with errors. 

See https://www2.ccs.neu.edu/racket/pubs/#pldi98-ff 

Ignore the material on types. 

;; - - - 

A simpler way to fix this problem (with fewer guarantees) is to 
use callbacks. 

So if 

module A contains f, which refers to g from module B 
module B contains g and h, and the latter refers to k from A 

break the cycle with a callbacks. That is, parameterize f over g, 
and wherever you call f, ask users to pass in the function g from B. 

— 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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] How to handle circular 'requires'

2018-05-14 Thread David Storrs
My application retrieves chunks of files from the network and writes
them to disk, as well as writing certain data about them to the
database (e.g. where they are on disk and where they came from on the
network).

I've split these functions into separate files:

app/network/chunks.rkt
app/lib/db/chunks.rkt

This worked fine until now, but I've gotten to a point where they're
circular -- the network code needs to receive the chunk and then
forward it to the DB code, but if certain error conditions come up
then the DB code needs to tell the network code to re-request the
data.

There's various ways I could work around this (simplest being to put
all the functions in one file), but I'm wondering if there's a
recommended way?  C would solve this with a .h file.  Perl would solve
it with function prototypes (or simply be able to sort it out without
intervention).  What is the preferred Racket way, or am I simply not
thinking about it correctly?

-- 
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] sxml:document

2018-05-14 Thread Neil Van Dyke



As soon as tools get into accessing resources via URLs from the XML (which I 
don't think is what you're doing, but it might be the next step for someone), 
it should be documented that there's a need for access control.


Sorry, I mostly meant to make a side comment for the list, and I 
overstated a little.


When we're developing tools that can have URIs within the XML that 
reference external resources, and we or other layered code might make 
requests for those URIs, we should be cautious.  Maybe it's also 
appropriate to document the delicate nature, even if our code layer 
itself is not vulnerable, since there are a lot of bad practices right 
now, and I think there's a need for remedial education.


ENGINEER: "Of course, in the real-world practice, no one could manage to 
do this insecurely."


WEBROGRAMMER: "Hold my Red Bull..."

:)


Honestly, I don’t really think that this package should have anything to do 
with resolving URIs; I’m tempted to redesign it in such a way that it accepts 
as an argument a function used to deal with the whole URI side of the world. 
Units, anyone?


Regarding units, if there's a problem to solve in code, and it can be 
done in the reusing API as closure optional keyword arguments, rather 
than as a units&sigs, I think the former is much easier for a reusing 
programmer.  (Units are a good idea for some purposes, but are not 
something many Racket programmers have ever had to use, so I think the 
payoff for mandating units for a given purpose should be big.  That 
said, newer Racket features, like submodules (and TR?), combined with a 
little syntactic sugar, could make units less of a bureaucratic burden 
to use than they were originally.)


--
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] sxml:document

2018-05-14 Thread 'John Clements' via Racket Users


> On May 13, 2018, at 11:42 PM, Neil Van Dyke  wrote:
> 
> John, thank you for your past&present work on the SXML stuff. Two comments:
> 
>> enforcing the “file://“ prefix.
> 
> Sounds like the following doesn't matter in this case, but another way to 
> support both a given URL or given filename is to resolve the given value as a 
> possibly relative URL, against a base URL that's a "file:" scheme URL for the 
> current working directory.

I’m not sure what you’re proposing here; the (now current) code simply strips 
off the “file://“ prefix and uses (IIRC) `file-exists?`. IIUC, that would 
support relative file urls. Is that what you’re proposing, or is that something 
different?

> 
>> I’ve made a change that appears to allow the use of “file://“ URIs with 
>> sxml:document,
> 
> As soon as tools get into accessing resources via URLs from the XML (which I 
> don't think is what you're doing, but it might be the next step for someone), 
> it should be documented that there's a need for access control.
> 
> For example, imagine an application in which user-supplied XML specifies a 
> (purported) schema with a "file:" or "http:" URL to some resource to which 
> user doesn't have access, but the XML processor does.  If the application (or 
> XML library it uses) attempts to access that URL too trustingly, then the 
> user maybe be able to cause some privileged side-effect (e.g., manipulate a 
> database, or control an IoT device), or learn some or all of the content at a 
> URL (via, e.g., a too-detailed error message), or learn something about the 
> system (e.g., something more about its location/operator/implementation, if 
> it makes an outgoing request to a server user can monitor).

I’ll be honest: I don’t think that the current system can resolve any http 
references (in fact, I see code that handles that which is commented out), and 
I also think that it’s not designed to resolve remote schemas, but I’d love to 
have a more detailed example, if you have one handy. 

Honestly, I don’t really think that this package should have anything to do 
with resolving URIs; I’m tempted to redesign it in such a way that it accepts 
as an argument a function used to deal with the whole URI side of the world. 
Units, anyone?

John

-- 
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] Macro from list to class init

2018-05-14 Thread Matthias Felleisen

> On May 14, 2018, at 6:55 AM, Denis Michiels  wrote:
> 
> Hello,
> 
> I'm trying to build a macro to be able to do :
> 
> ```
> (define data (list (cons 'label "My button")
>   (cons 'stretchable-width #t)))
> 
> (my-macro button% data)
> ```
> 
> to be translated in  :
> 
> ```
> (new button% [label "My button"] [stretchable-width #t])
> ```
> 
> (I take gui example, and incomplete (like no frame) but it is to show my
> goal)
> 
> Unfortunately, I didn't manage well macro in Racket...
> Can someone help me, or have a hint how to manage this macro?
>  

#lang racket/gui

(define data '((label "My button") (stretchable-width #t)))

(define-syntax (mk-but stx)
  (syntax-case stx ()
[(_ data)
 #'(let ()
 (match-define (cons label others) data)
 (define button (make-object button% (second label) frame))
 (for ((o others))
   (match-define (list m val) o)
   (dynamic-send button m val)))]))


(define frame (new frame% [label "test"]))

(mk-but data)

(send frame show #t)

-- 
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] Edmond's Blossom Algorithm

2018-05-14 Thread Jens Axel Søgaard
Context:

I have students A, B, C, ..., Z that needs to work in pairs for their exam.
Each student has made a wish list with 3 other students that they'd like to
work with.
I need to find the maximum possible pairing.

I think - maybe - that the algorithm I need is Edmond's blossom algorithm.

Am I so lucky that someone has this in Racket?

https://en.wikipedia.org/wiki/Blossom_algorithm
http://www.cs.dartmouth.edu/~ac/Teach/CS105-Winter05/Handouts/tarjan-blossom.pdf

/Jens Axel

-- 
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] Macro from list to class init

2018-05-14 Thread Denis Michiels
Hello,

I'm trying to build a macro to be able to do :

```
(define data (list (cons 'label "My button")
   (cons 'stretchable-width #t)))

(my-macro button% data)
```

to be translated in  :

```
(new button% [label "My button"] [stretchable-width #t])
```

(I take gui example, and incomplete (like no frame) but it is to show my
goal)

Unfortunately, I didn't manage well macro in Racket...
Can someone help me, or have a hint how to manage this macro?

Thank you in advance,
Denis

-- 
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] S10 - a new commercial product in Racket

2018-05-14 Thread 'Paulo Matos' via Racket Users
Hi all,

I was discussing this in racket-money and someone suggested I should
quickly reference it here.

S10 (https://linki.tools/s10) is a superoptimization framework in Racket.

If you are familiar with Mangpo's work on Greenthumb you know what I am
talking about. This work follows a similar pattern but builds a
commercial product on top of the Racket ecosystem.

A bit of background:

At Linki Tools (https://linki.tools) we have been interested in
development tools consultancy and for a few years now we have been
developing compiler optimizations and backends for GCC and LLVM, as well
as testing and verification frameworks for compiler technology.

Throughout my time in this market I have seen a few places where the
existence of a superoptimizer would have been welcomed however, there
was nothing available that could be easily retargetable to other
architectures (usually clients have their own internal cpu archs that
are not publicly available). I found Greenthumb back in 2016 and
contacted Mangpo back then, however after some careful study of the code
and some upstream patches I started heavily changing the code, to the
point where incremental upstream patches became unfeasible.

I ended up rewriting most of Greenthumb and adding many other features
expected by clients of a commercial product like straightforward
profiling and debugging, better error messages, distributed execution, a
plugin framework for internals extension, etc. Because of this S10
stopped working with the backends developed for Greenthumb and I decided
to target instead as a proof of concept RISC-V architectures rv32i,
rv64i, rv128i. This led me to present this work last week in Barcelona
in the RISC-V Workshop.

A lot of things remain to be done but I didn't want to let the
opportunity pass to thank everyone who is part of the Racket community
and made this possible. Thank you for the amazing Racket ecosystem.

Kind regards,
-- 
Paulo Matos

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