# How cmdargs subverts Haskell

**URL:** <https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238>\
**Category:** Uncategorized\
**Created:** [May 5, 2023, 8:15am UTC](https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238 "2023-05-05T08:15:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [May 5, 2023, 8:15am UTC](https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238/1 "2023-05-05T08:15:00Z")

</div>

Continuing the discussion from [Simple newbie-friendly CLI parser?](http://discourse.haskell.org/t/simple-newbie-friendly-cli-parser/6228/12):

Let me try to explain why I think `cmdargs` subverts Haskell. The package has a few different interfaces, but I’m talking about the implicit interface from the module [`System.Console.CmdArgs.Implicit`](https://hackage.haskell.org/package/cmdargs-0.10.22/docs/System-Console-CmdArgs-Implicit.html). The interface allows you to write a completely normal data type for your command line options, for example:

```haskell
data Sample = Sample {hello :: String} deriving (Show, Data)

```

The only special thing is that you need to derive the `Data` class which allows the package to inspect the structure of the data type generically (see the documentation in [Data.Data](https://hackage.haskell.org/package/base-4.18.0.0/docs/Data-Data.html)).

The magic of `cmdargs` happens in the way you specify the behavior of the command line arguments. The way you do that is by defining a value of your command line options data type, but with magic annotations, for example:

```haskell
sample = Sample{hello = "" &= help "World argument" &= opt "world"}
         &= summary "Sample v1"

```

Here we are assigning an annotated string value to the `hello` field of our `Sample` data type. But `String` in Haskell is just a list of characters. It should not be possible to add extra annotations to a `String`. So where do the annotations come from? Or rather, where do the annotations go?

The answer is that it uses `unsafePerformIO` to write the annotations to a global mutable variable. It writes this information in such a way that it is possible to recover the structure of the data type from the final result, so it is possible to recover the information about which annotation applies to which part of the options data structure.

There are some big problems with this approach as the documentation notes:

> Values created with annotations are not pure - the first time they are computed they will include the annotations, but subsequently they will not.

> Even using this scheme, sometimes GHC’s optimisations may share values who have the same annotation. To disable sharing you may need to specify `{-# OPTIONS_GHC -fno-cse #-}` in the module you define the flags.

So you need to stick very closely to the examples and even then you might need to ask the compiler not to interfere.

---

<div class="post-metadata">

**Author:** ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://discourse.haskell.org/u/halloleo)\
**Post date:** [May 6, 2023, 12:54am UTC](https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238/3 "2023-05-06T00:54:30Z")

</div>

Thanks @jaror. Very understandable explanation. Makes total sense.

---

<div class="post-metadata">

**Author:** ![atravers](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@atravers](https://discourse.haskell.org/u/atravers)\
**Post date:** [May 7, 2023, 6:19pm UTC](https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238/4 "2023-05-07T18:19:15Z")

</div>

> […] `cmdargs` subverts Haskell

…more or less than the examples presented [here](http://discourse.haskell.org/t/using-unsafeperformio-safely/4146) e.g:

> [@](#):
>
> [tomjaguarpaw:](http://discourse.haskell.org/t/using-unsafeperformio-safely/4146/16)
> 
> At work we have a Nix-based build system. It “should be” pure, since Nix is reproducible. However, it sometimes calls out to external hardware which is transiently broken, caching “hardware did not respond” into our build results, and with no way of purging it.

Alternately, is it more or less _“subversive”_ than GHC’s [internal](https://gitlab.haskell.org/ghc/ghc/-/blob/ghc-9.6.1-release/compiler/GHC/Types/Unique/Supply.hs) `UniqSupply` type:

> [@](#):
>
> (lines 123-135)
> 
> We [had] to rely on `-fno-state-hack`, full laziness and inlining to get the same  
> result. It was very brittle and required enabling `-fno-state-hack` globally. So  
> it has been rewritten using lower level constructs to explicitly state what we  
> want.

…if it is, you could be in good company:

> [@](#):
>
> [my] `bad_ctx` example has the same kernel of badness as the unique-supply tree example in Sec 10.2 of [Launchbury and Peyton Jones’s] [paper](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.52.3656&rep=rep1&type=pdf).
> 
> [Breaking referential transparency with `unsafeInterleaveST`](https://okmij.org/ftp/Haskell/#unsafeInterleaveST), Oleg Kiselyov.

To me at least, the approach used in `cmdargs` seems more a case of _“incurring technical debt”_ to avoid having to use the lumbering monadic `IO` type in a lot more places, rather than being _“subversive”_.

---

<div class="post-metadata">

**Author:** ![jaror](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jaror/32/3271_2.png) [@jaror](https://discourse.haskell.org/u/jaror)\
**Post date:** [May 8, 2023, 8:42am UTC](https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238/5 "2023-05-08T08:42:08Z")

</div>

I think the build system example is just pure, especially considering that throwing exceptions is still considered pure by Haskell. Their caching problem seems to be more of a problem with error handling than with purity.

The UniqSupply trick actually seems pure too. It creates an infinite tree where the nodes have integer labels that “happen to” correspond with the order in which you evaluate them. The fact that these labels could change every run of the program is not a problem because creating the tree can only be done in I/O:

> If the program was recompiled with, say, a different analysis technique which meant that the evaluation order changed, then indeed different uniques would be generated. But since the whole `mkUniqueSupply` operation is typed as an I/O operation there is no reason to suppose that the same uniques will be generated.

A way to argue that it is pure is that you could get the same tree by generating random labels and getting lucky. The `unsafeInterleaveIO` is just ensuring you always get lucky.

The note, titled “Optimising the unique supply”, about previously relying on `-fno-state-hack`, full laziness, and inlining is as the title suggests just about the performance, not about the correctness of the approach (as far as I can tell). And either way it is solved now with the new implementation.

I think Kiselyov’s `bad_ctx` example would be less surprising (and more safe) if he gave it the I/O type `((Bool, Bool) -> Bool) -> IO Bool`, just like `mkUniqueSupply` has. Kiselyov is right that `unsafeInterleaveIO` is very unsafe, perhaps almost as unsafe as `unsafePerformIO`. But you can still write a safe API (like the unique supply) that uses those unsafe functions under the hood.

So, I think both the examples you mention are actually pure or at least much more benign than what cmdargs is doing.

---

<div class="post-metadata">

**Author:** ![markus](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/markus/32/2194_2.png) [@markus](https://discourse.haskell.org/u/markus)\
**Post date:** [April 1, 2024, 12:14pm UTC](https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238/6 "2024-04-01T12:14:57Z")

</div>

I’ve just spent some time debugging cmdargs, finding the `{-# OPTIONS_GHC -fno-cse #-}` solution, then trying to understand why do I need it. Can’t say I enjoyed it: it’s like lazy IO, all is great until it bites you.

It comes up often enough in the issue tracker as well [Issues · ndmitchell/cmdargs · GitHub](https://github.com/ndmitchell/cmdargs/issues?q=-fno-cse).

---

<div class="post-metadata">

**Author:** ![atravers](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@atravers](https://discourse.haskell.org/u/atravers)\
**Post date:** [April 1, 2024, 5:15pm UTC](https://discourse.haskell.org/t/how-cmdargs-subverts-haskell/6238/7 "2024-04-01T17:15:18Z")

</div>

> […] it’s like lazy IO […]

…you mean lazy I/O like this:

- [Lazy Stream Programming in Prolog](https://arxiv.org/pdf/1907.11354) (2019)

Perhaps it’s just the choice of interface:

> [@](#):
>
> [Prolog] is a real-world programming langauge that does not demand the programmer to twist herself into knots just to be able to perform ordinary tasks that are very simple to do in imperative languages (looking to you Haskell and your hare-brained puritan monads).
> 
> [The limits of the declarative approach - #17 by stassa.p - General - SWI-Prolog](https://swi-prolog.discourse.group/t/the-limits-of-the-declarative-approach/1693/17)
