> On Apr 23, 2016, at 11:25 AM, liweijian <[email protected]> wrote:
> 
> I was reading the document of Typed 
> Racket(https://docs.racket-lang.org/ts-guide/types.html#%28part._.Non-.Uniform_.Variable-.Arity_.Functions%29).
> 
> It saids  that "Typed Racket can handle some uses of rest arguments". 
> 
> I was wondering if maybe Typed Racket could handle the follow case:
> 
> ```
> (define *memoize-tbl* (make-hasheq))
> 
> (define (bind fn . args)
>  (let ([res (apply fn args)])
>    (hash-set! *memoize-tbl*
>               (equal-hash-code (cons fn args))
>               res)
>    res))
> ```

For this case, where you're not trying to look anything up from the memoize 
table, yes:

#lang typed/racket

(: *memoize-tbl* : (HashTable Fixnum Any))
(define *memoize-tbl* (make-hasheq))

;; The `A ... A` means that there are `#of A` repetitions of the type `A`.
;; The last `A` is there to say how many repetitions it has, so for instance
;; `(Listof A) ...` A would mean `#of A` repetitions of `(Listof A)`.
(: bind : (∀ (B A ...) [-> (-> A ... A B) A ... A B]))
(define (bind fn . args)
 (let ([res (apply fn args)])
   (hash-set! *memoize-tbl*
              (equal-hash-code (cons fn args))
              res)
   res))

However, if you try to find the result value in the table first, you can have 
it return Any, but not B:

#lang typed/racket

(: *memoize-tbl* : (HashTable Fixnum Any))
(define *memoize-tbl* (make-hasheq))

;; This has to return Any instead of B, because
(: bind : (∀ (B A ...) [-> (-> A ... A B) A ... A Any]))
(define (bind fn . args)
  ;; a hash-ref call returns Any, so this hash-ref! call does too,
  (hash-ref! *memoize-tbl*
             (equal-hash-code (cons fn args))
             (λ ()
               ;; even though this has type B.
               (apply fn args))))

Since the hash table's type is (HashTable Fixnum Any), this function can't 
return B, it has to return Any. 

I tried making a better version using lists, but I think a perfect version 
would need to use exists types, which I don't think typed racket has.

Alex Knauth

-- 
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].
For more options, visit https://groups.google.com/d/optout.

Reply via email to