Perché questo Haskell si lamenta dei tipi ambigiosi quando è esteso?
Domanda
Il seguente ritorna vera (perché 2147483647 è un primo).
length [f | f <- [2..(floor(sqrt 2147483647))], 2147483647 `mod` f == 0 ] == 0
Perché non funziona quando provo ad estenderlo come di seguito?
Prelude> [n | n <- [2..], length [f | f <- [2..(floor(sqrt n))], n `mod` f == 0 ] == 0 ]
<interactive>:1:39:
Ambiguous type variable `t' in the constraints:
`RealFrac t' arising from a use of `floor' at <interactive>:1:39-51
`Integral t' arising from a use of `mod' at <interactive>:1:56-64
`Floating t' arising from a use of `sqrt' at <interactive>:1:45-50
Probable fix: add a type signature that fixes these type variable(s)
Non capisco però, perché un RealFrac deriva da un uso del pavimento? Pensavo che il pavimento prendesse i reali e avessero prodotto integrali? Inoltre, non si è lamentato con l'esempio sopra, sto solo inserendo più numeri interi come ho fatto allora.
Prelude> :t floor
floor :: (RealFrac a, Integral b) => a -> b
Soluzione
Non lo sfushiamo leggermente:
Prelude> (\x -> x `mod` (floor . sqrt) x) 2
<interactive>:1:24:
Ambiguous type variable `b' in the constraints:
`Floating b' arising from a use of `sqrt' at <interactive>:1:24-27
`Integral b' arising from a use of `mod' at <interactive>:1:7-30
`RealFrac b' arising from a use of `floor' at <interactive>:1:16-20
Probable fix: add a type signature that fixes these type variable(s)
Stai usando il valore di n
come galleggiante, passandolo a sqrt
e floor
. Stai quindi usando quel risultato come un INT, passando quel risultato a mod
. Il compilatore non può nominare un tipo con tutte quelle istanze.
Il motivo per cui funziona nel tuo primo esempio, in altre parole
Prelude> 2 `mod` (floor . sqrt) 2
0
è perché stai usando due diversi letterali numerici. Uno può essere un int e si può essere un galleggiante. Se stai usando lo stesso valore per entrambi, devi chiamare fromIntegral
Per convertire l'INT in un galleggiante.
È possibile ricevere un messaggio di errore diverso aggiungendo una firma del tipo, cambiando [2..]
a [2..] :: [Integer]
:
No instance for (RealFrac Integer)
arising from a use of `floor' at <interactive>:1:52-64
No instance for (Floating Integer)
arising from a use of `sqrt' at <interactive>:1:58-63
Questo potrebbe rendere più chiaro che stai usando il valore di n
come due tipi diversi.
Altri suggerimenti
Come sottolineato da CA McCann di seguito, la mia risposta non è corretta :-)
Per quanto posso vedere, è perché l'elenco che produci può consistere in qualsiasi istanza di Floating
Dal momento che la firma del tipo di sqrt
è
sqrt :: Floating a => a -> a
Pregospendo sqrt
insieme a fromIntegral :: (Integral a, Num b) => a -> b
, ottieni il risultato desiderato:
Prelude> take 10 $ [n | n <- [2..], length [f | f <- [2..(floor(sqrt (fromIntegral n)))], n `mod` f == 0 ] == 0 ]
[2,3,5,7,11,13,17,19,23,29]