Illegal instance declaration for 'Monad (Writer String)' [duplicate]












0















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)









share|improve this question















marked as duplicate by Daniel Wagner haskell
Users with the  haskell badge can single-handedly close haskell questions as duplicates and reopen them as needed.

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.




















    0















    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)









    share|improve this question















    marked as duplicate by Daniel Wagner haskell
    Users with the  haskell badge can single-handedly close haskell questions as duplicates and reopen them as needed.

    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.


















      0












      0








      0








      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)









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Daniel Wagner

      100k7157277




      100k7157277










      asked yesterday









      Jane

      226




      226




      marked as duplicate by Daniel Wagner haskell
      Users with the  haskell badge can single-handedly close haskell questions as duplicates and reopen them as needed.

      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 haskell
      Users with the  haskell badge can single-handedly close haskell questions as duplicates and reopen them as needed.

      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.


























          1 Answer
          1






          active

          oldest

          votes


















          7















          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.






          share|improve this answer





















          • 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


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7















          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.






          share|improve this answer





















          • 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
















          7















          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.






          share|improve this answer





















          • 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














          7












          7








          7







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 define instance 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










          • Yeah, that's what I meant. You need to first define instance 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



          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas