Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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. Re:  Why can't I print an IO Integer? (Petr V?penka)
   2.  duplicate monoids... (Mike Houghton)
   3. Re:  duplicate monoids... (Joel Williamson)
   4. Re:  duplicate monoids... (Mike Houghton)


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

Message: 1
Date: Tue, 20 Oct 2015 21:37:38 +0200
From: Petr V?penka <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Why can't I print an IO Integer?
Message-ID:
        <CANdMeUdt=trcasvrdfevyplmywrnmbku3311svcdn7pktps...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Using your wording, sequence makes from a `list of promises of integer` a
`promise of list of integers`.
You can get the value out of a promise using bind, ie using `<-` in do
notation as written below.

ioA :: IO Int
ioA = return 1

main :: IO ()
main = do
  a <- ioA
  print a

This may help: http://learnyouahaskell.com/input-and-output

On Tue, Oct 20, 2015 at 9:28 PM, Dan Stromberg <[email protected]> wrote:

>
> Please correct my inference if I'm wrong:
>
> An IO Integer is not an integer, it's a promise to read an Integer later.
> The "sequence" function tells the runtime it's time to make good on that
> promise.
>
> Sound about right?
>
> Thanks!
>
> On Tue, Oct 20, 2015 at 9:15 AM, Petr V?penka <[email protected]>
> wrote:
>
>> Hello Dan,
>>
>> `IO Integer` is something that, when executed, returns and `Integer` and
>> there is no instance of `Show` for `IO Integer` as the compiler says.
>>
>> You have to run the computations that will return the numbers and then
>> print them, like so:
>>
>> main :: IO ()
>> main = do
>>     let filenames = ["/etc/services"]
>>     let ioSizes = map get_size filenames :: [IO Integer]
>>     sizes <- sequence ioSizes
>>     mapM_ print sizes
>>
>> -- sequence :: Monad m => [m a] -> m [a]
>>
>> One important part is the use of sequence which transforms (ioSizes ::
>> [IO Integer]) to `IO [Integer]` that is run and the result bound to (sizes
>> : [Integer]).
>>
>> Hope that's clear enough to get the point :)
>>
>> Petr
>>
>> On Tue, Oct 20, 2015 at 5:58 PM, Dan Stromberg <[email protected]>
>> wrote:
>>
>>>
>>> Here's a small program that replicates the compilation issue I'm seeing:
>>>
>>> import qualified System.Posix.Files
>>>
>>> get_size :: String -> IO Integer
>>> get_size filename = do
>>>     file_status <- System.Posix.Files.getFileStatus filename
>>>     let file_size = System.Posix.Files.fileSize file_status
>>>     let integer_file_size = fromIntegral file_size
>>>     return integer_file_size
>>>
>>> main :: IO ()
>>> main = do
>>>     let filenames = ["/etc/services"]
>>>     let sizes = map get_size filenames
>>>     mapM_ print sizes
>>>
>>> The compilation error I get is:
>>>
>>> ghc -Wall --make -o stat2 stat2.hs
>>> [1 of 1] Compiling Main             ( stat2.hs, stat2.o )
>>>
>>> stat2.hs:15:11:
>>>     No instance for (Show (IO Integer)) arising from a use of `print'
>>>     Possible fix: add an instance declaration for (Show (IO Integer))
>>>     In the first argument of `mapM_', namely `print'
>>>     In a stmt of a 'do' block: mapM_ print sizes
>>>     In the expression:
>>>       do { let filenames = ...;
>>>            let sizes = map get_size filenames;
>>>            mapM_ print sizes }
>>> make: *** [stat2] Error 1
>>>
>>> I've googled quite a bit, and guessed quite a bit, and added type
>>> declarations some, but I'm still not converging on a solution.
>>>
>>> Why can't I print an IO Integer?
>>>
>>> Thanks!
>>>
>>> --
>>> Dan Stromberg
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Dan Stromberg
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151020/64c3d0f4/attachment-0001.html>

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

Message: 2
Date: Tue, 20 Oct 2015 22:24:10 +0100
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] duplicate monoids...
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hi,

I?m looking at a blog post on Monoids and finger trees at 
http://apfelmus.nfshost.com/articles/monoid-fingertree.html 
<http://apfelmus.nfshost.com/articles/monoid-fingertree.html>
and would appreciate  a bit of advice

I have  

type Size = Int
type Priority = Int 

instance Monoid Size where
    mempty  = 0
    mappend = (+)

instance Monoid Priority where
    mempty  = maxBound
    mappend = min


and I get compiler error

  Duplicate instance declarations:
      instance Monoid Size
        -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:60:10
      instance Monoid Priority
        -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:64:10

Which I can sort  of understand as Size and Priority are both Int but on the 
other hand, internally, the monoids  are different.

Is this genuinely incorrect code or is there a language extension to get around 
this?

Thanks

Mike



-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151020/f3cb10b4/attachment-0001.html>

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

Message: 3
Date: Tue, 20 Oct 2015 21:40:21 +0000
From: Joel Williamson <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] duplicate monoids...
Message-ID:
        <CAJGxSeoawD93T1f62uSPE8L9gnBnQJk4L4FB=ry-gktef6b...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

A type can only have a single instance of a given class. Imagine if this
weren't true. The compiler would have to guess which of the instances you
meant to use. The solution is to use newtype. That will introduce a
different type, allowing separate instances, but is optimised out so it
carries no runtime cost.

On Tue, 20 Oct 2015, 17:24 Mike Houghton <[email protected]>
wrote:

> Hi,
>
> I?m looking at a blog post on Monoids and finger trees at
> http://apfelmus.nfshost.com/articles/monoid-fingertree.html
> and would appreciate  a bit of advice
>
> I have
>
> type Size = Int
> type Priority = Int
>
> instance Monoid Size where
>     mempty  = 0
>     mappend = (+)
>
> instance Monoid Priority where
>     mempty  = maxBound
>     mappend = min
>
>
> and I get compiler error
>
>   Duplicate instance declarations:
>       instance Monoid Size
>         -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:60:10
>       instance Monoid Priority
>         -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:64:10
>
> Which I can sort  of understand as Size and Priority are both Int but on
> the other hand, internally, the monoids  are different.
>
> Is this genuinely incorrect code or is there a language extension to get
> around this?
>
> Thanks
>
> Mike
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151020/b491799e/attachment-0001.html>

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

Message: 4
Date: Tue, 20 Oct 2015 22:55:41 +0100
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] duplicate monoids...
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

Ahh! Nice. 
Thanks 

> On 20 Oct 2015, at 22:40, Joel Williamson <[email protected]> wrote:
> 
> A type can only have a single instance of a given class. Imagine if this 
> weren't true. The compiler would have to guess which of the instances you 
> meant to use. The solution is to use newtype. That will introduce a different 
> type, allowing separate instances, but is optimised out so it carries no 
> runtime cost.
> 
> 
> On Tue, 20 Oct 2015, 17:24 Mike Houghton <[email protected] 
> <mailto:[email protected]>> wrote:
> Hi,
> 
> I?m looking at a blog post on Monoids and finger trees at 
> http://apfelmus.nfshost.com/articles/monoid-fingertree.html 
> <http://apfelmus.nfshost.com/articles/monoid-fingertree.html>
> and would appreciate  a bit of advice
> 
> I have  
> 
> type Size = Int
> type Priority = Int 
> 
> instance Monoid Size where
>     mempty  = 0
>     mappend = (+)
> 
> instance Monoid Priority where
>     mempty  = maxBound
>     mappend = min
> 
> 
> and I get compiler error
> 
>   Duplicate instance declarations:
>       instance Monoid Size
>         -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:60:10
>       instance Monoid Priority
>         -- Defined at /Users/mike/haskell/FingerTrees/Ftree.hs:64:10
> 
> Which I can sort  of understand as Size and Priority are both Int but on the 
> other hand, internally, the monoids  are different.
> 
> Is this genuinely incorrect code or is there a language extension to get 
> around this?
> 
> Thanks
> 
> Mike
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> [email protected] <mailto:[email protected]>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners 
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151020/80137267/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 88, Issue 18
*****************************************

Reply via email to