haskell - Defining bind for home-made monad transformer -
i have defined own version of writert along function unwrap it:
newtype wt w m = wt (m a, w) unwt :: (monoid w, monad m) => wt w m -> (m a, w) unwt (wt cmaw) = cmaw now trying define monad (wt w m), without success:
instance (monoid w, monad m) => monad (wt w m) return x = wt (return x, mempty) wtwma >>= fawtwmb = wt $ let (ma, w1) = unwt wtwma (mb, w2) = unwt $ <- ma fawtwmb in (mb, mappend w1 w2) error located in do-expression, @ point try extract a ma:
expected type: wt w m a, actual type: m i have tried few variations, similar outcome. unable define bind monad.
my main question is: if monad inside couple, how extract value?
imagine following computation:
tellline :: wt string io () tellline = input <- wt (getline, "") wt (return (), input) impossible :: string impossible = snd (unwt tellline) if works "expected", should impossible string user entered. however, cannot outside of io; such impossible not pure string here. not possible yield w recieved result of computation in monad.
another possibility return w occurred before first bind, don't have depend on monadic actions. alas, 1 of identity laws thwarts us.
return x >>= f = f x here can see "before first bind" cannot meaningful concept, since there bind on left not on right. returning w occurs before first bind surely violate monad law.
the remaining possibility w mempty. not work either, because of other identity law.
m >>= return = m so if m has non-mempty value of w, bind annihilate , violate law.
this wt cannote monad transformer. there writert transfomer, defined m (a,w) avoids these problems.
Comments
Post a Comment