简体   繁体   中英

Instantiate the type of the “rest” parameter of a variadic polymorphic procedure when arity is not known in advance in typed Racket

Let's say I want to transpose a 2 xn "matrix" (list of lists) mat . The idiomatic way of doing so in racket is

(apply map list mat)

To do the same thing in typed/racket, I have to help the type checker a little. The type of map in that case is

(All (c a b ...) 
  (-> (-> a b ... b c) (Listof a) (Listof b) ... b (Listof c)))

Since I'm dealing with a 2 xn matrix, I must instantiate both a and b as Number :

(apply (inst map (Listof Number) Number Number) 
       (inst list Number)
       mat)

If mat was a 3 xn matrix,

(apply (inst map (Listof Number) Number Number Number)
       (inst list Number)
       mat)

Would do the trick. Now, let's say that I'm dealing with an mxn matrix where m is some unknown positive integer. Is there a general way of instantiating map that would work for any value of m?

Thank you Sorawee Porncharoenwase, that pretty much solved my problem. Here's what I've done:

  1. Define a polymorphic zip function as in the link provided:
(: zip (∀ (a) (-> (Listof a) (Listof a) * (Listof (Listof a)))))
(define (zip lst . lsts)
  (apply map (inst list a) lst lsts))
  1. Apply it to mat :
(apply zip (car mat) (cdr mat))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM