Programming Languages

A Typed Continuation-Passing Translation for Lexical Effect Handlers

by Philipp Schus­ter, Jonathan Im­manuel Brachthäuser, Mar­ius Müller, and Klaus Os­ter­mann

In Proc. Conf. Pro­gram­ming Lan­guage De­sign and Im­ple­men­ta­tion (PLDI), 2022.

This pub­li­ca­tion is re­lated to the Ef­fects re­search pro­ject.

Ab­stract

Ef­fect han­dlers are a lan­guage fea­tures which en­joys pop­u­lar­ity in acad­e­mia and is also gain­ing trac­tion in in­dus­try. Pro­grams use ab­stract ef­fect op­er­a­tions and han­dlers pro­vide mean­ing to them in a de­lim­ited scope. Each ef­fect op­er­a­tion is han­dled by the dy­nam­i­cally clos­est han­dler. Using an ef­fect op­er­a­tion out­side of a match­ing han­dler is mean­ing­less and re­sults in an error. A type-and-ef­fect sys­tem pre­vents such er­rors from hap­pen­ing. Lex­i­cal ef­fect han­dlers are a re­cent vari­ant of ef­fect han­dlers with a num­ber of at­trac­tive prop­er­ties. Just as with tra­di­tional ef­fect han­dlers, pro­grams use ef­fect op­er­a­tions and han­dlers give mean­ing to them. But un­like with tra­di­tional ef­fect han­dlers, the con­nec­tion be­tween ef­fect op­er­a­tions and their han­dler is lex­i­cal. Con­se­quently, they typ­i­cally have dif­fer­ent type-and-ef­fect sys­tems. The se­man­tics of lex­i­cal ef­fect han­dlers as well as their im­ple­men­ta­tions use multi-prompt de­lim­ited con­trol. They rely on the gen­er­a­tion of fresh la­bels at run­time, which as­so­ci­ate ef­fect op­er­a­tions with their han­dlers. This use of la­bels and multi-prompt de­lim­ited con­trol is the­o­ret­i­cally and prac­ti­cally un­sat­is­fac­tory. Our main re­sult is that typed lex­i­cal ef­fect han­dlers do not need the full power of multi-prompt de­lim­ited con­trol. We pre­sent the first CPS trans­la­tion for lex­i­cal ef­fect han­dlers to pure Sys­tem F. It pre­serves well-typed­ness and sim­u­lates the tra­di­tional op­er­a­tional se­man­tics. Im­por­tantly, it does so with­out re­quir­ing run­time la­bels. The CPS trans­la­tion can be used to study the se­man­tics of lex­i­cal ef­fect han­dlers as well as as an im­ple­men­ta­tion tech­nique.