Syntax and Semantics for Operations with Scopes

Motivated by the problem of separating syntax from semantics in programming with algebraic effects and handlers, we propose a categorical model of abstract syntax with so-called scoped operations. As a building block of a term, a scoped operation is not merely a node in a tree, as it can also encompass a whole part of the term (a scope). Some examples from the area of programming are given by the operation catch for handling exceptions, in which the part in the scope is the code that may raise an exception, or the operation once, which selects a single solution from a nondeterministic computation. A distinctive feature of such operations is their behaviour under program composition, that is, syntactic substitution. Our model is based on what Ghani et al. call the monad of explicit substitutions, defined using the initial-algebra semantics in the category of endofunctors. We also introduce a new kind of multi-sorted algebras, called scoped algebras, which serve as interpretations of syntax with scopes. In generality, scoped algebras are given in the style of the presheaf formalisation of syntax with binders of Fiore et al. As the main technical result, we show that our monad indeed arises from free objects in the category of scoped algebras. Importantly, we show that our results are immediately applicable. In particular, we show a Haskell implementation together with practical, real-life examples.

[1]  Conor McBride,et al.  Do be do be do , 2017, POPL.

[2]  Marcelo P. Fiore,et al.  Second-Order Algebraic Theories - (Extended Abstract) , 2010, MFCS.

[3]  Wouter Swierstra,et al.  Data types à la carte , 2008, Journal of Functional Programming.

[4]  Roland Carl Backhouse,et al.  Categorical Fixed Point Calculus , 1995, Category Theory and Computer Science.

[5]  Koen Claessen,et al.  A poor man's concurrency monad , 1999, Journal of Functional Programming.

[6]  Tom Schrijvers,et al.  Fusion for Free - Efficient Algebraic Effect Handlers , 2015, MPC.

[7]  Andrej Bauer,et al.  Programming with algebraic effects and handlers , 2012, J. Log. Algebraic Methods Program..

[8]  Ralf Hinze,et al.  Just do it: simple monadic equational reasoning , 2011, ICFP.

[9]  Gordon D. Plotkin,et al.  Notions of Computation Determine Monads , 2002, FoSSaCS.

[10]  Torben Amtoft,et al.  Faithful Translations between Polyvariant Flows and Polymorphic Types , 2000, ESOP.

[11]  Edwin Brady,et al.  Programming and reasoning with algebraic effects and dependent types , 2013, ICFP.

[12]  Bart Demoen,et al.  Heuristics Entwined with Handlers Combined: From Functional Specification to Logic Programming Implementation , 2014, PPDP '14.

[13]  Jeremy Gibbons,et al.  Tracing monadic computations and representing effects , 2012, MSFP.

[14]  Makoto Hamana,et al.  Explicit substitutions and higher-order syntax , 2006, High. Order Symb. Comput..

[15]  Amr Sabry,et al.  Extensible effects: an alternative to monad transformers , 2013, Haskell '13.

[16]  Gordon D. Plotkin,et al.  Algebraic Operations and Generic Effects , 2003, Appl. Categorical Struct..

[17]  Gordon D. Plotkin,et al.  Abstract syntax and variable binding , 1999, Proceedings. 14th Symposium on Logic in Computer Science (Cat. No. PR00158).

[18]  Patricia Johann,et al.  Initial Algebra Semantics Is Enough! , 2007, TLCA.

[19]  Gordon D. Plotkin,et al.  Computational Effects and Operations: An Overview , 2004, Electron. Notes Theor. Comput. Sci..

[20]  Dan R. Ghica,et al.  Leaving the Nest: Nominal Techniques for Variables with Interleaving Scopes , 2015, CSL.

[21]  M. Barr Coequalizers and free triples , 1970 .

[22]  Gordon D. Plotkin,et al.  Handlers of Algebraic Effects , 2009, ESOP.

[23]  Thorsten Altenkirch,et al.  Relative Monads Formalised , 2014, J. Formaliz. Reason..

[24]  Tom Schrijvers,et al.  Effect handlers in scope , 2014, Haskell 2014.

[25]  Maarten M. Fokkinga,et al.  Law and order in algorithmics , 1992 .