Re: [racket-users] TR: cast on mutable hash table...

2020-02-14 Thread Ben Greenman
On 2/14/20, 'John Clements' via users-redirect  wrote:
> I think I may understand what’s going on here, but a student and I worked on
> this for quite a while today before I found the problem.
>
> 
>
> In this case, one easy error is to change the ‘cast’ into an ‘ann’, which
> works fine.

You can also declare a type for `top-store`, instead of the hash expression:


#lang typed/racket

(define-type Store (Mutable-HashTable Integer Value))
(define-type Value (U Real Boolean String))

(: top-store Store)
(define top-store
  (make-hash (list (cons -1 14)
   (cons 1 #t)
   (cons 2 #f

(hash-set! top-store 5 1234)

-- 
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/CAFUu9R6rzKL_A8H2YjiDhS_JT7W%3DZ%3DTq8hVmccsQTTu9f%3DDrcQ%40mail.gmail.com.


Re: [racket-users] TR: cast on mutable hash table...

2020-02-14 Thread Stephen Chang
Your second suggestion is described in this (hard to find) section of
the TR Guide on Type Generalization:
https://docs.racket-lang.org/ts-guide/caveats.html#%28part._.Type_generalization%29

I agree it should be much more prominent. At the very least, any docs
for invariant constructors should link to that section I think?

On Fri, Feb 14, 2020 at 2:41 PM 'John Clements' via users-redirect
 wrote:
>
> I think I may understand what’s going on here, but a student and I worked on 
> this for quite a while today before I found the problem.
>
> Here’s a program:
>
> #lang typed/racket
>
> (define-type Store (Mutable-HashTable Integer Value))
> (define-type Value (U Real Boolean String))
>
> (define top-store (cast (make-hash (list
> (cons -1 14)
> (cons 1 #t)
> (cons 2 #f)))
> Store))
>
> (hash-set! top-store 5 1234)
>
> It fails with this error:
>
> contract violation
>   expected: (or/c (and/c byte? positive?) #t #f)
>   given: 1234
>   in: the values of
>   the 3rd conjunct of
>   (and/c
>hash?
>hash-mutable?
>(hash/c
> exact-integer?
> (or/c (and/c byte? positive?) #t #f)
> #:immutable
> #f))
>   contract from: typed-world
>   blaming: cast
>(assuming the contract is correct)
>   at: unsaved-editor:6.18
>
> If I understand what’s going on here, the basic issue is that the mutable 
> hash table’s type is being inferred as (Immutable-HashTable Exact-Integer (U 
> Boolean Positive-Byte)), and then as part of the cast, a contract is being 
> inserted, which checks that all added values match the expected value type. 
> The outer cast allows type checking to proceed, but then at runtime it fails 
> because the given value doesn’t match the inferred value type.
>
> This error doesn’t occur with immutable hash tables, because it’s fine to 
> extend an immutable hash table to a larger one that contains it; the original 
> one’s contract isn’t violated.
>
> In this case, one easy error is to change the ‘cast’ into an ‘ann’, which 
> works fine.
>
> This is the first time I’ve encouraged my students to use a mutable hash, 
> which is presumably why I haven’t encountered this before.
>
> I’m trying to formulate a solution that I can put in a Hints for TR file, and 
> I think the answer is probably this:
>
> - When you use “make-hash” in TR, you should always specify the types 
> explicitly using an “inst”.
> or maybe
> - When you call “make-hash” in TR, the call should be immediately wrapped 
> with an “ann” type annotation.
>
> In a perfect world, I think I would ask for a warning when casting a mutable 
> hash table. It could go in that nice “warnings” box. Oh, wait…
>
> … joking aside, actually I just did turn on the “log” window and I don’t see 
> any warning about this, which is not too surprising. Oh, wait, I see another 
> bug…
>
> Thanks for reading, let me know if there’s any more obvious solution.
>
> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/f24851ca-32aa-40e1-9db4-691da815ebe7%40mtasv.net.

-- 
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/CAFfiA1L1MzEsLmhR9xxyd0j-%3D7c_7Ak-gMz609m_D7sE2wfSmQ%40mail.gmail.com.


[racket-users] TR: cast on mutable hash table...

2020-02-14 Thread 'John Clements' via users-redirect
I think I may understand what’s going on here, but a student and I worked on 
this for quite a while today before I found the problem.

Here’s a program:

#lang typed/racket

(define-type Store (Mutable-HashTable Integer Value))
(define-type Value (U Real Boolean String))

(define top-store (cast (make-hash (list
(cons -1 14)
(cons 1 #t)
(cons 2 #f)))
Store))

(hash-set! top-store 5 1234)

It fails with this error:

contract violation
  expected: (or/c (and/c byte? positive?) #t #f)
  given: 1234
  in: the values of
  the 3rd conjunct of
  (and/c
   hash?
   hash-mutable?
   (hash/c
exact-integer?
(or/c (and/c byte? positive?) #t #f)
#:immutable
#f))
  contract from: typed-world
  blaming: cast
   (assuming the contract is correct)
  at: unsaved-editor:6.18

If I understand what’s going on here, the basic issue is that the mutable hash 
table’s type is being inferred as (Immutable-HashTable Exact-Integer (U Boolean 
Positive-Byte)), and then as part of the cast, a contract is being inserted, 
which checks that all added values match the expected value type. The outer 
cast allows type checking to proceed, but then at runtime it fails because the 
given value doesn’t match the inferred value type.

This error doesn’t occur with immutable hash tables, because it’s fine to 
extend an immutable hash table to a larger one that contains it; the original 
one’s contract isn’t violated.

In this case, one easy error is to change the ‘cast’ into an ‘ann’, which works 
fine.

This is the first time I’ve encouraged my students to use a mutable hash, which 
is presumably why I haven’t encountered this before.

I’m trying to formulate a solution that I can put in a Hints for TR file, and I 
think the answer is probably this:

- When you use “make-hash” in TR, you should always specify the types 
explicitly using an “inst”.
or maybe
- When you call “make-hash” in TR, the call should be immediately wrapped with 
an “ann” type annotation.

In a perfect world, I think I would ask for a warning when casting a mutable 
hash table. It could go in that nice “warnings” box. Oh, wait…

… joking aside, actually I just did turn on the “log” window and I don’t see 
any warning about this, which is not too surprising. Oh, wait, I see another 
bug…

Thanks for reading, let me know if there’s any more obvious solution.

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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/f24851ca-32aa-40e1-9db4-691da815ebe7%40mtasv.net.


Re: [racket-users] Racket v7.6

2020-02-14 Thread Juan Carlos Olivo
Thanks for the Valentine in the splash screen!

-- 
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/ffc0fb68-f6da-4be8-8feb-6db8a48cdd78%40googlegroups.com.


Re: [racket-users] How to stream file uploads with the Racket web server?

2020-02-14 Thread Brian Adkins
Hmm... I don't see a new message in the thread, so I'm not sure what you 
approved. No worries though.

On Friday, February 14, 2020 at 7:36:26 AM UTC-5, Robby Findler wrote:
>
> It is possible that your post wasn't deleted but got held up in 
> google's spam traps. I just approved a message from you (that I was 
> alerted to only this morning). Was that the message? 
>
> Robby 
>
> On Thu, Feb 13, 2020 at 2:57 PM Brian Adkins  > wrote: 
> > 
> > I tried replying earlier today, but somehow the post got deleted - 
> could've been user error I suppose. 
> > 
> > Anyway, the gist of the response was how I continue to be amazed by how 
> often I get pleasant surprises with Racket - either there is some facility 
> to do what I want that I just haven't found yet, or in this case where 
> there soon will be :) 
> > 
> > I have a couple Racket web apps in production now, so I think I have 
> enough real world examples to begin extracting code into a web framework. 
> Although it will look significantly different than Rails (more functional, 
> less object oriented), I'm hoping to achieve similar ease-of-use 
> functionality. I first viewed the Rails "weblog in 15 minutes" video 
> fourteen years ago, and it had a significant impact on my professional life 
> (at the time I was working in Java w/ Spring & Hibernate, etc.). The video 
> is dated now & has some annoying idiosyncrasies, but if you haven't seen 
> it, and you're interested in web development (in any language), it's worth 
> viewing just to get the overview of Rails ease of use: 
> > 
> > https://www.youtube.com/watch?v=Gzj723LkRJY=youtu.be 
> > 
> > On Thursday, February 13, 2020 at 11:02:33 AM UTC-5, bogdan wrote: 
> >> 
> >> The version of the web-server that will be included with Racket 7.6 
> >> changes the way file uploads are handled so that they get offloaded to 
> >> disk after a certain threshold (similar to how that nginx module you 
> >> linked to works). 
> >> 
> >> You can check out the pre-release docs for details: 
> >> 
> >> * 
> https://pre-release.racket-lang.org/doc/web-server/http.html?q=binding%3Afile#%28def._%28%28lib._web-server%2Fhttp%2Frequest-structs..rkt%29._make-binding~3afile%2Fport%29%29
>  
> >> * 
> https://pre-release.racket-lang.org/doc/web-server-internal/dispatch-server-unit.html#%28part._safety-limits%29
>  
> >> 
> >> To get these changes ahead of the release, you should be able to 
> install 
> >> an updated version of `web-server-lib' from the package server or from 
> git. 
> >> 
> >> Hope that helps! 
> >> 
> >> - Bogdan 
> >> 
> >> Brian Adkins writes: 
> >> 
> >> > I'm posting a file to my web app using the following form: 
> >> > 
> >> >  >> >   method="post" *enctype="multipart/form-data"* 
> >> > class="file-upload-form"> 
> >> > ... 
> >> >  
> >> > 
> >> > I use a simple function to create a hashtable of attributes: 
> >> > 
> >> > (define (form-values req) 
> >> >   (for/hash ([ b (in-list (request-bindings/raw req)) ]) 
> >> > (cond [ (binding:form? b) (values 
> >> >(bytes->string/utf-8 (binding-id b) 
> #\space) 
> >> >(bytes->string/utf-8 
> (binding:form-value b) 
> >> > #\space)) ] 
> >> >   [ (binding:file? b) (values 
> >> >(bytes->string/utf-8 (binding-id b) 
> #\space) 
> >> >(binding:file-content b)) ]))) 
> >> > 
> >> > It appears that the entire file contents are in the binding by the 
> time the 
> >> > request is available to me. This is fine for "small enough" files, 
> but for 
> >> > larger files, it would be great to be able to stream the file 
> contents. The 
> >> > solution may be to use something like nginx's upload module: 
> >> > 
> >> > https://www.nginx.com/resources/wiki/modules/upload/ 
> >> > 
> >> > But before I go down that route, I thought I'd ask if the Racket web 
> server 
> >> > provides a more direct way to accomplish this. 
> >> > 
> >> > Thanks, 
> >> > Brian 
> > 
> > -- 
> > 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...@googlegroups.com . 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/6a3298ff-a40c-4a6e-9f16-b1c95144e482%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/278f757e-1965-4298-a3e1-6483f06e7d14%40googlegroups.com.


Re: [racket-users] How to stream file uploads with the Racket web server?

2020-02-14 Thread Robby Findler
It is possible that your post wasn't deleted but got held up in
google's spam traps. I just approved a message from you (that I was
alerted to only this morning). Was that the message?

Robby

On Thu, Feb 13, 2020 at 2:57 PM Brian Adkins  wrote:
>
> I tried replying earlier today, but somehow the post got deleted - could've 
> been user error I suppose.
>
> Anyway, the gist of the response was how I continue to be amazed by how often 
> I get pleasant surprises with Racket - either there is some facility to do 
> what I want that I just haven't found yet, or in this case where there soon 
> will be :)
>
> I have a couple Racket web apps in production now, so I think I have enough 
> real world examples to begin extracting code into a web framework. Although 
> it will look significantly different than Rails (more functional, less object 
> oriented), I'm hoping to achieve similar ease-of-use functionality. I first 
> viewed the Rails "weblog in 15 minutes" video fourteen years ago, and it had 
> a significant impact on my professional life (at the time I was working in 
> Java w/ Spring & Hibernate, etc.). The video is dated now & has some annoying 
> idiosyncrasies, but if you haven't seen it, and you're interested in web 
> development (in any language), it's worth viewing just to get the overview of 
> Rails ease of use:
>
> https://www.youtube.com/watch?v=Gzj723LkRJY=youtu.be
>
> On Thursday, February 13, 2020 at 11:02:33 AM UTC-5, bogdan wrote:
>>
>> The version of the web-server that will be included with Racket 7.6
>> changes the way file uploads are handled so that they get offloaded to
>> disk after a certain threshold (similar to how that nginx module you
>> linked to works).
>>
>> You can check out the pre-release docs for details:
>>
>> * 
>> https://pre-release.racket-lang.org/doc/web-server/http.html?q=binding%3Afile#%28def._%28%28lib._web-server%2Fhttp%2Frequest-structs..rkt%29._make-binding~3afile%2Fport%29%29
>> * 
>> https://pre-release.racket-lang.org/doc/web-server-internal/dispatch-server-unit.html#%28part._safety-limits%29
>>
>> To get these changes ahead of the release, you should be able to install
>> an updated version of `web-server-lib' from the package server or from git.
>>
>> Hope that helps!
>>
>> - Bogdan
>>
>> Brian Adkins writes:
>>
>> > I'm posting a file to my web app using the following form:
>> >
>> > > >   method="post" *enctype="multipart/form-data"*
>> > class="file-upload-form">
>> > ...
>> > 
>> >
>> > I use a simple function to create a hashtable of attributes:
>> >
>> > (define (form-values req)
>> >   (for/hash ([ b (in-list (request-bindings/raw req)) ])
>> > (cond [ (binding:form? b) (values
>> >(bytes->string/utf-8 (binding-id b) #\space)
>> >(bytes->string/utf-8 (binding:form-value b)
>> > #\space)) ]
>> >   [ (binding:file? b) (values
>> >(bytes->string/utf-8 (binding-id b) #\space)
>> >(binding:file-content b)) ])))
>> >
>> > It appears that the entire file contents are in the binding by the time the
>> > request is available to me. This is fine for "small enough" files, but for
>> > larger files, it would be great to be able to stream the file contents. The
>> > solution may be to use something like nginx's upload module:
>> >
>> > https://www.nginx.com/resources/wiki/modules/upload/
>> >
>> > But before I go down that route, I thought I'd ask if the Racket web server
>> > provides a more direct way to accomplish this.
>> >
>> > Thanks,
>> > Brian
>
> --
> 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/6a3298ff-a40c-4a6e-9f16-b1c95144e482%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/CAL3TdOO%3DgfxA3H6o92qmSogygwRaTMZHYi62Sio_db6%2BkrKzKA%40mail.gmail.com.


Re: [racket-users] How to stream file uploads with the Racket web server?

2020-02-14 Thread Brian Adkins
Will Racket's pleasant surprises never end? :) This is great news! 

I've had a blast coding web applications in Racket over the last 15 months. 
I now understand "The Lisp Curse" a bit more :)

http://winestockwebdesign.com/Essays/Lisp_Curse.html

In other words, developing web apps with what is already provided by 
Racket, and a few packages, has been easy & enjoyable enough that it's 
delayed my work on a web framework. That's probably been for the best 
because now I have a few real world examples from which I can begin to 
extract the code that belongs in a general framework. I'll be resuming that 
work this month.

I watched the "how to create a blog in 15 minutes w/ Rails" video 14 years 
ago, and it had a huge impact on my work. There are still some very nice 
things to steal from Rails, and a whole lot of things to leave :) If you're 
interested in web development, and you haven't seen this video, I highly 
recommend it. There are some annoyances for sure, but be patient.

https://www.youtube.com/watch?v=Gzj723LkRJY=youtu.be


On Thursday, February 13, 2020 at 11:02:33 AM UTC-5, bogdan wrote:
>
> The version of the web-server that will be included with Racket 7.6 
> changes the way file uploads are handled so that they get offloaded to 
> disk after a certain threshold (similar to how that nginx module you 
> linked to works). 
>
> You can check out the pre-release docs for details: 
>
> * 
> https://pre-release.racket-lang.org/doc/web-server/http.html?q=binding%3Afile#%28def._%28%28lib._web-server%2Fhttp%2Frequest-structs..rkt%29._make-binding~3afile%2Fport%29%29
>  
> * 
> https://pre-release.racket-lang.org/doc/web-server-internal/dispatch-server-unit.html#%28part._safety-limits%29
>  
>
> To get these changes ahead of the release, you should be able to install 
> an updated version of `web-server-lib' from the package server or from 
> git. 
>
> Hope that helps! 
>
> - Bogdan 
>
> Brian Adkins writes: 
>
> > I'm posting a file to my web app using the following form: 
> > 
> >  >   method="post" *enctype="multipart/form-data"* 
> > class="file-upload-form"> 
> > ... 
> >  
> > 
> > I use a simple function to create a hashtable of attributes: 
> > 
> > (define (form-values req) 
> >   (for/hash ([ b (in-list (request-bindings/raw req)) ]) 
> > (cond [ (binding:form? b) (values 
> >(bytes->string/utf-8 (binding-id b) 
> #\space) 
> >(bytes->string/utf-8 (binding:form-value 
> b) 
> > #\space)) ] 
> >   [ (binding:file? b) (values 
> >(bytes->string/utf-8 (binding-id b) 
> #\space) 
> >(binding:file-content b)) ]))) 
> > 
> > It appears that the entire file contents are in the binding by the time 
> the 
> > request is available to me. This is fine for "small enough" files, but 
> for 
> > larger files, it would be great to be able to stream the file contents. 
> The 
> > solution may be to use something like nginx's upload module: 
> > 
> > https://www.nginx.com/resources/wiki/modules/upload/ 
> > 
> > But before I go down that route, I thought I'd ask if the Racket web 
> server 
> > provides a more direct way to accomplish this. 
> > 
> > Thanks, 
> > Brian 
>

-- 
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/d9c81358-1e32-438e-88cb-433bdd71ebd2%40googlegroups.com.