• Stars
    star
    357
  • Rank 114,986 (Top 3 %)
  • Language
    Haskell
  • License
    Other
  • Created over 12 years ago
  • Updated 20 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Monad Transformer Library

mtl Hackage Build Status

MTL is a collection of monad classes, extending the transformers package, using functional dependencies for generic lifting of monadic actions.

Structure

Transformers in MTL are divided into classes and data types. Classes define the monadic operations of transformers. Data types, generally from the transformers package, implement transformers, and MTL provides instances for all the transformer type classes.

MTL and transformers use a common module, data type, and function naming scheme. As an example, let's imagine we have a transformer Foo.

In the Control.Monad.Foo module, we'd find:

  • A type class MonadFoo with the transformer operations.
  • A data type FooT with instances for all monad transformer classes.
  • Functions to run the transformed computation, e.g. runFooT. For the actual transformers, there are usually a number of useful runner functions.

Lifting

When using monad transformers, you often need to "lift" a monadic action into your transformed monadic action. This is done using the lift function from MonadTrans in the Control.Monad.Trans.Class module:

lift :: (Monad m, MonadTrans t) => m a -> t m a

The action m a is lifted into the transformer action t m a.

As an example, here we lift an action of type IO a into an action of type ExceptT MyError IO a:

data MyError = EmptyLine

mightFail :: ExceptT MyError IO ()
mightFail = do
  l <- lift getLine
  when (null l) (throwError EmptyLine)

Transformers

The following outlines the available monad classes and transformers in MTL and transformers. For more details, and the corresponding documentation of the mtl version you are using, see the documentation on Hackage.

  • Control.Monad.Cont

    The Continuation monad transformer adds the ability to use continuation-passing style (CPS) in a monadic computation. Continuations can be used to manipulate the control flow of a program, e.g. early exit, error handling, or suspending a computation.

    • Class: Control.Monad.Cont.Class.MonadCont
    • Transformer: Control.Monad.Cont.ContT
  • Control.Monad.Error (deprecated!)

    The Error monad transformer has been deprecated in favor of Control.Monad.Except.

  • Control.Monad.Except

    The Except monad transformer adds the ability to fail with an error in a monadic computation.

    • Class: Control.Monad.Except.Class.MonadError
    • Transformer: Control.Monad.Except.ExceptT
  • Control.Monad.Identity

    The Identity monad transformer does not add any abilities to a monad. It simply applies the bound function to its inner monad without any modification.

    • Transformer: Control.Monad.Trans.Identity.IdentityT (in the transformers package)
    • Identity functor and monad: Data.Functor.Identity.Identity (in the base package)
  • Control.Monad.RWS

    A convenient transformer that combines the Reader, Writer, and State monad transformers.

    • Lazy transformer: Control.Monad.RWS.Lazy.RWST (which is the default, exported by Control.Monad.RWS)
    • Strict transformer: Control.Monad.RWS.Strict.RWST
  • Control.Monad.Reader

    The Reader monad transformer represents a computation which can read values from an environment.

    • Class: Control.Monad.Reader.Class.MonadReader
    • Transformer: Control.Monad.Reader.ReaderT
  • Control.Monad.State

    The State monad transformer represents a computation which can read and write internal state values. If you only need to read values, you might want to use Reader instead.

    • Class: Control.Monad.State.Class.MonadState
    • Lazy transformer: Control.Monad.State.Lazy.StateT (the default, exported by Control.Monad.State)
    • Strict transformer: Control.Monad.State.Strict.StateT
  • Control.Monad.Writer

    The Writer monad transformer represents a computation that can produce a stream of data in addition to the computed values. This can be used to collect values in some data structure with a Monoid instance. This can be used for things like logging and accumulating values throughout a computation.

    • Class: Control.Monad.Writer.Class.MonadWriter
    • Lazy transformers: Control.Monad.Writer.Lazy.WriterT
    • Strict transformers: Control.Monad.Writer.Strict.WriterT
  • Control.Monad.Accum

    The Accum monad transformer represents a computation which manages append-only state, or a writer that can read all previous inputs. It binds a function to a monadic value by lazily accumulating subcomputations via (<>). For more general access, use State instead.

    • Class: Control.Monad.Accum
    • Transformer: Control.Monad.Trans.Accum.AccumT
  • Control.Monad.Select

    The Select monad transformer represents a computation which can do backtracking search using a 'ranked' evaluation strategy. Binding a function to a monad value chains together evaluation strategies in the sense that the results of previous strategies may influence subsequent rank and evaluation strategies in subcomputations.

    • Class: Control.Monad.Select
    • Transformer: Control.Monad.Trans.Select.SelectT

Resources

More Repositories

1

haskell-language-server

Official haskell ide support via language server (LSP). Successor of ghcide & haskell-ide-engine.
Haskell
2,576
star
2

haskell-ide-engine

The engine for haskell ide-integration. Not an IDE
Haskell
2,384
star
3

cabal

Official upstream development repository for Cabal and cabal-install
Haskell
1,566
star
4

haskell-mode

Emacs mode for Haskell
Emacs Lisp
1,306
star
5

aeson

A fast Haskell JSON library
Haskell
1,228
star
6

stylish-haskell

Haskell code prettifier
Haskell
968
star
7

parsec

A monadic parser combinator library
Haskell
831
star
8

ghcide

A library for building Haskell IDE tooling
Haskell
588
star
9

vscode-haskell

VS Code extension for Haskell, powered by haskell-language-server
TypeScript
544
star
10

attoparsec

A fast Haskell library for parsing ByteStrings
Haskell
509
star
11

criterion

A powerful but simple library for measuring the performance of Haskell code.
Haskell
497
star
12

hackage-server

Hackage-Server: A Haskell Package Repository
Haskell
407
star
13

text

Haskell library for space- and time-efficient operations over Unicode text.
Haskell
397
star
14

haskell-platform

Distribution of Haskell with batteries included
Haskell
380
star
15

wreq

Haskell
378
star
16

vector

An efficient implementation of Int-indexed arrays (both mutable and immutable), with a powerful loop optimisation framework .
Haskell
361
star
17

haddock

Haskell Documentation Tool
HTML
360
star
18

lsp

Haskell library for the Microsoft Language Server Protocol
Haskell
354
star
19

network

Low-level networking interface
Haskell
319
star
20

containers

Assorted concrete container types
Haskell
312
star
21

statistics

A fast, high quality library for computing with statistics in Haskell.
Haskell
295
star
22

alex

A lexical analyser generator for Haskell
Haskell
292
star
23

bytestring

An efficient compact, immutable byte string type (both strict and lazy) suitable for binary or 8-bit character data.
Haskell
283
star
24

happy

The Happy parser generator for Haskell
Haskell
272
star
25

ghcup

DEPRECATED IN FAVOR OF haskell/ghcup-hs
Shell
264
star
26

ghcup-hs

Haskell
253
star
27

c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
Haskell
197
star
28

fgl

A Functional Graph Library for Haskell
Haskell
183
star
29

HTTP

Haskell HTTP package
Haskell
179
star
30

hie-bios

Set up a GHC API session for various Haskell Projects
Haskell
172
star
31

ThreadScope

A graphical tool for profiling parallel Haskell programs
Haskell
150
star
32

actions

Github actions for Haskell CI
TypeScript
147
star
33

critbit

A Haskell implementation of crit-bit trees.
Haskell
137
star
34

time

A time library
Haskell
118
star
35

primitive

This package provides various primitive memory-related operations.
Haskell
114
star
36

play-haskell

Haskell Playground
Haskell
113
star
37

unix

POSIX functionality
Haskell
108
star
38

rfcs

This repo is archived, consider using https://github.com/ghc-proposals/ghc-proposals instead
TeX
98
star
39

win32

Haskell support for the Win32 API
Haskell
96
star
40

core-libraries-committee

95
star
41

parallel

a library for parallel programming
Haskell
91
star
42

stm

Software Transactional Memory
Haskell
90
star
43

haskell-report

Haskell Language Report
TeX
89
star
44

process

Library for dealing with system processes
Haskell
85
star
45

error-messages

73
star
46

hoopl

Higher-order optimization library
Haskell
70
star
47

pretty

Haskell Pretty-printer library
Haskell
68
star
48

filepath

Haskell FilePath core library
Haskell
66
star
49

docker-haskell

Dockerfile
63
star
50

directory

Platform-independent library for basic file system operations
Haskell
57
star
51

hackage-security

Hackage security framework based on TUF (The Update Framework)
Haskell
56
star
52

mwc-random

A very fast Haskell library for generating high quality pseudo-random numbers.
Haskell
54
star
53

random

Random number library
Haskell
52
star
54

ecosystem-proposals

Proposals for the Haskell Ecosystem
51
star
55

text-icu

This package provides the Haskell Data.Text.ICU library, for performing complex manipulation of Unicode text.
Haskell
46
star
56

base64-bytestring

Fast base64 encoding and decoding for Haskell.
Haskell
45
star
57

math-functions

Special mathematical functions
Haskell
40
star
58

deepseq

Deep evaluation of data structures
Haskell
39
star
59

security-advisories

Haskell
39
star
60

tar

Reading, writing and manipulating ".tar" archive files.
Haskell
38
star
61

hsc2hs

Pre-processor for .hsc files
Haskell
38
star
62

pvp

Haskell Package Version Policy (PVP)
CSS
37
star
63

zlib

Compression and decompression in the gzip and zlib formats
C
35
star
64

ghc-events

Library and tool for parsing .eventlog files from GHC
Haskell
33
star
65

text-format

A Haskell text formatting library optimized for ease of use and high performance.
Haskell
32
star
66

ghcup-metadata

GHCup metadata repository
Haskell
30
star
67

cabal-userguide

A handy user guide for the Cabal build tool
Nix
29
star
68

base16-bytestring

Fast base16 (hexadecimal) encoding and decoding for Haskell bytestrings.
Haskell
27
star
69

network-uri

URI manipulation facilities
Haskell
24
star
70

entropy

Easy entropy source for Haskell users.
Haskell
23
star
71

winghci

Simple Windows GUI for GHCi.
C
17
star
72

double-conversion

A fast Haskell library for converting between double precision floating point numbers and text strings. It is implemented as a binding to the V8-derived C++ double-conversion library.
C++
15
star
73

xhtml

XHTML combinator library
Haskell
9
star
74

old-time

This package provides the old time library.
Haskell
7
star
75

meta

A place for discussing & documenting the github.com/haskell organization
7
star
76

ghc-builder

ghc builder bot
Haskell
6
star
77

array

This is just a mirror. See https://gitlab.haskell.org/ghc/packages/array/.
Haskell
6
star
78

cabal-website

The http://www.haskell.org/cabal/ website
HTML
4
star
79

network-bsd

POSIX network database (<netdb.h>) API
Haskell
4
star
80

haskell-wiki-configuration

Issue tracking for Haskell Wiki
PHP
4
star
81

clc-stackage

Meta-package to facilitate impact assessment for CLC proposals
Nix
3
star
82

hiw

Haskell Implementors Workshop
TeX
3
star
83

text-test-data

Test data for the Haskell text project
Text
3
star
84

old-locale

This package provides the ability to adapt to locale conventions such as date and time formats.
Haskell
3
star
85

tokenize

Simple tokenizer for English text
Haskell
3
star
86

bzlib

Compression and decompression in the bzip2 format
Haskell
2
star
87

hpc

1
star
88

os-string

Haskell
1
star