例如,如何map
在Typed 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)
其原因是类型化的球拍手册的说明在这里:
Typed Racket的局部类型推断算法当前无法推断本身用于多态的高阶参数上使用的多态函数的类型。
(有关详细说明和示例,请参阅文档)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句