[plt-scheme] typed scheme problem

From: Jon Zeppieri (zeppieri at gmail.com)
Date: Sat May 2 11:56:19 EDT 2009


I'm getting a type error I can't resolve, and my best attempt to
resolve it has resulted in a nonsensical error message.

In one module I have:


#lang typed-scheme
(require scheme/promise)

;; Lazy streams
(define-type-alias (Stream A) (Promise (U StreamNil (StreamPair A))))
(define-struct: StreamNil ())
(define-struct: (A) StreamPair ((x : A) (xs : (Stream A))))

(provide StreamNil StreamPair Stream)


And in another:


#lang typed-scheme
(require scheme/match)
(require scheme/promise)
(require "stream.ss")

;; Parser Combinators
(define-struct: (A T) Ok ((ans : A)
                          (tks : (Stream T))
                          (ncur : Integer)
                          (nmax : Integer)))
(define-struct: Fail ((nmax : Integer)))
(define-type-alias (ParserResult A T) (U (Ok A T) Fail))

(define-type-alias (Parser A T) ((Stream T) Integer Integer ->
(ParserResult A T)))

(: any (All (A) (-> (Parser A A))))
(define (any)
  (λ (tks ncur nmax)
    (match (force tks)
      ((struct StreamNil ())
       (make-Fail nmax))
      ((struct StreamPair (x xs))
       (let* ((ncur (+ ncur 1))
              (nmax (if (> ncur nmax) ncur nmax)))
         (make-Ok x xs ncur nmax))))))


'any' fails to typecheck with the message:

typecheck: Polymorphic function force could not be applied to arguments:
Domain: #(struct:Promise (a))
Arguments: #(struct:Promise ((U StreamNil16 (StreamPair18 A))))
 in: (force tks)

So, fair enough: maybe I have to instantiate force's type variable.  So, I try:

(match (#{force @ (U StreamNil (StreamPair A))} tks)

... and get the error:

typecheck: Wrong function argument type, expected #(struct:Promise ((U
StreamNil16 (StreamPair18 A)))), got #(struct:Promise ((U StreamNil16
(StreamPair18 A)))) for argument 1 in: tks

Unfortunately, there's no way to tell from that error message why the
argument type is wrong.  In fact, it looks to be correct.  Any


Posted on the users mailing list.