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.