Illegal instance declaration for 'Monad (Writer String)' [duplicate]
This question already has an answer here:
“Illegal instance declaration” when declaring instance of IsString
2 answers
I've tried to create my own Writer type and afterwards I also made an instance for it. Anyway, I keep on getting this error:
Illegal instance declaration for ‘Monad (Writer String)’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for ‘Monad (Writer String)’
This is my code:
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
return a = Writer (a, "")
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 ++ log2)
haskell monads
marked as duplicate by Daniel Wagner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
“Illegal instance declaration” when declaring instance of IsString
2 answers
I've tried to create my own Writer type and afterwards I also made an instance for it. Anyway, I keep on getting this error:
Illegal instance declaration for ‘Monad (Writer String)’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for ‘Monad (Writer String)’
This is my code:
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
return a = Writer (a, "")
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 ++ log2)
haskell monads
marked as duplicate by Daniel Wagner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
“Illegal instance declaration” when declaring instance of IsString
2 answers
I've tried to create my own Writer type and afterwards I also made an instance for it. Anyway, I keep on getting this error:
Illegal instance declaration for ‘Monad (Writer String)’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for ‘Monad (Writer String)’
This is my code:
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
return a = Writer (a, "")
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 ++ log2)
haskell monads
This question already has an answer here:
“Illegal instance declaration” when declaring instance of IsString
2 answers
I've tried to create my own Writer type and afterwards I also made an instance for it. Anyway, I keep on getting this error:
Illegal instance declaration for ‘Monad (Writer String)’
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for ‘Monad (Writer String)’
This is my code:
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
return a = Writer (a, "")
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 ++ log2)
This question already has an answer here:
“Illegal instance declaration” when declaring instance of IsString
2 answers
haskell monads
haskell monads
edited yesterday
Daniel Wagner
100k7157277
100k7157277
asked yesterday
Jane
226
226
marked as duplicate by Daniel Wagner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Daniel Wagner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
All instance types must be of the form (T a1 ... an)
...meaning, you could write
instance Monad (Writer a) where ...
but not
instance Monad (Writer String) where ...
because String
is not a type variable.
This is just a silly restriction standard Haskell has had since Haskell98. Apparently, the restriction makes it easier to write a compiler, I don't know. Everybody uses the FlexibleInstances
extension, which has been in GHC for ages and disables the restriction.
{-# LANGUAGE FlexibleInstances #-}
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
...
Alternatively, you could use the more polymorphic instance, but Monad (Writer a)
doesn't quite work because you need to be able to have empty logs and concatenate logs. The standard solution is to invoke the generic class for concatenable types:
import Data.Monoid
instance Monoid a => Monad (Writer a) where
return a = Writer (a, mempty)
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 <> log2)
On another note, to have a Monad
instance you must first also instantiate Applicative
.
Right, thanks! Now I'm getting this one.. :)) "No instance for (Applicative (Writer String)) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad (Writer String)’ "
– Jane
yesterday
Yeah, that's what I meant. You need to first defineinstance Applicative (Writer String)
before you can have the monad instance.
– leftaroundabout
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
All instance types must be of the form (T a1 ... an)
...meaning, you could write
instance Monad (Writer a) where ...
but not
instance Monad (Writer String) where ...
because String
is not a type variable.
This is just a silly restriction standard Haskell has had since Haskell98. Apparently, the restriction makes it easier to write a compiler, I don't know. Everybody uses the FlexibleInstances
extension, which has been in GHC for ages and disables the restriction.
{-# LANGUAGE FlexibleInstances #-}
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
...
Alternatively, you could use the more polymorphic instance, but Monad (Writer a)
doesn't quite work because you need to be able to have empty logs and concatenate logs. The standard solution is to invoke the generic class for concatenable types:
import Data.Monoid
instance Monoid a => Monad (Writer a) where
return a = Writer (a, mempty)
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 <> log2)
On another note, to have a Monad
instance you must first also instantiate Applicative
.
Right, thanks! Now I'm getting this one.. :)) "No instance for (Applicative (Writer String)) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad (Writer String)’ "
– Jane
yesterday
Yeah, that's what I meant. You need to first defineinstance Applicative (Writer String)
before you can have the monad instance.
– leftaroundabout
yesterday
add a comment |
All instance types must be of the form (T a1 ... an)
...meaning, you could write
instance Monad (Writer a) where ...
but not
instance Monad (Writer String) where ...
because String
is not a type variable.
This is just a silly restriction standard Haskell has had since Haskell98. Apparently, the restriction makes it easier to write a compiler, I don't know. Everybody uses the FlexibleInstances
extension, which has been in GHC for ages and disables the restriction.
{-# LANGUAGE FlexibleInstances #-}
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
...
Alternatively, you could use the more polymorphic instance, but Monad (Writer a)
doesn't quite work because you need to be able to have empty logs and concatenate logs. The standard solution is to invoke the generic class for concatenable types:
import Data.Monoid
instance Monoid a => Monad (Writer a) where
return a = Writer (a, mempty)
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 <> log2)
On another note, to have a Monad
instance you must first also instantiate Applicative
.
Right, thanks! Now I'm getting this one.. :)) "No instance for (Applicative (Writer String)) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad (Writer String)’ "
– Jane
yesterday
Yeah, that's what I meant. You need to first defineinstance Applicative (Writer String)
before you can have the monad instance.
– leftaroundabout
yesterday
add a comment |
All instance types must be of the form (T a1 ... an)
...meaning, you could write
instance Monad (Writer a) where ...
but not
instance Monad (Writer String) where ...
because String
is not a type variable.
This is just a silly restriction standard Haskell has had since Haskell98. Apparently, the restriction makes it easier to write a compiler, I don't know. Everybody uses the FlexibleInstances
extension, which has been in GHC for ages and disables the restriction.
{-# LANGUAGE FlexibleInstances #-}
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
...
Alternatively, you could use the more polymorphic instance, but Monad (Writer a)
doesn't quite work because you need to be able to have empty logs and concatenate logs. The standard solution is to invoke the generic class for concatenable types:
import Data.Monoid
instance Monoid a => Monad (Writer a) where
return a = Writer (a, mempty)
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 <> log2)
On another note, to have a Monad
instance you must first also instantiate Applicative
.
All instance types must be of the form (T a1 ... an)
...meaning, you could write
instance Monad (Writer a) where ...
but not
instance Monad (Writer String) where ...
because String
is not a type variable.
This is just a silly restriction standard Haskell has had since Haskell98. Apparently, the restriction makes it easier to write a compiler, I don't know. Everybody uses the FlexibleInstances
extension, which has been in GHC for ages and disables the restriction.
{-# LANGUAGE FlexibleInstances #-}
newtype Writer log a = Writer {runWriter :: (a,log)}
instance Monad (Writer String) where
...
Alternatively, you could use the more polymorphic instance, but Monad (Writer a)
doesn't quite work because you need to be able to have empty logs and concatenate logs. The standard solution is to invoke the generic class for concatenable types:
import Data.Monoid
instance Monoid a => Monad (Writer a) where
return a = Writer (a, mempty)
ma >>= k = let (a, log1) = runWriter ma
(b, log2) = runWriter (k a)
in Writer (b, log1 <> log2)
On another note, to have a Monad
instance you must first also instantiate Applicative
.
answered yesterday
leftaroundabout
78.8k3117233
78.8k3117233
Right, thanks! Now I'm getting this one.. :)) "No instance for (Applicative (Writer String)) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad (Writer String)’ "
– Jane
yesterday
Yeah, that's what I meant. You need to first defineinstance Applicative (Writer String)
before you can have the monad instance.
– leftaroundabout
yesterday
add a comment |
Right, thanks! Now I'm getting this one.. :)) "No instance for (Applicative (Writer String)) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad (Writer String)’ "
– Jane
yesterday
Yeah, that's what I meant. You need to first defineinstance Applicative (Writer String)
before you can have the monad instance.
– leftaroundabout
yesterday
Right, thanks! Now I'm getting this one.. :)) "No instance for (Applicative (Writer String)) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad (Writer String)’ "
– Jane
yesterday
Right, thanks! Now I'm getting this one.. :)) "No instance for (Applicative (Writer String)) arising from the superclasses of an instance declaration In the instance declaration for ‘Monad (Writer String)’ "
– Jane
yesterday
Yeah, that's what I meant. You need to first define
instance Applicative (Writer String)
before you can have the monad instance.– leftaroundabout
yesterday
Yeah, that's what I meant. You need to first define
instance Applicative (Writer String)
before you can have the monad instance.– leftaroundabout
yesterday
add a comment |