On the Expressive Power of User-Defined Effects: Effect Handlers, Monadic Reflection, Delimited Control
We compare the expressive power of three programming abstractions for user-defined computational effects: Bauer and Pretnar's effect handlers, Filinski's monadic reflection, and delimited control without answer-type-modification. This comparison allo…
Authors: Yannick Forster, Ohad Kammar, Sam Lindley
39 On the Expressiv e Power of User-Defined Eects: Eect Handlers, Monadic Ref lection, Delimited Control Y ANNICK FORSTER, Saarland Universi ty and Universi ty of Cambridge OHAD KA MMAR, Univ ersity o f Oxf ord and Universi ty of Cambridge SAM LINDLE Y, University of Edi nburgh MA TIJA PR ETNAR, Univ ersity of Ljubljana W e compare the e xpressive power of three programming abstractions for user-dened computational eects: Bauer and Pretnar’s eect handlers, Filinski’s mo nadic reection, and delimited control witho u t answ er-type-mo dication. This com- parison allows a precise discussion about the relativ e expressive ness of each programming abstraction. It also demonstrates the sensitivity of the relative expressiv eness of user-dened eects to se emingly ort ho gonal language features. W e present three calculi, one p er abstract ion, extending Levy’s call-by-push-value. For each calculus, we present syntax, operational sema ntics, a natural type-and-eect system, and, for eect handlers and monadic reection, a set-theo retic deno- tational semantics. W e establish their basic meta-theoretic properties: safety , termination, and, where applicable , so u ndness and ade quacy . Using Felleise n’s notion of a macr o translation, we show t hat these abstractio ns can macro-e xpress ea ch other , and show which translations preserve typeability . W e use the adequ ate nitary set-theoretic denotational semantics fo r the monadic calculus t o show that e ect handlers cannot be macr o-expressed while pre serving t yp eability either by monadic reection or by delimited control. W e supp lement our development with a mechanised Abella formalisation. CCS Concepts: • Theor y of co mputat ion → Control primitives; Functional constructs; T ype structures; Denota- tional sem a ntics; Operat ional sema nt ics; Categorical sema ntics; Additional Key W ords and Phrases: a lgebraic eects and handlers, monads, delimited control, comput ational eects, shift0 and reset, monadic reection, reify and reect, macro express iveness, type-and-e ect systems, denotational semantics, lan- guage extension, call-by-push-value, lambda calcul i A CM Reference format: Y annick Forster, Ohad Kammar, Sam Lindley, an d Matija Pretnar . 2017 . On the Expressive Power of User-D ened Eects:. P ACM Progr . Lang. 1, 1, Article 39 (March 2017), 2 7 pages. DOI: 00 0 0001.0000001 1 IN TRODUCTION How should we com pare abstractions for user-dened ee cts? The use of c omputational ee cts, such as le, terminal, and netw ork I/O, random-numb er generation, and memory allo cation and mutation, is controv ersial in functional programming. While languages like Scheme and ML allow these eec ts to o ccur everywhere, pure languag es like Haskell restri ct the use of ee cts. A main trade- o when incorporating computational eects into the language is giving up some o f the most basic properties of the lam bda calculus, like β -equality , refere ntial transpar ency , and c onuence. The loss of these properties may lead to unpredictable behaviour in lazy languages like Haskell, or l imit the applicability of c orrectness preserving transformations like com mon subexpression elimination or c ode motion. Permission to make digital or hard copies of part or all of this work for personal or classroo m use is granted with out fee pro vided that copies are not made or distributed for prot or commercial advantage and that copies bear this notic e and the full citatio n on the rst page. Copyrights for th ird-party components of this wo rk must be honor ed. For all othe r use s, co ntact the owner / author(s). © 2017 Copy right held by the owner/author(s). 2475-1421/2017/3- ART 39 $15.00 DOI: 0000001.0000001 P ACM Pr ogr . Lang., V ol. 1, No . 1, Article 39. Publication date: March 2017. 39:2 • Y annick Forster, Ohad Kammar, Sam Li ndley, and Matija Pretnar Monads (M oggi 1989; Spivey 1990; W adler 1990) are the est ablished abstraction fo r incorporating eects into pure lang uages. T he introduction of monads int o Haskell led to their additi onal use as a programming abstrac- tion, allo wing new eects to be dec lared and us ed as if they were nativ e. Examples include parsing (Hutton and Meijer 1998), backtrac king and constraint solving (Sch rijvers et al. 2013), and m echanised reasoning (Ziliani et al. 201 5; Bulwahn et al . 2008). Libraries now exist for monadic programm ing ev en in impure languages such as OCaml 1 , Scheme 2 , and C+ + (Sinkovics and Porkoláb 2 0 13). Bauer and Pretnar (20 15) propose the us e of al gebraic eect s and handlers to structure programs with us er- dened eects. In this approach, the pr ogramm er rst declares algebraic op erations as the syntactic constructs she will use to c ause the eects, in analogy with declaring new exceptions. Then, she denes eect handlers that describe how to handle these op erations, i n analogy wi th exception h andlers. While exceptions immediately transfer control to the enclosing handl er without resumption, a computation may continue in the same position following an eect operation. In order to supp ort resumption, an eect handler has access to the continuation at the p oint of ee ct invocation. Thus alg ebraic eects and handlers provide a form of d elimited control . Delimited control operators have long b een used to encode eects (Danv y 200 6) and algorithms with so- phisticated control ow ( Felleisen et al. 1988). There ar e many variants of s uch control operators, and their inter-relati onships are subtle (Shan 2007), and often appear only in folklore. H ere we f ocus on a specic such op- erator: shift-zero and dollar without answer-type-modi cation (Materzok and Biernacki 201 2), whose operational semantics and type system a re the closest to eect handlers and m onads. W e study these three dierent abstractions for user-dened eec ts: eec t handlers, m onads, and delimited control operators. Our goal is to enable languag e designers to conduct a precise and informe d discussion about the r elative e xpressiv eness of eac h abstraction. In order to compare them, we bui ld on an ide alised calculus for functional-imp erative programming, namely call-by-push-value (Levy 20 04), and extend it with each of the three abstractions and their corresponding natural type systems. W e then ass ess the expr essive power of each abstraction by rigorously c omparing and analysing these calculi. W e use Felleisen’s notion of m acro expres sibility (Felleisen 1991): when a program m ing languag e L is ex- tended by some featu re, we say that the extended language L + is macr o expre ssible when there i s a syntax- directed translation f rom L + to L that k eeps the features in L xed. Fellei sen introduces this notion of reduc- tion to study the e xpressiv e p ower of T uring-complete calc uli, as macro expressivit y is more sensitiv e in these contexts than computability and c o mplexity notions of reduction. W e adapt Felleisen’s notion to the situation where one extension L 1 + of a base c al c ulus L is m acro expressible in another extension L 2 + of the same base calculus L . Doing so enable us t o form ally co mpare the expressi ve pow er f o r each appr oach to user-dened eects. In the rst instance , we show that, disregarding types, all three abstractions are macro-expr essible in terms of one another , g iving six macro-expressi on translations. Some of these translations are known in less rigorous forms, either published, or in fol k lore. One translation, ma c ro-expr essing eec t-ha ndl ers in delimited c ontrol, improv es on prev ious conc rete implementations (Kammar et al. 2013), which rely o n the existence o f a g l o bal higher-order memor y cell storing a stack o f eect-handlers. The translation from monadic reection to ee ct handlers is c ompletely novel. W e also es tablish whether these translations preserve typeability: the translati ons of some well-type d pro- grams are untypeable. W e show that the translation from delimited control to monadic reection preserves typeability . A p o tential dierence b etween the expressiv e power of handler type systems and between monadic reection and delimited control type systems was recently pointed out by Kammar and Pret nar (2017), who give 1 http://www.cas.mc master .ca/~car ette/pa_monad/ 2 http://okmij.org/ ftp/Scheme/monad- in- Scheme.html P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expressive Power of User-Define d Eects • 39:3 eff mon del typed eff typed d el typed mo n established conjectured status ∗ ∗ ∄ ? ∄ ? ∄ ? ∄ ∄ Fig. 1. Existing and conjectured macro translations a straightforward typ eability preserving mac ro-translation of delimited dynamic state int o a calculus eect han- dlers, whereas existing translations using monads and delimited control r eq uire more sophistication (Kiselyov et al. 2006). Here, we establish this dier ence: we demonstrate h ow to use the denotati onal semantics for the monadic calculus to prov e that ther e exists no no macro translation from the eect handlers calculus to the monadic reection calculus that preserves typeability . This set-t he oretic denotational semantics and its adequacy for Filinski’s multi-monadic metalanguag e (201 0) is another piece of folklore which w e pro ve here . W e conj ecture that a similar proof, though with more mathem a tical sophistication, can b e us ed to pro ve t he non-existence of a typeability-preserving mac ro-expr ession translation from the monadic c alculus to eect handlers. T o this end, we g ive adequate set-theoretic semantics to the eect handler calculus with its type-and-ee ct system, and highlight the cr itical semantic invariant a monadic cal c ulus will invali date. Fig. 1 summarises our contributions and conje ctured results. Untype d calc uli app ear on the left and their typed equivalents on the right. U nl a b elled arrows between the typ ed calculi si gnify that the corresponding macro trans lation between the untyped calculi preserves typeability . Arrows labelled by ∗ ar e ne w untyped translations. Arrows l ab elled by ∄ signify that no macro translation exists between the calculi, not even a partial macro translation that is o nl y dened for well-typed program s. The non-expressivi ty results are sensitiv e to the precise collec tion of f eatures in each calculus. For example, extending the base calculus with inducti ve types and primiti ve recursion would c reate g aps in our non-existence arguments, and we conjec ture that extendi ng the calculi with various forms of polymorph ism would make o ur untyped translations typeability-pr eserving. Adding more feature s to each calculus blurs the distinction b etween each abstraction. This sensiti vity means that in a reali stic programm ing language, such as Haskell, OCaml, or Scheme, the dierent abstractions are often practically equivalent (Sc hrijvers et al. 2016). It also teaches us that meaningful relativ e express ivity results must be stated within a rigorous framework such a s a formal calculus, where the exact assumptions and features are made explicit. The full picture is still far from c omplete, and our work lays the foundation for such a precise treatment. W e supplement our pencil-and-paper proofs with a mechanised formalisation in the Abella proof assis tant (Gacek 2008, 2009) of the mor e syntactic aspe cts of our work. Specically , we formalise a Wright and Felleisen style progress- and-preservation soundness theorem (1994), which w e also call safety , for each calculus, and correct- ness theo rems for our translations. W e make the following contributions: • three formal ca l c uli, i .e., syntax and semantics, fo r eect handlers, monadic r eec tion, and delimited control extending a shared call-by -push-value core, and their meta-theory : – set-theoretic denotational semantics for ee ct handlers and monadic r eec tion; – denotational soundness and ade quacy proofs for ee ct handlers and m onadic reection; – a terminati on proo f for monadic reection (proof s for the other c alculi appear in e xisting work); • six mac ro-translations b etween the three untype d calculi, and variati ons on three of those translations; P ACM Pr ogr . Lang., V ol. 1, No . 1, Article 39. Publication date: March 2017. 39:4 • Y an n ick Forster, Ohad Kammar, Sam Lin dley, and Matija Pretnar V , W :: = values x variable | () unit value | ( V 1 , V 2 ) pairing | inj ℓ V variant | { M } thunk M , N :: = computations case V of product ( x 1 , x 2 ) → M matching | case V of { variant inj ℓ 1 x 1 → M 1 matching . . . inj ℓ n x n → M n } | V ! force | return V returner | x ← M ; N sequencing | λ x . M abstraction | M V application | h M 1 , M 2 i pairing | prj i M projection Fig. 2. mam syntax • formally me chanised meta-theor y in Ab ella 3 comprising: – progress and preservation theorems; – the translations between the untyped calculi; and – their c o rrectness proofs in terms of formal simulation r esults; • typeability pr eservation of the macro translation from delimited c ontrol to monadic reection; and • a proo f that there exists no typeab ility-preserving macro translation from eect handlers to either monadic reection or delimited control. W e structure the remainder of the paper as follows. Sections 2– 5 present the core cal c ulus and its extensions with ee ct handlers, monadic reection, and delimited control, in this order , along with their meta-theoretic properties. Section 6 presents the m acro translations between these calculi, th eir correctness, and typeability- preservation. Se ction 7 conc ludes and outlines f urther work. 2 THE C ORE-CALCULUS: M AM W e are interes ted in a functional-imperative calc ulus where eec ts and higher-order features interact well. Levy’s call-by-push-value (CBPV) calculus (Levy 2004) ts the bill. The CBP V paradigm subsumes call-by- name and call-by-value, both syntactically and semantically . In CB PV evaluation order is explicit, and the way it combines computational eects with higher-order f eatures yields simpler program logic reasoning prin- ciples (Plotkin and Pretnar 2008; Kammar and Plo tkin 2 0 12). CBP V allo ws us to uniformly deal with c all-by- value and c all-by-name evaluation strategies, mak ing the theoretical dev elopment relevant to both ML-like and Haskell-like languages. W e extend it wit h a type-and-eec t system, and, a s adjunctions form the semantic basis for CBP V , we call the resulting c alculus the multi-adjunctive metalanguage ( mam ). Fig. 2 presents mam ’s raw term syntax, which distinguishes between values (data) and c omputations (pr o- grams). W e assume a countable set of variables ranged over by x , y , . . . , and a countable set of variant constructor literals ranged over b y ℓ . The unit v alue, product of values, and nite variants/sums are standard. A compu- tation can b e suspended as a thunk { M } , which may be passed around. Products and variants are eliminated with standard pattern match ing constructs. Thunks can be f orced to resume their e xec ution. A c omputation may simply return a value, and two computations can be sequenc ed, as i n Haskell’s do notation. A function computation abstracts ov er values to which it may b e applied. In order to pass a function λx . M as data, it must rst be suspended as a thunk { λ x . M } . For c ompleteness, we also include CBP V’s binar y com putation products, which subsume proje ctions on product values in call-by-name languages. Example 2.1. Using the boolean values inj True () and inj False () , we can implement a logical not op eration: not = { λ b . case b of { inj True x → return i nj False () inj False x → return i nj True () } } 3 https://github.co m/matijapr etnar/user- dened- eects- forma lization P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expressive Power of User-Define d Eects • 39:5 Frames and contexts B :: = x ← [ ] ; N | [ ] V | prj i [ ] basic frames F :: = B computation frames C :: = [ ] | C [F [ ]] evaluation context H :: = [ ] | H [ B [ ]] hoisting context Reduct i on M { M ′ M { β M ′ C [ M ] { C [ M ′ ] Beta re d uction M { β M ′ (×) case ( V 1 , V 2 ) of ( x 1 , x 2 ) → M { β M [ V 1 / x 1 , V 2 / x 2 ] ( + ) case inj ℓ V of { . . . inj ℓ x → M . . . } { β M [ V / x ] ( F ) x ← return V ; M { β M [ V / x ] ( U ) { M } ! { β M (→) ( λ x . M ) V { β M [ V / x ] ( & ) prj i h M 1 , M 2 i { β M i Fig. 3. mam operational semantics Fig. 3 presents mam ’s standard structural o perational semantics, in th e style of Felleisen and Fri edm an (19 87). In order to reuse the core denitions as much as possible, we refactor the semantics into β -reduction rules and a single congruence rule . As usual, a β -reduction reduces a matching pair of introduction and elimination forms. W e specify in the deni tion of evaluation contexts the basic fr ames , which all our extensions will share. W e use [ ] to denote the hole in each frame or context, which signies which term should evaluate rst, and dene su bstitution frames and terms for holes ( C [ F [ ]] , C [ M ] ) in the standard way . Later , in each c alculus we will make use of hoisting fr ames in order to capture c ontinuations, stacks of basic frames, extending from a control o perator to the nearest delimiter . As usual, a reducible term can be decom posed into at most o ne pair of evaluation context and β -reducible term, making th e semantics determinist ic. Example 2.2. With this semantics we hav e not ! ( inj True () ) { + return inj ( False() ) . In this development, we use the following standard syntactic sugar . W e use nested patterns in our p attern matching constructs. W e abbrev iate the variant constructors to th eir labels, and omit the unit value, e.g., True desugars to inj True () . W e allow the application of f unctions and the elimination constructs to apply to arbitrary computations, and not just values, by setting for example M N ≔ x ← N ; M x for some fresh x , giving a m ore readable, albeit call-by-value, app ear a nc e. Example 2.3. As a running example we express bo olean state in each of our c alculi such that we can write code like t oдд l e in Fig. 4 ( a) which tog gles the state and returns the value of the or iginal state. In mam , we do so via a st andard stat e-passing transformation, as in Fig. 4 (b), and run t oд д l e with the initial value T rue to g et the expecte d result r u nS t at e ! t oд д l e True { ⋆ (True , Fa lse) . This transformation is not a macro translation. In addition to th e denition o f p u t and g e t , it globally threads the state through t oд дl e ’s structure . In later section, each abstraction pr ovides a dierent means for mac ro-expr essing state. Fig. 5 presents mam ’s typ es and eects. mam is a variant of K ammar and P lotkin’s multi-adjunctiv e inter- mediate lang uage (2012) without ee c t op erations or co ercions. As a core calculus for three calculi with very t oд д l e = { x ← g e t !; y ← n ot ! x ; p u t ! y ; x } (a) I deal style дet = { λs . ( s , s ) } put = { λs ′ . λ _ . (() , s ′ ) } r u nS t at e = λc . λs . c ! s t oд д l e = { λs . ( x , s ) ← g et ! s ; y ← n ot ! x ; ( _ , s ) ← p ut ! y s ; ( x , s ) } (b) State-passing style Fig. 4. User -defined bo olean state P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:6 • Y an n ick Forster, Ohad Kammar, Sam Lin dley, and Matija Pretnar E : : = eects ∅ pure eect K :: = kinds | E eects | V al values | Comp E computations | Ctxt environments A , B :: = value types α type variable | 1 unit | A 1 × A 2 products | { inj ℓ 1 A 1 variants | . . . | inj ℓ n A n } | U E C thunks C , D :: = computation types F A returners | A → C functions | C 1 & C 2 products Environments: Θ :: = α 1 , . . . , α n Γ , ∆ :: = x 1 : A 1 , . . . , x n : A n Fig. 5. mam kinds and types dierent no tions of eect, mam i s pure , and th e only shar ed eect is the empty e ec t ∅ . W e include a kind system, unnee ded in traditional CBP V where a context-free distinction between values and computations forces types to be well-forme d. Th e two po ints of dierence from CBP V are the kind of eects, and the renement of the c o mputation kind by well-kinded ee cts E . The oth er av ailable k inds are the standard value kind and a kind for well-formed environments (without typ e dependenc ies). Our typ e system includes value-typ e vari- ables (which we will later use for dening monads parametrically). Simple value types are standard CBP V value types, and each type of thunks includes an ee ct annotation describing the ee cts of these thunks. Computation types include r eturners F A , which are co mputations that return a value of type A , similar to the monadic typ e Monad m = ⇒ m a in H askell. Functions are computations and only take values as arguments. W e include CBP V’s computation products, which ac c ount for product elimination via projection in call-by-name lang uages. T o ensure the w ell-kindedness of types, which may contain type-variables, we use typ e envir onments in a list notation that denotes sets of type-variables. Similarly , we use a list notation for value envi ronments, which ar e functions from a nite set of variable names to the set of value types. Example 2.4. The typ e of b ooleans bit is giv en by { inj False 1 | inj True 1 } . Fig. 6 presents the kind and typ e systems. The only eect ( ∅ ) is well-kinded. T ype variables m ust appear in the current typ e environment, and they are always value types. The remaining value and com putation types and environments have straightforward structural kinding conditions . Thunks of E -c o mputations of type C re- quire the type C to b e well-kinded, which includes the side-condition that E i s a well-kinded eect. This kind system has the property that eac h valid kinding judgem ent has a unique deri vation. V alue typ e judgements assert that a value term has a well-formed value type under a well-formed envi ronment in some type variable environment. The rules for simple types are straightfor ward. Observe how the eect annotation mov es between the E -computation type judg em ent and the type of E -thunks. The side condition for c omputation type j udge- ments asserts that a computation term has a well-formed E -computation type under a well-formed environment for some well-formed eect E under some type variable environment. The rules for variables, value and com - putation products, variants, and functions are straightforward. The rules for thunk ing and forcing ensure the computation’s ee ct annotation agrees with the eect annotation of the thunk. The rule for return allows us to return a value at any eect annotation, reecting the fact that this is a may -eect system: the eect annotations track which eects may b e caused, without prescribing that any eect must o ccur . The rule for se quencing reects our choice to omit any fo rm of e ec t c oercion, subee cting, or eec t p olymorphism: the three eect annotations must agree. T here are more sophisticated eec t systems which allow more exibility (Katsumata 2014). W e leave the pr ecise treatment of such extensions to later work. Example 2.5. The values from Fig. 4( b) have the following types: n ot : U ∅ ( bit → F bit ) g e t : U ∅ ( bit → F ( bit × bit )) p u t : U ∅ ( bit → bit → F ( bi t × bit )) t oд д l e : U ∅ ( bit → F ( bit × bit )) r u nS t at e : U ∅ ( U ∅ ( bit → F ( bit × bit )) → bit → F ( bit × bit )) P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expressive Power of User-Define d Eects • 39:7 Eect kinding Θ ⊢ k E : E Θ ⊢ k ∅ : E V alue kind ing Θ ⊢ k A : V al α ∈ Θ Θ ⊢ k α : V al Θ ⊢ k 1 : V al Θ ⊢ k A 1 : V a l Θ ⊢ k A 1 : V a l Θ ⊢ k A 1 × A 2 : V a l for every 1 ≤ i ≤ n : Θ ⊢ k A i : V al Θ ⊢ k { i nj ℓ 1 A 1 | . . . | inj ℓ n A n } : V al Θ ⊢ k C : Comp E Θ ⊢ k U E C : V al Computation kind i ng Θ ⊢ k C : Comp E ( Θ ⊢ k E : E ) Θ ⊢ k A : V al Θ ⊢ k F A : Comp E Θ ⊢ k A : V al Θ ⊢ k C : Comp E Θ ⊢ k A → C : Comp E Θ ⊢ k C 1 : Comp E Θ ⊢ k C 2 : Comp E Θ ⊢ k C 1 & C 2 : Comp E Context kinding Θ ⊢ k Γ : Ctx t for all x ∈ Dom ( Γ ) : Θ ⊢ k Γ ( x ) : Val Θ ⊢ k Γ : Ctxt V alue typing Θ ; Γ ⊢ V : A ( Θ ⊢ k Γ : Ctxt , A : V al ) ( x : A ) ∈ Γ Θ ; Γ ⊢ x : A Θ ; Γ ⊢ () : 1 Θ ; Γ ⊢ V 1 : A 1 Θ ; Γ ⊢ V 2 : A 2 Θ ; Γ ⊢ ( V 1 , V 2 ) : A 1 × A 2 Θ ; Γ ⊢ V : A i Θ ; Γ ⊢ inj ℓ i V : { inj ℓ 1 A 1 | . . . | inj ℓ n A n } Θ ; Γ ⊢ E M : C Θ ; Γ ⊢ { M } : U E C Computation typing Θ ; Γ ⊢ E M : C ( Θ ⊢ k Γ : Ctxt , E : E , C : Comp E ) Θ ; Γ ⊢ V : A 1 × A 2 Θ ; Γ , x 1 : A 1 , x 2 : A 2 ⊢ E M : C Θ ; Γ ⊢ E case V of ( x 1 , x 2 ) → M : C Θ ; Γ ⊢ V : U E C Θ ; Γ ⊢ E V ! : C Θ ; Γ ⊢ V : { i nj ℓ 1 A 1 | · · · | inj ℓ n A n } for every 1 ≤ i ≤ n : Θ ; Γ , x i : A i ⊢ E M i : C Θ ; Γ ⊢ E case V of { i nj ℓ 1 x 1 → M 1 ; · · · ; inj ℓ n x n → M n } : C Θ ; Γ ⊢ V : A Θ ; Γ ⊢ E return V : F A Θ ; Γ ⊢ E M : C 1 & C 2 Θ ; Γ ⊢ E prj i M : C i Θ ; Γ ⊢ E M : F A Θ ; Γ , x : A ⊢ E N : C Θ ; Γ ⊢ E x ← M ; N : C Θ ; Γ , x : A ⊢ E M : C Θ ; Γ ⊢ E λ x . M : A → C Θ ; Γ ⊢ E M : A → C Θ ; Γ ⊢ V : A Θ ; Γ ⊢ E M V : C Θ ; Γ ⊢ E M 1 : C 1 Θ ; Γ ⊢ E M 2 : C 2 Θ ; Γ ⊢ E h M 1 , M 2 i : C 1 & C 2 Fig. 6. mam kind and typ e system Theorem 2.6 (mam safety) . W ell-typed programs don’t go wrong: for all closed mam returners Θ ; ⊢ ∅ M : F A , either M { N for some Θ ; ⊢ ∅ N : F A or else M = return V for some Θ ; ⊢ V : A . The proof is standard and form alised in Ab ella, established b y inducti ve pr o ofs of progress and preservation. W e extend existing termination results for CBP V (Do czkal 2007; Doc zk al and Schwinghammer 2009). W e say that a term M diverges , and write M { ∞ if for every n ∈ N there exists some N such that M { n N . W e say that M does not dive rge when M 6 { ∞ . Theorem 2.7 (mam termina tion). There are no innite reduction sequences: for all mam terms ; ⊢ ∅ M : F A , we have M 6 { ∞ , and there exists some unique ; ⊢ V : A such that M { ⋆ return V . P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:8 • Y an n ick Forster, Ohad Kammar, Sam Lin dley, and Matija Pretnar The proof uses T ait’s metho d (1967) to establish totality , dening a relational interpr etation to types and establishing a basic lemma, and the notion of lifting from Hermida’s thesis (19 93) to dene the monadic lifting of a pr edicate. The remainder of the proo f is imm ediate as the semantics is deterministic. For the purpose of dening contextual equivalence, we dene the subclass of ground typ es : ( ground values ) G :: = 1 | G 1 × G 2 | { inj ℓ 1 G 1 | . . . | inj ℓ n G n } The deni tion of program contexts X [ ] and their typ e judgements is straightfor ward but tedious and lengthy with four kinds of judgements, and so we take a dierent approach. Inf o r mally , given two computation terms M 1 and M 2 , in order to dene their contextual equivalence, we need to quantify over the set Ξ [ M 1 , M 2 ] ≔ { h X [ M 1 ] , X [ M 2 ]i | X [ ] is a well - t y p Once we dene this set, we do not need c o ntexts, their type system, or their semantics in the remainder of the dev elopment, and so we will dene this set dir ectly . W e say that an environment Γ ′ ext ends an environment Γ , a nd write Γ ′ ≥ Γ if Γ ′ extends Γ a s a partial function from identiers to value types. Given two well-typed computations Θ 0 ; Γ 0 ⊢ E 0 M 1 : C 0 and Θ 0 ; Γ 0 ⊢ E 0 M 2 : C 0 , let Ξ [ Θ 0 ; Γ 0 ⊢ E 0 M 1 , M 2 : C 0 ] b e the smallest set of tuples h Θ ′ , Γ ′ , V 1 , V 2 , A i and h Θ ′ , Γ ′ , E ′ , N 1 , N 2 , C i that is co m pati- ble with the typing rules and contains all the tuples h Θ , Γ , E 0 , M 1 , M 2 , C 0 i , where Θ ⊇ Θ 0 and Γ ≥ Γ 0 . The tuples h Θ ′ , Γ ′ , V 1 , V 2 , A i and h Θ ′ , Γ ′ , E ′ , N 1 , N 2 , C i represent Θ ′ ; Γ ′ ⊢ V 1 , V 2 : A and Θ ′ ; Γ ′ ⊢ E ′ N 1 , N 2 : C , respectively . The compatibility wit h the rules means, for e xample, that if h Θ ′ , Γ ′ , V 1 , V 2 , A i is in Ξ [ Θ 0 ; Γ 0 ⊢ E 0 M 1 , M 2 : C 0 ] , then so is h Θ ′ , Γ ′ , ∅ , return V 1 , return V 2 , F A i . If we do dene program c ontexts X [ ] , w e ca n then show that this set consists of all the pairs of c ontexts plugged with M 1 and M 2 , i.e., tuples such as h Θ , Γ , E 0 , X [ M 1 ] , X [ M 2 ] , Y i where X [ ] is a c ontext of type Y whose hole expe cts type X . Dene the set Ξ [ Θ 0 ; Γ 0 ⊢ V 1 , V 2 : A ] for contexts plugge d with values analogously . For uniformity’s sake, we let types X range over both value and E -com putation ty p es, a nd phrases P range ov er both value and computation terms. Judgem ents of the form Θ ; Γ ⊢ E P : X are m eta-judgements, ranging ov er value judgements Θ ; Γ ⊢ P : X and E -c omputation j udgement Θ ; Γ ⊢ E P : X . Let Θ ; Γ ⊢ E P , Q : X b e two mam phrases. W e say that P and Q are context ually equivalent and write Θ ; E ⊢ Γ P ≃ Q : X when, for all pairs of plugg ed closed ground-returner p ure contexts ∅ , ∅ , ∅ , M P , M Q , F G in Ξ [ Θ ; Γ ⊢ E P , Q : X ] and for all closed ground value terms ; ⊢ V : G , we have: M P { ∗ return V ⇐ ⇒ M Q { ∗ return V mam has a straightforward set-theoretic denotational semantics. Presenting the semantics for the co re calcu- lus will simplify our later presentation. T o do so, we rst recall the following established fac ts about m onads, specialised and concretised to the set -theo retic setting. A monad is a triple h T , return , ≫ = i where T assigns to eac h set X a set T X , return assigns to each set X a function return X : X → T X and ≫ = assigns to each func tion f : X → T Y a function ≫ = f : T X → T Y , and moreover these assignments satisfy well-known algebraic identi ties. Given a monad h T , return , ≫ = i we dene for every function f : X → Y the f unc torial action fmap f : T X → T Y as fmap f x s ≔ x s ≫ = ( return ◦ f ) . A T -algebr a for a monad h T , return , ≫ = i is a pair C = h | C | , c C i where | C | is a set and c C : T | C | → | C | is a function satisfying c ( return x ) = x , and c ( fmap c x s ) = c ( x s ≫ = id ) for all x ∈ | C | and x s ∈ T 2 | C | . Th e set | C | is called the carrier and w e call c the algebra structure . For each set X , the pair F X ≔ h T X , ≫ = id i forms a T -algebra c alled the free T -algebra o ver X . W e parameterise mam ’s semantics function Θ ⊢ k E : E b y an assignment θ of sets θ ( α ) to each o f the type variables α in Θ . Given such a typ e variable assignment θ , we assign to eac h • eect: a monad Θ ⊢ k E : E θ , denoted by T ⟦ E ⟧ θ , return ⟦ E ⟧ θ , ≫ = ⟦ E ⟧ θ ; • value type: a set Θ ⊢ k A : V al θ ; • E -computation type: a T ⟦ E ⟧ θ -algebra Θ ⊢ k C : Comp E θ ; and P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expressive Power of User-Define d Eects • 39:9 Eects ∅ θ ≔ h Id , id , id i V alue typ es α θ ≔ θ ( α ) 1 θ ≔ { ⋆ } A 1 × A 2 θ ≔ A 1 θ × A 2 θ U E C θ ≔ C θ { i nj ℓ 1 A 1 | . . . | inj ℓ n A n } θ ≔ ( { ℓ 1 } × A 1 θ ) ∪ · · · ∪ ( { ℓ n } × A n θ ) Computation types F A θ ≔ F A θ A → C θ ≔ h C θ ⟦ A ⟧ θ , λ f s . λx . c ( fmap ( λ f . f ( x )) f s )i C 1 & C 2 θ ≔ C 1 θ × C 2 θ , λc s . h c 1 ( fmap π 1 c s ) , c 2 ( fmap π 2 c s )i Fig. 7. mam denotational semantics for types V alue terms x θ ( γ ) ≔ π x ( γ ) ( V 1 , V 2 ) θ ( γ ) ≔ V 1 θ ( γ ) , V 2 θ ( γ ) () θ ( γ ) ≔ ⋆ inj ℓ V θ ( γ ) ≔ ℓ , V θ ( γ ) { M } θ ( γ ) ≔ M θ ( γ ) Computation te r ms case V of ( x 1 , x 2 ) → M θ ( γ ) ≔ M θ ( γ [ x 1 7→ a 1 , x 2 7→ a 2 ]) where V θ ( γ ) = h a 1 , a 2 i case V of { inj ℓ 1 x 1 → M 1 · · · inj ℓ n x n → M n } θ ≔ M i θ ( γ [ x i 7→ a i ]) where V θ ( γ ) = h ℓ i , a i i V ! θ ( γ ) ≔ V θ ( γ ) return V θ ( γ ) ≔ ret urn ( V θ ( γ )) x ← M ; N θ ( γ ) ≔ M θ ( γ ) ≫ = λa . N θ ( γ [ x 7→ a ]) λ x . M θ ( γ ) ≔ λa . M θ ( γ [ x 7→ a ]) M V θ ( γ ) ≔ ( M θ ( γ ))( V θ ( γ )) h M 1 , M 2 i θ ( γ ) ≔ M 1 θ ( γ ) , M 2 θ ( γ ) prj i M θ ( γ ) ≔ π i ( M θ ( γ )) Fig. 8. mam denotational semantics for terms • context: the set Θ ⊢ k Γ : Ctx t θ ≔ Î x ∈ Dom ( Γ ) Γ ( x ) θ . Fig. 7 denes the standard set-theoretic semantics function over the structur e of typ es. T he pure ee ct denotes the identity monad, which sends each set to itself, and extends a function by doi ng nothing. The extended languages in the following sec tions will assign mo re sophisticated m onads to other eec ts. The semantics of type variables uses the typ e assignment given as param eter . The unit type always denotes the singleton set. Product typ es and variants denote th e corresponding set-theoretic operations of cartesian product and dis joint union, and thus th e empty variant type 0 ≔ { } denotes the empty set. The type of thunked E -computations of type C denotes the carrier of the T ⟦ E ⟧ θ -algebra C θ . The E -computation type of A r eturners denotes the free E θ -algebra. Function and product typ es denote well-known algebra structure s ove r the sets of functions and pairs, respectively ( B arr and W ells 1985, Theorem 4.2). T erms can have multiple typ es, for example the function λ x . return x has the types 1 → 1 and 0 → 0, and type judgements can have multiple typ e derivations. W e thus give a Church-style semantics (Reynolds 2009) by dening the semantic function for typ e judgement derivations r a th er than for terms. T o increase readability , we write P instead of including the entire typing derivation for P . The semantic function for terms is par a m eterised by an assignment θ o f sets to type variables. It assigns to each well-typed deriv ation for a: • value term: a function Θ ; Γ ⊢ V : A θ : Γ θ → A θ ; and • E -computation term: a func tion Θ ; Γ ⊢ E M : C θ : Γ θ → C θ . Fig. 8 denes the standard set-theoretic semantics ov er the structure of derivations. The semantics of sequenc- ing uses the Kleisli extension function (≫ = f ) : T X → | C | f o r functions into non-free algebras f : X → | C | , given by ( ≫ = f ) ≔ c ◦ return ◦ f . Theorem 2.8 (mam compositionality). The meaning of a term dep ends only on the meaning of its su b-terms: for all pairs of well-type d plugge d mam conte xts M P , M Q in Ξ [ Θ ; Γ ⊢ E P , Q : X ] , if P = Q then M P = M Q . The proof is a straightfo r ward induction on the set of plugged contexts. P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:10 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar M , N :: = . . . computations | op V operation call | ha ndle M with H handling construct H :: = handlers { return x 7→ M } return clause | H ⊎ { op p k 7→ N } operation clause Frames and contexts · · · F :: = . . . | handle [ ] with H computation frame Beta reduct ion ( ret ) handle ( return V ) with H { β H return [ V / x ] ( op ) h andle H [ op V ] with H { β H op [ V / p , { λ x . hand le H [ return x ] with H } / k ] (a) Syntax extensions to Fig. 2 (b) Op erational semantics extensions to Fig. 3 Fig. 9. eff T o phrase our simulation results in later development, we adopt a relaxed variant of simulation: let { cong be the smallest relati on containing { β that is closed under the term form ation constructs, and so contains { as well, and let ≃ cong be the smallest congruence relation c o ntaining { β . Theorem 2.9 (mam sou ndness). Reduction preserves the semant ics: for every pair of well-typed mam terms Θ ; Γ ⊢ E P , Q : X , if P ≃ cong Q then P = Q . In p articular , for every w ell-typ e d closed t erm of ground typ e ; ⊢ ∅ P : F G , if P { ∗ return V then P = V . The proof is standard: check that { β preserves the semantics via calculation, and app eal to comp ositionality . Combining the The orem 2.6 (safety), Theorem 2.7 (termination), c ompositionality , and soundness, w e h ave: Theorem 2.10 (mam adeqacy). Denotationa l equivalence implies contextua l equivalence: for all w ell-type d mam terms Θ ; Γ ⊢ E P , Q : X , if P = Q then P ≃ Q . As a consequence, we deduce that our operational semantics is very well-behaved: for all w ell-type d compu- tations Θ ; Γ ⊢ E M , M ′ : C , if M { cong M ′ then M ≃ M ′ . In the f o llowing sections, we will extend the mam calculus using the following convention. W e use an ellipsis to mean that a ne w denition consists of the old denition verbatim with the new description appended, as in the following: M , N :: = · · · | op V eect operation 3 EFFECT HANDLER S: E FF Bauer and P retnar (201 5) propo se algebr a ic ee cts and handlers as a basis f or modular programming with user- dened eects. Programm able eect handlers arose as part of P l o tkin and Pow er’s computational eects (2 002), which inv estigates the consequences of usi ng the additional structure in algebraic pres entations of monadic models of eects. This ac count renes Mogg i’s m onadic acc ount (1989) by incorporating into the the ory the syntactic constructs that generate eects as algebraic operations for a mona d ( Plotkin and Power 2003): eac h monad is ac companied by a collection of syntactic operations, whose interaction is spec ied b y a collection of e quations, i.e ., an algebraic theory , which fully determines the monad. T o t e xception handlers into this account, Plo tk in and Pretnar (2009) generalise to the handling of arbitrary alg ebraic eects, giving a computa- tional interpr etation to algebras for a mo nad. By allowing th e user to declare op erations, the user can describe new eects in a comp osable manner . By dening alg ebras for th e free monad with these operations, users give the abstract op erations dierent meanings similarly to Swie rstra’s use o f free monads (2008). Fig. 9 ( a) presents the e xtension eff , Kammar et al. ’s core calculus of eec t h andlers ( Kammar et al. 2013). W e assume a countable set of elements of a separate syntactic class, ranged over by op . W e call these operation names . For eac h op eration name op , e ff ’s op eration call construct allows the programmer to invoke the eect ass o ciated P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:11 t oд д l e = { x ← get () ; y ← n ot ! x ; put y ; x } H S T = { return x 7→ λs . return x get _ k 7→ λs . k ! s s put s ′ k 7→ λ _ . k ! () s ′ } r u nS t at e = { λc . ha ndle c ! with H S T } S t at e = { get : 1 → bit , put : bit → 1 } : E t oд д l e : U S t a t e F bit H S T : bit S t a t e ⇒ ∅ bit → F bit r u nS t at e : U ∅ (( U S t a t e F bit ) → bit → F bit ) Fig. 10. User -defined bo olean state in e ff with o p by passing it a value as an argument. Op eration nam es are the only interface to eects the language has. T he h andling construct allows the pr ogram mer to use a handler to int erpret the operation calls of a given returner computation. As the giv en computation may call thunks r eturned by functions, the dec ision which handler will handle a given operation call is dynamic. Handlers are specied by two k inds of clauses. A return clause describ es how to procee d when returning a value. An op eration clause describes how to proce ed when invoking an operation o p . The body of an operation clause c an acc ess the value passed in the op eration call usi ng the rst b ound variable p , which is similar to the b ounding occurrence of an e xception variable when handling exceptions. But unlike e xceptions, we expec t arbitrary eects like reading from or writing to memory to resume . Therefore th e bo dy of an operation clause can also access the continuation at the op eration’s calling point. Even though w e use a list notation in this pres entation of the syntax, the abstract syntax tr ee repr esentation of a handler H is in fact a pair H = H return , H − consisting of a single return cl a use H return , and a function H − from a nite subset of the operation names assigning to eac h operation nam e o p its assoc iate d operation clause H op . Example 3.1. T he two left columns o f Fig. 10 demo nstrate how to add user-dened boolean state in e ff . T he handler H S T is parameteri sed by the current state . When the c o mputation terminates, we discard this state. When the program calls get , the handler returns the current state and leaves it unc hanged. When the program calls put , the handler returns the unit value, and instates the newly given state. Fig. 9(b) presents eff ’s extension to mam ’s operational semantics. Com putation frames F now include the handling co nstruct, while the basic frames B do not, allowing a handled computation to β -reduce under the handler . W e add two β -reduction cases. When the returner computation ins ide a handler is fully evaluated, the r eturn clause procee ds with the return value. When the returner computation inside a handler needs to evaluate an operation call, the denition of hoisting conte xts H ensur es H is precisely the continuation of the operation c all delimited by the handler . Put dierently , it ensures that the h andler in the root of the reduct is the closest ha ndl er to the operation call in the call stack. T he o peration clause corresponding to the operation called then pr o c eeds with the sup plied parameter and curr ent c ontinuation. Rewrapping the handler around this continuati on ensures that all op eration ca l l s inv oked in the continuation are handled in the same way . An alternative (Kammar et al. 20 13; Kiselyo v et al. 2013; Lindley et al. 2 017) is to dene ins tead: handle H [ op V ] with H { β N [ V / p , { λ x . H [ return x ] } / k ] This variant is known as sha llow h andlers, as opposed to the d eep handlers of Fig. 9(b). W e focus on de ep handlers as they are c l o ser to monadic reec tion and have a clean denotational semantics. Example 3.2. With this semantics, the user-dened state from Fig. 10 b ehaves as expec ted: r u nS t at e ! t oдд l e Tru e { ∗ ( handle True with H S T ) Fals e { ∗ True More g enerally , the handler H S T expres ses dynamically scop ed state (Kammar and Pretnar 2 017). For additional handlers for state and other eects, see Pr etnar’s ( 2015) tutorial. Fig. 11 presents eff ’s extension to the k ind and type system. Th e eect anno tations in e ff are functions from nite sets of operation names, assigning to eac h op eration nam e its param eter typ e A and its return type B . W e add a new kind f o r handler types, which descr ib e the kind and th e returner type the handler can ha ndle, and the kind and computation typ e the handl ing c lause will have. P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:12 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar Kinds and t ypes E :: = . . . eects | { op : A → B } ⊎ E arity assignment K :: = . . . kinds | Hndlr handlers R :: = A E ⇒ E ′ C handler typ es Computation typing · · · ( op : A → B ) ∈ E Θ ; Γ ⊢ V : A Θ ; Γ ⊢ E op V : F B Θ ; Γ ⊢ E M : F A Θ ; Γ ⊢ H : A E ⇒ E ′ C Θ ; Γ ⊢ E ′ handle M with H : C Eect kinding · · · Θ ⊢ k A : V al Θ ⊢ k B : V a l op < E Θ ⊢ k E : E Θ ⊢ k { o p : A → B } ⊎ E : E Handler kinding Θ ⊢ k R : Hndlr Θ ⊢ k A : V al Θ ⊢ k E , E ′ : E Θ ⊢ k C : Comp E ′ Θ ⊢ k A E ⇒ E ′ C : Hndlr Handler typing Θ ; Γ ⊢ H : R ( Θ ⊢ k Γ : Ctxt , R : Hndlr ) Θ ; Γ , x : A ⊢ E M : C for all 1 ≤ i ≤ n : Θ ; Γ , p : A i , k : U E ( B i → C ) ⊢ E N i : C Θ ; Γ ⊢ { return x 7→ M } ⊎ op i p k 7→ N i 1 ≤ i ≤ n : A { op i : A i → B i | 1 ≤ i ≤ n } ⇒ E C Fig. 11. eff ’s k inding and typing (e xtending F ig. 5 and 6) In the kinding judgement for eects, the types in each operation’s arity assignment must be value types. The kinding judgement for handlers requires all the types and eects involved to be well-kinded. Computation type judgements now include two additional rules fo r each ne w com putation construct. An op eration call is well- typed wh en the parameter and return typ e agree with the arity assignment in the eect annotation. A use of the handling construct is well-typed when the type and eect of the handled c omputation and the type-and-eect of the c onstruct agree with the types and eects in the handler type. The set of h a ndl e d operations must strictly agree with the set of op era tions in the eect annotation. The variable bound to the return value has the returner type in the handler type. In each op eration clause, the b ound parameter variable h a s the parameter type from the arity assignment for this operation, and the continuation variable ’s input type matches the return type in the operation’s arity assignment. The ov erall type of all operation c lauses agrees wit h the computation typ e of the handler . The second eect annotation on the handler type matches the eect annotations on th e continuation and the b ody of the op eration and return cla uses, in accordance with the de ep handler semantics. Example 3.3. The typ e system assigns th e boolean state terms the typ es given in Fig. 1 0. eff ’s design involves se veral decisions. First, handlers h ave their own kind, unlike Pretnar’s calculus in wh ich they are values (Pretnar 2015). This distinction is minor , as handlers as values c an b e express ed by thunking the handling construct, c f . H S T and r u nS t at e a b ove . Next, the eect annotations involv ed in the handling c onstruct have to agree precisely . Another option is to check inclusion of operation set s, i.e ., a handler may handle more eects than the annotation on the eect. This distinction is minor , as we c an expr ess co ercions f rom an ee ct annotation into a superset o f eects using a tri vial handler: { λ x . return x } ⊎ { op p k 7→ k ( o p p ) | op ∈ E } : A E ⇒ E ⊎ E ′ F A A more signicant cho ice is to use closed handlers: e xecution halts/crashes when a handled computation calls an o p eration the handler does not handle. Th e other o ption is to use forwarding handlers (Kammar et al . 2013), in which unhandle d operation calls are fo r warded to the nearest enclo sing ha ndl er t hat can h andle them. In our simple typ e-and-eect system, this decision has no immediate i mpact, as w e can us e the trivi al handler above to re-raise unhandle d eects whenever ne eded. Howev er , in more expres sive type systems, which we do P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:13 not c onsider here, in particular type systems with eect polymorp hism ( Lucassen and Giord 1 988; Leijen 201 7; Kiselyo v et al. 2013 ; ? ), this distinction is more si gnicant. In this case , we b eliev e that the language should include both vari ants: the forwarding variant to support co de extensibility and mo dularity , and the clo sed variant to allow the programmer to guarante e that a computation cannot cause unhandled ee cts, or a m echanism for ascribing eect anno tations to ensure all eects have be en handled. Finally , it is possible to remove the eect system. I n t hat case, the arity assignments for the op erations need to be p lace d globally at the top lev el of the program , as in P retnar’s tutorial (Pretnar 201 5). Removing the eect system has dramatic consequences o n expres sivity: as we are about to see, well-typed eff terms always terminate. I f we r emove the eect annotations, we can enco de a form of Landin’s knot (Landin 1964), making the c alculus non-terminating. eff ’s meta-theo retic development f o llows mam ’s deve lopment closely , with an Abella formalisation of safety: Theorem 3 .4 (eff safety). W ell-typed programs don’t go wrong: for all closed eff returners Θ ; ⊢ ∅ M : F A , either M { N for some Θ ; ⊢ ∅ N : F A or else M = return V for some Θ ; ⊢ V : A . Using the monadic lifting from Kammar’s thesis (20 14), we obtain terminati on for e ff (Kammar et al. 20 13): Theorem 3 .5 ( eff t e rmina tion) . There ar e no innite red uction seq uences: for all eff terms ; ⊢ ∅ M : F A , we have M 6 { ∞ , and there exists some unique ; ⊢ V : A such that M { ⋆ return V . eff shares mam ’s ground types, and we dene plugged c ontexts and the equivalences ≃ and ≃ cong as in mam . W e give an adequate set-theoretic denotati onal semantics for eff . First, recall the f o llowing well established concepts in univers al and catego r ica l al g ebra. A signatu re Σ is a pair consist ing of a set | Σ | wh o se elements we call op eration symb ols , and a function ar i t y Σ from | Σ | as signing to each op eration symb ol f ∈ | Σ | a (po ssibly innite) set ar i t y ( f ) . W e write ( f : A ) ∈ Σ when f ∈ | Σ | and ar i t y Σ ( f ) = A . G iven a signature Σ and a set X , we inductively form the set T Σ X of Σ -terms ove r X by: t :: = x | f h t a i a ∈ A ( x ∈ X , ( f : A ) ∈ Σ ) The assignment T Σ together with the fo l l owing assignments f orm a monad return x ≔ x t ≫ = f ≔ t [ f ( x )/ x ] x ∈ X ( f : X → T Σ Y ) The T Σ -algebras h C , c i are in bijective corres pondenc e with Σ -algebr as on the s ame carrier . These ar e pairs h C , − i where − assigns to each ( f : A ) ∈ Σ a f unction − : C A → C f rom A -ary tuples of C elements to C . The bijection is given by setting f h ξ a i a ∈ A to be c ( f h ξ a i a ∈ A ) . eff ’s denotational semanti cs is g iven by e xtending mam ’s semantics as f o llows. Giv en a type variable assign- ment θ , we assign to each · · · • handler type: a pair Θ ⊢ k X : Hndlr = h C , f i c onsisting of an algebra C and a function f into the | C | carrier of this algebra. Fig. 1 2 presents how e ff extends mam ’s denotational semantics. Each eect E gives rise to a signature whose operation symb o ls are the operation names in E tagg ed by an element of the denotation of the co rresponding parameter type. This signature g ives rise to the mona d E denotes. When E = ∅ , the induced signature is em pty , and gives rise to the identity monad, and so this semantic function extends mam ’s semantics. Handlers handling E -computations r eturning A -values using E ′ -computations of type C denote a pair . Its rs t component is an E θ -algebra structure ov er the c arrier C θ , which may have nothing to do with the E ′ θ -algebra structur e C θ already possesses. The se cond comp onent is a function from A θ to the c arrier C θ . The denotati on of an operation call to op makes use of the fact that the e ec t annotation E contains the operation nam e op . Conse quently , the resulting signature contains an operation symbol op q for every q ∈ A θ . The denotation of op is then the term op q h a i a ∈ ⟦ B ⟧ θ . The denotation of the handling construct uses the Kleisli extension o f th e second component in the denotation of the h andler . The denotation of a handler term denes P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:14 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar Eects E θ ≔ T n op p : ⟦ A ⟧ θ ( op : A → B ) ∈ E , p ∈ ⟦ A ⟧ θ o Handler type s A E ⇒ E ′ C ≔ { E -algebras with carrier | C | } × | C | ⟦ A ⟧ Computation terms · · · op V θ ( γ ) ≔ op ⟦ V ⟧ θ γ h return a i a ∈ ⟦ B ⟧ θ handle M with H θ ( γ ) ≔ M θ ( γ ) ≫ = f where H ( γ ) = h D , f : A → | C | i Handler ter ms { return x 7→ M } ⊎ { op p k 7→ N op } op θ ( γ ) ≔ h D , f i where D ’s algebra structure and f given by : op q D h ξ a i a ≔ N op θ ( γ [ q / p , h ξ a i a / k ] f ( a ) ≔ M θ ( γ [ a / x ]) Fig. 12. eff denotational s emantics (extending Fig. 7 and 8) the T Σ -algebras by dening a Σ -algebra for the associated signature Σ . The operation clause for o p allows us to interpret eac h of the operation symbols associated to op . Th e denotation of the return clause gives the second compo nent of the handler . Theorem 3.6 (eff compositionality ) . The meaning of a term dep end s only on the meaning of its sub-terms: for all pairs of well-type d plugge d eff conte xts M P , M Q in Ξ [ Θ ; Γ ⊢ E P , Q : X ] , if P = Q then M P = M Q . The proof is identical to mam , with two more cases for { β . Similarly , w e have: Theorem 3 .7 (eff soundness). Reduction preserves the semantics: for e very pair of well-typed eff te rms Θ ; Γ ⊢ E P , Q : X , if P ≃ cong Q then P = Q . In particular , for every well-type d closed term of ground typ e ; ⊢ ∅ P : F G , if P { ∗ return V then P = V . W e combine the pr evious results, as with mam : Theorem 3 .8 ( eff adeqacy). Denotational equivalence implies conte xtual e q uivalence: for all we ll-typ ed eff terms Θ ; Γ ⊢ E P , Q : X , if P = Q then P ≃ Q . Therefore, e ff also has a w ell-b eh aved operational semantics: for all well-type d computations Θ ; Γ ⊢ E M , M ′ : C , if M { cong M ′ then M ≃ M ′ . 4 MONADIC REFLECTION: MON Languages that use monads as an abstraction for user-dened eects employ other mech anisms to support them, usually an overloading resolution mechanism, such as typ e-classes in Haskell and Coq , and functors/implicits in OCaml. As a c onsequence, such accounts for m o nads do not study them a s an abstraction in their own right, and are intertwined wit h implementation details and c o ncepts stemming from the added mec h anism. Fi linski’s work on monadic reection (Filinski 1994, 199 6, 19 99, 2010) serves precisely this purpose: a cal c ulus in which user-dened mo nads stand independently . Fig. 13(a) p resents m on ’s syntax. T he where { return x = N u ; y ≫ = f = N b } construct binds x in the term N u and y and f in N b . The term N u describes the unit and the term N b describes the Klei sli e xtension/bind operation. W e elabo rate on th e choice of the ke y word where when w e describe mon ’s type system. Using monads, the programm er can write programs as if the new ee ct was native to the l a ng uage. W e c all the mo de of programming when the eect appears native the opaque vie w of the eect. In contrast, the transparent mode occ urs when the co de can ac cess the implementation of the eect directly in terms of its dened monad. The reect construct µ ( N ) allows the programmer to graft co de executing in transpar ent mode into a blo ck of c ode executing in opaque mode. The re ify construct [ N ] T turns a block of opaq ue code int o the result obtained by the implementation of the ee ct. Example 4.1. Fig. 14 demonstrates h ow to add us er-dened boolean state in mon using the standard S t at e monad. T o expr ess g e t and p ut , we reec t the c oncrete de nition of the corresponding operations of the state P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:15 monad. T o run a computation, we use reication to get the monadic r epresentation of the computation as a state transformer , and apply it to the initial state. Fig. 13(b) describes the extension to the op erational semantics. The ret transition uses the user-dened monadic re turn to reify a value. T o explain the reection transi tion, note that the hoisting context H captures the continuation at the p oint of reection, with a n o paq ue view o f the ee ct T . The reecte d computation N views this eect transpar ently . By reifying H , we can use the user-dened monadic bind to gr aft the two together . Example 4.2. With this semantics we hav e r u nS t at e ! t oд д l e True { ⋆ return ( T rue , Fa lse ) . The example we have g iven here ts with the way in which monadic reection is of ten used, but is not as exible as the ee ct handler version bec ause g e t and p u t are concrete functions rather than abstract oper ations, which means we c annot abstract ov er how to interpr et them. T o write a version of toggle that can be interpr eted in dierent ways is p ossible using monadic reection but requir es more sophistication. W e shall see how to do so once w e have dened the translation of eff into mon . Fig. 15 presents the natural extension to mam ’s kind and typ e system for monadic reection. Eec ts are a stack of monads. The empty ee ct is the identity monad. A monad T can be layered on top o f an existing stack E : E ≺ instance monad ( α . C ) where { return x = M ; y ≫ = f = N } The intention is th a t the type constructor C [ −/ α ] has an asso ciated monad structure given by the bo dies of the return M and t he bind N , and can use eects from the res t of the stack E . T o be well-kinded, C must be an E -computation, and T must be a well-typed monad, i.e. , the return should hav e type C [ A / α ] when substit uted for some value V : A , and the bind should implement a Kleisli extensi on operation. Example 4.3. Fig. 14 demo nstrates a kind and type assignment to the user-de ned global state example. The choice o f keywords for m onads and their types is mo delled o n their syntax in Hask ell. W e stres s that our calculus do es not, how eve r , include a type-class me chanism. The type of a monad co ntains the return and bind terms , which means that we nee d to chec k for equality of terms during typ e-chec k ing, for example, to ensure T :: = monads where { return x = M ; return clause y ≫ = f = N } bind c lause M , N :: = . . . computations | µ ( N ) reect | [ N ] T reify Frames and contexts · · · F : : = B | [ [ ] ] T computation frames · · · Beta re d uction · · · for every T = where { λ x . N u ; λ y . λ f . N b } : ( ret ) [ return V ] T { β N u [ V / x ] ( reection ) [ H [ µ ( N ) ] ] T { β N b [ { N } / y , { ( λx . [ H [ return x ] ] T ) }/ f ] (a) Syntax extens ions to Fig. 2 (b) Op era tional semantics extensions to Fig. 3 Fig. 13. mon t oд д l e = { x ← g e t !; y ← n ot ! x ; p u t ! y ; x } g e t = { µ ( λs . ( s , s ) ) } p u t = { λs ′ . µ ( λ _ . (() , s ′ )) } S t at e = where { return x = λs . ( x , s ) ; f ≫ = k = λs . ( x , s ′ ) ← f s ; k ! x s ′ } r u nS t at e = { λc . [ c ! ] S t a t e } ∅ ≺ instance mona d ( α . bit → F ( α × bit )) S t at e : E t oд д l e : U S t a t e F bit r u nS t at e : U ∅ (( U S t a t e F bit ) → bit → F ( bit × b it )) g e t : U S t a t e F bit p u t : U S t a t e ( bit → F 1 ) Fig. 14. User -defined bo olean state in mon P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:16 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar that we are sequencing two computations wit h compatible eect annotation. For our purposes, α -equivalence suces. This nee d com es from our c hoice to use structural, anonymous, monads. In practice, monads are given nominally , and two monads are co mpatible if they have exactly the same nam e. It is f o r this reason also that the bo dies of the return and the bi nd operations must be closed, apart from their immediate arguments. If they were allow ed to co ntain op en terms, typ es in type contexts would contain these open terms through the eect annotations in thunks, requiring us to support dependently-typed contexts . T he monad abstraction is parametric, so naturally requires the use of type variables, and for this reason we include type variables in the base calculus mam . W e cho ose mona ds to b e structural and closed to ke ep them c loser to the other abstractions and to reduc e the additi onal lingual constructs involv ed. Our calculus deviates from Filinski’s (2 010) in the following ways. First, our eec t denitions are loc a l and structural, whereas his allow nominal dec laration of new eects only at the top le vel. Be cause we do not allow the bo dies of the return and the bind to contain open terms, this distinction b etween the two calculi is minor . As a conse q uence, eect deni tions in both calc uli are static , and the m onadic bindings can b e resolved at compile time. Filinski’s calculus also includes a sophisticated eect-basing mech anism, that allo ws a computation to immediately use , via reec tion, eects from any layer in the hierarchy below it, whereas our calculus only allows reecting eects from the layer immediately below . In the presence of Filinski’s type system, this deviation do es not signicantly change the expressi veness of the ca l c ulus: the monad stack is statically known, and, having access to the type information, we can insert multiple reection o perators and lift eects from lower levels into the current level. W e also prove mon ’s Felleisen- Wright soundness in our A b ella formalisation: Theorem 4.4 (mon safety). W ell-type d programs don’t go wrong: for all closed mon returners Θ ; ⊢ ∅ M : F A , either M { N for some Θ ; ⊢ ∅ N : F A or else M = return V for some Θ ; ⊢ V : A . As with eff , mon ’s ground typ es are the same as mam ’s. While we c an dene an obser vational equivalence relation in the same way as for mam a nd eff , w e will not do so. M onads as a programming abstraction have a w ell-known conc eptual c o mplication — us er-dened monads must ob ey the monad laws . These laws ar e a syntactic c ounterpart to the three equations in the denition of (set-theoretic/categorical) monads. T he diculty involv es dec iding what equality between such terms means. The natural candidate is obser vational equivalence, but as the contexts can themselves dene additional monads, it is not straightfo r ward to do so. Giving an Kinds and t ypes E : : = . . . eects | E ≺ instance mona d ( α . C ) T lay ered mona d Eect ki nd ing · · · Θ , α ⊢ k C : Comp E ⊢ m T : E ≺ in stance monad ( α . C ) T Θ ⊢ k E ≺ instance m onad ( α . C ) T : E Monad typing Θ ⊢ m T : E Θ , α ; x : α ⊢ E N u : C Θ , α , β ; y : U E C , f : U E ( α → C [ β / α ] ) ⊢ E N b : C [ β / α ] Θ ⊢ m where { return x = N u ; y ≫ = f = N b } : E ≺ instance m onad ( α . C ) where { return x = N u ; y ≫ = f = N b } Computation typing · · · Θ ; Γ ⊢ E N : C [ A / α ] Θ ; Γ ⊢ E ≺ instance m onad ( α . C ) T µ ( N ) : F A Θ ⊢ m T : E ≺ instance monad ( α . C ) T Θ ; Γ ⊢ E ≺ instance m onad ( α . C ) T N : F A Θ ; Γ ⊢ E [ N ] T : C [ A / α ] Fig. 15. mon ’s k inding and typing (e xtending Fig. 5 and 6) P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:17 Eects · · · E ≺ instance monad ( α . C ) N u N b θ ≔ h T , return , ≫ = i where T X ≔ C ( θ [ α 7→ X ]) return X ≔ N u ( θ [ α 7→ X ]) : X → T X ≫ = X , Y ≔ N b ( θ [ α 1 7→ X , α 2 7→ Y ]) : T X → ( X → T Y ) → T Y (provi ded these form a monad) Monads Θ ⊢ m T : E ≔ E Computation te r ms · · · [ N ] T ( γ ) ≔ N ( γ ) µ ( N ) ( γ ) ≔ N ( γ ) Fig. 16. mon denotational semantics (extending Fig. 7 and 8) acceptable op erational interpretati on to the monad laws is a n open problem. W e avoid the issue by g iving a partial denotational semanti cs to mon . Extend mam ’s denotational semantics to mon as fol l ows. Given a typ e variable assignment θ , we assign to each · · · • monad type and eect: a monad Θ ⊢ m T : E θ = Θ ⊢ k E : E θ , if the sub-derivati ons h ave w ell- dened denotations, and this data does indee d form a set-theoretic m onad. Consequently , the denotation of any derivati on is undened if at least one of its sub-derivations has undene d semantics. Moreover , the deni tion o f kinding judgement denotations no w dep end on term denotati on. Fig. 16 shows how mon extends mam ’s denotational semantics. The assigned typ e-constructor , and user- dened r eturn and bind, if well-dened, have the a ppropriate type to giv e the st ructure of a monad, and the se- mantics’s denition posits they do. For th e term semantics, recall that T ⟦ E ≺ instance monad ( α . C ) T ⟧ X = C ( θ [ α 7→ X ]) and th erefore, semantically , we can vie w any com putation of typ e and kind Θ ⊢ k F A : Comp E ≺ instance m onad ( α . C ) T as an E -com putation of type C [ A / α ] . W e dene a prop er d erivation to b e a derivation whose semantics is well-dened for all type variable assi gn- ments, and a p roper term or type to be a term or type that h as a proper deriv ation. Thus, a term is proper when all the syntactic m onads it contains denote semantic set-the oretic monads. When dealing with the type d fragment of mon , we restrict our attention to such proper terms as they reect the intended meaning of m onads. Doing so allows us to mirror the m eta-the ory of ma m and eff for proper terms. W e dene plugged prop er c ontexts as with ma m and eff with t he addi tional r eq uirement that all term s are proper . T he denitions of the equivalences ≃ and ≃ cong are then identical to tho se of mam and eff . Theorem 4.5 ( mon termina tion). There are no innite reduction se quences: for all proper mon terms ; ⊢ ∅ M : F A , we have M 6 { ∞ , and there exists some unique ; ⊢ V : A such that M { ⋆ return V . Our proof uses Lindley and Stark’s ⊤⊤ -lifting (2005). Theorem 4.6 (mon compositionality). The semantics depends only on the sema ntics of sub-terms: for all pairs of well-typed plugged proper mon cont exts M P , M Q in Ξ [ Θ ; Γ ⊢ E P , Q : X ] , if P = Q then M P = M Q . The proof is identical to mam , with two more cases for { β . Similarly , w e have: Theorem 4.7 (mon soundness). Re duction preserves the se mantics: for e very pair of well-typed proper mon terms Θ ; Γ ⊢ E P , Q : X , if P ≃ cong Q then P = Q . in p articular , for every w ell-type d closed term of ground type ; ⊢ ∅ P : F G , if P { ∗ return V then P = V . W e combine the pr evious results, as with mam and eff : Theorem 4.8 (mon adeqacy). Denotational equivalence implies contextua l equivalence: for all well-type d proper mon terms Θ ; Γ ⊢ E P , Q : X , if P = Q then P ≃ Q . Therefore, the proper fragm ent of mon also has a well-behaved operational semantics: for a l l well-typed proper computations Θ ; Γ ⊢ E M , M ′ : C , if M { cong M ′ then M ≃ M ′ . P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:18 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar M , N :: = . . . com putations | S 0 k . M shift-0 | h M | x . N i reset Frames and contexts · · · F :: = . . . | h [ ] | x . N i computation fr ame Beta reduct ion · · · ( ret ) h( return V ) | x . M i { β M [ V / x ] ( capture ) hH [ S 0 k . M ] | x . N i { β M [ λy . hH [ return y ] | x . N i / k ] (a) Syntax extens ions to Fig. 2 ( b) Operational semantics e xtensions to Fig. 3 Fig. 17. del In contrast to e ff the semantics for mon is nite: Lemma 4.9 (finite denot a tion property). For e very type variable assignment θ = h X α i α ∈ Θ of nit e sets, ev ery proper mon value type Θ ⊢ k A : and computat ion type Θ ⊢ k C : denote nite sets A θ , C θ . 5 DELIMI TED CON TR OL: DEL Delimited c ontrol operators can implement alg orithms with sophisticated control structure, such as tree-fringe comparison, and other control mec hanisms, s uch as coroutines (Felleisen 1988) yet enjo y an improv ed meta- theor y in comparison to their undelimited c o unterparts (Felleisen et al. 1988). The operator closest in spirit to handlers, S 0 pronounced “shift zero ” , was introduced by Danvy a nd Filinski (Danvy and Fi linski 199 0) as part of a systematic stu dy of continuation-passing-style conv ersion. Fig. 1 7(a) presents th e extension del . The construct S 0 k . M , which we often call “shift” (as we nd “shift zero ” awkward), captures the current continuati on and binds it to k , and replaces it with M . The construct h M | x . N i , which we will c all “rese t” , delimits any continuations captured by shift inside M . Once M runs its co ur se and returns a value, this value i s bound to x and N executes. For delimited control cognoscenti this construct is known as “ dollar” , and it is capable of macro e xpressing the entire CPS hierar chy (Ma terzok and Biernack i 2012). Example 5.1. Fig. 18 demonstrates how t o add user-dened b oolean state in del (Danvy 2006). T he code assumes the environment outside the closes t reset wi ll apply it to the currently stored state. B y shi fting and abstracting over th is state , g e t and p ut can access this stat e and return the appropri ate result to the continuation. When running a stateful c omputation, we discard the state when we reach the nal re turn value. The extension to the operational semantics in Fig. 17(b) reects our informal description. The ret rule states that once the delimited c omputation returns a value, this value is substituted in the remainder of the reset com- putation. For the capture rule, the denition of hoisting contexts guar ante es that in the reduct hH [ S 0 k . M ] | x . N i there are no intervening resets in H , and as a consequenc e H is the delimited continuation of the evaluated shift. After the reduction tak es place, the continuation is re -wrappe d with the res et, while the body of the shift has access to the enclosing continuation. If we were to , instead, not re-wrap the continuation with a reset, we t oд д l e = { x ← g e t !; y ← n ot ! x ; p u t ! y ; x } g e t = { S 0 k . λs . k ! s s } p u t = { λs ′ . S 0 k . λ _ . k ! ( ) s ′ } r u nS t at e = { λc . h c ! | x . λs . x i } S t at e = ∅ , bit → F bit : E t oд д l e : U S t a t e F bit r u nS t at e : U ∅ (( U S t a t e F bit ) → bit → F bit ) g e t : U S t a t e F bit p u t : U S t a t e ( bit → F 1 ) Fig. 18. User -defined bo olean state in del P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:19 Kinds and t ypes E : : = . . . eects | E , C enclosing c ontinuation typ e Eect ki nd ing · · · Θ ⊢ k E : E Θ ⊢ k C : Comp E Θ ⊢ k E , C : E Computation typing . . . Θ ; Γ , k : U E ( A → C ) ⊢ E M : C Θ ; Γ ⊢ E , C S 0 k . M : F A Θ ; Γ ⊢ E , C M : F A Θ ; Γ , x : A ⊢ E N : C Θ ; Γ ⊢ E h M | x . N i : C Fig. 19. del ’s k inding and typing (e xtending F ig. 5 and 6) would obtain the co ntrol/prompt-zero operators, (cf. Shan’s (20 07) and Kiselyo v et al.’s (2005) analyses of mac ro expres sivity relations hips between these two, and other , variations on untyp ed delimited c ontrol). Example 5.2. With this semantics, we h ave: r u nS t at e ! t oд дl e True { ∗ h True | x . λs . x i Fal se { ∗ return Tr ue Fig. 19 pr esents the natural extension to mam ’s k ind and type system for delimit ed control. I t is based on Danvy and Fili nski’s description (Danvy and Filins ki 198 9); they wer e the rst to propose a type system for delimited control. Eects are now a stack of computation types, with the empty eect standing for the empty stack. The top of this stack is the return type of th e currently delimited continuation. Thus, as Fig. 19 presents, a shift pops the top-most typ e o this stack and uses it to typ e the current c ontinuation, and a reset pushes the type of th e delimited return type d onto it. Example 5.3. Fig. 18 demo nstrates a type assignment to the user-dened global state example. In this typ e system, t he return typ e of the c ontinuation remains xe d inside every reset. Ex isting work o n type systems for delimited control (Kiselyov and Shan (20 07) provide a substantial lis t of references) foc uses on type systems that allow answer-type modication , as these c an expres s typed printf and type-state computation (as in Asai’s analysis (2009)). W e exclude answer-type modication to keep the fundamental account clearer and simpler: the typ e system with answer-type m odication is further removed from the well-known abstractions for eect-handlers and monadic reection. W e conje cture that the relativ e expressiv eness of delimited control do es not chang e ev en with answer-type mo dication, once we add ana l o gous capabilities to eect h andlers (Brady 2013; Kiselyov 2016) and m onadic reection (Atkey 2009). Our Abella f ormalisation establishes: Theorem 5.4 (del safety ). W ell-typed programs don’t go wr ong: for all closed del returners Θ ; ⊢ ∅ M : F G , either M { N for some Θ ; ⊢ ∅ N : F G or else M = return V for some Θ ; ⊢ V : G . Using the translation from del to mon we pr esent in the next sec tion, del inheri ts some of mon ’s meta-theory . W e dene del ’s ground types, plug g ed contexts and the equivalences ≃ and ≃ cong as in mam . 6 MA CRO TR ANSLA TIONS Felleisen (1991) ar g ues that the usual noti ons of computabilit y and complexity reduction do not capture the expres siveness of general-purpose programming languages. The Ch urch- Turing thesis and its extensions assert that any r easonably e xpressiv e m odel of c o mputation can be eciently reduced to any other reas onably ex- pressi ve model of computation. Therefore the notion of a pol y nomial-time reduction with a T uring-machine is too crude to die rentiate express ive power of tw o general-purpose pr ogram m ing languages. A s an alternative , P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:20 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar Felleisen introduces macro translation : a local reduction of a lang uage extension, in the sense that it is homo- morphic with respec t to the syntactic c onstructs, and conse r vative , in the sense that it do es not chang e the c ore language. W e e xtend this c o nc ept to local translations b etween conservative extensions of a shared c ore. T ranslation notation. W e dene translations S → T from eac h source c alculus S to each targ et c alculus T . By default w e assume untyped translations, writing eff , mon , and del i n t ranslations that disregard typeability . In typeability preserving translati ons (which must also respe ct the monad laws wher e mon is concerne d) we explicitly write typed eff , typed mon , and ty ped del . W e allow translations to be hygienic and introduce f resh binding occurrences. W e write M 7→ M for the translation at hand. W e include only the non-core cases in the denition of each translati on. Out of the s ix possible untyped macro-translati ons, the ideas b ehind th e following fo ur al ready appear i n the l iterature: de l → mon (W adler 1994), mon → de l (Filinski 1994), del → eff (Bauer and Pretnar 2015), and eff → mon (Kammar et al. 2013). T h e Ab ella for m alisation contains the proofs of the simulation results for each of the six translations . Three translations form ally simulate the source calculus by the target calculus: mon → del , del → eff , and mon → eff . The other translations, de l → mon , eff → del , and eff → mon , introduce suspended redexes during reduction that invalidate simulati on on the nose. For the translations that introduce suspended redexes, we use a relaxed vari ant of simulation, namely th e relations { cong , wh ich are the smallest relations containing { that are closed under the term forma tion c on- structs. W e say that a translation M 7→ M is a simulation up to congruence if for every reduction M { N in the source calculus we have M { + cong N in the target calc ulus. In fact, the suspende d redexes alway s β -reduc e by substituti ng a variable, i.e., { λx . M } ! x { + cong λx . M , thus only performing simple rewi ring. 6.1 D elimited continuations as monadic ref lection ( del → mon ) W e adapt W adler’s anal y sis o f delimited control (W adler 1994), using th e continuation m o nad (Mogg i 198 9): Lemma 6.1. For all Θ ⊢ k E : E , Θ ⊢ k C : Comp E , we have the follo wing proper monad Cont : Θ ⊢ k E ≺ instance monad ( α . U E ( α → C ) → C ) where { return x = λc . c ! x ; m ≫ = f = λc . m ! { λ y . f ! y c } } : E Using Cont we dene the m ac ro translation del → mon as follows: S 0 k . M : = µ ( λk . M ) h M | x . N i : = [ M ] Cont { λ x . N } Shift is interpreted as reection and reset as reication in the continuation mo nad. Theorem 6.2 (del → mon correctness). mon simulates del up to congruence: M { N = ⇒ M { + cong N . The only suspended redex arises in simulating the reec tion rule, where we subs titute a continuation into the bind of the continuation monad yielding a term of the form { λy . { λy . M } y c } which we must reduce to { λy . M c } . del → mon extends to a ma c ro translation at the typ e level: E , C ≔ E ≺ instance mona d α . U E α → C → C Cont Theorem 6.3 ( del → mon preserves typeability ). Every well-typed del phrase Θ ; Γ ⊢ E P : X translates into a proper well-typed m on phrase: Θ ; Γ ⊢ E P : X . W e use this res ult to e xtend the meta-theory of del : Corollary 6.4 (del termina tion) . All well-typed closed ground returners in d el must reduce to a unique normal form: if ; ⊢ ∅ M : F G then there exists V such that ; ⊢ V : G and M { ⋆ return V . P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:21 6.2 M onadic ref lection as delimited continuations ( mon → del ) W e dene the macro translation mon → de l as follows: µ ( M ) : = S 0 k . λb . b ! ( { M } , { λ x . k ! x b }) [ M ] where { return x = N u ; y ≫ = f = N b } : = D M x . λb . N u E { λ ( y , f ) . N b } Reection is interpr eted by capturing th e current c ontinuation and abstracting o ver the bind operator which is then invoked with the reected co m putation and a f unction that wraps the continuation in order to ensure it uses the same bind op erator . Reication is interpre ted as an application o f a reset. The continuation of the reset contains the unit of the monad. W e apply this reset to the bind of the mo nad. Theorem 6.5 (mon → del correctness). del simulates mon up to congruence: M { N = ⇒ M { + cong N . This translation does not preserve typ eability be cause the bind op erator can b e used at dierent types. W e conjec ture th at a) any other m acro translation will suer from the same iss ue and b) adding (predicativ e) poly- morphism to the base calc ulus is sucient to adapt this translation to one that does preserve typ eability . Filinski’s translation from monadic reec tion to delimited continuations (19 94) does preserve typeability , but it is a global translation. I t is much like our translation except each instance o f bind is inlined ( hence it does not need to b e poly m orphic). 6.2.1 Alternative translation with nested de limited c ontinuations. An alternative to mon → del is to use two nested shifts f or reection and two nested res ets for reicati on: µ ( M ) : = S 0 k . S 0 b . b ! ( { M } , { λ x . h k ! x | z . b ! z i } ) [ M ] where { return x = N u ; y ≫ = f = N b } : = D D M x . S 0 b . N u E ( y , f ) . N b E In the translation of reection, the reset inside the wrapp ed continuation ensures that any further reections in the continuation are interpreted appropriately: the rst shift, which binds k , has p oppe d one continuation o the stack so we ne ed to add one back on. In the translation of reication, the shift guarding the unit garbage collec ts the bind once it is no longer nee ded. 6.3 D elimited continuations as eect handlers ( del → eff ) W e dene del → eff as foll ows: S 0 k . N ≔ shi0 { λ k . N } h M | x . N i ≔ ha ndle M with { return x 7→ N } ⊎ { shi0 y f 7→ f ! y } Shift is interpreted as an operation and reset is interpreted as a straightfor ward handler . Theorem 6.6 (del → eff correctne ss). eff simulates del on the nose: M { N = ⇒ M { + N . This translation do es not preserve typ eability be cause inside a single reset shifts can be used at dier ent types. W e c o njecture that a) any other mac ro translation will suer from the same issue and b) a dding polymo r ph ic operations ( Kammar et al . 2013) to e ff is sucient to ensur e this translation does preserve typ eability . One can adapt our translation to a globa l translation in wh ich every static instance of a shift is interpreted as a separate operation, th us avoiding the need for polymorphic operations. P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:22 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar 6.4 E ect handlers as delimited continuations ( eff → del ) W e dene eff → del as foll ows: op V ≔ S 0 k . λh . h ! ( inj op ( V , { λ y . k ! y h } ) ) h andle M with H ≔ M H ret { H ops } © « handle M with { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ® ® ¬ ret ≔ x . λh . N ret © « handle M with { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ® ® ¬ ops ≔ λ y . case y of { inj op 1 ( p 1 , k 1 ) → N 1 . . . inj op n ( p n , k n ) → N n } Operation invocation is interpreted by capturing the curr ent continuation and abst racting over a dispatcher which is passed an enco ding o f the op eration. The enco ded operation is an injection whose label is the name of the operation c ontaining a pair of the operation parameter and a wrappe d version of the captured continuation, which ensures the same dis patcher is threaded th rough the continuation. Handling is interpreted as an application of a reset whose continuation c ontains the return c l a use. The reset is applied to a dispatcher f unction th a t encodes the operation clauses. Theorem 6.7 (eff → del correctne ss). del simulates eff up to congr uence: M { N = ⇒ M { + cong N . The eff → del translation is simpler than Kammar et al. ’s which uses a global higher-order memor y cell storing the handler stack (Kammar et al. 2 013). This translation do es not preserve typeability b ecause the interpr etation of operations needs to b e p olymor- phic in the return type of the dispatcher over which it abstracts. W e conje cture that a) any other m acro transla- tion will suer from the same issue and b) adding polymorphism to the b a se calculus is sucient to adapt this translation to one that preserves typ eability . 6.4.1 Alternative translation with nested delimited c ontinuations. Si milarly to the mon → del translation there is an al ternative to eff → del which uses two nested shi fts fo r operations and two nested r esets fo r h andlers: op V ≔ S 0 k . S 0 h . h ! ( inj op ( V , { λ x . h k ! x | y . h ! y i } ) ) handle M with H ≔ M H ret H ops © « { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ¬ ret ≔ x . S 0 h . N ret © « { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ¬ ops ≔ y . ca se y of { inj op 1 ( p 1 , k 1 ) → N 1 . . . inj op n ( p n , k n ) → N n } 6.5 M onadic ref lection as eect ha ndlers ( mon → eff ) W e simulate reection with an op eration a nd reication with a handler . Formally , for every anony m ous monad T given by where { return x = N u ; y ≫ = f = N b } we dene mon → eff as f o llows: µ ( N ) ≔ r ef l e ct { N } [ M ] T ≔ handle M with T T ≔ { return x 7→ N u } ⊎ { refle ct y f 7→ N b } Reection is interpreted as a reect op eration and reication as a handler with the unit of the monad as a handler and the bind of the handler as the implementation of the reect op eration. Theorem 6.8 (mon → eff co rrectne ss). eff simulates mon on the nose: M { N = ⇒ M { + N . P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:23 mon → eff do es no t preserve typeability . For instance, consider the following com putation of type F bit using the environment monad Re ader given on the right: [ b ← µ ( { λ ( b , f ) . b } ) ; f ← µ ({ λ ( b , f ) . f } ) ; f ! b ] Reader ( inj true () , { λb . return b } ) ⊢ k ∅ ≺ i nstance m onad ( α . bit × U ∅ ( bit → F bit ) → F α ) where { return x = λe . return x ; m ≫ = f = λe . x ← m ! e ; f ! x e } : E Its translation into eff is not typeable: reection can appear at a ny type, whereas a single operation is monomor- phic. W e c onjecture that a) this observation can b e used to prov e th at no macro translation typed mon → typed e ff exists and that b) adding polymorphic op erations (Kammar et al. 2013) to eff is sucient for typing this trans- lation. 6.6 E ect handlers as monadic ref lecti on ( eff → mon ) W e dene eff → mon as fo l l ows: op V ≔ µ ( λk . λh . h ! ( in j op ( V , { λ y . k ! y h } ) ) ) hand le M with H ≔ [ M ] Cont { H ret } { H ops } © « handle M with { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ® ® ¬ ret ≔ λ x . λh . N ret © « handle M with { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ® ® ¬ ops ≔ λ y . case y of { inj op 1 ( p 1 , k 1 ) → N 1 . . . inj op n ( p n , k n ) → N n } The translation is muc h like eff → del , using the c ontinuation m onad in plac e of rs t class continuations. Operation invocation is interpreted by using reection to capture the current continuation and abstracting ov er a dispatcher which is passed a n enco ding of the operation. Th e encode d o p eration is an injec tion whose label is the name of the op eration containing a pair of the operation parameter and a wrapp e d version of the captured continuation, wh ich ensures the same dispatcher is threaded th rough the continuation. Handling is interpreted as a n application of a reied continuation monad computation to the return clause and a dispatcher function that enc odes the operation cl a uses. Theorem 6.9 (eff → mon co rrectne ss). mon simulates eff up to congruence: M { N = ⇒ M { + cong N . This translation does not preserve typ eability for the same reas on as the eff → del translations: the interpre- tation of operations nee ds to be polymorph ic in the return type of the dispatcher over which it abstracts. W e conjec ture that a) any other macro translation will suer from the same issue and b) adding polymorphism to the base calc ulus is sucient to adapt this translation to one that does preserve typeability . P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:24 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar 6.6.1 Alternative translation using a free monad. An alternative to interpreting ee ct handlers using a c ontin- uation monad is to use a free monad: op V ≔ µ ( return ( inj op ( V , λx . return x ) )) han dle M with H ≔ H ⋆ [ M ] H † © « { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ¬ † ≔ where { return x = return ( inj ret x ) ; y ≫ = k = case y of { inj ret x → k ! x inj op 1 ( p 1 , k 1 ) → return ( inj op ( p 1 , λ x . k 1 ! x ≫ = k ) ) . . . inj op n ( p n , k n ) → return ( inj op ( p n , λ x . k n ! x ≫ = k ) ) } } © « { return x 7→ N ret } ⊎ { op 1 p 1 k 1 7→ N 1 } ⊎ . . . ⊎ { op n p n k n 7→ N n } ª ® ® ® ¬ ⋆ ≔ h = λ y . case y of { inj ret x → N ret inj op 1 ( p 1 , k ) → k 1 ← return { λ x . y ← k ! x ; h ! y } ; N 1 . . . inj op n ( p n , k ) → k n ← return { λx . y ← k ! x ; h ! y } ; N n } Both the bind op eration for the free monad H † and th e function h that interpr ets the free monad H ⋆ are recursive. Given that we are in an unt yp e d set ting we can straightfor wardly implem ent the r ec ursion usi ng a s uitable variation of the Y combinator . Th is t ranslation does not e xtend to the t yp ed calculi as they do not supp o r t recursion. Nevert heless, we c onjecture that it can be adapted to a typ ed translation if we extend our base calculus to include inductive data types, as the r ec ursive functions are structurally recursiv e. 6.7 Nonexistence results Theorem 6.10. The following mac ro translations do not exist: • typed eff → typed mon satis fying: M { N = ⇒ M ≃ N . • typed eff → typed del satisfying: M { N = ⇒ M ≃ N . Our proof of the rst part h inges on the ni te denotation property (Lemm a 4.9). Briey , assume to the contrary that there was such a translation. Consider a single ee ct op eration symb ol tick : 1 → 1 and the terms: tick 0 ≔ return () tick n + 1 ≔ tick () ; ti ck n All these terms have the same type, and by the h omomorphic property o f the hyp othesised translation, thei r translations all have th e same ty p e. By the nite denotation property there are two observationally equivalent translations and by virtue of a m a c ro translation the two original terms are obser vationally eq uivalent in eff . But ev ery distinct pair of ti c k n terms is observationally dist inguishable using an appropriate ha ndl er . S ee Forster’s thesis (2 016) for th e full details. T h e second part fol l ows from Th eorem 6.3. Regarding the remaining four p o ssibilities, w e have seen that there is a typ eability-pres er ving macro transla- tion typed de l → typed mon (Theo rem 6.3) , but we conjecture that there are no typ eability-prese r ving transla- tions typed mon → typed d e l , typed del → typed e ff , or , typed mon → type d e ff . 7 CONCLUSION AND F URTHER W ORK W e have giv en a uniform fa m ily of form al calc uli expres sing the common abstractions for user-dened eects: eect handlers ( eff ), monadic reection ( mon ), and delimited control ( del ) together with their natural typ e-and- eect systems. W e have use d these calculi to formally analyse th e relati ve express ive p ower of the abstractions: monadic reection and delimit ed control have eq uivalent expressivity; b oth are equivalent in expressiv e power to eec t h andlers when types are not taken into co nsideration; and neither abstraction c an macro-express eect handlers and preserve typeability . W e hav e formalised the mor e s yntactic aspects of our work in the Abella proof assistant, and have used set-the oretic denotational semantics to establish inexpressiv ity results. P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:25 Further work ab ounds. W e want to extend each type system until each translation preserves typeability . W e conjec ture that adding po l y morphic operations to eff would allow it to macro expres s de l and mon , and that adding polym o r phism to mon and del would allow them to macro express eff . W e c onjecture polymorph ism would also allow del to macro express mon , and inductive data typ es with primitive recursion would also allow mon to ma c ro express eff . W e are also interest ed in analy sing global translations between these abstractions. In particular , while mon and del allow reection/shifts to appear any where inside a piece of c ode, in practice, library designers dene a xed set of primitives using reec tion/shifts and only expose those primitives to users. This observation suggests calculi in which eac h reify/reset is accompanied by declarations of this xed set of primitives. W e con- jecture that mon and del c a n be simulated on the nose via a global translation into the corresponding restri cte d calculus, and that the restricted calc uli can b e macro translated into eff while preserving typeability . Such two- stage translations would g ive a de ep er reason why so many examples typically used for monadic reection and delimited control can be directly recast using eect handlers. Other glo bal pre-pr ocessing may also eliminate administrativ e reductions from our translations and establish simulation on the nose . W e hop e the basic type systems we analysed will form a foundation for systematic further investigation, especially along the following extensions. Supporting answer-type modication (Asai 200 9; Kobori et al. 2015) can inform more express ive type system design for eec t handlers and monadic reection, and account for typ e- state (Atkey 2009) and session types (Kiselyov 2016). In practice, eect systems are extended with sub-eecting or eect polymo r ph ism (Bauer and Pre tnar 201 4; Pretnar 2014; Leijen 2017; Hillerström and Lindle y 2 016; ? ). T o these we add eect-forwarding ( Kammar et al. 2013) and rebasing (Filinski 201 0). W e have tak en the perspective of a program ming languag e designer deciding which programming abstrac tion to select for express ing user-dened eects. I n contrast, Schr ijvers et al. (2016) take the p erspec tive of a library designer for a specic programming language, Haskell, and compare the abstractions provided by libraries based on monads with those provided by eect handlers. They argue that both libraries conv erge on the same interface for user-dened eects via Haskell’s type-class me chanism. Relative express iveness r esults are subtle, and the potentially negative results that are hard to establish mak e them a risky line of research. W e view denotational m odels as provi ding a fruitful method for establishing such inexpr essivity results. It would be interes ting to co nnect our work with that of ?? Laird (201 7), who analyses the macro-expres siveness of a hierarchy of combinations of control operators and exceptions using game semantics, and in particular uses such denotational techniques to sho w certain combinations c annot macro express other combinations. W e w ould like to apply similar tec hniques to com pare the expressi ve power o f local eects such as ML -style reference cells with eect handlers. A CKNO WLEDGMEN TS Supported by the European Research Council grant ‘ events causality and symmetry — the next-generation se- mantics’ , and the Engineering and Phy sical Sciences R esearch Council grants EP/N0073 87/1 ‘quantum c o mput- ing as a programming language’ and EP/H005 633/1 ‘Semantic Foundations fo r Real- W orld Systems’ . The material is based upon w ork supporte d by the Air Force Oce of Scienti c Research, Air Force Materiel Command, USAF under A ward No. F A9550-1 4 -1-0096. W e thank Andrej Bauer , Paul Downen, M arcelo Fiore, M athieu Huot, Kay- van Memarian, Sean Moss, Alan Mycroft, Ian Orton, Hugo Paquet, Jean Pichon-Pharab od, Matthew Pickering, Reuben R owe , P hilip Saville, Sam Staton, and T amara von Gleh n for useful suggestions and discuss ions. REFERENCES Kenichi Asai. 2009. On typing d elimited continua tions: th ree new solutions to the printf problem. Higher-Order and Symbolic Computation 22, 3 (2009), 275–291. Robert Atkey . 2009. Parameterised notions of computation. J. Funct. Program. 19, 3-4 (2009), 335–376. P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. 39:26 • Y anni c k Forster, Ohad Kammar, Sam Li ndley, and M atija Pretnar M. Barr and C. W ell s. 1985. T oposes, triples, and theories . Springer- V er lag. Andrej Bauer and Matija Pretnar . 2 0 14. An Eect System for Algebraic Eects and Ha ndler s. Logical Methods in Computer Science 10, 4 (2014). Andrej Bauer and Matija Pretnar . 2015. Pro gramming with algebraic eects and handl ers. J . Log. Algebr. Meth. Program. 84, 1 (2015 ) , 108–123. Edwin Brady . 2013. Programming and reasoning with algebraic e e cts and depende nt types. In ICFP . A CM, 133–144. Lukas Bulwahn, Ale xander Krauss, Florian Haftmann, Le vent Erkök, and John Matthews. 2008. Imperative Functional Programming with Isabell e/HOL. In TPHOLs (Lecture Notes in Computer Science) , V ol. 5170. Springer , 134–149. Olivier Danvy . 2006. A n A nalytical Ap proach to Programs as Data Objects . DSc dissertation. Department of Com puter Science, Univ ersity of Aarhus. Olivier Danvy and And r zej Fil inski. 1989. A Functional Abstraction of Typed Co ntexts . Technical Re port 89/12. D IKU. Olivier Danvy and And r zej Fil inski. 1990. Abstracting Control. In LISP and Functional Pr ogramming . 151–160. Christian Doczkal. 2007. Str ong Normalization of CBPV . Technical Report. Saarland University. Christian Doczkal and Jan Schwinghammer . 2009. Formalizing a Strong Normalization Proof for Moggi’s Computational Metal anguage . In LFMTP . ACM, 57–63. Matthias Felleisen. 1988. The Theor y and Practice of First-Class Pr ompts. In POPL . ACM, 180–190. Matthias Felleisen. 1991. On the Expressive Power of Programming Languages. Sci. Comput. Program. 17, 1-3 ( 1991), 3 5 –75. Matthias Fel leisen and D aniel P. Friedman. 1987. A Reduction Semantics for Imperative Hig her-Order Languag es. In P ARLE (2) (Lecture Notes in Computer Science) , V ol. 2 59. Springer , 206–223. Matthias Felle isen, M itchell W and, Daniel P. Friedman, and Br uce F. D uba. 1988. Abstr act Continuations: A Mathematical Semantics for Handling Full Jumps. In LISP and Functional Programming . 52–62. Andrzej Filinski. 1994. Representing Monads. In POPL . ACM. Andrzej Filinski. 1996. Controlling eects . Ph.D. D issertation. School of Computer Science, Carnegie Mellon University , Pittsburgh, Pennsyl - vania. Andrzej Filinski. 1999. Representing Layered Monads. In POPL . A CM. Andrzej Filinski. 2010. Monads in Action. SIGPLAN Not. 45, 1 (Jan. 2010), 483–494. Y annick Forster . 2016. On the ex pressiv e power of eect handlers and monadic reection . Technical Report. U niversity of Cambridge. Andrew Ga cek. 2008. The Abella Interactive The orem Prover ( System Description). In IJCAR , V ol. 5195. Springer , 154–161. Andrew Gacek. 2009. A Framework for Specifyin g, Prototyping, and Reasoning abou t Computati onal Systems . Ph.D. D is sertation. U niversity of M innesota. Claudio Hermida. 1993. Fi brations, logical pr edicates and related topics . Ph. D. Dissertation. Univ ersity of Edinburgh. Daniel Hillerström and Sam Lindley . 2016. Lib erating eects with rows and handle rs. In T yDe@ICFP . A CM, 15–27. Graham Hutton and Erik Meijer . 1998. Monadic Parsing in Haskell. J. Funct. Program. 8, 4 (1998), 437–444. Ohad K ammar . 2014. A n Algebraic Theor y of Typ e-and-Eect Systems . P h.D. Disser tation. University of Edinburgh. Ohad K ammar , Sam Lindley , and Nicolas Our y . 2013. Handlers in ac tion. In ICFP . A CM, 145–158. Ohad K ammar and Gor don D. P lotkin. 2012. Algebraic foundations for eect-dependent optim isations. In POPL . A CM. Ohad K ammar and Matija Pretnar . 2017. No value restriction is needed for algeb raic ee cts and handler s. J. Funct. Program. 2 7 (2017 ) , e7. Shin-ya Ka tsumata. 2014. Parametric Eect Monads and Semantics of E ect Systems. SIGPLAN Not. 49, 1 (Jan. 2014), 633–645. Oleg Kiselyov . 2016. Parameterized extensible eects and session types (extende d abstract). In TyDe@ICFP . ACM, 41–42. Oleg Kiselyov , D aniel P. Friedman, and Amr A. Sabry . 2005. How to remov e a dynamic prompt: static and dynamic delimited continuat ion operators are equall y expressible . Technical Report. 16 pages. T e chnical Report TR611. Oleg Kiselyov , Amr Sabry , and Camero n Swords. 2013. Extensible e e cts: an alternative to monad transformers. I n H askell . ACM, 59–70. Oleg Kiselyov and Chung-chieh Shan. 2007. A Substructural T ype System for Delimited Continuations. In TLCA . 223–239. Oleg Kiselyov , Chung-chieh Shan, and Amr Sabr y . 2006. Delimited dynamic binding. In ICFP . ACM, 26–37. Ikuo Kobori, Yukiy oshi Kameya ma, and O l eg Kiselyov . 2 015. Answer- T ype Modication w ithout T ears: Prom pt-Passing Style Translation for T yped Del imited-Control Operators. In W oC 201 5 (EPTCS) , V ol. 212. 36–52. James Laird. 2002. Exceptions, Continuations and Macr o-expressiveness. I n ESOP . 133–146. James Laird. 2013. Combining and Relating Contr ol Eects and the ir Se mantics. I n COS . 113–129. J. Laird. 20 1 7. Combining control e e cts and th eir mo del s: Game semantic s for a hierarchy of static, dynamic and delimited control eects. A nnals of Pure and Ap plied Logic 16 8 , 2 (201 7 ), 470–500. Eighth Gam es for Logic and Programming Languages W orkshop (GaLoP). P. J. Landin. 1964. The Mechanical Evaluation of Expressions. Comput. J. 6, 4 (1964 ) , 308–320. Daan Leijen. 20 1 7. T ype directed co mpilation of row- typed algebr aic eects. In POPL . A CM, 486–499. Paul Blain Levy . 2004. Call-By-Push- V alu e: A Functional/Imperativ e Synthesis . Semantics Structures in Computation, V ol. 2. Springer . Sam Lindley , Conor McBride, and Craig McLaughlin. 2017. Do b e do be do. I n POPL . ACM, 5 0 0 –514. Sam Lindley and Ian Stark. 2005. Reducibility and ⊤⊤ -Lifting for Computatio n T ypes. In TLCA (Lecture Notes in Computer Science) , V ol. 3461. Springer , 262–277. P ACM Progr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017. On the Expr essive Power of User-Define d E ects • 39:27 John M. Luc assen and David K. Giord. 1988. Polymorphic E ect Systems. In POPL . A CM, 47–57. Marek Materzok and Dariusz Biernacki. 2012. A Dynamic Interpretation of the CPS Hierarchy . In APLAS (LNCS) , V ol. 7705. Springer , 296 – 311. Eugenio Moggi. 1989. Comp utational Lambda-Calculus and Monads. In LICS . IEEE C omputer Society , 14 –23. Gordon D . Plotkin and John Power . 2002. Notions of Com putation Determine Monads. In FoSSaCS . Springer- V erlag. Gordon D . Plotkin and John Power . 2003. Algebraic Operations and Generic Eects. A ppl. Categ. Structures 11, 1 (2003), 69 –94. Gordon D . Plotkin and Matija Pretnar . 2008. A Logic for Algebraic Eects. In LICS . IEEE Computer Soc iety , 118–129. Gordon D . Plotkin and Matija Pretnar . 2009. Handlers of Algebraic Eects. In ESOP . Springer-V e rlag. Matija Pretnar . 2014. Inferring Algebraic Eects. Log ical Methods in Computer Scie nce 10, 3 (2014). Matija Pretnar . 2015. An Introduction to Algebraic Eects and Ha ndle rs. Invited tutorial pap er . Electr . Notes Theor. Comput. Sci. 319 (2015), 19–35. John C. Reynolds. 2009. Theories of Pr ogramming Languages . Cambridge University Press. T om Schrijvers and others. 2016 . Monad transformers and modular alge b raic e ects . Technical Report. University of Leuven. T om Schrijvers, Guido T ack, Pieter Wuille, Horst Samulowitz, and Pe ter J. Stuckey . 2013. Search co mbinators. Constraints 18, 2 (2 013), 269–305. Chung-chieh Shan. 2007. A static simulation of dynamic delimited contr ol. Higher-Order and Symbol ic Computation 20, 4 (2007), 371–401. Ábel Sinkovics and Zoltán Porkoláb. 2013. Implementing monads for C++ template metaprogram s. Sci. Comput. Program. 78, 9 (2013), 1600–1621. J. Michael Spivey . 1990. A Functional Theor y of Exceptions. Sci. Comput. Program. 14, 1 (1990), 25–42. W outer Swierstra. 2008. Data types à la ca rte. J. Funct. Program. 18, 4 (2008), 423–436. William W Tait. 1967. Inte ns ional interpretations of functiona ls of nite typ e I. The journal of symb olic logic 32, 02 (1 9 6 7), 198–212. Philip W adler . 1990. Comprehending Monads. In LISP and Functi onal Programming . 61–78. Philip W adler . 1994. Monads and Comp osable Continuations. Lisp and Symbolic Computati on 7, 1 (1994), 39–56. Andrew K. W right and Matthias Fel l eisen. 1994. A Syntactic Appro ach to Ty pe Soundness. Inf. Comput. 115, 1 (1994), 38–94. Beta Ziliani, Derek Dreyer , Neelakantan R. Krishnaswami, Ale ksandar Nanevski, and Viktor V afeiadis. 2015. Mtac: A monad for typed tactic program ming in Coq. J. Funct. Program. 25 (2015). Received Febru ar y 201 7 P ACM Pr ogr . Lang., V ol. 1, No. 1, Article 39. Publication date: March 2017.
Original Paper
Loading high-quality paper...
Comments & Academic Discussion
Loading comments...
Leave a Comment