Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Get things from IO monad ([email protected])
   2. Re:  Get things from IO monad (Antoine Latter)
   3. Re:  Get things from IO monad (aditya siram)
   4. Re:  Get things from IO monad (Felipe Almeida Lessa)
   5.  Making a generic interpreter (Erik Helin)
   6. Re:  Get things from IO monad (Daniel Fischer)
   7. Re:  Get things from IO monad (Ertugrul Soeylemez)


----------------------------------------------------------------------

Message: 1
Date: Thu, 05 May 2011 22:48:17 +0000
From: [email protected]
Subject: [Haskell-beginners] Get things from IO monad
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed";
        DelSp="yes"

Hi,

I have a silly question, Can i get things out of IO monad?

for example, function 'anotherFun' doesn't work.

genFunc :: IO String

anotherFunc :: (Monad m) => m String
-- m is a complex monad and is not an IO monad
-- m is not an instance of MonadIO so liftIO will not work
anotherFunc = do
genStr <- genFunc
return genStr


I just want to get something from IO monad and wrap it with some other  
monad. Is there anyway to do that? Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110505/20abac1e/attachment-0001.htm>

------------------------------

Message: 2
Date: Thu, 5 May 2011 22:22:34 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Get things from IO monad
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, May 5, 2011 at 5:48 PM,  <[email protected]> wrote:
> Hi,
>
> I have a silly question, Can i get things out of IO monad?
>
> for example, function 'anotherFun' doesn't work.
>
> genFunc :: IO String
>
> anotherFunc :: (Monad m) => m String
> -- m is a complex monad and is not an IO monad
> -- m is not an instance of MonadIO so liftIO will not work
> anotherFunc = do
> genStr <- genFunc
> return genStr
>
>
> I just want to get something from IO monad and wrap it with some other
> monad. Is there anyway to do that? Thanks.

Not being able to do that is the entire point of the IO monad.

What are you trying to do?

Antoine


> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



------------------------------

Message: 3
Date: Thu, 5 May 2011 22:25:46 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Get things from IO monad
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Generally every function that uses something in the IO monad *has* to
return something in the IO monad or a monad transformer that has IO at
its base.

This is so that you can track any function that could potentially have
some side-effect just by looking at the signature.

There is a function called "unsafePerformIO" which subverts this by
turning an 'IO a' into an 'a', but it's not really meant for general
use.

This frustrated me in the beginning too, but I found that if I was
using 'unsafePerformIO' too much the design was probably bad.
-deech

On Thu, May 5, 2011 at 5:48 PM,  <[email protected]> wrote:
> Hi,
>
> I have a silly question, Can i get things out of IO monad?
>
> for example, function 'anotherFun' doesn't work.
>
> genFunc :: IO String
>
> anotherFunc :: (Monad m) => m String
> -- m is a complex monad and is not an IO monad
> -- m is not an instance of MonadIO so liftIO will not work
> anotherFunc = do
> genStr <- genFunc
> return genStr
>
>
> I just want to get something from IO monad and wrap it with some other
> monad. Is there anyway to do that? Thanks.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



------------------------------

Message: 4
Date: Fri, 6 May 2011 00:28:35 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Get things from IO monad
To: aditya siram <[email protected]>
Cc: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Fri, May 6, 2011 at 12:25 AM, aditya siram <[email protected]> wrote:
> There is a function called "unsafePerformIO" which subverts this by
> turning an 'IO a' into an 'a', but it's not really meant for general
> use.
>
> This frustrated me in the beginning too, but I found that if I was
> using 'unsafePerformIO' too much the design was probably bad.

If you are a beginner, you should never use anything that starts with
'unsafe'.  And you should *never* use 'unsafePerformIO'.  And you
should *NEVER* use 'unsafeCoerce'.

Cheers,  =)

-- 
Felipe.



------------------------------

Message: 5
Date: Fri, 6 May 2011 09:01:35 +0200
From: Erik Helin <[email protected]>
Subject: [Haskell-beginners] Making a generic interpreter
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I have made the following (very small and basic) interpter:

data Code = PUSH Integer
                 | ADD
                 | BRANCH [Code] [Code]
                 | CMP
                 deriving (Show)

data StackElement = StackInteger Integer
                              | StackBool Bool
                              deriving (Show)
type Stack = [StackElement]

eval :: [Code] -> Stack -> Stack
eval [] s = s
eval (PUSH n:t) s =  eval t (StackInteger n:s)
eval (ADD:t) (StackInteger x:StackInteger y:s) = eval t (StackInteger (x + y):s)
eval (CMP:t) (StackInteger x:StackInteger y:s) = eval t (StackBool (x == y):s)
eval (BRANCH c1 c2:t) (StackBool cond:s) = if cond then eval (c1 ++ t) s

      else eval (c2 ++ t) s

Now, I would like to change StackElement to be able to work with any
types, that is:

data StackElement a b = StackInteger a
                                   | StackInteger b
                                   deriving (Show)

How would I make this change?

I thought about using typeclasses, but since type a and type b in
StackElement a b clearly are related (the == operation on type a must
result in type b), I need to use multi-parameter type classes (and
maybe also functional dependecies?).

I also thought about introducing a new type Operation:

data Operation a b = Operation { cmp :: a -> a -> b, add :: a -> a ->
a, (more required functions) }

Is any of these solutions the preferred one, or is there another
solution that I don't know of (this is very likely, I'm completely new
to Haskell)?

Thanks for taking your time and reading this lengthy post,
Erik



------------------------------

Message: 6
Date: Fri, 6 May 2011 09:46:39 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Get things from IO monad
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Friday 06 May 2011 05:28:35, Felipe Almeida Lessa wrote:
> If you are a beginner, you should never use anything that starts with
> 'unsafe'.

Basically yes, use something beginning with 'unsafe' only when you know why 
it's not unsafe in your specific case.

But of course there are different levels of unsafeness, 
Data.Array.Base.unsafeAt for example is relatively benign (same as in other 
languages: if you don't let the compiler check array bounds and screw your 
check up, you can get nonsense or segfaults, but it's relatively easy to 
understand and control).

> And you should never use 'unsafePerformIO'.  And you
> should NEVER use 'unsafeCoerce'.

Those two indeed have a different calibre, use them only 
whenYouReallyKnowWhatYouAreDoing.

They have their uses and sometimes are the right thing, but like goto, in 
most cases one is tempted to use them, one shouldn't.

> 
> Cheers,  =)
> 
> -- 
> Felipe.



------------------------------

Message: 7
Date: Fri, 6 May 2011 11:41:38 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] Get things from IO monad
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

[email protected] wrote:

> I just want to get something from IO monad and wrap it with some other
> monad. Is there anyway to do that? Thanks.

You've got quite a "just" there.  This is conceptually impossible,
because it does not make sense.  There is no "some other monad", because
every monad has a precise, significant meaning and in general you cannot
just cast values from one type to another.  If conversion is possible,
there needs to be a specific conversion function, called a natural
transformation, which knows the semantics of both monads.  For all 'a'
and 'm', it is in general possible to convert values of type 'IO a' to
'MonadIO m => m a'.  The natural transformation for that is called
liftIO.

What you can try to do is to find a transforming variant of whatever
monad you are working in.  So instead of MyMonad use MyMonadT IO.

It is a common wrong interpretation that you want to "get out of" the IO
monad, what technically means:  You want to convert IO Something to just
Something.  There is indeed a combinator for accessing that Something,
but it has not the type you would expect.  It's called (>>=) and has the
following type signature:

  (>>=) :: Monad m => m a -> (a -> m b) -> m b

In this case:

  (>>=) :: IO a -> (a -> IO b) -> IO b

Just look at the type and think about how you can use this function to
do what you want.  It converts your IO Something to the Something, which
you want, but only for a certain function, which is its second argument.

Why do people want to get out of the IO monad?  Because they interpret
the arrow as "going to", and there is a general 'a -> IO a' function
(called 'return'), but not an 'IO a -> a' function.  A function of this
type cannot exist (or it can, but it cannot do anything useful,
particularly not what you want).  But it's also not needed, because you
have (>>=), which is consistent with Haskell's type system and the
semantics of IO.

Less relevantly, let me also support what Felipe said.  Aditya Siram's
statement is very misleading, because it suggests that using
unsafePerformIO is fine, if you don't overuse it.  Wrong!  Using it to
cast between types is *wrong* and *will* get you into trouble (in this
case you are casting from IO Something to Something).  Do not use any of
the "unsafe" functions, until you understand why you do *not* need them.
At that point you may start using them.  I'm programming in Haskell for
a few years now, and I have used unsafePerformIO exactly once.


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 35, Issue 11
*****************************************

Reply via email to