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.  Tyepclasses and creating 'Num' instances (Stuart Hungerford)
   2. Re:  Tyepclasses and creating 'Num' instances (Michael Orlitzky)
   3. Re:  Tyepclasses and creating 'Num' instances (Alex Hammel)
   4.  how to do this ? (Roelof Wobben)
   5. Re:  how to do this ? (Roelof Wobben)


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

Message: 1
Date: Tue, 24 Feb 2015 07:46:56 +1100
From: Stuart Hungerford <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Tyepclasses and creating 'Num' instances
Message-ID:
        <cag+kmrehjen6dfzmxeju4co1qds3vjp-tfcqqmhnx1bmjpt...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hi,

Many thanks to everyone who responded to my earlier question about
hierarchies of typeclasses. I have a followup question regarding
creating instances.  Suppose I have this additive semigroup typeclass:

class (Num a) => AddSemigroup a where
  (|+|) :: a -> a -> a
  (|+|) = (+)

I'd like to make 2-element tuples instances of AddSemigroup, provided
their elements are:

instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)

GHC says it "Could not deduce (Num (a, b))" in this situation, which
seems fair enough so I tried:

instance (AddSemigroup a, Num a, AddSemigroup b, Num b) => AddSemigroup (a, b)

With the same result.  Separate instances seem to work though:

instance (Num a, Num b) => Num (a, b)

instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)

At this point I feel like I must be duplicating instances that are
provided in a standard way in the prelude somewhere, or more likely
I've misunderstood the ideas around creating instances?

Any advice much appreciated,

Stu


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

Message: 2
Date: Mon, 23 Feb 2015 16:03:27 -0500
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Tyepclasses and creating 'Num'
        instances
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

On 02/23/2015 03:46 PM, Stuart Hungerford wrote:
> 
> instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)
> 
> GHC says it "Could not deduce (Num (a, b))" in this situation, which
> seems fair enough so I tried:
> 
> instance (AddSemigroup a, Num a, AddSemigroup b, Num b) => AddSemigroup (a, b)

Since you've defined the "AddSemigroup plus" to be the "Num plus", when
you attempt to make (a,b) an instance of AddSemigroup, GHC tries to use
the "Num plus" on them; i.e.

  (x1,y1) + (x2,y2) = ???

Since (a,b) isn't an instance of Num, it doesn't know what to do here.
It's obvious that you want,

  (x1,y1) + (x2,y2) = (x1+x2, y1+y2)

(which is fine, since both 'a' and 'b' are instance of Num), but you
could just as well declare,

  (x1,y1) + (x2,y2) = (x1*x2, y1*y2)

The compiler doesn't know, so declaring 'a' and 'b' as instances of Num
isn't enough. You really have to tell it how to add the pair.


> 
> With the same result.  Separate instances seem to work though:
> 
> instance (Num a, Num b) => Num (a, b)
> 
> instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)
> 

Now it knows how to add (x1,y1) and (x2,y2), since (a,b) is an instance
of Num. The compiler won't infer the pair instance from the individual
ones though.



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

Message: 3
Date: Mon, 23 Feb 2015 13:08:28 -0800
From: Alex Hammel <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Tyepclasses and creating 'Num'
        instances
Message-ID:
        <CA+_xFepwy1vwsLg=0vhmlbvew5t148o50xqgwhk6ey+_ave...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Be careful with that. I seem to recall a StackOverflow post where a really
confusing bug was eventually tracked down to a declaration of

instance Num (Double,Double)

somewhere in an imported package.

On Mon, Feb 23, 2015 at 1:03 PM, Michael Orlitzky <[email protected]>
wrote:

> On 02/23/2015 03:46 PM, Stuart Hungerford wrote:
> >
> > instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)
> >
> > GHC says it "Could not deduce (Num (a, b))" in this situation, which
> > seems fair enough so I tried:
> >
> > instance (AddSemigroup a, Num a, AddSemigroup b, Num b) => AddSemigroup
> (a, b)
>
> Since you've defined the "AddSemigroup plus" to be the "Num plus", when
> you attempt to make (a,b) an instance of AddSemigroup, GHC tries to use
> the "Num plus" on them; i.e.
>
>   (x1,y1) + (x2,y2) = ???
>
> Since (a,b) isn't an instance of Num, it doesn't know what to do here.
> It's obvious that you want,
>
>   (x1,y1) + (x2,y2) = (x1+x2, y1+y2)
>
> (which is fine, since both 'a' and 'b' are instance of Num), but you
> could just as well declare,
>
>   (x1,y1) + (x2,y2) = (x1*x2, y1*y2)
>
> The compiler doesn't know, so declaring 'a' and 'b' as instances of Num
> isn't enough. You really have to tell it how to add the pair.
>
>
> >
> > With the same result.  Separate instances seem to work though:
> >
> > instance (Num a, Num b) => Num (a, b)
> >
> > instance (AddSemigroup a, AddSemigroup b) => AddSemigroup (a, b)
> >
>
> Now it knows how to add (x1,y1) and (x2,y2), since (a,b) is an instance
> of Num. The compiler won't infer the pair instance from the individual
> ones though.
>
> _______________________________________________
> 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/20150223/ea2dc781/attachment-0001.html>

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

Message: 4
Date: Tue, 24 Feb 2015 10:28:28 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] how to do this ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed

Hello,

Im trying to solve exercise 2 of this course : 
http://www.seas.upenn.edu/~cis194/spring13/hw/02-ADTs.pdf

I have this till so far :

-- | Main entry point to the application.
{-# OPTIONS_GHC -Wall #-}

module LogAnalysis where

import           Data.Char (isDigit, isLetter)
import           Log

-- | checks if the String is of the format char int
isValid :: String -> Bool
isValid s =
   case words s of
     [a]:b:_ -> isLetter a && all isDigit b
     _       -> False

-- | Parse the String to make the right logmessage
parse_line :: String -> LogMessage
parse_line s =
    case words s of
         ("I":time:text)           -> LogMessage Info (read time) 
(unwords text)
         ("W":time:text)           -> LogMessage Warning (read time) 
(unwords text)
         ("E":errorcode:time:text) -> LogMessage (Error (read 
errorcode)) (read time) (unwords text)
         _                         ->  Unknown "This is not in the right 
format"


-- | here the actual function which uses isValid and parse to make the 
right log messages
parseMessage :: String -> LogMessage
parseMessage s =
     if  isValid(s) then parse_line(s) else Unknown "This is not the 
right format"


parse :: String -> [logMessage]
parse s =  parse_line s


-- | The main entry point.
main :: IO ()
main = do
     print $ show (parseMessage "I 4681 ehci 0xf43d000:15: regista14: 
[0xbffff 0xfed nosabled 00-02] Zonseres: brips byted nored)")
     print $ show (parseMessage "W 3654 e8] PGTT ASF! 00f00000003.2: 
0x000 - 0000: 00009dbfffec00000: Pround/f1743colled")
     print $ show (parseMessage "E 47 1034 'What a pity it wouldn't 
stay!' sighed the Lory, as soon as it was quite")

But I do not see how I can get the output of the parse_line into a List.

Can anyone give me a tip ?

Roelof





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

Message: 5
Date: Tue, 24 Feb 2015 10:44:39 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] how to do this ?
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed

Yes, that is one of the books I have to read.
For this exercises chapter 2 and 3 .

Roelof


Francesco Ariis schreef op 24-2-2015 om 10:37:
> off list: try to give a more meaningful title to your posts
> (i.e. "cis194 help exercise 2" instead of "how to do this").
> It might lead to more answers!
>
> Also, have you tried "Learn you a haskell for great good"?
>
>
> On Tue, Feb 24, 2015 at 10:28:28AM +0100, Roelof Wobben wrote:
>> Hello,
>>
>> Im trying to solve exercise 2 of this course :
>> http://www.seas.upenn.edu/~cis194/spring13/hw/02-ADTs.pdf
>>
>> I have this till so far :
>>
>> -- | Main entry point to the application.
>> {-# OPTIONS_GHC -Wall #-}
>>
>> module LogAnalysis where
>>
>> import           Data.Char (isDigit, isLetter)
>> import           Log
>>
>> -- | checks if the String is of the format char int
>> isValid :: String -> Bool
>> isValid s =
>>    case words s of
>>      [a]:b:_ -> isLetter a && all isDigit b
>>      _       -> False
>>
>> -- | Parse the String to make the right logmessage
>> parse_line :: String -> LogMessage
>> parse_line s =
>>     case words s of
>>          ("I":time:text)           -> LogMessage Info (read time)
>> (unwords text)
>>          ("W":time:text)           -> LogMessage Warning (read time)
>> (unwords text)
>>          ("E":errorcode:time:text) -> LogMessage (Error (read
>> errorcode)) (read time) (unwords text)
>>          _                         ->  Unknown "This is not in the
>> right format"
>>
>>
>> -- | here the actual function which uses isValid and parse to make
>> the right log messages
>> parseMessage :: String -> LogMessage
>> parseMessage s =
>>      if  isValid(s) then parse_line(s) else Unknown "This is not the
>> right format"
>>
>>
>> parse :: String -> [logMessage]
>> parse s =  parse_line s
>>
>>
>> -- | The main entry point.
>> main :: IO ()
>> main = do
>>      print $ show (parseMessage "I 4681 ehci 0xf43d000:15: regista14:
>> [0xbffff 0xfed nosabled 00-02] Zonseres: brips byted nored)")
>>      print $ show (parseMessage "W 3654 e8] PGTT ASF! 00f00000003.2:
>> 0x000 - 0000: 00009dbfffec00000: Pround/f1743colled")
>>      print $ show (parseMessage "E 47 1034 'What a pity it wouldn't
>> stay!' sighed the Lory, as soon as it was quite")
>>
>> But I do not see how I can get the output of the parse_line into a List.
>>
>> Can anyone give me a tip ?
>>
>> Roelof
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 80, Issue 64
*****************************************

Reply via email to