Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is numCapabilities a pure function?

Tags:

In the concurrency library GHC.Conc there is a function called numCapabilities. Its type is numCapabilities :: Int and it actually returns some number you passed by the command line flag (e.g. 5 if the options are +RTS -N 5).

However, getArgs (type: IO [String]) does essentially the same (it returns the unparsed non-runtime arguments) but isn't a pure function.

If the only excuse is that numCapabilities is often needed in pure code, in what way aren't other command line options not needed in pure code?

Am I something missing or is either numCapabilities a design flaw or am I allowed to write the following monster?

myGetArgs = unsafePerformIO getArgs 
like image 409
helami Avatar asked Aug 07 '12 17:08

helami


1 Answers

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.

like image 156
dflemstr Avatar answered Jan 11 '23 13:01

dflemstr