types - Understanding Haskell's RankNTypes -


while working way through ghc extensions, came across rankntypes @ school of haskell, had following example:

main = print $ rankn (+1)  rankn :: (forall n. num n => n -> n) -> (int, double) rankn f = (f 1, f 1.0) 

the article offered alternative type rankn:

rankn :: forall n. num n => (n -> n) -> (int, double)  

the explanation of difference "the latter signature requires function n n num n; former signature requires function n n every num n."

i can understand former type requires signature what's in parentheses or more general. not understand explanation latter signature requires function n -> n "some num n". can elaborate , clarify? how "read" former signature sounds means? latter signature same num n => (n -> n) -> (int, double) without need forall?

in normal case (forall n. num n => (n -> n) -> (int, double)), choose n first , provide function. pass in function of type int -> int, double -> double, rational -> rational , on.

in rank 2 case ((forall n. num n => n -> n) -> (int, double)) have provide function before know n. means function has work any n; none of examples listed previous example work.

we need example code given because function f that's passed in applied 2 different types: int , double. has work both of them.

the first case normal because that's how type variables work default. if don't have forall @ all, type signature equivalent having @ beginning. (this called prenex form.) num n => (n -> n) -> (int, double) implicitly same forall n. num n => (n -> n) -> (int, double).

what's type of function works any n? it's forall n. num n => n -> n.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -