Why is numCapabilities a pure function?

自闭症网瘾萝莉.ら 提交于 2019-12-03 09:47:49

I've seen very varying views on what to do in situations like this. Some think that values that might vary between compiles should not be pure, and some think that as long as a value doesn't change during your program's local run-time (i.e. after some "configuration" has been "set up" in main), it should be pure.

The base package seems to have settled on a middle-ground. numCapabilities will not (as far as I know) change during run-time, but getArgs might.

This is because there is a withArgs function that changes the args you get via getArgs. So, that answers that.

Oh dear. If you look at the definition of numCapabilities, you can see it is just:

numCapabilities :: Int
numCapabilities = unsafePerformIO $ getNumCapabilities

and the following ghci sessions illustrate the problem:

[ben@euler ~]$ ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
Loading [...]
ghci> :m +GHC.Conc
ghci> numCapabilities
1
ghci> setNumCapabilities 2
ghci> numCapabilities
1
ghci> :q
Leaving GHCi.

[ben@euler ~]$ ghci
GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
Loading [...]
ghci> :m +GHC.Conc
ghci> setNumCapabilities 2
ghci> numCapabilities
2

This is definitely bad - the value of numCapabilities depends on when it is evaluated with respect to any setNumCapabilities calls that may exist in your program. Note that in the first session, numCapabilities remained consistent, since the IO is only executed the first time it is evaluated. However, in the presence of inlining (the name isn't marked NOINLINE or anything) even that may not be true - you could in principle get two different values from two occurrences of numCapabilities (although in practice I haven't been able to make this happen).

So the answer is that numCapabilities isn't a pure function, but is erroneously marked as such by the notorious back door of unsafePerformIO.

I'd say it's a mistake, but it depends on what one thinks purity is. See the post Notions of purity in Haskell and its discussion. Simply put, the post's argument is that types have meaning, and there is not room in the meaning of Int for something like numCapabilities, which is execution-context-dependent.

numCapabilities gives the initial value of getNumCapabilities, wether or not the RTS -N flag argument is present, so the type should be the same.

Have you tried it with a higher number than your computer max. simultaneous threads ?

$ ghci +RTS -N99
GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
Prelude> :m +GHC.Conc
Prelude GHC.Conc> numCapabilities
99
Prelude GHC.Conc> getNumCapabilities
99   !!!
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!