2013 Oleg ExtEff
2013 Oleg ExtEff
Since call delimits the effect of cutfalse, it is supposed to com- run $ runState (runState doubleIncr (0:: Int )) (5:: SInt)
pletely handle the CutFalse exception. As for the Choose effect, −− (((1,6),1),6)
call should intercept these requests (to accumulate the choice points The two explicit type annotations Int and SInt are sufficient to
it may have to discard) and re-issue them later. The signature of call distinguish the usage of incr in both calls of doubleIncr; we can use
summarizes these properties. The complete code follows: the newtype trick to navigate our “transformer stack” implicitly.
call :: Member Choose r ⇒ Eff (Exc CutFalse B r ) a → Eff r a
call m = loop [] (admin m) where
Handlers for algebraic effects. Our approach is closely related to
loop jq (Val x) = return x 8 mplus8 next jq the recent congruence of ideas about effect handlers [1, 2, 13, 21,
loop jq (E u) = case decomp u of 25, 33]. Each of these effect handler implementations incorporates
Right (Exc CutFalse) → mzero an effect typing mechanism and decides on the subtle trade-offs of
Left u → check jq u effect processing.
Effect handlers are the generalization of exception handlers
check jq u | Just (Choose [] ) ← prj u = next jq to other effects – the insight first realized by Plotkin and Pret-
check jq u | Just (Choose [x] k) ← prj u = loop jq (k x) nar [25]. Our handle relay in Figure 2 is indeed quite like catch
check jq u | Just (Choose lst k) ← prj u = from the current Control.Exception library [20]: handle relay only
next $ map k lst +
+ jq
check jq u = send (\k → fmap k u) = loop jq
deals with requests/exceptions of a certain type, automatically
re-throwing all other requests for an ‘upstream’ handler to pro-
next [] = mzero cess. Using existentials and Typeable to implement open union is
next (h: t) = loop t h also the same. However, our requests (‘exceptions’) are resumable
rather than abortive and our open unions are typed.
Each clause corresponds to an axiom of call or cutfalse, cover-
Like Frank [21], our library implements shallow handlers: each
ing all axioms. The code clearly expresses the intuition that call
runM selects by case analysis only those effects it wishes to handle.
watches the choice points of its argument computation. When it en-
Much other work [1, 2, 13, 33] focuses on deep handlers, in which
counters a cutfalse request, it discards the remaining choice-points.
all effects of a particular kind are handled uniformly. Compared
The accompanying Eff.hs has several examples of using cutfalse
to shallow ones, deep handlers are often more efficient but less
and call, including nested occurrences of call, which present no
flexible.
problems.
Our library manages sets of effects using both type-level con-
We have demonstrated how to define, and reason about, the
straints and type-level lists; Kammar et al. [13] rely only on type-
interaction of two existing effects, Choose and Exc. We simply
class constraints. Constraints truly represent an unordered set. Us-
defined a new, joint handler, which can be used with the previously
ing constraints exclusively however requires all effect handler def-
written code with Choose effects and alongside other handlers of
initions be top-level since Haskell does not support local type class
Choose (e.g., makeChoice).
instances. Kammar et al. rely on Template Haskell to avoid much of
the inconvenience of type-class encoding and provide a pliable user
6. Related Work interface. Our library is syntactically lighter-weight and requires no
special syntax.
Effect systems have become an active research area, with several Another trade-off is allowing multiple instances of the same
approaches being pursued concurrently and sometimes indepen- effect. We permit not only both State Int and State Float in the
dently. The main approaches improve monad transformers [27], same computation but also several instances of State Int (although
encode handlers for algebraic effects [1, 25], or start with a type- the latter can be easily disabled). A request for state value or change
and-effect system [8, 17, 32]. Our system, independently developed will be handled by the closest handler of the appropriate state type.
from EDLS years ago, ended up with many similarities to the above Eff [1] uses a region-based approach where effect instances are tied
approaches, and also with several notable differences. First, our to region indices while Brady’s library for Idris [2] allows users to
framework is not a stand-alone language; rather, it is an ordinary name and nominally refer to individual instances.
Haskell library. Haskell with common extensions turns out capable
of expressing the type-and-effect system similar to those mentioned Type-level denotation of effects. Swierstra, Filinski, and others
above. (e.g. [8]) provide type-level systems for layering effects. However,
such systems lack the ability to subtract effects (which occurs when [5] D. Espinosa. Building interpreters by transforming stratified monads.
these effects have been handled in our system). Our continuation Unpublished manuscript, 1994.
encoding and union type are similar to Filinski’s, but our union [6] D. A. Espinosa. Semantic Lego. PhD thesis, Columbia University,
type is shrunk as effects are removed. New York, NY, USA, 1995. UMI Order No. GAX95-33546.
[7] A. Filinski. Representing monads. In POPL [26], pages 446–457.
7. Conclusions [8] A. Filinski. Representing layered monads. In POPL ’99, pages 175–
We have presented a new framework for extensible effects that 188, New York, NY, USA, 1999. ACM.
subsumes previous Haskell approaches in expressiveness. The main [9] R. Hinze. Deriving backtracking monad transformers. In ICFP ’00,
highlights of our system are: pages 186–197. ACM Press, 2000.
[10] J. Hughes. The design of a pretty-printing library. In First Intl. Spring
• easy combination of computations performing arbitrary effects; School on Adv. Functional Programming Techniques, pages 53–96,
the type system collects the effects from each computation into London, UK, UK, 1995. Springer-Verlag.
a larger union. [11] ICFP. ICFP ’13, 2013. ACM Press.
• effects arise from an interaction between a client and a handler; [12] M. Jaskelioff and E. Moggi. Monad transformers as monoid trans-
a handler can choose to service one effect or several possibly formers. Theor. Comp. Science, 411(51/52):4441 – 4466, 2010.
interleaving effects; [13] O. Kammar, S. Lindley, and N. Oury. Handlers in action. In ICFP
• the effects that have been completely handled are subtracted [11], page To Appear.
from the type leaving a computation with fewer effects that can [14] D. King and P. Wadler. Combining monads. In J. Launchbury
be further composed with other effects or handled. and P. Sansom, editors, Functional Programming, Glasgow 1992,
Workshops in Computing, pages 134–143. Springer London, 1993.
We have demonstrated that our framework of extensible effects
[15] O. Kiselyov, R. Lämmel, and K. Schupke. Strongly typed heteroge-
subsumes the MTL: any effectful MTL computation can be re- neous collections. In Proc. 2004 workshop on Haskell, pages 96–107,
written in our framework, in essentially the same syntax; we can New York, NY, USA, 2004. ACM.
also write code that is unwieldy or impossible using monad trans-
[16] O. Kiselyov, C.-c. Shan, and A. Sabry. Delimited dynamic binding. In
formers. Furthermore, we have gained flexible efficiency: our dy- ICFP ’06, pages 26–37. ACM Press, 2006.
namic order of layers is contained in one monad, parts of the com-
putation that don’t use a particular effect don’t pay for it (meaning [17] D. Leijen. Koka: A language with row-polymorphic effect inference.
In 1st Workshop on Higher-Order Programming with Effects (HOPE
each effect is “pay-per-use”). 2012). ACM, September 2012.
There are several natural avenues for future work:
[18] S. Liang, P. Hudak, and M. Jones. Monad transformers and modular
• Partitioning “large” monads like the IO monad into compart- interpreters. In POPL ’95, pages 333–343. ACM Press, 1995.
mentalized sections that may each be added and removed from [19] C. Lüth and N. Ghani. Composing monads using coproducts. In ICFP
computations. These partitions include IORefs, IO exceptions, ’02, pages 133–144. ACM Press, 2002.
time functions, pure reading, and reading and writing. [20] S. Marlow. An extensible dynamically-typed hierarchy of exceptions.
• Implementing ST-like effects, explicitly marking state, and pro- In Proc. 2006 Haskell Workshop, pages 96–106. ACM Press, 2006.
viding an allocation system using monadic regions to the user. [21] C. McBride. The Frank manual. https://personal.cis.strath.
ac.uk/conor.mcbride/pub/Frank/, 2012.
• Providing a delimited control interface with shift, prompt and
control, or a similar collection of operators. [22] E. Moggi. Computational lambda-calculus and monads. In LICS,
pages 14–23, Piscataway, NJ, USA, 1989. IEEE Press.
• Cleaning up the implementation by including the Eff (continu-
[23] E. Moggi. An abstract view of programming languages. Technical
ation) monad natively in Haskell (like IO and ST). We would Report ECS-LFCS-90-113, Edinburgh Univ., 1989.
then come full-circle to the Scheme and ML families of lan-
[24] B. O’Sullivan, J. Goerzen, and D. Stewart. Real world Haskell – code
guages, providing efficient native state, IO, and continuations you can believe in. O’Reilly, 2008.
and mechanisms for users derive other effects from them. The
advantage here is that we have a type system to track these user- [25] G. Plotkin and M. Pretnar. Handlers of algebraic effects. In ESOP ’09,
pages 80–94, Berlin, Heidelberg, 2009. Springer-Verlag.
defined effects.
[26] POPL. POPL ’94, 1994. ACM Press.
Acknowledgments [27] T. Schrijvers and B. C. Oliveira. Monads, zippers and views: virtualiz-
ing the monad stack. In ICFP ’11, pages 32–44, New York, NY, USA,
We are very grateful to Simon Peyton-Jones for many helpful 2011. ACM.
comments and discussions. Insightful comments and suggestions [28] M. Shields and E. Meijer. Type-indexed rows. In POPL ’01, pages
by Sam Lindley and anonymous reviewers are greatly appreciated. 261–275, London, United Kingdom, Jan. 17–19, 2001. ACM Press.
This material is based upon work supported by the National Science [29] G. L. Steele, Jr. How to compose monads. Technical report, Thinking
Foundation under Grant No. 1117635. Machines Corporation, Cambridge, Massachusetts, July 1993.
[30] G. L. Steele, Jr. Building interpreters by composing monads. In POPL
References [26], pages 472–492.
[1] A. Bauer and M. Pretnar. Programming with algebraic effects and [31] N. Swamy, N. Guts, D. Leijen, and M. Hicks. Lightweight monadic
handlers. arXiv:1203.1539 [cs.PL], 2012. programming in ML. In ICFP’11, pages 15–27, Sept. 2011.
[2] E. C. Brady. Programming and reasoning with algebraic effects and [32] W. Swierstra. Data types à la carte. J. Funct. Program., 18(4):423–
dependent types. In ICFP [11], page To Appear. 436, July 2008.
[3] R. Cartwright and M. Felleisen. Extensible denotational language [33] S. Visscher. Control.effects, 2012. URL http://github.com/
specifications. In M. Hagiya and J. C. Mitchell, editors, Theor. Aspects sjoerdvisscher/effects.
of Comp. Soft., number 789 in LNCS, pages 244–272. Springer, 1994. [34] P. Wadler. The essence of functional programming. In POPL ’92,
[4] D. Espinosa. Modular denotational semantics. Unpublished pages 1–14, New York, NY, USA, 1992. ACM.
manuscript, 1993.