On 7/11/21, Kiong-Gē Liāu <gongyi.l...@gmail.com> wrote:
> Hi, in non-typed racket, I can define a generalized Fibonacci sequence
>
> X[n+k] = f(X[n], X[n+1], ...., X[n+k-1])
>
> using the following code
>
> #lang racket
>
> (require  racket/stream)
>
> (define (gfib f xs)
>   (define (gfib_t xs)
>     (stream-cons (last xs) (gfib_t (append (cdr xs) (list (f xs))))))
>   (stream-append (drop-right xs 1) (gfib_t xs)))
>
> (define (sum xs) (apply + xs))
> ;; Example of a (0, 1) initialized Fibonacci sequence
> (define gfib20 (gfib sum '(0 1 )))
>
> But using typed racket,  the following code
>
> #lang typed/racket
>
> (require pfds/stream)
>
> (define (sum [xs : (Listof Number)] ) (apply + xs))
>
> (define (gfib [f : (-> (Listof Number) Number)]  [xs : (Listof Number)] )
>   (define (gfib_t [ys : (Listof Number)] )
>     (stream-cons (last ys) (gfib_t (append (cdr ys) (list (f ys))))))
>   (stream-append (stream (drop-right xs 1)) (gfib_t xs)))
>
> leads to error message
>
> ; /home/kiong-ge/Programming/Racket/typed_racket_test.rkt:8:11: Type
> Checker: insufficient type information to typecheck. please add more type
> annotations
> ;   in: gfib_t
>
> How should I set the type signature in the typed racket in order to get the
> same result generated non-typed racket code ?
>
> Thanks,
> Kiong-Ge.

First, gfib_t needs a return type annotation. You can either add `:
(Stream Number)` to the end of the line, or write a full signature
above the define

  (: gfib_t (-> (Listof Number) (Stream Number)))
  (define (gfib_t ys)

After this, the typechecker can run. But it finds a problem with
stream-append. The issue here is that the two arguments to
stream-append have different types. One contains lists of numbers and
the other contains numbers.

  typed.rkt:11:2: Type Checker: Polymorphic function `stream-append'
could not be applied to arguments:
  Argument 1:
    Expected: (Rec Stream (U (Boxof (U (-> (Pairof A Stream)) (Pairof
A Stream))) Null))
    Given:    (Rec x₀ (U (Boxof (U (-> (Pairof (Listof Number) x₀))
(Pairof (Listof Number) x₀))) Null))
  Argument 2:
    Expected: (Rec Stream (U (Boxof (U (-> (Pairof A Stream)) (Pairof
A Stream))) Null))
    Given:    (Rec x₀ (U (Boxof (U (-> (Pairof Number x₀)) (Pairof
Number x₀))) Null))

    in: (stream-append (stream (drop-right xs 1)) (gfib_t xs))

Change (stream (drop-right xs 1)) to (apply stream (drop-right xs 1))
and you should be OK.

(It might be possible to call stream-append with a list and a stream
--- like the untyped code does --- but I haven't figured out how to do
that. Better stick with the Stream datatype.)

-- 
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/CAFUu9R6gdCbqBJ%3DyeZfvLUGKeQidNwXfy30gf--Fj5y52t3jQA%40mail.gmail.com.

Reply via email to