Re: [racket-users] Sequences in Typed Racket?
Yep, this works. Of course, it assumes that fibonacci is memoized or something to give good performance. #lang typed/racket (require math (for-syntax syntax/parse)) (: check-nat (-> Any Any)) (define (check-nat n) (unless (exact-nonnegative-integer? n) (raise-argument-error 'in-fibonacci "Natural" n))) (define-sequence-syntax in-fibonacci (λ (stx) (raise-syntax-error #f "only allowed in a for clause" stx)) (syntax-parser [[(name:id) (_ (~optional start-expr:expr #:defaults ([start-expr #'0])))] #'[(name) (:do-in ([(start) start-expr]) (check-nat start) ([n start]) #true ([(name) (fibonacci n)]) #true #true [(add1 n)])]])) (for/list : (Listof Integer) ([fib (in-fibonacci 5)] [i (in-range 10)]) fib) -Philip On Mon, Apr 23, 2018 at 3:10 PM, Philip McGrathwrote: > It looks like you copied the code incorrectly: it should be (module > adapter racket ...), but you have (module adapter racket/stream ...). > > To get the special performance you are referring to, you don't want to > make a structure that's a sequence: you need to use > `define-sequence-syntax` with `:do-in` (http://docs.racket-lang.org/ > reference/for.html#(form._((lib._racket%2Fprivate%2Fbase.. > rkt)._define-sequence-syntax))). I believe this should work with Typed > Racket, but I haven't tried it. > > -Philip > > On Mon, Apr 23, 2018 at 3:00 PM, HiPhish wrote: > >> The adapter submodule does not work, I get the following error (in both >> typed and untyped Racket): >> >> fib.rkt:9:0: module: no #%module-begin binding in the module's >> language >> in: (module adapter racket/stream (provide stream-first stream-rest >> (rename-out (stream-cons* stream-cons))) (define (stream-cons* make-first >> make-rest/seq) (stream-cons (make-first) (sequence->stream >> (make-rest/seq) >> >> If I understand your code correctly, the idea is to define `fibonacci` as >> a stream which returns a stream of Fibonacci numbers, correct? Won't that >> incur a performance loss when used like that in a for-loop if the user does >> not wrap it up in `in-stream`? The idea of `in-fibonacci` was to have a >> form which can be used in a foor-loop the same way `in-naturals` can be. >> Except `in-fibonacci` would have an optional argument at which Fibonacci >> number to start counting. >> >> >> On Sunday, April 22, 2018 at 10:48:12 AM UTC+2, Philip McGrath wrote: >>> >>> I hope there's a better way, but this works. The adapter submodule is >>> needed because the normal `stream-cons` is a macro that expands into some >>> private things that don't have types, and it requires that the rest >>> expression produce a stream, not just any sequence. Note also, if you >>> haven't worked with `racket/stream` before, that the arguments to the >>> normal `stream-cons` are evaluated lazily. >>> >> -- >> 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] Sequences in Typed Racket?
It looks like you copied the code incorrectly: it should be (module adapter racket ...), but you have (module adapter racket/stream ...). To get the special performance you are referring to, you don't want to make a structure that's a sequence: you need to use `define-sequence-syntax` with `:do-in` ( http://docs.racket-lang.org/reference/for.html#(form._((lib._racket%2Fprivate%2Fbase..rkt)._define-sequence-syntax))). I believe this should work with Typed Racket, but I haven't tried it. -Philip On Mon, Apr 23, 2018 at 3:00 PM, HiPhishwrote: > The adapter submodule does not work, I get the following error (in both > typed and untyped Racket): > > fib.rkt:9:0: module: no #%module-begin binding in the module's language > in: (module adapter racket/stream (provide stream-first stream-rest > (rename-out (stream-cons* stream-cons))) (define (stream-cons* make-first > make-rest/seq) (stream-cons (make-first) (sequence->stream > (make-rest/seq) > > If I understand your code correctly, the idea is to define `fibonacci` as > a stream which returns a stream of Fibonacci numbers, correct? Won't that > incur a performance loss when used like that in a for-loop if the user does > not wrap it up in `in-stream`? The idea of `in-fibonacci` was to have a > form which can be used in a foor-loop the same way `in-naturals` can be. > Except `in-fibonacci` would have an optional argument at which Fibonacci > number to start counting. > > > On Sunday, April 22, 2018 at 10:48:12 AM UTC+2, Philip McGrath wrote: >> >> I hope there's a better way, but this works. The adapter submodule is >> needed because the normal `stream-cons` is a macro that expands into some >> private things that don't have types, and it requires that the rest >> expression produce a stream, not just any sequence. Note also, if you >> haven't worked with `racket/stream` before, that the arguments to the >> normal `stream-cons` are evaluated lazily. >> > -- > 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] Sequences in Typed Racket?
The adapter submodule does not work, I get the following error (in both typed and untyped Racket): fib.rkt:9:0: module: no #%module-begin binding in the module's language in: (module adapter racket/stream (provide stream-first stream-rest (rename-out (stream-cons* stream-cons))) (define (stream-cons* make-first make-rest/seq) (stream-cons (make-first) (sequence->stream (make-rest/seq) If I understand your code correctly, the idea is to define `fibonacci` as a stream which returns a stream of Fibonacci numbers, correct? Won't that incur a performance loss when used like that in a for-loop if the user does not wrap it up in `in-stream`? The idea of `in-fibonacci` was to have a form which can be used in a foor-loop the same way `in-naturals` can be. Except `in-fibonacci` would have an optional argument at which Fibonacci number to start counting. On Sunday, April 22, 2018 at 10:48:12 AM UTC+2, Philip McGrath wrote: > > I hope there's a better way, but this works. The adapter submodule is > needed because the normal `stream-cons` is a macro that expands into some > private things that don't have types, and it requires that the rest > expression produce a stream, not just any sequence. Note also, if you > haven't worked with `racket/stream` before, that the arguments to the > normal `stream-cons` are evaluated lazily. > -- 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] Sequences in Typed Racket?
I hope there's a better way, but this works. The adapter submodule is needed because the normal `stream-cons` is a macro that expands into some private things that don't have types, and it requires that the rest expression produce a stream, not just any sequence. Note also, if you haven't worked with `racket/stream` before, that the arguments to the normal `stream-cons` are evaluated lazily. #lang typed/racket (module adapter racket (provide stream-first stream-rest (rename-out [stream-cons* stream-cons])) (define (stream-cons* make-first make-rest/seq) (stream-cons (make-first) (sequence->stream (make-rest/seq) (require/typed 'adapter [stream-first (All (A) (-> (Sequenceof A) A))] [stream-rest (All (A) (-> (Sequenceof A) (Sequenceof A)))] [stream-cons (All (A) (-> (-> A) (-> (Sequenceof A)) (Sequenceof A)))]) (: fibonacci (Sequenceof Integer)) (define fibonacci (stream-cons (λ () 0) (λ () (stream-cons (λ () 1) (λ () (let calculate : (Sequenceof Integer) ([fibonacci fibonacci]) (stream-cons (λ () (+ (stream-first fibonacci) (stream-first (stream-rest fibonacci (λ () (calculate (stream-rest fibonacci)) (for/list : (Listof Integer) ([fib fibonacci] [i (in-range 10)]) fib) -Philip On Sun, Apr 22, 2018 at 3:01 AM, HiPhishwrote: > Hello Racketeers, > > I have been playing around with the `math/number-theory` package and I > wanted to use a `for`-loop of Fibonacci numbers. I had to write something > like > > (for ([i (in-naturals)]) > (define fib (fibonacci i)) > ...) > > So I thought it would be nice to have an `in-fibonacci` sequence. > According to the Racket reference [1] one can implement a custom sequence > using structure type properties, but according to the Typed Racket guide > [2] structure type properties are not supported in Typed Racket. Is there > any other way to get `in-fibonacci` into the module or should I just live > without it? > > [1] https://docs.racket-lang.org/reference/sequences.html?q=sequences > [2] https://docs.racket-lang.org/ts-guide/caveats.html#%28part. > _.Unsupported_features%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. > -- 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] Sequences in Typed Racket?
Hello Racketeers, I have been playing around with the `math/number-theory` package and I wanted to use a `for`-loop of Fibonacci numbers. I had to write something like (for ([i (in-naturals)]) (define fib (fibonacci i)) ...) So I thought it would be nice to have an `in-fibonacci` sequence. According to the Racket reference [1] one can implement a custom sequence using structure type properties, but according to the Typed Racket guide [2] structure type properties are not supported in Typed Racket. Is there any other way to get `in-fibonacci` into the module or should I just live without it? [1] https://docs.racket-lang.org/reference/sequences.html?q=sequences [2] https://docs.racket-lang.org/ts-guide/caveats.html#%28part._.Unsupported_features%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.