Re: [Haskell-cafe] Re: Re: monad subexpressions

Sorry for the double post, I posted with the wrong email address and
haskell-cafe rejected it.
On 8/3/07, Neil Mitchell
Right. In effect, as a matter of fact, the notation
x <- a
would become equivalent to
let x = (<- a)
Hmm, interesting. Consider:
let x = 12 let x = (<- x)
Wouldn't that be forbidden ? I'd expect the x in ( <- x ) have to be of type m a. If you meant : x <- return 12 let x = ( <- x ) Then I imagine it would turn into x <- return 12 x >>= \tx -> let x = tx in .... Isn't that correct ?

david48
On 8/3/07, Neil Mitchell
wrote: Hmm, interesting. Consider:
let x = 12 let x = (<- x)
Wouldn't that be forbidden ?
I'd expect the x in ( <- x ) have to be of type m a.
Yes, unless of course you did: instance (Monad m, Num n) => Num (m n) or some such nonsense. :)
If you meant :
x <- return 12 let x = ( <- x )
This would be equally wrong. Perhaps you meant: do let x = return 12 let x = (<- x) ... Then this would become: do let x = return 12 t1 <- x let x = t1 ... Which is, in turn: let x = return 12 in x >>= (\t1 -> let x = t1 in ...) -- Chris Smith

On 8/3/07, Chris Smith
Yes, unless of course you did:
instance (Monad m, Num n) => Num (m n)
or some such nonsense. :)
I decided to take this as a dare - at first I thought it would be easy to declare (Monad m, Num n) => m n to be an instance of Num (just lift or return the operators as necessary), but I ran into trouble once I realized I needed two things I wasn't going to get: An instance of Eq (m n), and an instance of Show (m n) for all monads m. Eq would need a function of the form: (==) :: Monad m => m a -> m a -> Bool and Show would need a function of type m a -> String There's no way I'm getting a function of those types using return and join to operate on the monad. So, there went that idea. -Antoine

Antoine Latter wrote:
On 8/3/07, Chris Smith
wrote: Yes, unless of course you did:
instance (Monad m, Num n) => Num (m n)
or some such nonsense. :)
I decided to take this as a dare - at first I thought it would be easy to declare (Monad m, Num n) => m n to be an instance of Num (just lift or return the operators as necessary), but I ran into trouble once I realized I needed two things I wasn't going to get:
An instance of Eq (m n), and an instance of Show (m n) for all monads m. Eq would need a function of the form:
(==) :: Monad m => m a -> m a -> Bool
and Show would need a function of type m a -> String
What about Eq1 and Show1 classes? In the same vein as Typeable1:
class Eq1 f where eq1 :: Eq a => f a -> f a -> Bool neq1 :: Eq a => f a -> f a -> Bool
class Show1 f where show1 :: Show a => f a -> String showsPrec1 :: Show a => Int -> f a -> ShowS
Now you can declare the Num instance:
instance (Monad m, Eq1 m, Show1 m, Num n) => Num (m n) where (+) = liftM2 (+) (-) = liftM2 (-) (*) = liftM2 (*) abs = liftM abs signum = liftM signum negate = ligtM negate fromInteger = return . fromInteger
And just to show that such instances can exist:
instance Eq1 [] where eq1 = (==) neq1 = (/=)
instance Show1 [] where show1 = show showsPrec1 = showsPrec
Note: All of this is untested code. Twan
participants (4)
-
Antoine Latter
-
Chris Smith
-
david48
-
Twan van Laarhoven