Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are getArgs and getProgName IO actions?

Tags:

I'm a complete newbie currently trying to learn Haskell with "Learn You a Haskell for Great Good". I've reach the section explaining how to work with command line arguments, and something is bugging me.

From my understanding (and haskell.org's definition), actions are meant to encapsulate side effects. Command line arguments being immutable inputs for a given instance of the program, then what is the point of having getProgName :: IO String rather than getProgName :: String? Differently put: what's the point of preventing a pure function from calling getProgName?

Update

I had some great answers to this question so far. I'm accepting Don Stewart's as the most simple and concise, but Conal's (with its associated blog post) is definitely worth a read.

like image 852
icecrime Avatar asked Jan 12 '13 16:01

icecrime


1 Answers

Firstly, getArgs can change at runtime. See withArgs.

Secondly, getArgs and getProgName fall into an interesting class of impure computations - they are thought of as constants during a program run, however, they're are not values available at compile time, and they change from one program run to another. They don't have a clean denotation.

See e.g. earlier discussions, where getArgs, and floating point computations are discussed. And even minBound/maxBound can be thought to be in this class.

like image 93
Don Stewart Avatar answered Sep 18 '22 10:09

Don Stewart