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. Re:  Parallel mutation (Jon Harrop)
   2. Re:  Fwd: Type constraints question (Daniel Fischer)
   3. Re:  print all days of calendar (Chadda? Fouch?)
   4.  `coarbitrary' is not a (visible) method of class `Arbitrary'
      (Amy de Buitl?ir)
   5.  Pure Regex.PCRE Possible? (PLEAC, Fix Style) (Volkan YAZICI)
   6. Re:  `coarbitrary' is not a (visible) method of   class
      `Arbitrary' (Daniel Fischer)
   7. Re:  `coarbitrary' is not a (visible) method of   class
      `Arbitrary' (Amy de Buitl?ir)
   8. Re:  `coarbitrary' is not a (visible) method of   class
      `Arbitrary' (Daniel Fischer)
   9. Re:  Pure Regex.PCRE Possible? (PLEAC, Fix Style) (Stephen Tetley)


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

Message: 1
Date: Sat, 19 Dec 2009 01:52:54 +0000
From: Jon Harrop <[email protected]>
Subject: Re: [Haskell-beginners] Parallel mutation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Thursday 10 December 2009 01:00:55 Erik de Castro Lopo wrote:
> Erik de Castro Lopo wrote:
> > So why not look at Data Parallel Haskell:
> >
> >     http://www.haskell.org/haskellwiki/GHC/Data_Parallel_Haskell
>
> Quicksort example:
>
>    http://darcs.haskell.org/ghc-6.10/packages/dph/examples/qsort/
>
> I haven't tested this.

It isn't even a real quicksort => it will be orders of magnitude slower than a 
real quicksort.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


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

Message: 2
Date: Sat, 19 Dec 2009 02:15:10 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Fwd: Type constraints question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-15"

Am Samstag 19 Dezember 2009 00:55:34 schrieb David Gordon:
> Apologies for the spam, I had some difficulty getting my email address set
> up right on the list and wanted to make sure this finally got through (or
> perhaps there are like 5 copies on the list now... sorry.)
>
> thanks
>
> ---------- Forwarded message ----------
>
> Ok, now I will try asking the right question (previous try wasn't actually
> an example of the problem I'm having)
>
> Here's the code:
>
>
> data Test = Test
>
> data Test2 = Test2
>
> class MyClass a where
>     getChild :: MyClass b => a -> b
>
> instance MyClass Test where
>     getChild a = Test2
>
> instance MyClass Test2 where
>     getChild a = Test
>
> On HUGS I get:
>
> Error occurred
> ERROR line 9 - Inferred type is not general enough
> *** Expression    : getChild
> *** Expected type : (MyClass Test, MyClass a) => Test -> a
> *** Inferred type : (MyClass Test, MyClass Test2) => Test -> Test2
>
> So, what's the problem with always returning a particular instance of
> MyClass? I just want to constrain it to be an instance of MyClass, nothing
> more.

The signature of getChild promises that *whichever type the caller wants*, as 
long as it's 
a member of MyClass, it can be provided. The implementation returns one 
specific type.

It's different from interfaces in Java, where the callee decides which type is 
returned, 
here the caller can demand the type that the callee has to return.

The signature of getChild is actually

getChild :: forall b. MyClass b => a -> b

while the implementation has the signature (not legal Haskell)

getChild :: exists b. MyClass b => a -> b.

What you want is, I believe, that each instance a of MyClass specifies a type b 
belonging 
to MyClass which getChild returns.
You can achieve part of that for example with a multiparameter type class

{-# LANGUAGE MultiParamTypeClasses #-}

class MyClass2 a b where
    getChild :: a -> b

However, this allows that a has children of more than one type (

instance MyClass2 Test Test2 where
    getChild _ = Test2

instance MyClass2 Test Test where
    getChild _ = Test

) and this doesn't enforce that b itself has children.

To enforce that every type has only one type of children, you can use 
functional 
dependencies (or type families, see below):

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}

class MyClass3 a b | a -> b where
    getChild :: a -> b

The functional dependency "a -> b" (separated from the class head by "|") says 
that a 
uniquely determines b. But that still doesn't enforce that b has children. For 
that, you 
need

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleContexts #-}

class MyClass4 a b | a -> b where
    getChild :: MyClass4 b c => a -> b

Now each type can have only one type of children and they must have children 
too.

Another way to achieve the above is via type families:

{-# LANGUAGE TypeFamilies, FlexibleContexts #-}

class MyClass5 a where
    type Child a :: *
    getChild :: (MyClass5 (Child a)) => a -> Child a

instance MyClass5 Test where
    type Child Test = Test2
    getChild _ = Test2

instance MyClass5 Test2 where
    type Child Test2 = Test
    getChild _ = Test



>
> thanks,
>
> David
>
>
> 2009/12/18 David Gordon <[email protected]>
>
> > Hi Folks,
> > Total newbie here. I don't know if I am having a syntactic problem or a
>
> conceptual problem.
>
> > This code:
> > data Test = Test
> > data Test2 = Test2
> > class MyClass a where
> >     getChild :: (MyClass b) => a -> b
> > instance MyClass Test where
> >     getChild = Test2
> > instance MyClass Test2 where
> >     getChild = Test
> > results in:
> > [1 of 1] Compiling Main             ( test.hs, interpreted )
> > test.hs:10:15:
> >     Couldn't match expected type `Test -> b'
> >            against inferred type `Test2'
> >     In the expression: Test2
> >     In the definition of `getChild': getChild = Test2
> >     In the instance declaration for `MyClass Test'
> > test.hs:13:15:
> >     Couldn't match expected type `Test2 -> b'
> >            against inferred type `Test'
> >     In the expression: Test
> >     In the definition of `getChild': getChild = Test
> >     In the instance declaration for `MyClass Test2'
> > Failed, modules loaded: none.
> > Is this a reasonable thing to try and do in Haskell? If not, I have a lot
>
> more questions... ;)
>
> > many thanks,
> > David



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

Message: 3
Date: Sat, 19 Dec 2009 11:16:45 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] print all days of calendar
To: [email protected]
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Fri, Dec 18, 2009 at 5:01 PM,  <[email protected]> wrote:
> I also have a function from a previous exercise which checks if a given date 
> is valid. Is there a function for a loop that iterates from 1 to n and checks 
> if the date is valid. If it's valid it should return the date otherwise it 
> should jump to the next month or end at the end of the year? Or is it better 
> to do it on another way with this data I have?

Since you derived Enum for Month, you can do [Jan..Dec] and get the
list of the months in order. There are then two options, either you
generate all cartesian product of [Jan..Dec] and [1..31] and check
which are valid, or you write a function that for a given month and
year tells you how many days it counts and then generate for month "m"
all the pair in combination with [1..daysCount m].

Whatever your decision, list comprehensions are probably the tool of
choice to do it though it is by no mean harder to do without.

-- 
Jedaï


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

Message: 4
Date: Sat, 19 Dec 2009 13:20:43 +0000
From: Amy de Buitl?ir <[email protected]>
Subject: [Haskell-beginners] `coarbitrary' is not a (visible) method
        of class        `Arbitrary'
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I'm trying to follow the instructions in
http://haskell.org/haskellwiki/How_to_write_a_Haskell_program. I'm at
the step where we've just written a QuickCheck test:

a...@localhost ~/forbairt/haq $ cat Tests.hs
import Char
import List
import Test.QuickCheck
import Text.Printf

main  = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests

instance Arbitrary Char where
    arbitrary     = choose ('\0', '\128')
    coarbitrary c = variant (ord c `rem` 4)

-- reversing twice a finite list, is the same as identity
prop_reversereverse s = (reverse . reverse) s == id s
    where _ = s :: [Int]

-- and add this to the tests list
tests  = [("reverse.reverse/id", test prop_reversereverse)]


But when I try to run it, I get the following errors:

a...@localhost ~/forbairt/haq $ runhaskell Tests.hs

Tests.hs:10:4:
    `coarbitrary' is not a (visible) method of class `Arbitrary'

Tests.hs:17:33: Not in scope: `test'


I think this has something to do with the changes between QuickCheck
v1 and v2. Can someone tell me what I need to do to make this example
work? Thank you in advance!

Amy


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

Message: 5
Date: Sat, 19 Dec 2009 15:37:45 +0200
From: Volkan YAZICI <[email protected]>
Subject: [Haskell-beginners] Pure Regex.PCRE Possible? (PLEAC, Fix
        Style)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi,

I'm trying to implement FixStyle[1] program of PLEAC[2] for Haskell.
(It's missing in the Haskell version of PLEAC.) For this purpose, I
needed regular expressions. (Now, I have two problems!)  Despite, my
code is, IMHO, quite purely functional, Regex.PCRE.String functions
totally mess up the design with impure functions they introduce even for
really simple string matching stuff.

If you'd check out the sources[3], you'll see below impure functions.

  transDictRegex :: IO PCRE.Regex
  matchRegex :: String -> IO (String, String, String)
  translate :: String -> IO String

However, if I'm not mistaken, above functions _should_ be absolutely
pure. Is it possible to encapsulate impure PCRE functions into their
pure equivalents?

BTW, why are PCRE functions impure?


Regards.

[1] http://pleac.sourceforge.net/pleac_perl/strings.html
[2] http://pleac.sourceforge.net/
[3] http://hpaste.org/fastcgi/hpaste.fcgi/view?id=14422


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

Message: 6
Date: Sat, 19 Dec 2009 14:46:00 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] `coarbitrary' is not a (visible)
        method of       class `Arbitrary'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Samstag 19 Dezember 2009 14:20:43 schrieb Amy de Buitléir:
> I'm trying to follow the instructions in
> http://haskell.org/haskellwiki/How_to_write_a_Haskell_program. I'm at
> the step where we've just written a QuickCheck test:
>
> a...@localhost ~/forbairt/haq $ cat Tests.hs
> import Char
> import List
> import Test.QuickCheck
> import Text.Printf
>
> main  = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests
>
> instance Arbitrary Char where
>     arbitrary     = choose ('\0', '\128')
>     coarbitrary c = variant (ord c `rem` 4)
>
> -- reversing twice a finite list, is the same as identity
> prop_reversereverse s = (reverse . reverse) s == id s
>     where _ = s :: [Int]
>
> -- and add this to the tests list
> tests  = [("reverse.reverse/id", test prop_reversereverse)]
>
>
> But when I try to run it, I get the following errors:
>
> a...@localhost ~/forbairt/haq $ runhaskell Tests.hs
>
> Tests.hs:10:4:
>     `coarbitrary' is not a (visible) method of class `Arbitrary'
>
> Tests.hs:17:33: Not in scope: `test'
>
>
> I think this has something to do with the changes between QuickCheck
> v1 and v2.

Yes. coarbitrary has moved to its own class, CoArbitrary.
Now there's also method shrink in Arbitrary.

There is already an instance Arbitrary Char in QC 2, as well as instance 
CoArbitrary Char.

In QC 1, test was a synonym for quickCheck, so why not use quickCheck?

> Can someone tell me what I need to do to make this example
> work? Thank you in advance!
>
> Amy

HTH,
Daniel



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

Message: 7
Date: Sat, 19 Dec 2009 14:30:10 +0000
From: Amy de Buitl?ir <[email protected]>
Subject: Re: [Haskell-beginners] `coarbitrary' is not a (visible)
        method of       class `Arbitrary'
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Thank you Daniel, that solved it. In case anyone else is struggling
with this, here's a revised version of Test.hs which works fine for
me.

import Char
import List
import Test.QuickCheck
import Text.Printf

main  = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests

-- reversing twice a finite list, is the same as identity
prop_reversereverse s = (reverse . reverse) s == id s
    where _ = s :: [Int]

-- and add this to the tests list
tests  = [("reverse.reverse/id", quickCheck prop_reversereverse)]


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

Message: 8
Date: Sat, 19 Dec 2009 15:33:34 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] `coarbitrary' is not a (visible)
        method of       class `Arbitrary'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Samstag 19 Dezember 2009 15:30:10 schrieb Amy de Buitléir:
> import Char
> import List

The wrappers around the pre-hierarchical-modules standard libraries may not be 
around 
forever, so it's better to get used to

import Data.Char
import Data.List

before they're gone.


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

Message: 9
Date: Sat, 19 Dec 2009 17:05:36 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Pure Regex.PCRE Possible? (PLEAC, Fix
        Style)
To: Volkan YAZICI <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello

Under the hood PCRE.regex uses withCStringLen  which has the type:

withCStringLen :: String -> (CStringLen -> IO a) -> IO a

You can see the code here:
http://hackage.haskell.org/packages/archive/regex-pcre-builtin/0.94.2.1.7.7/doc/html/src/Text-Regex-PCRE-String.html

So the implementation is impure - it is built on impure code.

In the Real Word Haskell book, there is another implementation of a
PCRE interface, here the authors decided that their use of PCRE is
pure. The functions they have that use side effects - allocating
memory and freeing it - use the side effects entirely locally inside
each function body and the side effects never leak out of any
function.

http://book.realworldhaskell.org/read/interfacing-with-c-the-ffi.html

The implementation from the book is here:
http://hackage.haskell.org/package/pcre-light

Potentially the author of Text.Regex.PCRE could have decided that the
use of PCRE was pure and gone some way to hiding the IO type of
withCStringLen and similar functions (withCStringLen already
encapsulates its side effects). But as a design decision - if you are
using IO it is entirely reasonable to keep within IO even if what you
are doing appears pure (and personally I would choose to use the IO
version as I like to know when a library I'm using is performing side
effects).

Best wishes

Stephen

2009/12/19 Volkan YAZICI <[email protected]>:
> Hi,
>
> I'm trying to implement FixStyle[1] program of PLEAC[2] for Haskell.
> (It's missing in the Haskell version of PLEAC.) For this purpose, I
> needed regular expressions. (Now, I have two problems!)  Despite, my
> code is, IMHO, quite purely functional, Regex.PCRE.String functions
> totally mess up the design with impure functions they introduce even for
> really simple string matching stuff.
>
> If you'd check out the sources[3], you'll see below impure functions.
>
>  transDictRegex :: IO PCRE.Regex
>  matchRegex :: String -> IO (String, String, String)
>  translate :: String -> IO String
>
> However, if I'm not mistaken, above functions _should_ be absolutely
> pure. Is it possible to encapsulate impure PCRE functions into their
> pure equivalents?
>
> BTW, why are PCRE functions impure?
>
>
> Regards.
>
> [1] http://pleac.sourceforge.net/pleac_perl/strings.html
> [2] http://pleac.sourceforge.net/
> [3] http://hpaste.org/fastcgi/hpaste.fcgi/view?id=14422
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


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

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


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

Reply via email to