Re: [racket-users] A bug in the Typed Racket system?

2017-08-11 Thread Sourav Datta
Thanks for the clarification.

On 09-Aug-2017 3:29 AM, "Sam Tobin-Hochstadt" <sa...@cs.indiana.edu> wrote:

> The reason that Typed Racket chooses the istantiation `Nothing` here
> is that because `a` is used in both a positive and negative position
> (both as an input and an output) in the type of the `val` field,
> there's no way to pick a "best" choice for `a`. `(Foo Integer)` is not
> a subtype of `(Foo Any)`, for example. Furthermore, whatever Typed
> Racket chooses for `a`, the function application will type check.
> Typed Racket therefore picks the smallest type that works, which is
> `Nothing`.
>
> This actually tells you more about your second program -- in
> particular, you can't ever call the `val` function from the `(Foo a)`,
> because you can't construct a value of type `a`.
>
> Sam
>
> On Fri, Jul 21, 2017 at 1:40 PM, Sourav Datta <soura.ja...@gmail.com>
> wrote:
> > On Friday, July 21, 2017 at 9:06:16 PM UTC+5:30, Ben Greenman wrote:
> >> Type inference is failing you again.
> >
> > Yes, well, that seems to be a common theme whenever I hover around the
> these type of areas which are uncommon but works perfectly fine in untyped
> Racket.
> >
> >>
> >>
> >> If you instantiate `foo/s`, you get the type you expect:
> >>
> >>
> >>
> >> #lang typed/racket
> >>
> >>
> >> (struct (a) Foo ([val : (-> a a)]))
> >>
> >>
> >> (: foo/s (All (a b)
> >>   (-> (-> a b)
> >>   (-> (Foo a)
> >>   (Foo b)
> >>   (Foo b)
> >> (define (foo/s f)
> >>   (λ ([f1 : (Foo a)]
> >>   [f2 : (Foo b)])
> >> f2))
> >>
> >>
> >>
> >>
> >> (define r1 (#{foo/s @ Integer String} (λ ([x : Integer]) (format "~a"
> x
> >>
> >> ;> r1
> >> ;- : (-> (Foo Integer) (Foo String) (Foo String))
> >
> > Yes, that works fine. For my personal use it is good enough, but
> exporting that function as a library would require every user to annotate
> the type whenever they want to call it, even for very simple types - which
> is not a very good experience for a typed language.
> >
> >>
> >>
> >>
> >>
> >> I wouldn't call this a bug, but it's definitely a program that a better
> type inferencer should accept.
> >
> > In other cases where type inference fails, the compiler bails out with
> an error (even though that is a confusing one like `expected: (Seq a)
> given: (Seq Integer)`). But here:
> >
> > 1. The inference did not fail but assumed it's Nothing just because I
> changed the function signature from (-> a) to (-> a a). I can't understand
> why the inference fails for this change. And on top of this,
> >
> > 2. The function type is clearly specified as (-> (Foo a) (Foo b) (Foo
> b)) and it can unify a = Integer, b = String, from the first argument
> alone. So, even though a is Integer, it still produces a = Nothing, without
> a compile error. Integer =/= Nothing. Seems very close to a bug in the type
> deduction algorithm.
> >
> >
> >>
> >>
> >> On Fri, Jul 21, 2017 at 5:19 AM, Sourav Datta <soura...@gmail.com>
> wrote:
> >> Consider this program,
> >>
> >>
> >>
> >> #lang typed/racket
> >>
> >>
> >>
> >> (struct (a) Foo ([val : (-> a)]))
> >>
> >>
> >>
> >> (: foo/s (All (a b)
> >>
> >>   (-> (-> a b)
> >>
> >>   (-> (Foo a)
> >>
> >>   (Foo b)
> >>
> >>   (Foo b)
> >>
> >> (define (foo/s f)
> >>
> >>   (λ ([f1 : (Foo a)]
> >>
> >>   [f2 : (Foo b)])
> >>
> >> f2))
> >>
> >>
> >>
> >>
> >>
> >> (define r1 (foo/s (λ ([x : Integer]) (format "~a" x
> >>
> >>
> >>
> >> The type of r1 is correctly deduced as:
> >>
> >>
> >>
> >> - : (-> (Foo Integer) (Foo String) (Foo String))
> >>
> >> #
> >>
> >>
> >>
> >> However, if I slightly change the struct Foo and do the same, like this:
> >>
> >>
> >>
> >> #lang typed/racket
> >>
> >>
> >>
> >> (struct (a) Foo (

Re: [racket-users] A bug in the Typed Racket system?

2017-07-21 Thread Sourav Datta
On Friday, July 21, 2017 at 9:06:16 PM UTC+5:30, Ben Greenman wrote:
> Type inference is failing you again.

Yes, well, that seems to be a common theme whenever I hover around the these 
type of areas which are uncommon but works perfectly fine in untyped Racket.

> 
> 
> If you instantiate `foo/s`, you get the type you expect:
> 
> 
> 
> #lang typed/racket
> 
> 
> (struct (a) Foo ([val : (-> a a)]))
> 
> 
> (: foo/s (All (a b)
>               (-> (-> a b)
>                   (-> (Foo a)
>                       (Foo b)
>                       (Foo b)
> (define (foo/s f)
>   (λ ([f1 : (Foo a)]
>       [f2 : (Foo b)])
>     f2))
> 
> 
> 
> 
> (define r1 (#{foo/s @ Integer String} (λ ([x : Integer]) (format "~a" x
> 
> ;> r1
> ;- : (-> (Foo Integer) (Foo String) (Foo String))

Yes, that works fine. For my personal use it is good enough, but exporting that 
function as a library would require every user to annotate the type whenever 
they want to call it, even for very simple types - which is not a very good 
experience for a typed language.

> 
> 
> 
> 
> I wouldn't call this a bug, but it's definitely a program that a better type 
> inferencer should accept.

In other cases where type inference fails, the compiler bails out with an error 
(even though that is a confusing one like `expected: (Seq a) given: (Seq 
Integer)`). But here:

1. The inference did not fail but assumed it's Nothing just because I changed 
the function signature from (-> a) to (-> a a). I can't understand why the 
inference fails for this change. And on top of this,

2. The function type is clearly specified as (-> (Foo a) (Foo b) (Foo b)) and 
it can unify a = Integer, b = String, from the first argument alone. So, even 
though a is Integer, it still produces a = Nothing, without a compile error. 
Integer =/= Nothing. Seems very close to a bug in the type deduction algorithm.


> 
> 
> On Fri, Jul 21, 2017 at 5:19 AM, Sourav Datta <soura...@gmail.com> wrote:
> Consider this program,
> 
> 
> 
> #lang typed/racket
> 
> 
> 
> (struct (a) Foo ([val : (-> a)]))
> 
> 
> 
> (: foo/s (All (a b)
> 
>               (-> (-> a b)
> 
>                   (-> (Foo a)
> 
>                       (Foo b)
> 
>                       (Foo b)
> 
> (define (foo/s f)
> 
>   (λ ([f1 : (Foo a)]
> 
>       [f2 : (Foo b)])
> 
>     f2))
> 
> 
> 
> 
> 
> (define r1 (foo/s (λ ([x : Integer]) (format "~a" x
> 
> 
> 
> The type of r1 is correctly deduced as:
> 
> 
> 
> - : (-> (Foo Integer) (Foo String) (Foo String))
> 
> #
> 
> 
> 
> However, if I slightly change the struct Foo and do the same, like this:
> 
> 
> 
> #lang typed/racket
> 
> 
> 
> (struct (a) Foo ([val : (-> a a)]))
> 
> 
> 
> (: foo/s (All (a b)
> 
>               (-> (-> a b)
> 
>                   (-> (Foo a)
> 
>                       (Foo b)
> 
>                       (Foo b)
> 
> (define (foo/s f)
> 
>   (λ ([f1 : (Foo a)]
> 
>       [f2 : (Foo b)])
> 
>     f2))
> 
> 
> 
> 
> 
> (define r1 (foo/s (λ ([x : Integer]) (format "~a" x
> 
> 
> 
> Now, r1 has this type:
> 
> 
> 
> - : (-> (Foo Nothing) (Foo String) (Foo String))
> 
> #
> 
> 
> 
> Surprisingly (Foo Integer) has changed to (Foo Nothing)! Is this a possible 
> bug in the type system? Or, may be I'm missing something?
> 
> 
> 
> 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...@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] A bug in the Typed Racket system?

2017-07-21 Thread Sourav Datta
Consider this program,

#lang typed/racket

(struct (a) Foo ([val : (-> a)]))

(: foo/s (All (a b)
  (-> (-> a b)
  (-> (Foo a)
  (Foo b)
  (Foo b)
(define (foo/s f)
  (λ ([f1 : (Foo a)]
  [f2 : (Foo b)])
f2))


(define r1 (foo/s (λ ([x : Integer]) (format "~a" x

The type of r1 is correctly deduced as: 

- : (-> (Foo Integer) (Foo String) (Foo String))
#

However, if I slightly change the struct Foo and do the same, like this:

#lang typed/racket

(struct (a) Foo ([val : (-> a a)]))

(: foo/s (All (a b)
  (-> (-> a b)
  (-> (Foo a)
  (Foo b)
  (Foo b)
(define (foo/s f)
  (λ ([f1 : (Foo a)]
  [f2 : (Foo b)])
f2))


(define r1 (foo/s (λ ([x : Integer]) (format "~a" x

Now, r1 has this type:

- : (-> (Foo Nothing) (Foo String) (Foo String))
#

Surprisingly (Foo Integer) has changed to (Foo Nothing)! Is this a possible bug 
in the type system? Or, may be I'm missing something?

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] Polymorphic functions on Typed Racket classes

2017-07-18 Thread Sourav Datta
On Tuesday, July 18, 2017 at 7:41:05 PM UTC+5:30, Matthias Felleisen wrote:
> In essence, TR’s local inference algorithm cannot determine the type of the 
> sequence you created. It is one of those cases where you need to help along 
> the type checker with the equivalent of an explicit type application: 
>  
>  ((inst seq-first Integer) s1)
> 
>  ((inst seq-rest Integer) s1)
> 
> I have also taken the liberty to rewrite your code a bit. 
> 
> 
> 
> 
> 
> #lang typed/racket
> 
> (define-type (Seq a) (Object [first (-> a)] [rest (-> (Seq a))]))
> 
> (: seq-first (All (a) (-> (Seq a) a)))
> (define (seq-first s) (send s first))
> 
> (: seq-rest (All (a) (-> (Seq a) (Seq a
> (define (seq-rest s) (send s rest))
> 
> (: make-seq (-> (Listof Integer) (Seq Integer)))
> (define (make-seq si)
>   (new (class object%
>          (field [data : (Listof Integer) si])
>          
>          (: first (-> Integer))
>          (define/public (first)
>            (if (empty? data)
>                (error "Empty")
>                (car data)))
>          
>          (: rest (-> (Seq Integer)))
>          (define/public (rest)
>            (if (empty? data)
>                (error "Empty")
>                (make-seq (cdr data
>          
>          (super-new))
>        ))
> 
> 
> (define s1 (make-seq '(1 2 3 4)))
> (send s1 first)
> (send s1 rest)
> 
> ((inst seq-first Integer) s1)
> ((inst seq-rest Integer) s1)
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> On Jul 18, 2017, at 2:57 AM, Sourav Datta <soura...@gmail.com> wrote:
> 
> Hi!
> 
> I am encountering a type checker error for the following program but not sure 
> exactly what is wrong with it. The program is as follows:
> 
> #lang typed/racket
> 
> (define-type (Seq a) (Object [first (-> a)]
>                                                [rest (-> (Seq a))]))
> 
> 
> (: seq-first (All (a) (-> (Seq a) a)))
> (define (seq-first s) (send s first))
> 
> (: seq-rest (All (a) (-> (Seq a) (Seq a
> (define (seq-rest s) (send s rest))
> 
> 
> (: make-seq (-> (Listof Integer) (Seq Integer)))
> (define (make-seq si) (new (class object%
>                             (init-field [data : (Listof Integer) empty])
>                             (: first (-> Integer))
>                             (define/public (first)
>                               (if (empty? data)
>                                   (error "Empty")
>                                   (car data)))
>                             (: rest (-> (Seq Integer)))
>                             (define/public (rest)
>                               (if (empty? data)
>                                   (error "Empty")
>                                   (make-seq (cdr data
>                             (super-new))
>                           [data si]))
> 
> 
> This compiles fine. But calling seq-first or seq-rest on a specific instance 
> like (Seq Integer) fails to type check. Like below:
> 
> (define s1 (make-seq '(1 2 3 4)))
> (send s1 first)
> - : Integer
> 1
> (send s1 rest)
> - : (Seq Integer)
> (object:unsaved-editor:15:27 ...)
> (seq-first s1)
> . Type Checker: Polymorphic function `seq-first' could not be applied to 
> arguments:
> Argument 1:
>  Expected: (Seq a)
>  Given:    (Seq Integer)
> in: (seq-first s1)
> (seq-rest s1)
> . Type Checker: Polymorphic function `seq-rest' could not be applied to 
> arguments:
> Argument 1:
>  Expected: (Seq a)
>  Given:    (Seq Integer)
> in: (seq-rest s1)
> 
> 
> So, the confusion here is, why a (All (a) (Seq a)) is incompatible with a 
> (Seq Integer)?
> 
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Thanks for pointing that out. What I was actually trying to accomplish was 
basically to create a generic sequence type that can be extended by users by 
just providing two methods `first` and `rest` and then implement a set of 
algorithms that may work generically on any sequence (Seq a) - sort of like 
std::algorithm and iterator protocols in C++. I also, looked into the 
discussions regarding generic operations in Typed Racket. I guess it's not yet 
available/supported fully in TR, is that correct? Also, is there any way of 
doing the same thing with objects/structs without actually knowing the type (or 
instantiating) of the data that is coming to seq-first/seq-rest?


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.


[racket-users] Polymorphic functions on Typed Racket classes

2017-07-18 Thread Sourav Datta
Hi!

I am encountering a type checker error for the following program but not sure 
exactly what is wrong with it. The program is as follows:

#lang typed/racket

(define-type (Seq a) (Object [first (-> a)]
[rest (-> (Seq a))]))


(: seq-first (All (a) (-> (Seq a) a)))
(define (seq-first s) (send s first))

(: seq-rest (All (a) (-> (Seq a) (Seq a
(define (seq-rest s) (send s rest))


(: make-seq (-> (Listof Integer) (Seq Integer)))
(define (make-seq si) (new (class object%
 (init-field [data : (Listof Integer) empty])
 (: first (-> Integer))
 (define/public (first)
   (if (empty? data)
   (error "Empty")
   (car data)))
 (: rest (-> (Seq Integer)))
 (define/public (rest)
   (if (empty? data)
   (error "Empty")
   (make-seq (cdr data
 (super-new))
   [data si]))


This compiles fine. But calling seq-first or seq-rest on a specific instance 
like (Seq Integer) fails to type check. Like below:

> (define s1 (make-seq '(1 2 3 4)))
> (send s1 first)
- : Integer
1
> (send s1 rest)
- : (Seq Integer)
(object:unsaved-editor:15:27 ...)
> (seq-first s1)
. Type Checker: Polymorphic function `seq-first' could not be applied to 
arguments:
Argument 1:
  Expected: (Seq a)
  Given:(Seq Integer)
 in: (seq-first s1)
> (seq-rest s1)
. Type Checker: Polymorphic function `seq-rest' could not be applied to 
arguments:
Argument 1:
  Expected: (Seq a)
  Given:(Seq Integer)
 in: (seq-rest s1)
> 

So, the confusion here is, why a (All (a) (Seq a)) is incompatible with a (Seq 
Integer)?

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] Porting additional functions for vectors, sequences, hashes etc. to Typed Racket

2017-05-22 Thread Sourav Datta
On Monday, May 22, 2017 at 6:14:01 PM UTC+5:30, Matthias Felleisen wrote:
> Could you list the functions you’re missing? 
> 
> 
> 
> > On May 22, 2017, at 5:38 AM, Sourav Datta <soura.ja...@gmail.com> wrote:
> > 
> > Is there any ongoing or planned effort to port additional functions 
> > available for vectors, sequences etc. to TR? Many of these functions are 
> > very useful while writing code which uses things beyond normal Lists and it 
> > seems we have to manually require/typed for each individual program. 
> > Surprisingly, the additional functions for Strings (like string-append*) 
> > are available in TR even though they are not part of racket/base. And also, 
> > it does not show up in the TR documentation.
> > 
> > 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.

Ok, two things happened which made me confused about the "missing" functions. I 
had an older version of Racket installed and I found some sequence functions 
like sequence-map and sequence-fold gave top level type missing error. Then I 
switched to 6.9 and tried to sort a vector with vector-sort in TR which 
resulted in the same error. These made me think that 6.9 is missing those 
functions in TR.

So, I can confirm I get the typed versions of sequence and all of vector 
functions except the vector-sort functions. I guess I'd have to require/type 
it. Also, hash-union and hash-union! functions have no TR equivalent. 

Sorry for not double checking!

-- 
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] Porting additional functions for vectors, sequences, hashes etc. to Typed Racket

2017-05-22 Thread Sourav Datta
Is there any ongoing or planned effort to port additional functions available 
for vectors, sequences etc. to TR? Many of these functions are very useful 
while writing code which uses things beyond normal Lists and it seems we have 
to manually require/typed for each individual program. Surprisingly, the 
additional functions for Strings (like string-append*) are available in TR even 
though they are not part of racket/base. And also, it does not show up in the 
TR documentation.

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.


[racket-users] Typed Racket: Using (Sequenceof a) in place of (Listof a)

2017-03-20 Thread Sourav Datta
Hello all!

I have a question regarding how sequence abstraction works in TR. In the 
sequence part of the docs it says that a sequence can consist of lists or 
vectors among other things. But does this make a function which ideally accepts 
a (Listof a) to accept something that is declared as a (Sequenceof a)? For 
example, in TR this works:

(: x (Sequenceof Symbol))
(define x '(a b c d))

However, this shows a type mismatch:

#lang typed/racket

(: step (All (a)
 (-> (-> a (Sequenceof a) (Sequenceof a))
 a
 (Sequenceof a)
 (Sequenceof a
(define (step fn x seq)
  (fn x seq))


(step (ann cons (-> Symbol (Listof Symbol) (Listof Symbol)))
  'a
  '(b c))

Results in:

Type Checker: Polymorphic function `step' could not be applied to arguments:
Argument 1:
  Expected: (-> a (Sequenceof a) (Sequenceof a))
  Given:(-> Symbol (Listof Symbol) (Listof Symbol))
Argument 2:
  Expected: a
  Given:'a
Argument 3:
  Expected: (Sequenceof a)
  Given:(List 'b 'c)

I think, trying to instantiate the polymorphic step function might help but I 
was not able to figure out how.

Thanks in advance.

-- 
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] Using brag parser library with Typed Racket

2017-02-05 Thread Sourav Datta
On Sunday, February 5, 2017 at 4:06:13 AM UTC+5:30, Matthew Butterick wrote:
> On Feb 4, 2017, at 8:18 AM, Sourav Datta <soura...@gmail.com> wrote:
> 
> (require/typed "grammar1.rkt"
>  [parse (->* (Listof Token)
>  (Any)
>  Syntax)])
> 
> Type Checker: Type (->* (Listof Token) (Any) Syntax) could not be converted 
> to a contract: required a flat contract but generated a chaperone contract 
> in: (->* (Listof Token) (Any) Syntax)
> 
> I am not sure what this means exactly but is this because Racket does not see 
> the automatically exported function parse (or parse-tree) from a #lang brag 
> module? And, is there a way to correct this error so that I can call the 
> parse function from a TR module? 
> 
> 
> 1) That type declaration for `parse` seems both too strict (in terms of the 
> input `parse` will accept), and not quite accurate in terms of order of 
> arguments. I might do it this way:
> 
> 
> 
> (require/typed brag/support
>                [#:opaque Token token-struct?]
>                [token (->* ((U String Symbol))
>                            (Any
>                             #:line Positive-Integer
>                             #:column Natural
>                             #:position Positive-Integer
>                             #:span Natural
>                             #:skip? Boolean)
>                            Token)])
> 
> 
> (define-type Tok (U String Symbol Token Void))
> 
> 
> (require/typed "grammar1.rkt"
>                [parse (case-> ((U (Listof Tok) (-> Tok)) . -> . Any)
>                               (Any (U (Listof Tok) (-> Tok)) . -> . Any))])
> 
> 
> 
> 
> 2) If I use `Any` rather than `Syntax` as the output type, then `parse` 
> works. 

Thanks! That worked. Yes the type signatures were wrong for parse and I 
realized that after re-reading the documentation. Also, I found that instead of 
returning `Any` we could also return `(Syntaxof Any)` like this:

(define-type Input-Token (U String Symbol Token Void 'EOF))
(define-type Token-Source (U (-> Input-Token)
 (Listof Input-Token)))
   
 
(require/typed "grammar1.rkt"
  [parse (case-> (-> Token-Source (Syntaxof Any))
 (-> Any Token-Source (Syntaxof Any)))])

-- 
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] Using brag parser library with Typed Racket

2017-02-04 Thread Sourav Datta
Hi everyone!

I was taking a look at the brag library (http://docs.racket-lang.org/brag/) for 
AST generation and thought of using it in one of my projects which is in TR. I 
started by requiring brag/support with type annotations like this:

#lang typed/racket

(require/typed brag/support
   [#:opaque Token token-struct?]
   [token (->* ((U String Symbol))
   (Any
#:line Positive-Integer
#:column Natural
#:position Positive-Integer
#:span Natural
#:skip? Boolean)
   Token)])

This worked. So far so good. But when trying to import a file which has #lang 
brag in it I encountered this problem.

(require/typed "grammar1.rkt"
  [parse (->* (Listof Token)
  (Any)
  Syntax)])

Type Checker: Type (->* (Listof Token) (Any) Syntax) could not be converted to 
a contract: required a flat contract but generated a chaperone contract in: 
(->* (Listof Token) (Any) Syntax)

I am not sure what this means exactly but is this because Racket does not see 
the automatically exported function parse (or parse-tree) from a #lang brag 
module? And, is there a way to correct this error so that I can call the parse 
function from a TR module? 

Note: I found a github issue that could be related to this here: 
https://github.com/racket/typed-racket/issues/338

Thanks, 
Sourav

-- 
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] Re: Require/typed with racket/tcp

2016-10-01 Thread Sourav Datta
On Saturday, October 1, 2016 at 3:10:12 PM UTC+5:30, Sourav Datta wrote:
> I am currently trying to use racket/tcp library in a TR program and used 
> require/typed to import the necessary functions as I could not find this 
> provided with TR by default. However, the following code has a problem when 
> it runs:
> 
> #lang typed/racket
> 
> (require/typed racket/tcp
>[opaque TCP-Listener tcp-listener?]
>[tcp-listen (-> Number TCP-Listener)]
>[tcp-accept (-> TCP-Listener (Values Number Number))])
> 
> (: server (-> Number Void))
> (define (server port)
>   (let ([listener (tcp-listen port)])
> (define-values (inp outp) (tcp-accept listener))
> (displayln inp)
> (displayln outp)))
> 
> The code compiles fine but while running the "server" function with a valid 
> port it errors out:
> 
>  tcp-listen: broke its own contract
>   promised: tcp-listener?
>   produced: #
>   in: (-> any/c tcp-listener?)
>   contract from: (interface for tcp-listen)
>   blaming: (interface for tcp-listen)
>(assuming the contract is correct)
>   at: unsaved-editor:5.16
> 
> I am not sure what exactly this error message means as tcp-listener is 
> creating a valid listener as given by "produced: #" but still 
> the predicate seems to be failing. The program runs fine with #lang racket. 
> Any help?
> 
> Thanks!

I found the cause of the problem. Already there are base type TCP-Listener 
which I should have used in the code instead of defining a new opaque type. 
Using that solves the problem and I found there are types for Input-Port and 
Output-Port as well. So that fixes the issue. However, it's still a bit 
surprising why the opaque type definition did not work with the tcp-listener? 
predicate though.

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.


[racket-users] Require/typed with racket/tcp

2016-10-01 Thread Sourav Datta
I am currently trying to use racket/tcp library in a TR program and used 
require/typed to import the necessary functions as I could not find this 
provided with TR by default. However, the following code has a problem when it 
runs:

#lang typed/racket

(require/typed racket/tcp
   [opaque TCP-Listener tcp-listener?]
   [tcp-listen (-> Number TCP-Listener)]
   [tcp-accept (-> TCP-Listener (Values Number Number))])

(: server (-> Number Void))
(define (server port)
  (let ([listener (tcp-listen port)])
(define-values (inp outp) (tcp-accept listener))
(displayln inp)
(displayln outp)))

The code compiles fine but while running the "server" function with a valid 
port it errors out:

 tcp-listen: broke its own contract
  promised: tcp-listener?
  produced: #
  in: (-> any/c tcp-listener?)
  contract from: (interface for tcp-listen)
  blaming: (interface for tcp-listen)
   (assuming the contract is correct)
  at: unsaved-editor:5.16

I am not sure what exactly this error message means as tcp-listener is creating 
a valid listener as given by "produced: #" but still the 
predicate seems to be failing. The program runs fine with #lang racket. Any 
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] Importing untyped class into typed racket

2016-09-20 Thread Sourav Datta
On Tuesday, September 20, 2016 at 7:41:23 PM UTC+5:30, Matthias Felleisen wrote:
> > On Sep 20, 2016, at 4:23 AM, Sourav Datta <soura.ja...@gmail.com> wrote:
> > 
> > Is it possible to import an untyped class into typed Racket? I know how to 
> > require for functions or structs but there seems to be no documentation 
> > about classes. Thanks!
> 
> 
> 
> That is the break-though of Asumu’s addition. Here is a simple example, 
> because I couldn’t find one in the docs: 
> 
> #lang racket
> 
> (module server racket
>   (provide C%)
> 
>   (define C%
> (class object%
>   (init-field world)
>   (super-new)
>   (define/public (hello)
> world
> 
> (module client typed/racket
>   (require/typed (submod ".." server)
>  (C% (Class (init-field (world String)) (hello (-> String)
>   (send (new C% [world "good bye"]) hello))
> 
> (require 'client)

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


[racket-users] Importing untyped class into typed racket

2016-09-20 Thread Sourav Datta
Is it possible to import an untyped class into typed Racket? I know how to 
require for functions or structs but there seems to be no documentation about 
classes. 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] syntax-parse in typed racket

2016-09-06 Thread Sourav Datta
On Monday, September 5, 2016 at 10:54:57 PM UTC+5:30, Matthias Felleisen wrote:
> The easiest and proper fix is to write typed macros for typed modules. Below 
> is a naive but straightforward solution. It would be better if 
> define-memoized fished out the type declaration for f and used it to add 
> domain types for arg-s...
> 
> #lang typed/racket
> 
> (require (for-syntax syntax/parse))
> 
> (: memoize (All (r a ...)
> (-> (-> a ... a r)
> (-> a ... a r
> (define (memoize fn)
>   (let ([store : (HashTable Any r) (make-hash)])
> (define (memfn . [args : a ... a])
>   (hash-ref store args
> (lambda ()
>   (let ([result : r (apply fn args)])
> (hash-set! store args result)
> result
> memfn))
> 
> (: fibo (-> Integer Integer))
> (define fibo (memoize (lambda ([n : Integer])
> (if (<= n 1)
> 1
> (+ (fibo (- n 1))
>(fibo (- n 2)))
> 
> (define-syntax (define-memoized stx)
>   (syntax-parse stx ;; I didn’t get the ‘:’ syntax right the first time, so I 
> gave up on that 
> [(_ (fn-name:id {arg t} ...) Tresult body ...)
>  #'(begin
>  (: fn-name (-> t ... Tresult))
>  (define fn-name (memoize (lambda ({arg : t} ...) body ...]))
> 
> 
> (define-memoized (fib {n Integer}) Integer
>   (if (<= n 1)
>   1
>   (+ (fib (- n 1))
>  (fib (- n 2)
> 
> (fib 10)
> 
> 
> 
> > On Sep 5, 2016, at 3:11 AM, Sourav Datta <soura.ja...@gmail.com> wrote:
> > 
> > Another day, another typed racket question!
> > 
> > I was experimenting with memoize library in Racket and noticed that it does 
> > not always work with typed racket functions (or, may be I was not 
> > 'require'ing it properly). So I came up with this crude implementation 
> > below and it seems to be working for one or more arguments:
> > 
> > #lang typed/racket
> > 
> > (: memoize (All (r a ...)
> >(-> (-> a ... a r)
> >  (-> a ... a r
> > (define (memoize fn)
> >  (let ([store : (HashTable Any r) (make-hash)])
> >(define (memfn . [args : a ... a])
> >  (hash-ref store args
> >(lambda ()
> >  (let ([result : r (apply fn args)])
> >(hash-set! store args result)
> >result
> >memfn))
> > 
> > So the typical fibo function with this memoization function would look like:
> > 
> > (: fibo (-> Integer Integer))
> > (define fibo (memoize (lambda ([n : Integer])
> >(if (<= n 1)
> >1
> >(+ (fibo (- n 1))
> >   (fibo (- n 2)))
> > 
> > However, what I wanted is to define a macro similar to define/memo like in 
> > the Racket memoize package. A first approach like below did not work:
> > 
> > (define-syntax (define-memoized stx)
> >  (syntax-parse stx
> >[(_ (fn-name:id arg:id ...) body ...+)
> > #'(define fn-name (memoize (lambda (arg ...) body ...)))]))
> > 
> > The error comes in (<= n 1) call where it is given Any but expecting 
> > Integer. The problem is that the syntax splits the function definition in a 
> > lambda expression and then passes to memoize function, whose result is then 
> > assigned to the function name. The lambda expression without type 
> > annotation assumes that the arguments are Any. 
> > 
> > So in this case, is there any way we could write a macro on top of the 
> > above memoize that can identify the types of the underlying function and 
> > annotate the lambda accordingly - or is there any other way this could be 
> > achieved?
> > 
> > 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.

This approach seems like the easiest and flexible way to define the macro. Just 
to add, here's another crude version of the macro I came up with where we can 
specify the type declaration inside the macro, rather than outside which gives 
some way to use it in the function definition:

(define-syntax (memoized

[racket-users] syntax-parse in typed racket

2016-09-05 Thread Sourav Datta
Another day, another typed racket question!

I was experimenting with memoize library in Racket and noticed that it does not 
always work with typed racket functions (or, may be I was not 'require'ing it 
properly). So I came up with this crude implementation below and it seems to be 
working for one or more arguments:

#lang typed/racket

(: memoize (All (r a ...)
(-> (-> a ... a r)
  (-> a ... a r
(define (memoize fn)
  (let ([store : (HashTable Any r) (make-hash)])
(define (memfn . [args : a ... a])
  (hash-ref store args
(lambda ()
  (let ([result : r (apply fn args)])
(hash-set! store args result)
result
memfn))

So the typical fibo function with this memoization function would look like:

(: fibo (-> Integer Integer))
(define fibo (memoize (lambda ([n : Integer])
(if (<= n 1)
1
(+ (fibo (- n 1))
   (fibo (- n 2)))

However, what I wanted is to define a macro similar to define/memo like in the 
Racket memoize package. A first approach like below did not work:

(define-syntax (define-memoized stx)
  (syntax-parse stx
[(_ (fn-name:id arg:id ...) body ...+)
 #'(define fn-name (memoize (lambda (arg ...) body ...)))]))

The error comes in (<= n 1) call where it is given Any but expecting Integer. 
The problem is that the syntax splits the function definition in a lambda 
expression and then passes to memoize function, whose result is then assigned 
to the function name. The lambda expression without type annotation assumes 
that the arguments are Any. 

So in this case, is there any way we could write a macro on top of the above 
memoize that can identify the types of the underlying function and annotate the 
lambda accordingly - or is there any other way this could be achieved?

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.


[racket-users] Typed racket and continuations

2016-08-29 Thread Sourav Datta
Hey everyone,

I am a beginner in Racket and recently learned the basic concepts of 
continuations. I like Racket's support of multiple types of continuations as 
opposed one type in Scheme. Recently I also started learning about typed 
Racket. My problem is, I am not sure how I can annotate a continuation in a 
typed racket program. For example, when trying to annotate the below code:

#lang racket

(define d-or-s #f)

(define (double-or-same x)
  (call/cc (lambda (c)
 (set! d-or-s c)
 (+ x x

in typed racket, I can see that double-or-same function can have type (Integer 
-> Integer). But what should be the type of the variable do-or-s? If it is Any, 
then it cannot be called like a function after the continuation is set to it. 
So basically my question is how can we type annotate different types of 
continuations starting from a simple one like the above? Also a pointer to the 
relevant section in typed racket docs would be great.

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] Rsound build problem for versions >= 3

2016-07-17 Thread Sourav Datta
Worked fine! For some reason I was assuming that planet was the latest version 
of package manager whereas pkg was the old one! Thanks for the help! 

On Sunday, July 17, 2016 at 11:59:34 AM UTC+5:30, johnbclements wrote:
> > On Jul 16, 2016, at 11:13 PM, Sourav Datta <soura.ja...@gmail.com> wrote:
> > 
> > Hey hey,
> > 
> > I am pretty new to Racket and still experimenting with it. I came across 
> > the nice rsound package and thought to try the examples. I found that the 
> > version installed on my system is 1.10. So I tried to install the latest 
> > 4.4 with the command:
> > 
> > raco planet install clements rsound.plt 4 4
> 
> Racket has a new package management system, and planet is no longer the best 
> place to look for new packages. Try installing the newest version of rsound 
> with the new system:
> 
> raco pkg install rsound
> 
> You can see all of the packages associated with the new package management 
> system at
> 
> https://pkg.racket-lang.org/
> 
> Let me know how it goes!
> 
> John Clements
> 
> 
> > 
> > It had an an error like below:
> > 
> > raco setup: error: during making for /clements/rsound.plt/4/4 
> > (RSound)
> > raco setup:   
> > .racket/planet/300/6.5/cache/clements/rsound.plt/4/4/filter-typed.rkt:191:32:
> >  Type Checker: Bad arguments to function in `apply':
> > raco setup:   Domains: Real *
> > raco setup:   Arguments: (Listof Any) *
> > raco setup:
> > raco setup: in: (apply max delays)
> > raco setup: compiling: /clements/rsound.plt/4/4/filter-typed.rkt
> > raco setup: error: during making for 
> > /clements/rsound.plt/4/4/examples
> > raco setup:   
> > .racket/planet/300/6.5/cache/clements/rsound.plt/4/4/filter-typed.rkt:191:32:
> >  Type Checker: Bad arguments to function in `apply':
> > raco setup:   Domains: Real *
> > raco setup:   Arguments: (Listof Any) *
> > raco setup:
> > raco setup: in: (apply max delays)
> > raco setup: compiling: /clements/rsound.plt/4/4/filter-typed.rkt
> > raco setup: error: during building docs for 
> > /clements/rsound.plt/4/4/rsound.scrbl
> > raco setup:   
> > .racket/planet/300/6.5/cache/clements/rsound.plt/4/4/filter-typed.rkt:191:32:
> >  Type Checker: Bad arguments to function in `apply':
> > raco setup:   Domains: Real *
> > raco setup:   Arguments: (Listof Any) *
> > raco setup:
> > raco setup: in: (apply max delays)
> > PLaneT: raco setup: --- installing collections ---
> > 
> > Then I tried version 3.3 which had again this error:
> > 
> > raco setup: error: during making for /clements/rsound.plt/3/3 
> > (RSound)
> > raco setup:   
> > .racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34:
> >  Type Checker: Bad arguments to function in `apply':
> > raco setup:   Domains: Real *
> > raco setup:   Arguments: (Listof Any) *
> > raco setup:
> > raco setup: in: (apply max delays)
> > raco setup: compiling: /clements/rsound.plt/3/3/filter-typed.rkt
> > raco setup: error: during making for 
> > /clements/rsound.plt/3/3/examples
> > raco setup:   
> > .racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34:
> >  Type Checker: Bad arguments to function in `apply':
> > raco setup:   Domains: Real *
> > raco setup:   Arguments: (Listof Any) *
> > raco setup:
> > raco setup: in: (apply max delays)
> > raco setup: compiling: /clements/rsound.plt/3/3/filter-typed.rkt
> > raco setup: error: during making for /clements/rsound.plt/3/3/test
> > raco setup:   
> > .racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34:
> >  Type Checker: Bad arguments to function in `apply':
> > raco setup:   Domains: Real *
> > raco setup:   Arguments: (Listof Any) *
> > raco setup:
> > raco setup: in: (apply max delays)
> > raco setup: compiling: /clements/rsound.plt/3/3/filter-typed.rkt
> > raco setup: error: during building docs for 
> > /clements/rsound.plt/3/3/rsound.scrbl
> > raco setup:   
> > .racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34:
> >  Type Checker: Bad arguments to function in `apply':
> > raco setup:   Domains: Real *
> > raco setup:   Arguments: (Listof Any) *
> > raco setup:
> > 
> > 
> > However, version 2.12 got installed fine and I can play the ding as per the 
> > example. So, I'm wondering why the latest version had the build problem. 
> > Any suggestions?
> > 
> > System info:
> > 
> > U

[racket-users] Rsound build problem for versions >= 3

2016-07-17 Thread Sourav Datta
Hey hey,

I am pretty new to Racket and still experimenting with it. I came across the 
nice rsound package and thought to try the examples. I found that the version 
installed on my system is 1.10. So I tried to install the latest 4.4 with the 
command:

raco planet install clements rsound.plt 4 4

It had an an error like below:

raco setup: error: during making for /clements/rsound.plt/4/4 (RSound)
raco setup:   
.racket/planet/300/6.5/cache/clements/rsound.plt/4/4/filter-typed.rkt:191:32: 
Type Checker: Bad arguments to function in `apply':
raco setup:   Domains: Real *
raco setup:   Arguments: (Listof Any) *
raco setup:   
raco setup: in: (apply max delays)
raco setup: compiling: /clements/rsound.plt/4/4/filter-typed.rkt
raco setup: error: during making for /clements/rsound.plt/4/4/examples
raco setup:   
.racket/planet/300/6.5/cache/clements/rsound.plt/4/4/filter-typed.rkt:191:32: 
Type Checker: Bad arguments to function in `apply':
raco setup:   Domains: Real *
raco setup:   Arguments: (Listof Any) *
raco setup:   
raco setup: in: (apply max delays)
raco setup: compiling: /clements/rsound.plt/4/4/filter-typed.rkt
raco setup: error: during building docs for 
/clements/rsound.plt/4/4/rsound.scrbl
raco setup:   
.racket/planet/300/6.5/cache/clements/rsound.plt/4/4/filter-typed.rkt:191:32: 
Type Checker: Bad arguments to function in `apply':
raco setup:   Domains: Real *
raco setup:   Arguments: (Listof Any) *
raco setup:   
raco setup: in: (apply max delays)
PLaneT: raco setup: --- installing collections ---

Then I tried version 3.3 which had again this error:

raco setup: error: during making for /clements/rsound.plt/3/3 (RSound)
raco setup:   
.racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34: 
Type Checker: Bad arguments to function in `apply':
raco setup:   Domains: Real *
raco setup:   Arguments: (Listof Any) *
raco setup:   
raco setup: in: (apply max delays)
raco setup: compiling: /clements/rsound.plt/3/3/filter-typed.rkt
raco setup: error: during making for /clements/rsound.plt/3/3/examples
raco setup:   
.racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34: 
Type Checker: Bad arguments to function in `apply':
raco setup:   Domains: Real *
raco setup:   Arguments: (Listof Any) *
raco setup:   
raco setup: in: (apply max delays)
raco setup: compiling: /clements/rsound.plt/3/3/filter-typed.rkt
raco setup: error: during making for /clements/rsound.plt/3/3/test
raco setup:   
.racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34: 
Type Checker: Bad arguments to function in `apply':
raco setup:   Domains: Real *
raco setup:   Arguments: (Listof Any) *
raco setup:   
raco setup: in: (apply max delays)
raco setup: compiling: /clements/rsound.plt/3/3/filter-typed.rkt
raco setup: error: during building docs for 
/clements/rsound.plt/3/3/rsound.scrbl
raco setup:   
.racket/planet/300/6.5/cache/clements/rsound.plt/3/3/filter-typed.rkt:151:34: 
Type Checker: Bad arguments to function in `apply':
raco setup:   Domains: Real *
raco setup:   Arguments: (Listof Any) *
raco setup:   


However, version 2.12 got installed fine and I can play the ding as per the 
example. So, I'm wondering why the latest version had the build problem. Any 
suggestions?

System info:

Ubuntu 16.04 64 bit
Racket version 6.5

Please let me know if you need more system info or full logs. 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.