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:  How to divide a pair of Num values? (Iustin Pop)
   2.  applicativeParsec.hs in Real World Haskell not   compiling any
      more (Kees Bleijenberg)
   3. Re:  applicativeParsec.hs in Real World Haskell   not compiling
      any more (Daniel Fischer)
   4.  "downcasting" (Emmanuel Touzery)
   5. Re:  "downcasting" (Andres L?h)
   6. Re:  applicativeParsec.hs in Real World Haskell   not compiling
      any more (Kees Bleijenberg)


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

Message: 1
Date: Wed, 24 Oct 2012 12:40:22 +0200
From: Iustin Pop <[email protected]>
Subject: Re: [Haskell-beginners] How to divide a pair of Num values?
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Wed, Oct 24, 2012 at 09:01:34AM +0000, Costello, Roger L. wrote:
> Hi Folks,
> 
> Here is a function that takes a pair of Integral
> values and divides them:
> 
> divide_v1 :: Integral a => (a, a) -> a
> divide_v1 (m, n) = (m + n) `div` 2
> 
> I invoke the function with a pair of Integral
> values and it works as expected:
> 
> divide_v1 (1, 3)
> 
> Great. That's perfect if my numbers are always Integrals.
> 
> Here is a function that takes a pair of Fractional
> values and divides them:
> 
> divide_v2 :: Fractional a => (a, a) -> a
> divide_v2 (m, n) = (m + n) / 2
> 
> I invoke the function with a pair of Fractional
> values and it works as expected:
> 
> divide_v2 (1.0, 3.0)
> 
> Great. That's perfect if my numbers are always Fractionals.
> 
> I would like a function that works regardless of whether the
> numbers are Integrals or Fractionals:
> 
> divide_v3 :: Num a => (a, a) -> a
> divide_v3 (m, n) = (m + n) ___ 2
> 
> What operator do I use for ___?

As far as I know, you can't do this easily. Or rather, you can, if
you're fine to always use `div`, but not if you want different
behaviour.

If you really want to implement it, you can use a custom type-class, and
implement instances both Integral and Fractional for it, using div and
respectively (/). Something like (not tested):

class SmartDivision a where
  smartDivide :: a -> a -> a

instance (Integral a) => SmartDivision a where
  smartDivide = div

instance (Fractional a) => SmartDivision a where
  smartDivide = (/)

and your divide_v3 becomes:

divide_v3 :: (Num a, SmartDivision a) => (a, a) -> a
divide_v3 (m, n) = (m + n) `smartDivide` 2

But something doesn't sounds very right about your problem, so I'm not
sure I recommend actually implementing the above.

regards,
iustin



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

Message: 2
Date: Wed, 24 Oct 2012 19:40:02 +0200
From: "Kees Bleijenberg" <[email protected]>
Subject: [Haskell-beginners] applicativeParsec.hs in Real World
        Haskell not     compiling any more
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

A year ago I made a program. I took code from ApplicativeParsec.hs (capt. 16
Real World Haskell).
The program compiled and ran flawless. Now (after a year) I've upgraded to
GHC version 7.0.4 (Windows) and I want to change something. But the
unchanged version doesn't compile any more. I get warning:
ApplicativeParsec.hs:17:10:
    Illegal instance declaration for `Applicative (GenParser s a)'
      (All instance types must be of the form (T t1 ... tn)
       where T is not a synonym.
       Use -XTypeSynonymInstances if you want to disable this.)
    In the instance declaration for `Applicative (GenParser s a)'
 
ApplicativeParsec.hs:22:10:
    Illegal instance declaration for `Alternative ....(same advice as above)
 
So I added TypeSynonymInstances. Then I got:
ApplicativeParsec.hs:22:10:
    Overlapping instances for Applicative (GenParser s a)
      arising from the superclasses of an instance declaration
    Matching instances:
      instance Applicative (Text.Parsec.Prim.ParsecT s u m)
        -- Defined in Text.Parsec.Prim
      instance Applicative (GenParser s a)
        -- Defined at ApplicativeParsec.hs:17:10-36
    In the instance declaration for `Alternative (GenParser s a)'
 
Then I added the OverlappingInstances directive. Same error and no
compilation.
 
I wonder, is this the new GHC version? And what can I do about it?
 
Kees
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121024/3aa238d0/attachment.htm>

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

Message: 3
Date: Wed, 24 Oct 2012 20:08:54 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] applicativeParsec.hs in Real World
        Haskell not compiling any more
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Mittwoch, 24. Oktober 2012, 19:40:02, Kees Bleijenberg wrote:
> A year ago I made a program. I took code from ApplicativeParsec.hs (capt. 16
> Real World Haskell).
> The program compiled and ran flawless. Now (after a year) I've upgraded to
> GHC version 7.0.4 (Windows) and I want to change something. But the

Hum, the current version is 7.6.1, you should consider installing that, 7.0.4 
is already rather oldish.

> unchanged version doesn't compile any more. I get warning:
> ApplicativeParsec.hs:17:10:
>     Illegal instance declaration for `Applicative (GenParser s a)'
>       (All instance types must be of the form (T t1 ... tn)
>        where T is not a synonym.
>        Use -XTypeSynonymInstances if you want to disable this.)
>     In the instance declaration for `Applicative (GenParser s a)'
> 
> ApplicativeParsec.hs:22:10:
>     Illegal instance declaration for `Alternative ....(same advice as above)

That's unfortunate, it should better have reported the below immediately.

But it's hard to make the compiler always give the best error message, so 
sometimes it gives a not-best.

> 
> So I added TypeSynonymInstances. Then I got:
> ApplicativeParsec.hs:22:10:
>     Overlapping instances for Applicative (GenParser s a)
>       arising from the superclasses of an instance declaration
>     Matching instances:
>       instance Applicative (Text.Parsec.Prim.ParsecT s u m)
>         -- Defined in Text.Parsec.Prim
>       instance Applicative (GenParser s a)
>         -- Defined at ApplicativeParsec.hs:17:10-36
>     In the instance declaration for `Alternative (GenParser s a)'
> 
> Then I added the OverlappingInstances directive. Same error and no
> compilation.
> 
> I wonder, is this the new GHC version? And what can I do about it?

It's a new parsec version, that now provides Applicative and Alternative 
instances out-of-the-box.

Just remove (delete or comment out) the instance declarations from your code.

You might get a few further compilation errors due to the changes in parsec, 
in that case, either the error messages help you fix it, or you can ask a new 
question.



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

Message: 4
Date: Thu, 25 Oct 2012 00:00:59 +0200
From: Emmanuel Touzery <[email protected]>
Subject: [Haskell-beginners] "downcasting"
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello,

     using the Text.Json library (and I'm reading I should be using 
Aeson instead... I'll migrate to it later), I'm a bit frustrated.

     At one point I'm getting a JSValue object, which could be anything, 
including a JSObject.
     But in this case I know I'm looking at a JSObject. I don't mind if 
Haskell crashes if that's not the case (for now), either it's a JSObject 
or the JSON I'm parsing is invalid.

     I hoped to do that conversion using pattern matching by giving 
non-exhaustive pattern match, considering only cases where the JSValue 
is in fact a JSONObject but somehow I can't make it...

getObject :: JSValue -> JSObject JSValue
getObject x@(JSObject _) = x

--> doesn't compile the function returns a JSValue, I somehow wanted to 
point out to the compiler that if it gets there it's in fact a JSObject.

getObject :: JSValue -> JSObject JSValue
getObject (JSObject x) = JSObject x

-> same as with the other attempt.. the compiler is saying the function 
is returning a JSValue:

  Couldn't match expected type `JSObject JSValue'
              with actual type `JSValue'
  In the return type of a call of `JSObject'
  In the expression: JSObject x
  In an equation for `getObject': getObject (JSObject x) = JSObject x

  I think I'm missing an obvious pattern in Haskell here, probably 
something I've already used elsewhere too, but somehow I don't make it.

   Otherwise answers saying that if I need to make such a "cast" then 
I'm using that library the wrong way are welcome (together to maybe a 
link to a tutorial explaining it the right way), but by curiosity I 
would still be interested in how to actually do that "cast" in this case.

    Thank you!

Emmanuel



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

Message: 5
Date: Thu, 25 Oct 2012 00:15:39 +0200
From: Andres L?h <[email protected]>
Subject: Re: [Haskell-beginners] "downcasting"
To: Emmanuel Touzery <[email protected]>
Cc: [email protected]
Message-ID:
        <CALjd_v5L0HCs1CCqFqqNrxtgSG8=sw0N90cPshA0L=8lstn...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi Emmanuel.

It's somewhat confusing that in Haskell, data constructors and
datatypes can have the same name. They're nevertheless different
beasts. A data constructor is a way to construct values of a datatype,
or to destruct them via pattern matching. Such constructors themselves
are *not* types.

The definition of JSValue looks as follows:

> data JSValue
>     = JSNull
>     | JSBool     !Bool
>     | JSRational Bool{-as Float?-} !Rational
>     | JSString   JSString
>     | JSArray    [JSValue]
>     | JSObject   (JSObject JSValue)
>     deriving (Show, Read, Eq, Ord, Typeable)

So there are six different ways to construct a JSValue, the last one
is the JSObject constructor. It contains one item which is of *type*
JSObject JSValue. This time, JSObject refers to a datatype, also
defined in the library:

> newtype JSObject e = JSONObject { fromJSObject :: [(String, e)] }
>     deriving (Eq, Ord, Show, Read, Typeable )

Now to your functions: if you write

> getObject :: JSValue -> JSObject JSValue
> getObject x@(JSObject _) = x

or

> getObject :: JSValue -> JSObject JSValue
> getObject (JSObject x) = JSObject x

you are writing essentially the identity function, but trying to
assign a more specific type to the value. This doesn't quite work in
Haskell. There's no subtyping between different datatypes.

Instead, what you should do is perform the pattern match once and
extract its contents:

> getObject :: JSValue -> JSObject JSValue
> getObject (JSObject x) = x

Now you have the stuff that was "inside" the constructor, and isolated
the point of failure.

Cheers,
  Andres

-- 
Andres L?h, Haskell Consultant
Well-Typed LLP, http://www.well-typed.com



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

Message: 6
Date: Thu, 25 Oct 2012 10:46:39 +0200
From: "Kees Bleijenberg" <[email protected]>
Subject: Re: [Haskell-beginners] applicativeParsec.hs in Real World
        Haskell not compiling any more
To: "'Daniel Fischer'" <[email protected]>,
        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;       charset="us-ascii"

--original message
....
Hum, the current version is 7.6.1, you should consider installing that,
7.0.4 is already rather oldish.

I don't know what the latest version is for Windows. I'll give it a try.

> unchanged version doesn't compile any more. I get warning:
> ApplicativeParsec.hs:17:10:
>     Illegal instance declaration for `Applicative (GenParser s a)'
>       (All instance types must be of the form (T t1 ... tn)
>        where T is not a synonym.
>        Use -XTypeSynonymInstances if you want to disable this.)
>     In the instance declaration for `Applicative (GenParser s a)'
> 
> ApplicativeParsec.hs:22:10:
>     Illegal instance declaration for `Alternative ....(same advice as 
> above)

That's unfortunate, it should better have reported the below immediately.
But it's hard to make the compiler always give the best error message, so
sometimes it gives a not-best.

..... 
> Then I added the OverlappingInstances directive. Same error and no 
> compilation.
> 
> I wonder, is this the new GHC version? And what can I do about it?

It's a new parsec version, that now provides Applicative and Alternative
instances out-of-the-box.

Just remove (delete or comment out) the instance declarations from your
code.

--- end original message

Removing ApplicationParsec.hs from the project and adding a few new
references (Control.Applicative....) did the trick. 

Thanks,
Kees





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

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


End of Beginners Digest, Vol 52, Issue 29
*****************************************

Reply via email to