5

Ad esempio, come posso scrivere una versione di map che funzionerà con le funzioni polimorfiche in Racket tipizzato? Io uso una semplice funzione id definita come:Come si scrivono le funzioni di ordine superiore che accettano le funzioni polimorfiche come argomenti in Racket tipizzato?

(: id : (All (A) A -> A)) 
(define (id x) x) 

Quando provo a tracciare sopra un elenco ottengo un errore:

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

risposta

1

Dovete istanziare manualmente il polimorfismo in questo caso:

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

il motivo è spiegato nella Guida tipizzati Racket 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.

(vedere i documenti per ulteriori spiegazioni ed esempi)

+0

Questo è triste. Grazie. –

Problemi correlati