Types 如何编写将多态函数作为类型化Racket中的参数的高阶函数?
例如,我如何编写一个版本的Types 如何编写将多态函数作为类型化Racket中的参数的高阶函数?,types,racket,higher-order-functions,typed-racket,polymorphic-functions,Types,Racket,Higher Order Functions,Typed Racket,Polymorphic Functions,例如,我如何编写一个版本的map,它将在类型化的Racket中使用多态函数?我使用一个简单的id函数,定义如下: (: id : (All (A) A -> A)) (define (id x) x) 当我尝试将其映射到列表时,会出现一个错误: > (map id '(1 2 3)) Type Checker: Polymorphic function `map' could not be applied to arguments: Types: (-> a b ... b
map
,它将在类型化的Racket中使用多态函数?我使用一个简单的id
函数,定义如下:
(: id : (All (A) A -> A))
(define (id x) x)
当我尝试将其映射到列表时,会出现一个错误:
> (map id '(1 2 3))
Type Checker: Polymorphic function `map' could not be applied to arguments:
Types: (-> a b ... b c) (Listof a) (Listof b) ... b -> (Listof c)
(-> a c) (Pairof a (Listof a)) -> (Pairof c (Listof c))
Arguments: (All (A) (-> A A)) (List One Positive-Byte Positive-Byte)
Expected result: AnyValues
in: (map id (quote (1 2 3)))
在这种情况下,您必须手动实例化多态性:
-> (map (inst identity Integer) '(1 2 3))
- : (Listof Integer) [more precisely: (Pairof Integer (Listof Integer))]
'(1 2 3)
原因在打字球拍指南中解释:
类型化Racket的本地类型推断算法当前无法
推断高阶上使用的多态函数的类型
参数本身是多态的
(有关更多解释和示例,请参见文档)在这种情况下,您必须手动实例化多态性:
-> (map (inst identity Integer) '(1 2 3))
- : (Listof Integer) [more precisely: (Pairof Integer (Listof Integer))]
'(1 2 3)
原因在打字球拍指南中解释:
类型化Racket的本地类型推断算法当前无法
推断高阶上使用的多态函数的类型
参数本身是多态的
(有关更多解释和示例,请参见文档)这很令人伤心。谢谢你,这太令人伤心了。非常感谢。