[racket-dev] Experimenting with generics in TR

From: Neil Toronto (neil.toronto at gmail.com)
Date: Sat Feb 9 21:27:39 EST 2013

I'd like to play around with implementing generics or type classes in 
Typed Racket, but without changing its internals, just to see what it 
takes. I think I could get something pretty decent working if I could 
get the following program to type:


(define ops-hash (make-hasheq))

(: set-ring-ops! (All (A) (Symbol (ring-ops A) -> Void)))
(define (set-ring-ops! name ops)
   (hash-set! ops-hash name ops))

(: get-ring-ops (All (A) ((ring-member A) -> (ring-ops A))))
(define (get-ring-ops x)
   (define name (ring-member-ops-name x))
   (hash-ref ops-hash name (λ () (error 'get-ring-ops
                                        "unregistered ring ~a"
                                        name))))


Of course, I can't. In fact, I can't get this to work by writing 
`set-ring-ops!' and `get-ring-ops' in untyped Racket and importing them 
using `require/typed', because the contract system can't prove that the 
`A' in `get-ring-ops' is the same as the `A' in `set-ring-ops!'.

Is there a secret, hidden escape hatch that would allow me to get 
functions with these types and behaviors?

Neil ⊥

Posted on the dev mailing list.