{"title":"Extending monads with pattern matching","authors":"T. Petříček, A. Mycroft, Don Syme","doi":"10.1145/2034675.2034677","DOIUrl":null,"url":null,"abstract":"Sequencing of effectful computations can be neatly captured using monads and elegantly written using <b>do</b> notation. In practice such monads often allow additional ways of composing computations, which have to be written explicitly using combinators.\n We identify joinads, an abstract notion of computation that is stronger than monads and captures many such ad-hoc extensions. In particular, joinads are monads with three additional operations: one of type <i>m</i> <i>a</i> -> <i>m</i> <i>b</i> -> <i>m</i> (<i>a</i>, <i>b</i>) captures various forms of parallel composition, one of type <i>m</i> <i>a</i> -> <i>m</i> <i>a</i> -> <i>m</i> <i>a</i> that is inspired by choice and one of type <i>m</i> <i>a</i> -> <i>m</i> (<i>m</i> a) that captures aliasing of computations. Algebraically, the first two operations form a near-semiring with commutative multiplication.\n We introduce <b>docase</b> notation that can be viewed as a monadic version of <b>case</b>. Joinad laws imply various syntactic equivalences of programs written using <b>docase</b> that are analogous to equivalences about case. Examples of joinads that benefit from the notation include speculative parallelism, waiting for a combination of user interface events, but also encoding of validation rules using the intersection of parsers.","PeriodicalId":188691,"journal":{"name":"ACM SIGPLAN Symposium/Workshop on Haskell","volume":"36 1","pages":"0"},"PeriodicalIF":0.0000,"publicationDate":"2012-01-12","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"14","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"ACM SIGPLAN Symposium/Workshop on Haskell","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/10.1145/2034675.2034677","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
引用次数: 14
Abstract
Sequencing of effectful computations can be neatly captured using monads and elegantly written using do notation. In practice such monads often allow additional ways of composing computations, which have to be written explicitly using combinators.
We identify joinads, an abstract notion of computation that is stronger than monads and captures many such ad-hoc extensions. In particular, joinads are monads with three additional operations: one of type ma -> mb -> m (a, b) captures various forms of parallel composition, one of type ma -> ma -> ma that is inspired by choice and one of type ma -> m (m a) that captures aliasing of computations. Algebraically, the first two operations form a near-semiring with commutative multiplication.
We introduce docase notation that can be viewed as a monadic version of case. Joinad laws imply various syntactic equivalences of programs written using docase that are analogous to equivalences about case. Examples of joinads that benefit from the notation include speculative parallelism, waiting for a combination of user interface events, but also encoding of validation rules using the intersection of parsers.
有效计算的顺序可以使用单子整齐地捕获,并使用do表示法优雅地编写。在实践中,这样的单子通常允许以其他方式组合计算,这些计算必须使用组合子显式地编写。我们确定了连接点,这是一种抽象的计算概念,它比单子更强大,并捕获了许多这样的特别扩展。特别地,连接是带有三个附加操作的单子:一个是类型m a -> m b -> m (a, b),捕获各种形式的并行组合;一个是类型m a -> m a -> m a,受选择的启发;另一个是类型m a -> m (m a),捕获计算的混叠。在代数上,前两个运算与交换乘法形成近半环。我们引入的docase表示法可以看作是case的一元形式。连接定律意味着使用docase编写的程序的各种语法等价,类似于关于case的等价。受益于该符号的连接示例包括推测并行性,等待用户界面事件的组合,以及使用解析器的交集对验证规则进行编码。