# What is an effect and an effect system?

**URL:** <https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429>\
**Category:** Learn\
**Created:** [April 28, 2024, 7:14pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429 "2024-04-28T19:14:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpilgrem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mpilgrem/32/2529_2.png) [@mpilgrem](https://discourse.haskell.org/u/mpilgrem)\
**Post date:** [April 28, 2024, 7:14pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/1 "2024-04-28T19:14:20Z")

</div>

I read [Bluefin, a new effect system - #7 by tomjaguarpaw](http://discourse.haskell.org/t/bluefin-a-new-effect-system/9395/7) and wondered: What is an effect? What is an effect system?

There must be something in the air, because I then came across something similar and recent on Reddit: [https://www.reddit.com/r/haskell/comments/1c9czmn/what\_are\_effects/](https://www.reddit.com/r/haskell/comments/1c9czmn/what_are_effects/).

When I first encountered Haskell, I was puzzled by references to ‘actions’. I looked in the Haskell 2010 Language Report and there the term ‘action’ seemed to be used to mean an expression of a type `m a` where `m` was an instance of class `Monad`. The term ‘IO action’ seemed to be used to mean an expression of type `IO a`. The report suggested that ‘computation’ might be a synonym for ‘IO action’ or, perhaps, also `a -> IO b`. (On the other hand, I wondered, from other things that I had read, if, strictly speaking, actions/computations were expressions of type `IO a` and all other uses of ‘action’ were, to some degree, metaphorical.)

Is an ‘effect’ a synonym for an expression of type `IO a`?

---

<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 28, 2024, 7:41pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/2 "2024-04-28T19:41:33Z")

</div>

> Is an ‘effect’ a synonym for an expression of type `IO a`?

No. Consider the (trivial) I/O action:

```haskell
pure 'a' :: IO Char

```

By design, it has no associated effect. So the presence of `IO (...)` in a type of an expression merely indicates that the expression _could_ have effects (in the classic sense - causing a change in some other context, in this case the rest of reality).

What some find objectionable about the I/O-as-monadic-action approach is its agglomeration of _all_ classic I/O effects. What an (type-based) _“effect system”_ then enables is the restricted use of those classic effects, without e.g. having to define a separate `newtype` for each (combination of) classic effects - instead you just nominate which of those classic effects you need by selecting the appropriate _“type-modifiers”_ (one for each of the required effects).

`(`Note: I’m being vague here with terminology, because of the wide variety of effect-system implementations…and I’m not an effect-system designer `;-)`

---

<div class="post-metadata">

**Author:** ![mpilgrem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mpilgrem/32/2529_2.png) [@mpilgrem](https://discourse.haskell.org/u/mpilgrem)\
**Post date:** [April 28, 2024, 8:07pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/3 "2024-04-28T20:07:37Z")

</div>

Many thanks for the explanation. Can I put it like this:

All effects are expressions of type `IO a` but not all expressions of type `IO a` are effects, because some expressions of type `IO a` definitely do not correspond to any change in the ‘state’ of the real world: `pure x :: IO a` being an example.

An effect system provides a way of grouping effects that have something in common.

---

<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 28, 2024, 9:29pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/4 "2024-04-28T21:29:37Z")

</div>

> All effects are expressions of type `IO a` […]

No. To try to alleviate the confusion, here is another monadic action which can have an effect:

```haskell
newSTRef 3.14159 :: ST s (STRef s Double)

```

So I would simplify what you wrote to:

- Any I/O action - of type `IO (...)` - has the potential to cause a classic effect.

- Not all I/O actions can cause a classic effect e.g. `pure x :: IO Char`

Now as for effects and their systems:

> An effect system provides a way of grouping effects that have something in common.

…sometimes - some effect systems also allow for extensibility (yes, I forgot about this):

- You define a new _“type modifier”_ for the classic effect you’re interested in.
- Then you would define a suitable interpreter for that _“modifier”_ - it is that interpreter which then causes the actual change.

That interpreter could then use potentially any action (depending on what’s in scope) - it could use:

- I/O actions - `IO (...)`
- encapuslated-state actions - `ST s (...)`
- parser actions - `Parse (...)`
- some other action, or combination thereof.

It’s a bit old, but this:

- [DDC/EffectSystem - HaskellWiki](https://wiki.haskell.org/DDC/EffectSystem)

may help to clarify matters. But always remember:

- an I/O action may cause a classic effect (a change in some other context);

- an I/O action may be used to define the interpreter for a new _“type modifier”_ associated with a classic effect;

- but an I/O action isn’t an effect - it’s a Haskell expression, albeit a somewhat-unusual one…

---

<div class="post-metadata">

**Author:** ![mpilgrem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mpilgrem/32/2529_2.png) [@mpilgrem](https://discourse.haskell.org/u/mpilgrem)\
**Post date:** [April 28, 2024, 9:34pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/5 "2024-04-28T21:34:35Z")

</div>

Thanks for your patience! I’ll have a read and a think.

---

<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 28, 2024, 9:47pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/6 "2024-04-28T21:47:54Z")

</div>

Another thing to be aware of is the laziness in terminology. For example:

- `IO` (instead of `IO a`) isn’t a “type”, but a _type constructor_.
- `Just` isn’t a “constructor”, but a _data constructor_.
- `Maybe a` isn’t a “monad”, but a _monadic type_.

This is why I’ve been using the term _classic effect_ - because the word “effect” is now so often being used to describe those _“type modifiers”_ I keep referring to. It would be **very helpful** for there to be one common glossary for all effect systems, because there’s probably no chance of having one common basis (or library) for all effect systems!

---

<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 29, 2024, 1:28am UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/7 "2024-04-29T01:28:14Z")

</div>

I was in a bit of a hurry earlier, so now to address the other parts of your post:

> When I first encountered Haskell, I was puzzled by references to ‘actions’. I looked in the Haskell 2010 Language Report and there the term ‘action’ seemed to be used to mean an expression of a type `m a` where `m` was an instance of class `Monad`.

Some time ago when I was doing some tidying-up of the Haskell wiki, I noticed a monad _“introdutorial”_ on the [Monad](https://wiki.haskell.org/Monad) page which I then separated out into its own page:

- [Merely monadic - HaskellWiki](https://wiki.haskell.org/Merely_monadic)

…there I used a similar definition for “action”.

* * *

> The report suggested that ‘computation’ might be a synonym for ‘IO action’ […]

I suspect that’s more due to happenstance than deliberation: early texts about monadic I/O did use the term _“computation”_. But as my `ST s a` example showed, an action (or computation) need not engage in any form of I/O.

* * *

> […] or, perhaps, also `a -> IO b`.

…yeah, I think I’ve found it:

> [@](#):
>
> (page 176 of 329)
> 
> **Methods**
> 
> **`(>>=) :: m a -> (a -> m b) -> m b`**
> 
> Sequentially compose two actions, passing any value produced by the first as an argument to the second.
> 
> [The Haskell 2010 Report](https://www.haskell.org/definition/haskell2010.pdf)

…this would be a _classic_ example of _“laziness in terminology”_. So how would I (re)phrase this?

- 

> [@](#):
>
> **Methods**
> 
> **`(>>=) :: m a -> (a -> m b) -> m b`**
> 
> Sequentially compose two actions, passing any value produced by the first via a function to the second.

- 

> [@](#):
>
> **Methods**
> 
> **`(>>=) :: m a -> (a -> m b) -> m b`**
> 
> Sequentially compose two actions, using a function to pass any value produced by the first to the second.

…more thought needed here, too.

* * *

> On the other hand, I wondered, from other things that I had read, if, strictly speaking, actions/computations were expressions of type `IO a` and all other uses of ‘action’ were, to some degree, metaphorical.

When monadic I/O was introduced to Haskell 1.3, an expression with the type `IO (...)` was referred to as a _“computation”_. But by the time Haskell 98 arrived, it had been **largely** superseded by the term _“action”_ (the Report still used _“computation”_ ). So no metaphors `(`those are saved for the [tutorials](https://wiki.haskell.org/IO_tutorials_timeline) `;-)`…anyone for a [burrito](https://github.com/withoutboats/burrito)?

---

<div class="post-metadata">

**Author:** ![Ambrose](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/ambrose/32/5672_2.png) [@Ambrose](https://discourse.haskell.org/u/Ambrose)\
**Post date:** [April 29, 2024, 1:29am UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/8 "2024-04-29T01:29:09Z")

</div>

It’s not a direct answer to your question, but [the keynote from the 2023 Haskell Symposium at ICFP](https://m.youtube.com/watch?v=m821Vz8N_bo) was “The Evolution of Effects.” It’s a walk through history but I think for a term of art like “effect,” that’s a good thing to understand.

(It’s a wonderful talk btw.)

---

<div class="post-metadata">

**Author:** ![mpilgrem](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/mpilgrem/32/2529_2.png) [@mpilgrem](https://discourse.haskell.org/u/mpilgrem)\
**Post date:** [April 30, 2024, 10:02pm UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/9 "2024-04-30T22:02:49Z")

</div>

Thanks for the reference to the `ST s a` monadic type. That led me to the 1994 [Lazy Functional State Threads](https://www.microsoft.com/en-us/research/wp-content/uploads/1994/06/lazy-functional-state-threads.pdf) and ‘stateful computations’ and “[a stateful computation] is like a ‘script’, detailing the actions to be performed on its input state”.

Thanks also to @Ambrose for the reference to Nicholas Wu’s remarkable talk, which has introduced me to the term ‘algebraic effect handlers’ (which I think refers to ‘handlers of algebraic effects’).

---

<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 1, 2024, 2:00am UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/10 "2024-05-01T02:00:21Z")

</div>

Some more suggested reading regarding I/O in Haskell:

- _How to Declare an Imperative_ (1997) by Philip Wadler provides a more _“top-down”_ introduction to monadic I/O. It also shows a few (other) possible approaches to implementing monadic I/O.

- _State in Haskell_ (1995) by John Launchbury and Simon Peyton Jones is the _“extended edition”_ of _Lazy Functional State Threads_, including more examples of usage for `ST s a`.

(A cautionary note: in both _Lazy Functional State Threads_ and _State in Haskell_, `ST s a` is non-strict. So there’s a potential for _“time leaks”_ in the examples from either article as the current default implementation of `ST s a` in Glasgow Haskell is strict).

---

<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 1, 2024, 6:04am UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/11 "2024-05-01T06:04:20Z")

</div>

That’s interesting:

> [@](#):
>
> (page 15 of 17)
> 
> There are significant similarities with Gifford and Lucassen’s _effect system_ which uses types to record side effects performed by a program ([Gifford & Lucassen [1986]](https://dl.acm.org/doi/pdf/10.1145/319838.319848)). However, the effects system is designed to delimit the effect of side effects which may occur as a result of evaluation. Thus the semantic setting is still one which relies on a predictable order of evaluation.
> 
> _Lazy Functional State Threads_ (1994)

So there you have it:

- a [simple](https://www.interaction-design.org/literature/topics/keep-it-simple-stupid) explanation of what an effect system is (or at least simpler than mine was),

- and why most effect systems are monadic (to obtain that predictable evaluation order).

---

<div class="post-metadata">

**Author:** ![tomjaguarpaw](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/tomjaguarpaw/32/1230_2.png) [@tomjaguarpaw](https://discourse.haskell.org/u/tomjaguarpaw)\
**Post date:** [May 1, 2024, 7:33am UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/12 "2024-05-01T07:33:54Z")

</div>

I think you’ll have you’ll have trouble getting a definition of “effect”. It’s a very general concept like “type”, “set” or “number”. But if you take for granted a notion of “effect” then an “effect system” is a coherent framework for working with effects.

By that definition I would guess that `IO`, `ST` and `transformers` are all “effect systems”. Effect systems like [`eff`](https://github.com/lexi-lambda/eff), [`polysemy`](https://hackage.haskell.org/package/polysemy), [`effectful`](https://hackage.haskell.org/package/effectful) and [`Bluefin`](https://hackage.haskell.org/package/bluefin) are often called “extensible” because of the way they allow effects to be freely combined. I would say that MTL was an attempt at an extensible effect system, but it falls short because of [the n^2 instances problem](https://stackoverflow.com/a/35541483/997606) which is makes it too cumbersome to truly freely combine effects in practice.

---

<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 1, 2024, 8:50am UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/13 "2024-05-01T08:50:07Z")

</div>

> I think you’ll have you’ll have trouble getting a definition of “effect”.

For example:

- according to some, [recursion is a computational effect](https://legacy.cs.indiana.edu/ftp/techreports/TR546.pdf);

- according to [another](https://strathprints.strath.ac.uk/60166/1/McBride_LNCS2015_Turing_completeness_totally_free.pdf), Haskell currently relies on the _“monad of nontermination and ﬁnite failure”_ (see page 3 of 15);

- and according to [someone else](https://queue.acm.org/detail.cfm?id=2611829), _“even evaluation is an effect”_.

So it seems that a (more) rigorous definition of _“effect”_ is required…

---

<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 2, 2024, 2:19am UTC](https://discourse.haskell.org/t/what-is-an-effect-and-an-effect-system/9429/14 "2024-05-02T02:19:16Z")

</div>

> [@atravers](#):
>
> So how would I (re)phrase [the current description of]
> 
> > **`(>>=) :: m a -> (a -> m b) -> m b`**

Since the Haskell 2010 Report does (briefly) mention _continuations_ (page 132 of 329) and refers to _callbacks_ (pages 116 and 119), albeit in the context of the FFI:

> [@](#):
>
> **Methods**
> 
> **`(>>=) :: m a -> (a -> m b) -> m b`**
> 
> Using a continuation, sequentially compose two actions, passing any value produced by the first to the second.

- [Continuation tutorials timeline - HaskellWiki](https://wiki.haskell.org/Continuation_tutorials_timeline)
