Row-based type systems 

for algebraic effect handlers
Taro Sekiyama

Twitter: @skymountain_
1
Contents
1. Background: algebraic effects and handlers

2. Effect-and-type systems for effect handlers
!2
Algebraic effects & handlers
[Plotkin & Pretnar ’09, ’13]
• A mechanism to give user-defined effects

(a.k.a. to use continuations in a “well-structured” manner)

• Separating interfaces and implementations 

of effects

• Invoked via operations

• Interpreted by handlers
• Handlers give the ability to call continuations
• Implemented as libraries or primitives in
languages such as Eff, Koka, Frank, etc.
!3
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
!4
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Declare fail operation
!5
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Declare fail operation
Invoke fail
!6
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Declare fail operation
Invoke fail
Inject interpretation into effects
invoked in “div 42 x”
!7
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Declare fail operation
Invoke fail
Inject interpretation into effects
invoked in “div 42 x”
Interpretation of
fail operation
!8
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Declare fail operation
Invoke fail
Inject interpretation into effects
invoked in “div 42 x”
f 0 Left “div0”⟶
Interpretation of
fail operation
!8
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Declare fail operation
Invoke fail
f 0 Left “div0”⟶
Inject interpretation into effects
invoked in “div 42 x”
Evaluated with

the value of “div 42 x”
Interpretation of
fail operation
!9
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Declare fail operation
Invoke fail
f 7 Right 6⟶f 0 Left “div0”⟶
Inject interpretation into effects
invoked in “div 42 x”
Evaluated with

the value of “div 42 x”
Interpretation of
fail operation
!9
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle #choose(1,2) + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
!10
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle #choose(1,2) + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
!10
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle #choose(1,2) + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
!10
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle #choose(1,2) + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
Return x as 

the result of #choose
!11
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle #choose(1,2) + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
Return x as 

the result of #choose
!11
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle #choose(1,2) + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
x := 1
Return x as 

the result of #choose
!11
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle 1 + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
x := 1
Return x as 

the result of #choose
!12
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle 1 + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
Return x as 

the result of #choose
!13
Resumption
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
effect choose : int × int ! int
handle 1 + 

#choose(10,20) with

return (x:int) ! x

choose (x:int,y:int) ! resume x
Return x as 

the result of #choose
!13
Resumption
effect choose : int × int ! int
handle 1 + 

10 with

return (x:int) ! x

choose (x:int,y:int) ! resume x
Handlers support resumption of the computation
from the point of the effect invocation

• Reminiscent of delimited continuation
Return x as 

the result of #choose
!14
Parameterized effects
effect α choose : α × α ! α
handle if #choose(true,false)

then 1 else 2 with

return (x:int) ! #choose(10,20) + x

bool choose (x,y) ! resume (x && y)
!15
Parameterized effects
effect α choose : α × α ! α
handle if #choose(true,false)

then 1 else 2 with

return (x:int) ! #choose(10,20) + x

bool choose (x,y) ! resume (x && y)
!16
Signature is parameterized over types
Parameterized effects
effect α choose : α × α ! α
handle if #choose(true,false)

then 1 else 2 with

return (x:int) ! #choose(10,20) + x

bool choose (x,y) ! resume (x && y)
!17
Signature is parameterized over types
α := bool
Parameterized effects
effect α choose : α × α ! α
handle if #choose(true,false)

then 1 else 2 with

return (x:int) ! #choose(10,20) + x

bool choose (x,y) ! resume (x && y)
!18
Signature is parameterized over types
α := bool
Effect “bool choose” is handled
Parameterized effects
effect α choose : α × α ! α
handle if #choose(true,false)

then 1 else 2 with

return (x:int) ! #choose(10,20) + x

bool choose (x,y) ! resume (x && y)
!19
Signature is parameterized over types
α := bool
α := int
Effect “bool choose” is handled
Parameterized effects
effect α choose : α × α ! α
handle if #choose(true,false)

then 1 else 2 with

return (x:int) ! #choose(10,20) + x

bool choose (x,y) ! resume (x && y)
!19
Effect “int choose” will be
handled by an outer handler
Signature is parameterized over types
α := bool
α := int
Effect “bool choose” is handled
Contents
1. Background: algebraic effects and handlers

2. Effect-and-type systems for effect handlers
!20
Motivation
Detection of “Handler-Not-Found” error
!21
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
div 10 0
Motivation
Detection of “Handler-Not-Found” error
!21
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
div 10 0
There are no
handlers for #fail
Effect(-and-type) system
A means to detect invoked effects statically

• Types contain information about effects
!22
τ1 ! τ2Function type
Effect(-and-type) system
A means to detect invoked effects statically

• Types contain information about effects
!22
τ1 ! τ2Function type ε
Functions of this type may invoke effects ε
Row-based effect system

[Hillerström+ ’16; Leijen ’17; Lindley+ 17]
Effects ε are represented by a row
• It is originated from record typing

• Supported by Koka, Frank, Links, etc.
!23
Row-based effect system

[Hillerström+ ’16; Leijen ’17; Lindley+ 17]
Effects ε are represented by a row
• It is originated from record typing

• Supported by Koka, Frank, Links, etc.
!23
A sequence of effect names

〈 fail, int choose, … 〉
Row-based effect system

[Hillerström+ ’16; Leijen ’17; Lindley+ 17]
Effects ε are represented by a row
• It is originated from record typing

• Supported by Koka, Frank, Links, etc.
!23
A sequence of effect names

〈 fail, int choose, … 〉
τ1 !〈fail, int choose〉τ2
Row-based effect system

[Hillerström+ ’16; Leijen ’17; Lindley+ 17]
Effects ε are represented by a row
• It is originated from record typing

• Supported by Koka, Frank, Links, etc.
!23
A sequence of effect names

〈 fail, int choose, … 〉
Given to functions that may invoke
only “fail" and/or “int choose”
τ1 !〈fail, int choose〉τ2
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
!24
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
!24
div : int !〈〉int !〈fail〉int
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
Example
effect fail : str ! unit
let div (x:int) (y:int) =

if y = 0 then (#fail “div0”; -1)

else x / y
!24
div : int !〈〉int !〈fail〉int
let f (x:int) : (str,int) either =

handle (div 42 x) with

return (y:int) ! Right y

fail (y:str) ! Left y
f : int !〈〉(str, int) either
Features
1. Parametric effects by row polymorphism

2. Duplication of effect names
!25
Features
1. Parametric effects by row polymorphism
2. Duplication of effect names
!26
Example
!27
effect fail : str ! unit
type α opt = Some of α | None
let to-maybe (f : unit !〈fail〉α) : α opt =

handle f () with

return x ! Some x

fail _ ! None
to-maybe : ∀α. (unit !〈fail〉α) !〈〉α opt
Example
!27
effect fail : str ! unit
type α opt = Some of α | None
let to-maybe (f : unit !〈fail〉α) : α opt =

handle f () with

return x ! Some x

fail _ ! None
Problem: to-maybe cannot be applied to
functions invoking effects other than fail
Ex: to-maybe (fun () ! #choose(1,2))
to-maybe : ∀α. (unit !〈fail〉α) !〈〉α opt
Row polymorphism
Allowing functions to be parameterized over rows
!28
effect fail : str ! unit
let to-maybe (f : unit !〈fail, ρ〉α) =

handle f () with

return x ! Some x

fail _ ! None
Row polymorphism
Allowing functions to be parameterized over rows
!28
effect fail : str ! unit
let to-maybe (f : unit !〈fail, ρ〉α) =

handle f () with

return x ! Some x

fail _ ! None
Row variable
f will invoke fail and/or
effects substituted for ρ
Row polymorphism
Allowing functions to be parameterized over rows
!28
effect fail : str ! unit
let to-maybe (f : unit !〈fail, ρ〉α) =

handle f () with

return x ! Some x

fail _ ! None
∀α,ρ. (unit !〈fail,ρ〉α) !〈ρ〉α opt
to-maybe (fun () ! #choose(1,2))

by instantiating ρ with〈int choose〉
Row variable
f will invoke fail and/or
effects substituted for ρ
Distinctive features
1. Parametric effects by row polymorphism

2. Duplication of effect names
!29
Duplication of effect names
• An effect can occur in multiple positions in a row

• Multiple parameterized effects with the same
name can occur in a row
!30
〈fail, int choose, fail〉
〈fail, int choose, bool choose〉
Benefits of duplication
• Functions can invoke effects given different
type parameters

• Simplifying the effect system, especially, with
row polymorphism

• Support for abstract effects
!31
Benefits of duplication
• Functions can invoke effects given different
type parameters
• Simplifying the effect system, especially, with
row polymorphism

• Support for abstract effects
!32
Benefit 1
Functions can invoke effects given different type
parameters
!33
let f () =

if #choose(true, false)

then #choose(1, 2) 

else #choose(10, 20)
Benefit 1
Functions can invoke effects given different type
parameters
!33
let f () =

if #choose(true, false)

then #choose(1, 2) 

else #choose(10, 20)
bool choose
Benefit 1
Functions can invoke effects given different type
parameters
!33
let f () =

if #choose(true, false)

then #choose(1, 2) 

else #choose(10, 20)
bool choose
Int chooseint choose
Benefit 1
Functions can invoke effects given different type
parameters
!33
let f () =

if #choose(true, false)

then #choose(1, 2) 

else #choose(10, 20)
bool choose
Int chooseint choose
f : unit !〈bool choose, int choose〉int
Handling duplicated effects
Effects are handled from the leftmost among
ones having the same name
!34
let f

(g : unit !〈int choose, bool choose〉unit) =

handle g () with

return x ! 0

int choose (x,y) ! 1
Handling duplicated effects
Effects are handled from the leftmost among
ones having the same name
!35
let f

(g : unit !〈int choose, bool choose〉unit) =

handle g () with

return x ! 0

bool choose (x,y) ! 1
Conclusion
• Algebraic effects & handlers are a means 

to give user-defined effects

• Effect systems structure and analyze 

effectful behavior of programs

• Row-based systems are expressive and
amenable
!36
Call for collaboration
Research interests

• New effect systems to capture and structure
effectful behavior

• Efficient implementation of effect handlers

• Practical applications of effects 

(and continuations)
!37

More Related Content

PDF
Cheat Sheet for Machine Learning in Python: Scikit-learn
PPTX
Introduction to data analysis using python
PDF
Rouault imbert alpc_rpc_pacsec
PPT
Sockets
PDF
Linux Binary Exploitation - Heap Exploitation
PPT
Presentation: Project Preliminary
PDF
C Pointers
PPT
Class 5 - PHP Strings
Cheat Sheet for Machine Learning in Python: Scikit-learn
Introduction to data analysis using python
Rouault imbert alpc_rpc_pacsec
Sockets
Linux Binary Exploitation - Heap Exploitation
Presentation: Project Preliminary
C Pointers
Class 5 - PHP Strings

What's hot (20)

PDF
Ponylangとこれからの並行プログラミング
PDF
1 installing & Getting Started with R
PPTX
Aho-Corasick string matching algorithm
PDF
Python Pandas for Data Science cheatsheet
PDF
世界最速の正規表現JITエンジンの実装
PPT
Control Structures In Php 2
PDF
the-rapture సంఘము ఎత్తబడుట .pdf
PPTX
Joy of scala
PPTX
Python second ppt
PPTX
Javascript event handler
PDF
Bca sem 6 php practicals 1to12
PPTX
C formatted and unformatted input and output constructs
ODP
Form Processing In Php
PPTX
Regular Expressions 101 Introduction to Regular Expressions
PDF
An introduction to property based testing
PPT
Python List.ppt
PDF
Brief Life Sketch - Sant Teja Singh Ji
PDF
Php Tutorials for Beginners
PDF
Django Introduction & Tutorial
Ponylangとこれからの並行プログラミング
1 installing & Getting Started with R
Aho-Corasick string matching algorithm
Python Pandas for Data Science cheatsheet
世界最速の正規表現JITエンジンの実装
Control Structures In Php 2
the-rapture సంఘము ఎత్తబడుట .pdf
Joy of scala
Python second ppt
Javascript event handler
Bca sem 6 php practicals 1to12
C formatted and unformatted input and output constructs
Form Processing In Php
Regular Expressions 101 Introduction to Regular Expressions
An introduction to property based testing
Python List.ppt
Brief Life Sketch - Sant Teja Singh Ji
Php Tutorials for Beginners
Django Introduction & Tutorial
Ad

Similar to Row-based Effect Systems for Algebraic Effect Handlers (20)

PPTX
introduction to c programming and C History.pptx
PPTX
Operators in C programming language.pptx
PDF
Declare Your Language: Name Resolution
PPTX
python statement, expressions and operators.pptx
PPTX
OPERATOR IN PYTHON-PART2
PDF
A formalization of complex event stream processing
PPT
Module II Partition and Generating Function (2).ppt
PPTX
Lecture 3 and 4.pptx
PPTX
10. Recursion
PDF
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
PPTX
PPTX
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
PPTX
OPERATORS-PYTHON.pptx ALL OPERATORS ARITHMATIC AND LOGICAL
PDF
High Wizardry in the Land of Scala
DOCX
C – operators and expressions
PPTX
Python-CH01L04-Presentation.pptx
PDF
Python Functions (PyAtl Beginners Night)
PPTX
Introduction to python programming ( part-2 )
PDF
Data Handling.pdf
introduction to c programming and C History.pptx
Operators in C programming language.pptx
Declare Your Language: Name Resolution
python statement, expressions and operators.pptx
OPERATOR IN PYTHON-PART2
A formalization of complex event stream processing
Module II Partition and Generating Function (2).ppt
Lecture 3 and 4.pptx
10. Recursion
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
OPERATORS-PYTHON.pptx ALL OPERATORS ARITHMATIC AND LOGICAL
High Wizardry in the Land of Scala
C – operators and expressions
Python-CH01L04-Presentation.pptx
Python Functions (PyAtl Beginners Night)
Introduction to python programming ( part-2 )
Data Handling.pdf
Ad

Recently uploaded (20)

PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
A novel scalable deep ensemble learning framework for big data classification...
DOCX
search engine optimization ppt fir known well about this
PPTX
The various Industrial Revolutions .pptx
PDF
Architecture types and enterprise applications.pdf
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPT
What is a Computer? Input Devices /output devices
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
August Patch Tuesday
PPTX
Tartificialntelligence_presentation.pptx
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
CloudStack 4.21: First Look Webinar slides
PDF
Unlock new opportunities with location data.pdf
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
sustainability-14-14877-v2.pddhzftheheeeee
Chapter 5: Probability Theory and Statistics
Benefits of Physical activity for teenagers.pptx
A novel scalable deep ensemble learning framework for big data classification...
search engine optimization ppt fir known well about this
The various Industrial Revolutions .pptx
Architecture types and enterprise applications.pdf
O2C Customer Invoices to Receipt V15A.pptx
Web Crawler for Trend Tracking Gen Z Insights.pptx
NewMind AI Weekly Chronicles – August ’25 Week III
Enhancing emotion recognition model for a student engagement use case through...
What is a Computer? Input Devices /output devices
Developing a website for English-speaking practice to English as a foreign la...
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
August Patch Tuesday
Tartificialntelligence_presentation.pptx
Taming the Chaos: How to Turn Unstructured Data into Decisions
CloudStack 4.21: First Look Webinar slides
Unlock new opportunities with location data.pdf

Row-based Effect Systems for Algebraic Effect Handlers

  • 1. Row-based type systems 
 for algebraic effect handlers Taro Sekiyama Twitter: @skymountain_ 1
  • 2. Contents 1. Background: algebraic effects and handlers 2. Effect-and-type systems for effect handlers !2
  • 3. Algebraic effects & handlers [Plotkin & Pretnar ’09, ’13] • A mechanism to give user-defined effects
 (a.k.a. to use continuations in a “well-structured” manner) • Separating interfaces and implementations 
 of effects • Invoked via operations • Interpreted by handlers • Handlers give the ability to call continuations • Implemented as libraries or primitives in languages such as Eff, Koka, Frank, etc. !3
  • 4. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y !4
  • 5. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y Declare fail operation !5
  • 6. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y Declare fail operation Invoke fail !6
  • 7. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y Declare fail operation Invoke fail Inject interpretation into effects invoked in “div 42 x” !7
  • 8. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y Declare fail operation Invoke fail Inject interpretation into effects invoked in “div 42 x” Interpretation of fail operation !8
  • 9. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y Declare fail operation Invoke fail Inject interpretation into effects invoked in “div 42 x” f 0 Left “div0”⟶ Interpretation of fail operation !8
  • 10. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y Declare fail operation Invoke fail f 0 Left “div0”⟶ Inject interpretation into effects invoked in “div 42 x” Evaluated with
 the value of “div 42 x” Interpretation of fail operation !9
  • 11. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y Declare fail operation Invoke fail f 7 Right 6⟶f 0 Left “div0”⟶ Inject interpretation into effects invoked in “div 42 x” Evaluated with
 the value of “div 42 x” Interpretation of fail operation !9
  • 12. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle #choose(1,2) + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x !10
  • 13. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle #choose(1,2) + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x !10
  • 14. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle #choose(1,2) + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x !10
  • 15. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle #choose(1,2) + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x Return x as 
 the result of #choose !11
  • 16. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle #choose(1,2) + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x Return x as 
 the result of #choose !11
  • 17. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle #choose(1,2) + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x x := 1 Return x as 
 the result of #choose !11
  • 18. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle 1 + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x x := 1 Return x as 
 the result of #choose !12
  • 19. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle 1 + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x Return x as 
 the result of #choose !13
  • 20. Resumption Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation effect choose : int × int ! int handle 1 + 
 #choose(10,20) with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x Return x as 
 the result of #choose !13
  • 21. Resumption effect choose : int × int ! int handle 1 + 
 10 with
 return (x:int) ! x
 choose (x:int,y:int) ! resume x Handlers support resumption of the computation from the point of the effect invocation • Reminiscent of delimited continuation Return x as 
 the result of #choose !14
  • 22. Parameterized effects effect α choose : α × α ! α handle if #choose(true,false)
 then 1 else 2 with
 return (x:int) ! #choose(10,20) + x
 bool choose (x,y) ! resume (x && y) !15
  • 23. Parameterized effects effect α choose : α × α ! α handle if #choose(true,false)
 then 1 else 2 with
 return (x:int) ! #choose(10,20) + x
 bool choose (x,y) ! resume (x && y) !16 Signature is parameterized over types
  • 24. Parameterized effects effect α choose : α × α ! α handle if #choose(true,false)
 then 1 else 2 with
 return (x:int) ! #choose(10,20) + x
 bool choose (x,y) ! resume (x && y) !17 Signature is parameterized over types α := bool
  • 25. Parameterized effects effect α choose : α × α ! α handle if #choose(true,false)
 then 1 else 2 with
 return (x:int) ! #choose(10,20) + x
 bool choose (x,y) ! resume (x && y) !18 Signature is parameterized over types α := bool Effect “bool choose” is handled
  • 26. Parameterized effects effect α choose : α × α ! α handle if #choose(true,false)
 then 1 else 2 with
 return (x:int) ! #choose(10,20) + x
 bool choose (x,y) ! resume (x && y) !19 Signature is parameterized over types α := bool α := int Effect “bool choose” is handled
  • 27. Parameterized effects effect α choose : α × α ! α handle if #choose(true,false)
 then 1 else 2 with
 return (x:int) ! #choose(10,20) + x
 bool choose (x,y) ! resume (x && y) !19 Effect “int choose” will be handled by an outer handler Signature is parameterized over types α := bool α := int Effect “bool choose” is handled
  • 28. Contents 1. Background: algebraic effects and handlers 2. Effect-and-type systems for effect handlers !20
  • 29. Motivation Detection of “Handler-Not-Found” error !21 effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y div 10 0
  • 30. Motivation Detection of “Handler-Not-Found” error !21 effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y div 10 0 There are no handlers for #fail
  • 31. Effect(-and-type) system A means to detect invoked effects statically • Types contain information about effects !22 τ1 ! τ2Function type
  • 32. Effect(-and-type) system A means to detect invoked effects statically • Types contain information about effects !22 τ1 ! τ2Function type ε Functions of this type may invoke effects ε
  • 33. Row-based effect system
 [Hillerström+ ’16; Leijen ’17; Lindley+ 17] Effects ε are represented by a row • It is originated from record typing • Supported by Koka, Frank, Links, etc. !23
  • 34. Row-based effect system
 [Hillerström+ ’16; Leijen ’17; Lindley+ 17] Effects ε are represented by a row • It is originated from record typing • Supported by Koka, Frank, Links, etc. !23 A sequence of effect names
 〈 fail, int choose, … 〉
  • 35. Row-based effect system
 [Hillerström+ ’16; Leijen ’17; Lindley+ 17] Effects ε are represented by a row • It is originated from record typing • Supported by Koka, Frank, Links, etc. !23 A sequence of effect names
 〈 fail, int choose, … 〉 τ1 !〈fail, int choose〉τ2
  • 36. Row-based effect system
 [Hillerström+ ’16; Leijen ’17; Lindley+ 17] Effects ε are represented by a row • It is originated from record typing • Supported by Koka, Frank, Links, etc. !23 A sequence of effect names
 〈 fail, int choose, … 〉 Given to functions that may invoke only “fail" and/or “int choose” τ1 !〈fail, int choose〉τ2
  • 37. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y !24 let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y
  • 38. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y !24 div : int !〈〉int !〈fail〉int let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y
  • 39. Example effect fail : str ! unit let div (x:int) (y:int) =
 if y = 0 then (#fail “div0”; -1)
 else x / y !24 div : int !〈〉int !〈fail〉int let f (x:int) : (str,int) either =
 handle (div 42 x) with
 return (y:int) ! Right y
 fail (y:str) ! Left y f : int !〈〉(str, int) either
  • 40. Features 1. Parametric effects by row polymorphism 2. Duplication of effect names !25
  • 41. Features 1. Parametric effects by row polymorphism 2. Duplication of effect names !26
  • 42. Example !27 effect fail : str ! unit type α opt = Some of α | None let to-maybe (f : unit !〈fail〉α) : α opt =
 handle f () with
 return x ! Some x
 fail _ ! None to-maybe : ∀α. (unit !〈fail〉α) !〈〉α opt
  • 43. Example !27 effect fail : str ! unit type α opt = Some of α | None let to-maybe (f : unit !〈fail〉α) : α opt =
 handle f () with
 return x ! Some x
 fail _ ! None Problem: to-maybe cannot be applied to functions invoking effects other than fail Ex: to-maybe (fun () ! #choose(1,2)) to-maybe : ∀α. (unit !〈fail〉α) !〈〉α opt
  • 44. Row polymorphism Allowing functions to be parameterized over rows !28 effect fail : str ! unit let to-maybe (f : unit !〈fail, ρ〉α) =
 handle f () with
 return x ! Some x
 fail _ ! None
  • 45. Row polymorphism Allowing functions to be parameterized over rows !28 effect fail : str ! unit let to-maybe (f : unit !〈fail, ρ〉α) =
 handle f () with
 return x ! Some x
 fail _ ! None Row variable f will invoke fail and/or effects substituted for ρ
  • 46. Row polymorphism Allowing functions to be parameterized over rows !28 effect fail : str ! unit let to-maybe (f : unit !〈fail, ρ〉α) =
 handle f () with
 return x ! Some x
 fail _ ! None ∀α,ρ. (unit !〈fail,ρ〉α) !〈ρ〉α opt to-maybe (fun () ! #choose(1,2))
 by instantiating ρ with〈int choose〉 Row variable f will invoke fail and/or effects substituted for ρ
  • 47. Distinctive features 1. Parametric effects by row polymorphism 2. Duplication of effect names !29
  • 48. Duplication of effect names • An effect can occur in multiple positions in a row • Multiple parameterized effects with the same name can occur in a row !30 〈fail, int choose, fail〉 〈fail, int choose, bool choose〉
  • 49. Benefits of duplication • Functions can invoke effects given different type parameters • Simplifying the effect system, especially, with row polymorphism • Support for abstract effects !31
  • 50. Benefits of duplication • Functions can invoke effects given different type parameters • Simplifying the effect system, especially, with row polymorphism • Support for abstract effects !32
  • 51. Benefit 1 Functions can invoke effects given different type parameters !33 let f () =
 if #choose(true, false)
 then #choose(1, 2) 
 else #choose(10, 20)
  • 52. Benefit 1 Functions can invoke effects given different type parameters !33 let f () =
 if #choose(true, false)
 then #choose(1, 2) 
 else #choose(10, 20) bool choose
  • 53. Benefit 1 Functions can invoke effects given different type parameters !33 let f () =
 if #choose(true, false)
 then #choose(1, 2) 
 else #choose(10, 20) bool choose Int chooseint choose
  • 54. Benefit 1 Functions can invoke effects given different type parameters !33 let f () =
 if #choose(true, false)
 then #choose(1, 2) 
 else #choose(10, 20) bool choose Int chooseint choose f : unit !〈bool choose, int choose〉int
  • 55. Handling duplicated effects Effects are handled from the leftmost among ones having the same name !34 let f
 (g : unit !〈int choose, bool choose〉unit) =
 handle g () with
 return x ! 0
 int choose (x,y) ! 1
  • 56. Handling duplicated effects Effects are handled from the leftmost among ones having the same name !35 let f
 (g : unit !〈int choose, bool choose〉unit) =
 handle g () with
 return x ! 0
 bool choose (x,y) ! 1
  • 57. Conclusion • Algebraic effects & handlers are a means 
 to give user-defined effects • Effect systems structure and analyze 
 effectful behavior of programs • Row-based systems are expressive and amenable !36
  • 58. Call for collaboration Research interests • New effect systems to capture and structure effectful behavior • Efficient implementation of effect handlers • Practical applications of effects 
 (and continuations) !37