Combining IO and Either function to "EitherT e IO a"

Hey, I have a function |func1 :: IO String | and another: |func2 :: String -> Either String String | and I want to combine them, giving the output of the first as the input as the second. |func3 :: IO (Either String String) func3 = do tmp <- func1 return (func2 tmp) | Ok, possible. But I rather would like a result of type "EitherT String IO String". So how can I combine these function in a smart way, to get the needed result? Thanks! Nathan

On 05/03/14 13:53, Nathan Hüsken wrote:
Hey,
I have a function
|func1 :: IO String |
and another:
|func2 :: String -> Either String String |
and I want to combine them, giving the output of the first as the input as the second.
|func3 :: IO (Either String String) func3 = do tmp <- func1 return (func2 tmp) |
This is just ‘fmap func2 func1’ or using the operator, ‘func2 <$> func1’.
Ok, possible. But I rather would like a result of type "EitherT String IO String". So how can I combine these function in a smart way, to get the needed result?
After using ‘func2 <$> func1’ we have ‘IO (Either String String)’ as you point out. The ‘EitherT’ constructor has type ‘m (Either e a)’. Here if m = IO, e = String, a = String so we have exactly what we need:
:t EitherT $ func2 <$> func1 EitherT $ func2 <$> func1 :: EitherT String IO String
Thanks! Nathan
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Mateusz K.

On 05/03/14 14:31, Mateusz Kowalczyk wrote:
On 05/03/14 13:53, Nathan Hüsken wrote:
Hey,
I have a function
|func1 :: IO String |
and another:
|func2 :: String -> Either String String |
and I want to combine them, giving the output of the first as the input as the second.
|func3 :: IO (Either String String) func3 = do tmp <- func1 return (func2 tmp) |
This is just ‘fmap func2 func1’ or using the operator, ‘func2 <$> func1’.
Ok, possible. But I rather would like a result of type "EitherT String IO String". So how can I combine these function in a smart way, to get the needed result?
After using ‘func2 <$> func1’ we have ‘IO (Either String String)’ as you point out. The ‘EitherT’ constructor has type ‘m (Either e a)’.
Oops, that meant to say that it takes a sole argument of that type.
Here if m = IO, e = String, a = String so we have exactly what we need:
:t EitherT $ func2 <$> func1 EitherT $ func2 <$> func1 :: EitherT String IO String
Thanks! Nathan
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Mateusz K.

Mmh, I might not have used haskell terminology correctly. Its a function, in the sense of a function of an imperative language ... Anyhow, the tip from Mateusz worked helped me a lot, thanks for that! On 03/05/2014 05:45 PM, Kim-Ee Yeoh wrote:
On Wed, Mar 5, 2014 at 8:53 PM, Nathan Hüsken
mailto:nathan.huesken@posteo.de> wrote: I have a function
|func1 :: IO String|
How is func1 a function?
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Mar 6, 2014 at 12:49 AM, Nathan Hüsken
Mmh, I might not have used haskell terminology correctly. Its a function, in the sense of a function of an imperative language ...
Yes, you could model IO String in C as a function taking void and returning a pointer to char. Calling an IO String a Haskell function would confuse a lot of people. -- Kim-Ee

Kim-Ee,
If I see a function with the signature ":: IO String" I immediately assume
it is getLine or one of its ilk. Thus the Op's question doesn't seem odd at
all to me. Could you explicate on why you find this very confusing? After
all:
:Prelude> :t getLine
getLine :: IO String
I think you know a lot more about Haskell than I do so I'm curious what the
thinking behind your post was.
Thanks,
Tim
On Wed, Mar 5, 2014 at 10:18 AM, Kim-Ee Yeoh
On Thu, Mar 6, 2014 at 12:49 AM, Nathan Hüsken
wrote: Mmh, I might not have used haskell terminology correctly. Its a function, in the sense of a function of an imperative language ...
Yes, you could model IO String in C as a function taking void and returning a pointer to char.
Calling an IO String a Haskell function would confuse a lot of people.
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

In Haskell, the term "function" is reserved for things whose types include an arrow, ->. IO String has no -> in it, therefore it is not a function. Instead we might call it an "action" or a "procedure". -Brent On Wed, Mar 05, 2014 at 03:19:48PM -0800, Tim Perry wrote:
Kim-Ee,
If I see a function with the signature ":: IO String" I immediately assume it is getLine or one of its ilk. Thus the Op's question doesn't seem odd at all to me. Could you explicate on why you find this very confusing? After all:
:Prelude> :t getLine getLine :: IO String
I think you know a lot more about Haskell than I do so I'm curious what the thinking behind your post was.
Thanks,
Tim
On Wed, Mar 5, 2014 at 10:18 AM, Kim-Ee Yeoh
wrote: On Thu, Mar 6, 2014 at 12:49 AM, Nathan Hüsken
wrote: Mmh, I might not have used haskell terminology correctly. Its a function, in the sense of a function of an imperative language ...
Yes, you could model IO String in C as a function taking void and returning a pointer to char.
Calling an IO String a Haskell function would confuse a lot of people.
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

It's a function of zero arguments. It's also sort of, kind of a function
from RealWorld to (String, RealWorld) if you squint.
On Wed, Mar 5, 2014 at 10:18 AM, Kim-Ee Yeoh
On Thu, Mar 6, 2014 at 12:49 AM, Nathan Hüsken
wrote: Mmh, I might not have used haskell terminology correctly. Its a function, in the sense of a function of an imperative language ...
Yes, you could model IO String in C as a function taking void and returning a pointer to char.
Calling an IO String a Haskell function would confuse a lot of people.
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wed, Mar 05, 2014 at 03:22:39PM -0800, David Thomas wrote:
It's a function of zero arguments.
This is really stretching the meaning of the word "function" beyond any reasonable, useful definition, in my opinion. See the discussion here: http://conal.net/blog/posts/everything-is-a-function-in-haskell A much more useful (and precise) point of view is that in Haskell, all functions take exactly one argument. Under that view, of course, there is no such thing as a function of zero arguments.
It's also sort of, kind of a function from RealWorld to (String, RealWorld) if you squint.
I'm don't think this is a very helpful way to understand IO, though it is probably closer to the sense in which the OP understood IO String to be a function. My guess is that what is really going on here is a confusion of terminology from imperative languages, some of which use the word "function" for everything, whether they take any arguments or not, return any values or not, have any side effects or not, etc. -Brent
On Wed, Mar 5, 2014 at 10:18 AM, Kim-Ee Yeoh
wrote: On Thu, Mar 6, 2014 at 12:49 AM, Nathan Hüsken
wrote: Mmh, I might not have used haskell terminology correctly. Its a function, in the sense of a function of an imperative language ...
Yes, you could model IO String in C as a function taking void and returning a pointer to char.
Calling an IO String a Haskell function would confuse a lot of people.
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Agreed on all counts.
On Wed, Mar 5, 2014 at 3:35 PM, Brent Yorgey
On Wed, Mar 05, 2014 at 03:22:39PM -0800, David Thomas wrote:
It's a function of zero arguments.
This is really stretching the meaning of the word "function" beyond any reasonable, useful definition, in my opinion. See the discussion here:
http://conal.net/blog/posts/everything-is-a-function-in-haskell
A much more useful (and precise) point of view is that in Haskell, all functions take exactly one argument. Under that view, of course, there is no such thing as a function of zero arguments.
It's also sort of, kind of a function from RealWorld to (String, RealWorld) if you squint.
I'm don't think this is a very helpful way to understand IO, though it is probably closer to the sense in which the OP understood IO String to be a function.
My guess is that what is really going on here is a confusion of terminology from imperative languages, some of which use the word "function" for everything, whether they take any arguments or not, return any values or not, have any side effects or not, etc.
-Brent
On Wed, Mar 5, 2014 at 10:18 AM, Kim-Ee Yeoh
wrote: On Thu, Mar 6, 2014 at 12:49 AM, Nathan Hüsken <
nathan.huesken@posteo.de>wrote:
Mmh, I might not have used haskell terminology correctly. Its a
function,
in the sense of a function of an imperative language ...
Yes, you could model IO String in C as a function taking void and returning a pointer to char.
Calling an IO String a Haskell function would confuse a lot of people.
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thanks Brent. Your posts and the link helped.
On Wed, Mar 5, 2014 at 3:38 PM, David Thomas
Agreed on all counts.
On Wed, Mar 5, 2014 at 3:35 PM, Brent Yorgey
wrote: On Wed, Mar 05, 2014 at 03:22:39PM -0800, David Thomas wrote:
It's a function of zero arguments.
This is really stretching the meaning of the word "function" beyond any reasonable, useful definition, in my opinion. See the discussion here:
http://conal.net/blog/posts/everything-is-a-function-in-haskell
A much more useful (and precise) point of view is that in Haskell, all functions take exactly one argument. Under that view, of course, there is no such thing as a function of zero arguments.
It's also sort of, kind of a function from RealWorld to (String, RealWorld) if you squint.
I'm don't think this is a very helpful way to understand IO, though it is probably closer to the sense in which the OP understood IO String to be a function.
My guess is that what is really going on here is a confusion of terminology from imperative languages, some of which use the word "function" for everything, whether they take any arguments or not, return any values or not, have any side effects or not, etc.
-Brent
On Wed, Mar 5, 2014 at 10:18 AM, Kim-Ee Yeoh
wrote: On Thu, Mar 6, 2014 at 12:49 AM, Nathan Hüsken <
nathan.huesken@posteo.de>wrote:
Mmh, I might not have used haskell terminology correctly. Its a
function,
in the sense of a function of an imperative language ...
Yes, you could model IO String in C as a function taking void and returning a pointer to char.
Calling an IO String a Haskell function would confuse a lot of people.
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (6)
-
Brent Yorgey
-
David Thomas
-
Kim-Ee Yeoh
-
Mateusz Kowalczyk
-
Nathan Hüsken
-
Tim Perry