5

예를 들어 Typed Racket에서 다형 함수로 작동 할 수있는 map 버전을 작성하려면 어떻게해야합니까? 나는리스트를 매핑 할 때 오류가 얻을형식화 된 라켓에서 다형 함수를 인수로 사용하는 고차 함수를 작성하려면 어떻게해야합니까?

(: 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))) 

답변

1

를 : 나는 정의 간단한 id 기능을 사용

-> (map (inst identity Integer) '(1 2 3)) 
- : (Listof Integer) [more precisely: (Pairof Integer (Listof Integer))] 
'(1 2 3) 

이유는 형식화 된 라켓 가이드 here에 설명되어 있습니다 :

Typed Racket’s local type inference algorithm is currently not able to infer types for polymorphic functions that are used on higher-order arguments that are themselves polymorphic.

(자세한 설명 및 예는 docs 참조)

+0

이것은 슬프다. 고맙습니다. –