<p>That's three questions.</p>

<h3>Can handlers be decomposed?</h3>

<p>No, a handler cannot always be decomposed because the operations may <em>interact</em>. It is false that you can decompose <code>get</code> and <code>set</code> into separate handlers because they have to handle a <em>shared state</em>. You can <a href="http://www.eff-lang.org/try/" rel="noreferrer">try these examples online</a> in Eff. First, the ordinary state handler:</p>

<pre><code>type 'a state = effect
  operation get: unit -&gt; 'a
  operation set: 'a -&gt; unit
end

let r = new state

let state_handler x = handler
  | val y -&gt; (fun s -&gt; (y, s))
  | r#get () k -&gt; (fun s -&gt; k s s)
  | r#set s' k -&gt; (fun _ -&gt; k () s')
  | finally f -&gt; f x

;;

with state_handler 30 handle
   let x = r#get () in
   let () = r#set 20 in
   let y = r#get () in
     (x,y)
;;
</code></pre>

<p>The above example returns <code>(30, 20)</code>, as it should. Now let's separate the handler into two handlers and apply them, as you suggested:</p>

<pre><code>type 'a state = effect
  operation get: unit -&gt; 'a
  operation set: 'a -&gt; unit
end

let r = new state

let get_handler x = handler
  | val y -&gt; (fun _ -&gt; y)
  | r#get () k -&gt; (fun s -&gt; k s s)
  | finally f -&gt; f x

let put_handler x = handler
  | val y -&gt; (fun _ -&gt; y)
  | r#set s' k -&gt; (fun _ -&gt; k () s')
  | finally f -&gt; f x

;;

with put_handler 30 handle
with get_handler 30 handle
   let x = r#get () in
   let () = r#set 20 in
   let y = r#get () in
     (x,y)
;;
</code></pre>

<p>The result is <code>(30, 30)</code> which is unexpected. The problem is that each handler <code>put_handler</code> and <code>get_handler</code> is rolling its own state, so <code>get</code> takes state from one place and <code>put</code> places it into another place.</p>

<h2>Why do handlers have special types?</h2>

<p>In a language with operations and handlers we distinguish <em>values</em> from <em>computations</em>. A value is an inert piece of data which cannot trigger any effects and needs not be computed (such as a constant <code>true</code>, a tuple <code>(42, false)</code>, or a function <code>fun x =&gt; ...</code>). A computation is an expression which must still be evaluated, and it may trigger an effect, produce a value, or diverge. Now:</p>

<ul>
<li>functions map values to computations,</li>
<li>handlers map computations to computations.</li>
</ul>

<p>The difference is visible in the types: $\alpha \to \beta$ is the <em>function</em> type, but $\alpha \Rightarrow \beta$ is the <em>handler</em> type. In operational semantics there is also a difference: we <em>must</em> apply a function to a value, and we <em>must</em> apply a handler to a computation. Thus, instead of writing</p>

<pre><code>(fun x =&gt; print "Hello"; x + 3) (print "world" ; 8)
</code></pre>

<p>we must write</p>

<pre><code>let v = (print "world"; 8) in (fun x =&gt; print "Hello"; x + 3) v
</code></pre>

<p>Of course this is quite annoying and the programmer doesn't have to do it by hand, at least not in Eff. Eff will perform the transformation automatically. Also note that here <code>print "world"; 8</code> is evaluated first, and <em>then</em> the function is applied to <code>v</code>.</p>

<p>You can think of a handling construct as an application of a handler to a computation:</p>

<pre><code>with H handle C
</code></pre>

<p>means "apply <code>H</code> to the computation <code>C</code>". Note that we do <em>not</em> evaluate <code>C</code> first, contrary to how we evaluated <code>v</code> above before we applied the function.</p>

<h3>Why are computation types used?</h3>

<p>The computation types are part of an adjunction between <em>value types</em> and <em>computation types</em>. We have a category $\mathcal{V}$ of value types and a category $\mathcal{C}$ of computation types, with two functors
$$F : \mathcal{V} \to \mathcal{C} \qquad\text{and}\qquad
  U : \mathcal{C} \to \mathcal{V}$$
such that $F$ is left-adjoint to $U$. It is a general fact that such an adjunction determines a monad $T = U \circ F : \mathcal{V} \to \mathcal{V}$. By passing from the adjunction $F \dashv U$ to the monad $T$ we lose a little bit of information (but not much, since every monad can be decomposed into an adjunction in a canonical way).</p>

<p>The use of computation types is thus largely a matter of mathematical setup. It is quite natural to use value and computation types when we consider handlers. This is so because operations and handlers arise from a similar picture that occurs in algebra: there is a category of sets $\mathsf{Set}$ and the category of algebras $\mathsf{Alg}$ (for the given operations), together with functors
$$F : \mathsf{Set} \to \mathsf{Alg} \qquad\text{and}\qquad
  U : \mathsf{Alg} \to \mathsf{Set} $$
where $F(X)$ is the free algebra generated by the set $X$ and $U(A)$ is the underlying set of the algebra $A$. This again is an adjunction. (In fact, handlers were discovered starting from these two functors. We massaged them until algebra became programming.) In this instance value types are sets and computation types are <em>free</em> algebras. For example, a computation which returns a natural number, and may trigger <code>put</code> and <code>get</code> operations, is the free algebra $F(\mathbb{N})$ for the operations <code>put</code> and <code>get</code>. A handler for such an operation has the form</p>

<pre><code>handler
  put x k =&gt; P
  get x k =&gt; G
  val n =&gt; V(n)
</code></pre>

<p>and corresponds to an algebra homomorphism $\phi : F(\mathbb{N}) \to A$ where $A$ is an algebra with operations $P$ and $G$, and $\phi$ maps a generator $n$ to the element $V(n) \in A$.</p>

<p>Could we rephrase all of this in terms of the monad $T = U \circ F$? Probably, but it would be ugly, and it would start to look like we're trying to hack things in Haskell, rather than do them from foundations up.</p>

CC BY-SA 3.0
2017-08-31T10:06:56.810Z
This is a lovely answer.
Neel Krishnaswami
CC BY-SA 3.0
2017-08-31T11:12:20.467Z
Thank you so much for the explanation! I didn't expect to get such a comprehensive answer so soon :)
Labbekak
CC BY-SA 3.0
2017-08-31T09:38:22.190Z
2017-08-31T09:52:56.577Z
2017-08-31T09:52:56.577Z
Andrej Bauer
<p>Can a handler always be decomposed? Say a handler for Get and Set effects could be decomposed to a handler for Get and a handler for Set.
After they could be composed again by first applying the handler for Set and after the handler for Get.</p>

<p>Also why do handlers have special type instead of just being a function type from effectful type to effectful type.</p>

<p>And last question: why are computation types used so often in the papers instead of something more like monads in Haskell (where pure functions still exist).</p>

<pl.programming-languages>
Questions about algebraic effects and handlers, decomposed handlers
Labbekak
# Questions about algebraic effects and handlers, decomposed handlers

(Asked by: username_0 on 2017-08-30)

Can a handler always be decomposed? Say a handler for Get and Set effects could be decomposed to a handler for Get and a handler for Set.
After they could be composed again by first applying the handler for Set and after the handler for Get.

Also why do handlers have special type instead of just being a function type from effectful type to effectful type.

And last question: why are computation types used so often in the papers instead of something more like monads in Haskell (where pure functions still exist).

**Tags:** pl.programming-languages


---

## 1 answers

### Answer by username_1 on 2017-08-31. Score: 6 | Accepted answer

That's three questions.
Can handlers be decomposed?
No, a handler cannot always be decomposed because the operations may interact. It is false that you can decompose get and set into separate handlers because they have to handle a shared state. You can try these examples online in Eff. First, the ordinary state handler:
type 'a state = effect
  operation get: unit -> 'a
  operation set: 'a -> unit
end

let r = new state

let state_handler x = handler
  | val y -> (fun s -> (y, s))
  | r#get () k -> (fun s -> k s s)
  | r#set s' k -> (fun _ -> k () s')
  | finally f -> f x

;;

with state_handler 30 handle
   let x = r#get () in
   let () = r#set 20 in
   let y = r#get () in
     (x,y)
;;

The above example returns (30, 20), as it should. Now let's separate the handler into two handlers and apply them, as you suggested:
type 'a state = effect
  operation get: unit -> 'a
  operation set: 'a -> unit
end

let r = new state

let get_handler x = handler
  | val y -> (fun _ -> y)
  | r#get () k -> (fun s -> k s s)
  | finally f -> f x

let put_handler x = handler
  | val y -> (fun _ -> y)
  | r#set s' k -> (fun _ -> k () s')
  | finally f -> f x

;;

with put_handler 30 handle
with get_handler 30 handle
   let x = r#get () in
   let () = r#set 20 in
   let y = r#get () in
     (x,y)
;;

The result is (30, 30) which is unexpected. The problem is that each handler put_handler and get_handler is rolling its own state, so get takes state from one place and put places it into another place.
Why do handlers have special types?
In a language with operations and handlers we distinguish values from computations. A value is an inert piece of data which cannot trigger any effects and needs not be computed (such as a constant true, a tuple (42, false), or a function fun x => ...). A computation is an expression which must still be evaluated, and it may trigger an effect, produce a value, or diverge. Now:

functions map values to computations,
handlers map computations to computations.

The difference is visible in the types: $\alpha \to \beta$ is the function type, but $\alpha \Rightarrow \beta$ is the handler type. In operational semantics there is also a difference: we must apply a function to a value, and we must apply a handler to a computation. Thus, instead of writing
(fun x => print "Hello"; x + 3) (print "world" ; 8)

we must write
let v = (print "world"; 8) in (fun x => print "Hello"; x + 3) v

Of course this is quite annoying and the programmer doesn't have to do it by hand, at least not in Eff. Eff will perform the transformation automatically. Also note that here print "world"; 8 is evaluated first, and then the function is applied to v.
You can think of a handling construct as an application of a handler to a computation:
with H handle C

means "apply H to the computation C". Note that we do not evaluate C first, contrary to how we evaluated v above before we applied the function.
Why are computation types used?
The computation types are part of an adjunction between value types and computation types. We have a category $\mathcal{V}$ of value types and a category $\mathcal{C}$ of computation types, with two functors
$$F : \mathcal{V} \to \mathcal{C} \qquad\text{and}\qquad
  U : \mathcal{C} \to \mathcal{V}$$
such that $F$ is left-adjoint to $U$. It is a general fact that such an adjunction determines a monad $T = U \circ F : \mathcal{V} \to \mathcal{V}$. By passing from the adjunction $F \dashv U$ to the monad $T$ we lose a little bit of information (but not much, since every monad can be decomposed into an adjunction in a canonical way).
The use of computation types is thus largely a matter of mathematical setup. It is quite natural to use value and computation types when we consider handlers. This is so because operations and handlers arise from a similar picture that occurs in algebra: there is a category of sets $\mathsf{Set}$ and the category of algebras $\mathsf{Alg}$ (for the given operations), together with functors
$$F : \mathsf{Set} \to \mathsf{Alg} \qquad\text{and}\qquad
  U : \mathsf{Alg} \to \mathsf{Set} $$
where $F(X)$ is the free algebra generated by the set $X$ and $U(A)$ is the underlying set of the algebra $A$. This again is an adjunction. (In fact, handlers were discovered starting from these two functors. We massaged them until algebra became programming.) In this instance value types are sets and computation types are free algebras. For example, a computation which returns a natural number, and may trigger put and get operations, is the free algebra $F(\mathbb{N})$ for the operations put and get. A handler for such an operation has the form
handler
  put x k => P
  get x k => G
  val n => V(n)

and corresponds to an algebra homomorphism $\phi : F(\mathbb{N}) \to A$ where $A$ is an algebra with operations $P$ and $G$, and $\phi$ maps a generator $n$ to the element $V(n) \in A$.
Could we rephrase all of this in terms of the monad $T = U \circ F$? Probably, but it would be ugly, and it would start to look like we're trying to hack things in Haskell, rather than do them from foundations up.

> **Comments:**
> **username_2**: This is a lovely answer.  
> **username_0**: Thank you so much for the explanation! I didn't expect to get such a comprehensive answer so soon :)  

---

