"An environment contains local variable bindings, so no subcomputation will ever need to return its environment. " - That is not true. A subcomputation can possible modify an environment except the language forbids such a case.
On 06/30/2011 02:36 PM, Holger Siegel wrote: > Am 29.06.2011 um 23:50 schrieb Philipp Schneider: > >> Hi cafe, >> >> in my program i use a monad of the following type >> >> newtype M a = M (State -> (a, State)) >> >> i use the monad in two different ways. The type variable "a" can be a >> pair as in >> >> interp :: Term -> Environment -> M (Value,Environment) >> >> and it can be just a value as in >> >> type Environment = [(Name, Either Value (M Value))] > Simple rule: Never return an environment! > > An environment contains local variable bindings, so no subcomputation will > ever need to return its environment. I don't know anything about the language > your program interprets, but I'm sure that you can rewrite function interp as > > interp :: Term -> Environment -> M Value > > The structure of the interpreter will become clearer and your problem will > vanish. > > >> now in any case when i print the monad, i just want to print the value >> and never the environment. >> >> More specific i want to use somthing like the following >> >> instance (Show a,Show b) => Show (M (a,b)) where >> show (M f) = let ((v,_), s) = f 0 in >> "Value: " ++ show v ++ " Count: " ++ show s >> >> instance Show a => Show (M a) where >> show (M f) = let (v, s) = f 0 in >> "Value: " ++ show v ++ " Count: " ++ show s >> >> however this gives me the following error message: >> >> Overlapping instances for Show (M (Value, Environment)) >> arising from a use of `print' >> Matching instances: >> instance (Show a, Show b) => Show (M (a, b)) >> -- Defined at >> /home/phil/code/haskell/vorlesung/ue09/ue09-3c3.hs:78:10-42 >> instance Show a => Show (M a) >> -- Defined at >> /home/phil/code/haskell/vorlesung/ue09/ue09-3c3.hs:82:10-29 >> In a stmt of an interactive GHCi command: print it >> >> Any ideas how to fix it? Thanks! >> Philipp >> >> _______________________________________________ >> Haskell-Cafe mailing list >> Haskell-Cafe@haskell.org >> http://www.haskell.org/mailman/listinfo/haskell-cafe > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe