Computer Science
Monads
Monads are a programming concept used to manage side effects in functional programming languages. They provide a way to encapsulate impure operations, such as input/output or state changes, in a pure functional way. Monads are often used in languages like Haskell and Scala.
Written by Perlego with AI-assistance
Related key terms
1 of 5
3 Key excerpts on "Monads"
- No longer available |Learn more
- (Author)
- 2014(Publication Date)
- Learning Press(Publisher)
However, I/O and state management are by no means the only uses of Monads. They are useful in any situation where the programmer wants to carry out a purely functional computation while a related computation is carried out on the side. In imperative programming the side effects are embedded in the semantics of the programming language; with Monads, they are made explicit in the monad definition. The name monad derives from category theory, a branch of mathematics that describes patterns applicable to many mathematical fields. (As a minor terminological mismatch, the term monad in functional programming contexts is usually used with a meaning corresponding to that of the term strong monad in category theory, a specific kind of category-theoretical monad.) The Haskell programming language is a functional language that makes heavy use of Monads, and includes syntactic sugar to make monadic composition more convenient. All of the code samples in this page are written in Haskell unless noted otherwise. Concepts Definition A monad is a construction that, given an underlying type system, embeds a corresponding type system (called the monadic type system ) into it (that is, each monadic type acts as the underlying type). This monadic type system preserves all significant aspects of the underlying type system, while adding features particular to the monad. ________________________ WORLD TECHNOLOGIES ________________________ The usual formulation of a monad for programming is known as a Kleisli triple, and has the following components: 1. A type construction that defines, for every underlying type, how to obtain a corresponding monadic type. In Haskell's notation, the name of the monad represents the type constructor. If M is the name of the monad and t is a data type, then M t is the corresponding type in the monad. 2. A unit function that maps a value in an underlying type to a value in the corresponding monadic type. - Masahiko Sato, Yoshihito Toyama(Authors)
- 1998(Publication Date)
- World Scientific(Publisher)
That is the point where monad transformers come into play: in essence a monad transformer extends a given monad by a certain feature. A monad which supports a variety of features is built by applying a sequence of transformers to a base monad such as 10. One of the main innovations of the paper is the definition of a backtracking monad transformer which adds backtracking to an arbitrary monad. Among other things it allows to study the interplay of backtracking and interaction which was previously not possible. In addition we propose a simple scheme for encapsulating Monads. Encapsulation of computations is vital since it allows, for instance, to turn an embedded Prolog program into a pure function. 100 The paper is organized as follows. Section 2 defines Monads and introduces the different constructs of the embedded language. For each linguistic feature — nondeterminism, exception handling, and interaction — a subclass of the basic monadic class is given. Section 3 presents several motivating examples demonstrating among other things the use of higher order computations. A first attempt at an axiomatization of the computational primitives appears in Section 4. Section 5 describes the construction of a monad supporting the various linguistic features. Perhaps surprisingly, the implementation appears to compare favourably to that of logic languages like Prolog or Mercury n which offer non-determinism as a 'language primitive'. Section 6 provides some evidence for this claim. Finally, in Section 7 we relate our work to other approaches and suggest some directions for future work. 2 Monads Think of a monad as an abstract type for computations which comes equipped with two principal operations. class Monad m where return :: a -> m a (>E=) :: m a —> (a —► m b) -» m b ( » ) :: ma-*mb-+mb m > n = m >= A_ -> n An element of m a represents a computation which yields a value of type a. The trivial computation which immediately returns the value a is denoted by return a.- eBook - PDF
- Gilles Barthe, Joost-Pieter Katoen, Alexandra Silva(Authors)
- 2020(Publication Date)
- Cambridge University Press(Publisher)
Moreover, with ‘fold fusion’ (Malcolm, 1990; Hutton, 1999) we can easily derive an efficient implementation of the hyper-distribution semantics from its naïve specification. 1 “Kuifje” is the Flemish/Dutch name of Hergé’s Tintin, and refers to his hairstyle: a quiff. 12.2 Background 393 12.2 Background We begin by describing Monads in general (briefly) as they are used in Computer Science (§12.2.1), how they relate to quantitative information flow (§12.2.2), how they are instantiated in the functional programming language Haskell (§12.2.3), and how their Haskell instantiation can be used to build the tools necessary for a probabilistic information-flow-aware programming-language implementation (§12.2.4). The Haskell-oriented reader may want to skip the theoretical background and jump straight to §12.2.3. 12.2.1 Monads The mathematical structure of Monads was introduced to Computer Science by Moggi in order to have a model of computation that was more general than the “gross simplification” of identifying programs with total functions from values to values, a view that “wipes out completely behaviours like non-termination, non-determinism or side effects. . . ” (Moggi, 1991). Here we will be using that generality to capture the behaviour of programs that hide and, complementarily, leak information: and our particular focus will be on using the monadic facilities of the programming language Haskell (Peyton Jones, 2003) to illustrate our ideas (§12.2.3). Moggi’s insight was to model a “notion of computation” as a monad T, in order to distinguish plain values of some type A from the computations TA that yield such values.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.


