Thanks, following Ben's suggestion that follow code works
#lang typed/racket
;;
(require pfds/stream)
;;
(define-type (OverFoldable A) (-> (Listof A) A))
;;
(define-type (FibStreamCons A) (-> (Listof A) (Stream A)))
;;
(define-type (FibStream A) (-> (OverFoldable A) (Listof A) (Stream A)))
;;
(: sum (OverFoldable Number))
(define (sum xs) (apply + xs))
;;
(: gfib (FibStream Number))
(define (gfib f xs)
(: gfib_t (FibStreamCons Number))
(define (gfib_t ys)
(stream-cons (last ys) (gfib_t (append (cdr ys) (list (f ys))))))
(stream-append (apply stream (drop-right xs 1)) (gfib_t xs)))
;;
(: gfib_2 (All (A) (FibStream A)))
(define (gfib_2 f xs)
(: gfib_t (FibStreamCons A))
(define (gfib_t ys)
(stream-cons (last ys) (gfib_t (append (cdr ys) (list (f ys))))))
(stream-append (apply stream (drop-right xs 1)) (gfib_t xs)))
;;
(define gfib2_0
(gfib sum '(0 1)))
;;
(stream->list (take 30 gfib2_0))
;;
(define gfib2_1
(gfib_2 sum '(0 1)))
;;
(stream->list (take 30 gfib2_1))
;;
(= (last (stream->list (take 201 gfib2_1)))
(last (stream->list (take 201 gfib2_0))))
;; typed_racket_test.rkt ends here
On Tuesday, July 13, 2021 at 9:11:01 PM UTC-5 Ben Greenman wrote:
> On 7/13/21, Kiong-Gē Liāu <[email protected]> wrote:
> > Ben,
> >
> > Thanks, changing "stream" to "stream apply" does solve the issue.
> >
> > I tried to push it further little bit with the following code to see if
> > typed racket can support generic like Haskell or Scala:
> >
> > #lang typed/racket
> >
> > (require pfds/stream)
> >
> >
> > (define-type (OverFoldable A) (-> (Listof A) A))
> >
> > (define-type (FibStreamCons A) (-> (Listof A) (Stream A)))
> >
> > (define-type (FibStream A) (-> (OverFoldable A) (Listof A) (Stream A)))
> >
> > (: sum (OverFoldable Number))
> > (define (sum xs) (apply + xs))
> >
> > (: gfib_2 (All (A) (FibStream A)))
> > (define (gfib_2 f xs)
> > (: gfib_t (All (A) (FibStreamCons A)))
> > (define (gfib_t ys)
> > (stream-cons (last ys) (gfib_t (append (cdr ys) (list (f ys))))))
> > (stream-append (apply stream (drop-right xs 1)) (gfib_t xs)))
> >
> > However, I got the following error message:
> >
> > ; /home/kiong-ge/Programming/Racket/typed_racket_test.rkt:28:61: Type
> > Checker: type mismatch
> > ; expected: (Listof A)
> > ; given: (Listof A)
> > ; in: ys
> >
> > But, according to this error message, expected and given types are
> exactly
> > the same, not sure how to deal with this issue.
>
> I think the problem is 2 different type variables that both print as
> the letter A.
>
> When I remove the "All" from the type for gfib_t (FibStreamCons A), it
> typechecks.
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/441cdf96-532a-4c7e-9f03-dedce52508c3n%40googlegroups.com.