You don't need an effect system

Look, I mention unsafePerformIO in the post in passing as an example of a thing that could hypothetically wreck my codebase, with or without an effect system. The prime example is actually supposed to be lazy evaluation, since that’s the most realistic one, but I padded it up to three for decency and added a footnote about Safe Haskell. I can remove the mention of unsafePerformIO and it won’t change the meaning of the tangent, let alone that of the post.

If I’m to believe Reddit comments, this one point is easily the most important part of what I wrote, and, in fact, I must not care about type safety whatsoever if I dare utter such an outrageous claim.

The title is the exact same case. I never say that my solution represents every possible use case, or that I’m totally correct on all of this, or that what I wrote is final. The very reason I’m making this post is because I want people to tell me if my thought process is correct. The title is my conclusion as a sum total of all of the observations I make. I shouldn’t have to put “I think” before every single thing I write in my blog.

The problem has barely anything to do with statements themselves, it’s with people isolating them and giving them the most uncharitable readings imaginable. And the lack of insightful responses (except for @tomjaguarpaw’s unyielding fervor) is not leaving much room for me to speculate as to why this is the case.

2 Likes

I think your brand of aggressive minimalism when it comes to Haskell is always a fun perspective. The fact that it almost feels oxymoronic is what makes it intriguing. You clearly like Haskell and are highly skilled with it, but you eschew abstraction heavily in the name Haskell98++ (or is it “98–” lol) minimalism.

Please don’t be discouraged. As still somewhat of a beginner in Haskell I like posts like this. I feel like I can follow them more easily than the higher level stuff. It also gives me a chance to reflect and ask myself why exactly I’d want to track what kinds of effects and what is more safe about that.

I think “stopping you from using IO indiscriminately” is a good reason though, isn’t it? I’d also mostly just choose ReaderT IO or mtl (or recently dabbled with effectful) since I am trying to do the “default” or “standard” way of doing things. There is just more information out there about these and, as arybczak already mentioned, lots of pitfalls are taken care of that I wouldn’t even know about.

Although, no matter what way “X” you choose to implement things, there is some blog post that tells you you should have done “Y” instead … :slight_smile: (Not trying to be snarky. That’s just something that sometimes holds me off from even starting a project tbh.)

I took this post as a way to reflect on whether one should always use the proverbial “hammer” and to point out that there are always some simple alternatives that may be a better fit for the problem.

Honestly, I don’t really see the title (nor content for that matter) as anything particularly controversial or disingenuous. It’s just stating a fact. It’s certainly much better title than “I don’t need an effect system” / “Do you need an effect system?” as some have suggested.

A lot of people here seemingly fail to see the forest from the trees.

1 Like

I think what makes this post confusing is that it only considers a small part of what an effect system does. So yes, you don’t need an effect system if you don’t need all the features it provides. But that’s almost tautological.

What I expected after reading the title was an alternative approach that solves the same problems that effect systems solve. But that’s not what I see in the post (unless I’m missing something).

1 Like

What about this case, the one I described above? Someone (maybe you) uses Early to define a version of replicateM_:

myReplicateM_ :: Int -> IO () -> IO ()
myReplicateM_ n action = unless (n <= 0) $ do
  counter <- newIORef n
  fmap (either id id) $ runEarly $ \early ->
    forever $ do
      remaining <- readIORef counter
      if remaining <= 0
        then leave early ()
        else do
          action
          modifyIORef' counter (subtract 1)

So far so good. Then someone else (or maybe the same person) uses it to read 10 lines from stdin and print their sum:

-- > replicateFailureExample
-- Input: 1, 2, nope
-- Output:
-- Enter 10 integers:
-- Successful termination. Sum: 3
--
-- Expected: Aborted
replicateFailureExample :: IO ()
replicateFailureExample = do
  putStrLn "Enter 10 integers:"
  result <- runEarly $ \early -> do
    total <- newIORef 0
    myReplicateM_ 10 $ do
      line <- getLine
      case readMaybe line of
        Just n -> modifyIORef' total (+ n)
        Nothing -> leave early ()
    readIORef total
  case result of
    Right total -> putStrLn ("Successful termination. Sum: " ++ show total)
    Left () -> putStrLn "Aborted"

Oh dear. Because the author of replicateFailureExample didn’t know (or forgot) that the author of myReplicateM_ used an Early () in the definition of myReplicateM_ this function does not behave as it should. This is a breakdown of compositionality. Further evidence: if one of the authors switches () for an isomorphic type, say newtype Unit = Unit, then it does start working as expected. You cannot know how to safely use myReplicateM_ unless you know all the Early types it uses in its definition.

I don’t understand what that means at all. I would appreciate an illustrative example.

My hunch says it’s not, but unless we see some code it’s hard to tell.

I don’t follow that. What “ordinary” caveats are you referring to?

Sure, but you don’t have to use that operator …

Right, but you know that is what Bluefin does, or have I failed to get that point across?

I’ve shared a critical failure above.

Then it helps to be precise about what your thoughts actually are. I’ve shared an example where your current implementation of Early breaks down in a way that makes it unsuitable for serious use but I’m still not sure what you think of that.

In my experience there are practices that can make one’s life easier, even if one “shouldn’t” have to engage in them.

I hope what I’m sharing here is helpful to you, and if not at least to someone, but I’m having trouble interpreting the valence of “unyielding fervor”. Is that a good or a bad thing?

4 Likes

Not oxymoronic at all I think. One can opt for a no-frills IO-based interface between application components, and still use fancy features of Haskell for certain components. Say, “Servant” for defining servers, or “Rel8” for persistence.

I do wish we had something like the scoped thread-locals proposal mentioned earlier by @tomjaguarpaw, it already exists in other languages and it would make implementing some things in plain IO easier.

2 Likes

Every generation of haskellers discovers that “effect systems” are largely a cargo cult, but told they are not “real haskellers” if they don’t stay in line. They leave the community, an everything starts over.

4 Likes

An expert had spoken, time for me to archive the effectful repository since apparently it’s a cargo cult project.

2 Likes

Why though? There is nothing wrong with mtl or transformers, so I’m pretty sure there is nothing wrong with effectful as well.

The cargo cult part is mostly about the usage of these libraries. I can’t count how many times I saw people use “effect system” because they “need to keep effects under control”, then add MonadIO instance, and they don’t even see the problem.

Anyway, my main concern is not that - people are free to do with their code whatever they want. What’s really sad is how often I see the “then don’t use Haskell” argument.

What universe of discourse are you assuming when you say “nothing”? @arybczak has pointed out, in the documentation of effectful, some things wrong with mtl and transformers, in particular performance and dubious semantics. Additionally, I explained in Bluefin’s documentation how all “synthetic” effect systems (not only mtl and transformers but also polysemy, fused-effects, etc.) make it hard to achieve resource safety. I explain these issues further in my talk A History Of Effect Systems, along with an explanation of the O(n^2) instances problem with mtl.

If you’re saying that there’s not much value in using an effect system and then insisting on MonadIO almost everywhere, then I agree. But needing to do that may be another testament to something that is wrong with mtl: it makes it so cumbersome to define new effects in terms of existing effects that one ends up giving up and just using MonadIO. effectful and Bluefin have no such problem (for all I know nor do polysemy, fused-effects, etc. but I’m not so familiar with them).

Can you share some examples of that happening?

mtl is a somewhat deranged encoding. it’s only due to its age and familiarity that it’s the default.

A bit like using peano numbers when you have lists of atomic binary words available.

1 Like

OK, then it’s time to archive the mtl repo I guess. transformers too?

Or maybe (just maybe!) it’s a testament that we don’t need an effect system? I mean, if it works with MonadIO well enough that people don’t even notice any issue with it, then probably there is no issue to begin with?

Nope. You can reread the thread, and if you don’t find anything, just tell me I’m wrong, OK?

Thanks, I’ll keep it in mind. Not sure what your point is though. If you somehow concluded that I’m advocating for mtl, then you misread me.

Or maybe (just maybe!) it’s a testament that we don’t need an effect system? I mean, if it works with MonadIO well enough that people don’t even notice any issue with it

People do notice issues with mtl when they run large applications that use it at non-trivial scale :roll_eyes:

The effectful repo alone has tons of data about why mtl is not a great choice (from multiple angles). It’s ironic that you talk about cargo culting while repeating myths about mtl that were put under scrutiny and deemed to be untrue a few years ago (by multiple people).

I don’t see how you managed to conclude that I’m advocating for mtl. You are not the first one, so probably I’m the issue here? Sorry, I’m not a native speaker, probably I made a grammatical error somewhere. Let me try again: there is nothing wrong in mtl to justify archiving its repo. Is it clear now? Feel free to ask for clarification if it’s not yet.

I said literally nothing about mtl besides the above, so I don’t understand what “myths” you are talking about. If your line of thinking is “he’s against effect systems, so he’s advocating for mtl”, then no, mtl doesn’t make any difference. It’s cargo cult with mtl, or without mtl, simply because the problem is not a particular library, but the idea itself adopted by the community without proper analysis.

The author makes an honest attempt to do the analysis and offers valuable incites. And instantly regrets it, because you don’t touch the sacred cow. I don’t share author’s disappointment only because I didn’t have any illusions to begin with.

For the record, I’m of the camp of “sure, you don’t need an effect system, but they are nice” and “from some perspectives, pure computation is an effect” to the point that I’ve been exploring my own interpretation via tetration types (extending sum product and exponent types with the next operator) for better describing memory allocation, which things like memory mutation is very much an effect.

And, as far as “nothing wrong with mtl”, I’m using a bastardization of the ST monad / State# token / RealWorlds ZeroBitType passed as an ImplicitParam as a poor man’s effect system to provide eg thread-safe allocator / memory regions rather than use mtl and deal with the resulting quadratic number of typeclasses.

mtl is great for learning, like counting on your fingers with Peano numbers, but like how finger-counting sucks when you want to count larger numbers than like 3, mtl starts getting tedious when you want to compose more than a few things.

And please don’t take that negatively - accessibility and making learning easy is hugely important. Its just that the learning doesn’t stop here, it continues!

1 Like

Cool!

Oh. I think you didn’t…

And I never claimed the opposite. And I clarified multiple times that I never claimed the opposite. Yet you continue explaining me what I actually meant. Good faith?

I literally don’t care about mtl, I don’t even use it, and I don’t understand why we continue talking about it even after multiple clarifications from my side. Did you notice the “my main concern” part of my comment?

EDIT: the post I’m replying to was completely redacted.