Multi-parameter context constraints in haskell
up vote
1
down vote
favorite
I'm currently reading the paper Monad Transformers Step by Step
by Martin Grabmüller.
Theres a part in the paper where he lists off the following instance declaration:
instance (MonadError e m) => MonadError e (ReaderT r m) where...
I've looked all over but I can't really find any info on what exactly a multi-parameter constraint like (Foo a b)
means. I don't totally understand how e
and m
work together within the instance head => MonadError e (ReaderT r m)
.
How do these multi-parameter constraints work? Thanks!
haskell typeclass
add a comment |
up vote
1
down vote
favorite
I'm currently reading the paper Monad Transformers Step by Step
by Martin Grabmüller.
Theres a part in the paper where he lists off the following instance declaration:
instance (MonadError e m) => MonadError e (ReaderT r m) where...
I've looked all over but I can't really find any info on what exactly a multi-parameter constraint like (Foo a b)
means. I don't totally understand how e
and m
work together within the instance head => MonadError e (ReaderT r m)
.
How do these multi-parameter constraints work? Thanks!
haskell typeclass
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm currently reading the paper Monad Transformers Step by Step
by Martin Grabmüller.
Theres a part in the paper where he lists off the following instance declaration:
instance (MonadError e m) => MonadError e (ReaderT r m) where...
I've looked all over but I can't really find any info on what exactly a multi-parameter constraint like (Foo a b)
means. I don't totally understand how e
and m
work together within the instance head => MonadError e (ReaderT r m)
.
How do these multi-parameter constraints work? Thanks!
haskell typeclass
I'm currently reading the paper Monad Transformers Step by Step
by Martin Grabmüller.
Theres a part in the paper where he lists off the following instance declaration:
instance (MonadError e m) => MonadError e (ReaderT r m) where...
I've looked all over but I can't really find any info on what exactly a multi-parameter constraint like (Foo a b)
means. I don't totally understand how e
and m
work together within the instance head => MonadError e (ReaderT r m)
.
How do these multi-parameter constraints work? Thanks!
haskell typeclass
haskell typeclass
edited Nov 11 at 2:07
chepner
238k29223318
238k29223318
asked Nov 10 at 18:46
Vance Palacio
356311
356311
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Essentially, a constraint like Show a
constrains the type variable a
to represent a showable type. You seem to understand this.
A constraint of the form C a b
constrains the pair of type variables a
and b
. Intuitively, this constrains means that there is a relation between such types.
Consider this fictional class
class C a b where
sum :: a -> b -> (Int, b)
Intuitively, the constraint C a b
means that a
and b
can be sum
med together (in that order!), and the result of that sum will be a pair (Int, b)
.
In your case you are dealing with
class Monad m => MonadError e m | m -> e where
throwError :: e -> m a
Here, MonadError e m
expresses the following relation between e
and m
.
m
is a monad
e
is a type for which we can convert any valuex :: e
intothrowError x :: m a
, for anya
. Intuitively, this is an "error" type, representing the nature of some error, andthrowError
simply includes such value inside the monad- given
m
, there is only one error typee
. In other words, the relation is actually a function. This is expressed through a functional dependency... | m -> e
in the class above.
The simple version is: MonadError m a
means that m
is a monad which can express some "error values", of type e
.
For example, if we have MonadError M String
available, we can write
foo :: Int -> M Int
foo n | n == 0 = throwError "can't handle zero!"
| otherwise = return (100 `div` n)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Essentially, a constraint like Show a
constrains the type variable a
to represent a showable type. You seem to understand this.
A constraint of the form C a b
constrains the pair of type variables a
and b
. Intuitively, this constrains means that there is a relation between such types.
Consider this fictional class
class C a b where
sum :: a -> b -> (Int, b)
Intuitively, the constraint C a b
means that a
and b
can be sum
med together (in that order!), and the result of that sum will be a pair (Int, b)
.
In your case you are dealing with
class Monad m => MonadError e m | m -> e where
throwError :: e -> m a
Here, MonadError e m
expresses the following relation between e
and m
.
m
is a monad
e
is a type for which we can convert any valuex :: e
intothrowError x :: m a
, for anya
. Intuitively, this is an "error" type, representing the nature of some error, andthrowError
simply includes such value inside the monad- given
m
, there is only one error typee
. In other words, the relation is actually a function. This is expressed through a functional dependency... | m -> e
in the class above.
The simple version is: MonadError m a
means that m
is a monad which can express some "error values", of type e
.
For example, if we have MonadError M String
available, we can write
foo :: Int -> M Int
foo n | n == 0 = throwError "can't handle zero!"
| otherwise = return (100 `div` n)
add a comment |
up vote
3
down vote
accepted
Essentially, a constraint like Show a
constrains the type variable a
to represent a showable type. You seem to understand this.
A constraint of the form C a b
constrains the pair of type variables a
and b
. Intuitively, this constrains means that there is a relation between such types.
Consider this fictional class
class C a b where
sum :: a -> b -> (Int, b)
Intuitively, the constraint C a b
means that a
and b
can be sum
med together (in that order!), and the result of that sum will be a pair (Int, b)
.
In your case you are dealing with
class Monad m => MonadError e m | m -> e where
throwError :: e -> m a
Here, MonadError e m
expresses the following relation between e
and m
.
m
is a monad
e
is a type for which we can convert any valuex :: e
intothrowError x :: m a
, for anya
. Intuitively, this is an "error" type, representing the nature of some error, andthrowError
simply includes such value inside the monad- given
m
, there is only one error typee
. In other words, the relation is actually a function. This is expressed through a functional dependency... | m -> e
in the class above.
The simple version is: MonadError m a
means that m
is a monad which can express some "error values", of type e
.
For example, if we have MonadError M String
available, we can write
foo :: Int -> M Int
foo n | n == 0 = throwError "can't handle zero!"
| otherwise = return (100 `div` n)
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Essentially, a constraint like Show a
constrains the type variable a
to represent a showable type. You seem to understand this.
A constraint of the form C a b
constrains the pair of type variables a
and b
. Intuitively, this constrains means that there is a relation between such types.
Consider this fictional class
class C a b where
sum :: a -> b -> (Int, b)
Intuitively, the constraint C a b
means that a
and b
can be sum
med together (in that order!), and the result of that sum will be a pair (Int, b)
.
In your case you are dealing with
class Monad m => MonadError e m | m -> e where
throwError :: e -> m a
Here, MonadError e m
expresses the following relation between e
and m
.
m
is a monad
e
is a type for which we can convert any valuex :: e
intothrowError x :: m a
, for anya
. Intuitively, this is an "error" type, representing the nature of some error, andthrowError
simply includes such value inside the monad- given
m
, there is only one error typee
. In other words, the relation is actually a function. This is expressed through a functional dependency... | m -> e
in the class above.
The simple version is: MonadError m a
means that m
is a monad which can express some "error values", of type e
.
For example, if we have MonadError M String
available, we can write
foo :: Int -> M Int
foo n | n == 0 = throwError "can't handle zero!"
| otherwise = return (100 `div` n)
Essentially, a constraint like Show a
constrains the type variable a
to represent a showable type. You seem to understand this.
A constraint of the form C a b
constrains the pair of type variables a
and b
. Intuitively, this constrains means that there is a relation between such types.
Consider this fictional class
class C a b where
sum :: a -> b -> (Int, b)
Intuitively, the constraint C a b
means that a
and b
can be sum
med together (in that order!), and the result of that sum will be a pair (Int, b)
.
In your case you are dealing with
class Monad m => MonadError e m | m -> e where
throwError :: e -> m a
Here, MonadError e m
expresses the following relation between e
and m
.
m
is a monad
e
is a type for which we can convert any valuex :: e
intothrowError x :: m a
, for anya
. Intuitively, this is an "error" type, representing the nature of some error, andthrowError
simply includes such value inside the monad- given
m
, there is only one error typee
. In other words, the relation is actually a function. This is expressed through a functional dependency... | m -> e
in the class above.
The simple version is: MonadError m a
means that m
is a monad which can express some "error values", of type e
.
For example, if we have MonadError M String
available, we can write
foo :: Int -> M Int
foo n | n == 0 = throwError "can't handle zero!"
| otherwise = return (100 `div` n)
answered Nov 10 at 19:06
chi
71.6k279132
71.6k279132
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242275%2fmulti-parameter-context-constraints-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown