Information Technology Reference
In-Depth Information
The trivial computation
Return
(
d
) simply returns the value
d
as its result. When
the computation
e
computes a value
a
and
f
is a function mapping values to
computations,
e>>
=
f
follows the computation
e
with the computation
f
(
a
).
(The symbol '
>>
=', pronounced 'bind', is from the notation for monads pro-
vided by the language Haskell, which also allows
e
1
>>
=
λx.e
2
to be written as
'
x
←
e
1
;
e
2
'.) The operations
Return
and
>>
= are required to satisfy three laws:
(
Return
(
d
)
>>
=
f
)=
f
(
d
)
(16)
(
e>>
=
Return
)=
e
(17)
((
e>>
=
f
)
>>
=
g
)=
e>>
=
λx.
(
f
(
x
)
>>
=
g
))
(18)
where
x
mustnotbefreein
f
or
g
in the last law above.
The simplest possible example of a monad is the identity monad, where
T
(
D
)=
D
,
Return
(
d
)=
d
,and
e>>
=
f
=
f
(
e
).
Particular kinds of monads provide further operations. Such monads can of-
ten be constructed by applying standard monad transformers [21] to existing
monads.
Side-Effect Monads:
Given domains
L
of locations and
V
of values, a side-effect
monad provides the operations:
-
Update
:
L
×
V
→
T
()
-
Inspect
:
L
→
T
(
V
)
Environment Monads:
Given a domain
Env
, an environment monad provides:
-
GetEnv
:
T
(
Env
)
-
UseEnv
:
Env
→
T
(
D
)
→
T
(
D
)
Exception Monads:
Given a domain
A
of exception identifiers, an exception
monad provides:
-
Raise
:
A
→
T
(
D
)
-
Handle
:
A
×
T
(
D
)
×
T
(
D
)
→
T
(
D
)
All the above operations satisfy some equational axioms, which allow algebraic
reasoning about equivalence (and can even be used to define the respective mon-
ads [22]). In fact we have already seen some examples corresponding closely to
such monads:
Oxford style, direct semantics:
For an example of a side-effect monad, let
T
(
D
)=
S
→
D
×
S
, then define
Return
(
d
)=
λσ.
d, σ
and
e>>
=
f
=
(
λ
are special
cases of
Return
and
>>
= (the latter with the arguments reversed). Defining
Update
(
l, v
)=
λσ.
a, σ
.f
(
a
)(
σ
))
◦
e
. Scott and Strachey's combinators
P
and
∗
provides the required operations, and shows their close relationship to Scott
and Strachey's combinators.
, Assign
(
l, v
)(
σ
)
and
Inspect
(
l
)=
λσ.
Contents
(
l
)
,σ
Search WWH ::
Custom Search