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.  type vs data, again... (Emmanuel Touzery)
   2. Re:  type vs data, again... (Andres L?h)
   3. Re:  type vs data, again... (Emmanuel Touzery)
   4. Re:  type vs data, again... (Andres L?h)
   5. Re:  Confused about lazy IO (Gesh hseG)


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

Message: 1
Date: Sun, 17 Mar 2013 12:58:10 +0100
From: Emmanuel Touzery <[email protected]>
Subject: [Haskell-beginners] type vs data, again...
To: "[email protected]" <[email protected]>
Message-ID:
        <CAC42RemA8m26v8sfvFs1D+KS=2szwg5vw8rxv_ujezfdzcb...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hello,

 I'm again a bit stuck on a simple problem, and I think it's the same
problem that I faced some time ago:
http://comments.gmane.org/gmane.comp.lang.haskell.beginners/10843

 There is obviously something that I didn't get yet about "data" versus
"type".

 In the program in attachment, I want to parse a JSON file to a Value, then
if that value is in fact a JSON object, I want to explore its hash member
name -> member value.

 Now we have:

data Value = Object Object <--- the case I hope for           | Array
Array           | String Text           | Number Number           |
Bool !Bool           | Null             deriving (Eq, Show, Typeable,
Data)



And then:

-- | A JSON \"object\" (key\/value map).type Object = Map Text Value

So my clearly flawed plan is to get the value, pattern mach it against
(Object hash) and then work on the hash. However for the program in
attachment, which I would expect to compile, I get this compile error:

question.hs:12:62:
    Couldn't match expected type `Map.Map T.Text Value'
                with actual type `Object'
    In the first argument of `parseConfigMap', namely `map'
    In the second argument of `($)', namely `parseConfigMap map'
    In the expression: return $ parseConfigMap map

That confuses me, because if the error means by Object the data, then I
understood nothing. However if the error means by Object the type, then I
would expect it to work, since the type exactly means "Map Text Value"...

Any hint? And that really doesn't look like a good idea to me to define a
type and a data by the same name, but it's apparently the right way to do
it...

Thank you!

Emmanuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130317/303ddd81/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: question.hs
Type: application/octet-stream
Size: 412 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130317/303ddd81/attachment-0001.obj>

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

Message: 2
Date: Sun, 17 Mar 2013 13:23:37 +0100
From: Andres L?h <[email protected]>
Subject: Re: [Haskell-beginners] type vs data, again...
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <caljd_v64bvx7pmveaepyjovpdgkp9lioqzabntc_vpyl4sw...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi there.

> -- | A JSON \"object\" (key\/value map).
> type Object = Map Text Value

See https://github.com/bos/aeson/blob/master/release-notes.markdown

Quoting:

"
0.3 to 0.4

[...]

We switched the intermediate representation of JSON objects from
Data.Map to Data.HashMap, which has improved type conversion
performance.
"

> So my clearly flawed plan is to get the value, pattern mach it against
> (Object hash) and then work on the hash. However for the program in
> attachment, which I would expect to compile, I get this compile error:
>
> question.hs:12:62:
>     Couldn't match expected type `Map.Map T.Text Value'
>                 with actual type `Object'
>     In the first argument of `parseConfigMap', namely `map'
>     In the second argument of `($)', namely `parseConfigMap map'
>     In the expression: return $ parseConfigMap map

This has nothing to do with "type" vs. "data". The type synonym
expansion of Object doesn't match your type, because it uses a HashMap
rather than a Map.

Cheers,
  Andres

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



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

Message: 3
Date: Sun, 17 Mar 2013 14:27:35 +0100
From: Emmanuel Touzery <[email protected]>
Subject: Re: [Haskell-beginners] type vs data, again...
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <cac42rem0suwflnkkc4wb5y2j1vrqubzibksenfr7yumjwjq...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

 thank you for the tip! It helps but it's not quite there yet. If you see
the program in attachment, I can make it compile only by commenting the
type declaration for the second function, otherwise it's the exact same
error message as before.

In -Wall the compiler does suggest me the signature, but it's not
enlightening for me at this point, I've yet to dig that deep in haskell's
type system:
parseConfigMap :: forall t (m :: * -> *). Monad m => t -> m ()

 Can you explain me why my type signature is not correct?

 Thank you again!

Emmanuel


On Sun, Mar 17, 2013 at 1:23 PM, Andres L?h <[email protected]> wrote:

> Hi there.
>
> > -- | A JSON \"object\" (key\/value map).
> > type Object = Map Text Value
>
> See https://github.com/bos/aeson/blob/master/release-notes.markdown
>
> Quoting:
>
> "
> 0.3 to 0.4
>
> [...]
>
> We switched the intermediate representation of JSON objects from
> Data.Map to Data.HashMap, which has improved type conversion
> performance.
> "
>
> > So my clearly flawed plan is to get the value, pattern mach it against
> > (Object hash) and then work on the hash. However for the program in
> > attachment, which I would expect to compile, I get this compile error:
> >
> > question.hs:12:62:
> >     Couldn't match expected type `Map.Map T.Text Value'
> >                 with actual type `Object'
> >     In the first argument of `parseConfigMap', namely `map'
> >     In the second argument of `($)', namely `parseConfigMap map'
> >     In the expression: return $ parseConfigMap map
>
> This has nothing to do with "type" vs. "data". The type synonym
> expansion of Object doesn't match your type, because it uses a HashMap
> rather than a Map.
>
> Cheers,
>   Andres
>
> --
> Andres L?h, Haskell Consultant
> Well-Typed LLP, http://www.well-typed.com
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130317/c7e2990e/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: question.hs
Type: application/octet-stream
Size: 432 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130317/c7e2990e/attachment-0001.obj>

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

Message: 4
Date: Sun, 17 Mar 2013 14:51:31 +0100
From: Andres L?h <[email protected]>
Subject: Re: [Haskell-beginners] type vs data, again...
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <CALjd_v67-m5935MhEGhxyzN2T9EqfTwgH2h486S=1m1ur7r...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi again.

>  thank you for the tip! It helps but it's not quite there yet. If you see
> the program in attachment, I can make it compile only by commenting the type
> declaration for the second function, otherwise it's the exact same error
> message as before.

You're probably using the wrong HashMap. There are several packages on
Hackage implementing similarly-named data structures (which is
confusing, I know, but such is life in a decentralized and open
world). You should use what aeson is using (because that's the library
defining the Object type synonym).

Looking at the import list of

http://hackage.haskell.org/packages/archive/aeson/0.6.0.2/doc/html/src/Data-Aeson-Types-Internal.html

we see

import Data.HashMap.Strict (HashMap)

and by checking the package dependencies of aeson, we figure out that
this module is provided by the unordered-containers package.

Cheers,
  Andres

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



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

Message: 5
Date: Sun, 17 Mar 2013 16:10:37 +0200
From: Gesh hseG <[email protected]>
Subject: Re: [Haskell-beginners] Confused about lazy IO
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Message-ID:
        <cacs5xqn8b-b3x1fzao3yetzwt301lxggatqdgswtjq1xag4...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

That would have been useful to know three weeks ago.
Better late than never I guess.
Thanks.


On Fri, Mar 15, 2013 at 1:24 AM, Kim-Ee Yeoh <[email protected]> wrote:

> On Fri, Mar 15, 2013 at 5:52 AM, Jacek Dudek <[email protected]> wrote:
> > readFile fileName >>= \ contents ->
> > writeFile fileName (f contents)
>
> You're reading and writing to the /same/ file back to back. With Lazy
> I/O. Those two just don't mix.
>
> The "permission denied" probably stems from the attempted write
> clashing with the previous exclusive read handle.
>
> > I thought lazy IO was implemented in such a way that you were safe to
> > INTERPRET the IO action as having been fully performed.
>
> Nope.
>
> The usual gotcha lying for the unwary is
>
> do
>     h <- openFile fname1 ReadMode
>     s <- hGetContents h
>     hClose h
>     writeFile fname2 s
>
> In the case fname1==fname2, a different surprise lies in store as
> you've just witnessed.
>
> Importing System.IO.Strict from the strict package should fix all of the
> above.
>
> -- Kim-Ee
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130317/5945bf4b/attachment.htm>

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

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


End of Beginners Digest, Vol 57, Issue 24
*****************************************

Reply via email to