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 avoid repeating code (Federico Mastellone)
   2. Re:  How to avoid repeating code (Daniel Fischer)
   3. Re:  Simple UTF-8 output (Christopher Howard)
   4. Re:  Simple UTF-8 output (Christopher Howard)


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

Message: 1
Date: Fri, 27 May 2011 14:23:54 -0300
From: Federico Mastellone <[email protected]>
Subject: Re: [Haskell-beginners] How to avoid repeating code
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Now I have a new problem, it's getting really difficult to program
generically and to create highly parameterized libraries.

So far so good with type families, but when I want to return a generic Set
for the getValues function and provide a default implementation for
getValuesCount function I don't know how to do it, I don't even know if it
is possible.

newtype MultiMap k v = MultiMap (Map.Map k (Set.Set v))

newtype IntMultiMap = IntMultiMap (IntMap.IntMap IntSet.IntSet)

class MultiMapClass m where
 type Key m
 type Value m
 empty :: m
 addValue :: Key m -> Value m -> m -> m
 getValues :: Key m -> m -> Set (Value m)
 getValueCount :: Key m -> m -> Int
 getValueCount k m = Set.size $ getValues k m

instance (Ord k, Ord v) => MultiMapClass (MultiMap k v) where
 type Key (MultiMap k v) = k
 type Value (MultiMap k v) = v
 empty = MultiMap Map.empty
 addValue k v m = ..........
 getValues k (MultiMap map) = map Map.! k

instance MultiMapClass IntMultiMap where
 type Key IntMultiMap = Int
 type Value IntMultiMap = Int
 empty = IntMultiMap IntMap.empty
 addValue k v m = ..........
 getValues k (IntMultiMap map) = map IntMap.! k

On Thu, May 26, 2011 at 1:47 PM, Federico Mastellone <[email protected]>wrote:

>
> On Thu, May 26, 2011 at 8:47 AM, Daniel Fischer <
> [email protected]> wrote:
>
>> On Thursday 26 May 2011 02:23:06, Federico Mastellone wrote:
>> > Hi,
>> >
>> > I created a Data.MultiMap module based on Data.Map and Data.Set like
>> > this:
>> >
>> > data MultiMap k v = MultiMap (Map k (Set v))
>> >
>> > and a Data.IntMultiMap module based on Data.IntMap and data.IntSet like
>> > this:
>> >
>> > data IntMultiMap = IntMultiMap (IntMap IntSet)
>>
>> Both of these would better be newtypes instead of data, I think.
>> Using data incurs some run-time overhead (the newtype doesn't exist at
>> run-
>> time, only during compile-time [type checking phase], so it's a strictly
>> controlled type alias in practice, making it easier [or possible at all]
>> to
>> apply optimisations available for the underlying type) due to the extra
>> indirections via the constructor and introduces the new value
>> (MultiMap _|_), which complicates strictness analysis and optimisations in
>> general.
>>
>>
> Thanks for the tip, I use newtype where I can but I thought it would be
> simpler here to explain my problem using data. Anyway, now I know better why
> I should use newtype instead.
>
>
>> >
>> > For example the functions to add a value I wrote are:
>> >
>> > For MultiMap:
>> > addValue :: k -> v -> MultiMap k v -> MultiMap k v
>> > addValue k v (MultiMap m) = MultiMap $ Map.insertWith (\new old ->
>> > Set.insert v old) k (Set.singleton v) m
>> >
>> > For IntMultiMap:
>> > addValue :: Int -> Int -> IntMultiMap -> IntMultiMap
>> > addValue k v (IntMultiMap m) = IntMultiMap $ IntMap.insertWith (\new old
>> > -> IntSet.insert v old) k (IntSet.singleton v) m
>> >
>> > Both modules look almost the same, with the same
>> > documentation, same behavior, same function names but with different
>> > type signatures.
>>
>> Well, we have the same situation with Map/IntMap and Set/IntSet, so
>>
>>
> Yes, both pairs, (Set, InSet), and (Map, IntMap), have the exact same
> problem as my MultiMap module.
>
> But I think that as your solution involves using non-standard extensions we
> could not solve this problem in GHC's libraries. Am I OK?
>
> >
>> > Is there a way to make this simpler?
>>
>> Not a really good one (at least, none I know).
>>
>> >
>> > The same thing happens to the modules that are using MultiMap and
>> > IntMultiMap, I have to write two versions of each.
>>
>> You can reduce the code duplication at the use sites with a type class,
>>
>> {-# LANGUAGE TypeFamilies #-}
>>
>
> Is this a mostly experimental extension or I can use it safely? Is it used
> around the GHC packages?
>
>
>>
>> class MultiMapClass m where
>>  type Key m
>>  type Value m
>>  empty :: m
>>  singleton :: Key m -> Value m -> m
>>  addValue :: Key m -> Value m -> m -> m
>>  ...
>>
>> instance (Ord k, Ord v) => MultiMapClass (MultiMap k v) where
>>  type Key (MultiMap k v) = k
>>  type Value (MultiMap k v) = v
>>  empty = MultiMap Map.empty
>>  ...
>>
>> instance MultiMapClass IntMultiMap where
>>  type Key IntMultiMap = Int
>>  type Value IntMultiMap = Int
>>  empty = IntMultiMap IntMap.empty
>>  ...
>>
>>
> I'm going to read about this extension, try it and comment about it here.
>
>
>> >
>> > Thanks!
>>
>
> Thank you very much for your complete response, really helpful!
>
>
> --
> Federico Mastellone
> Computer Science Engineer - ITBA
>
> ".. there are two ways of constructing a software design: One way is to
> make it so simple that there are obviously no deficiencies, and the other
> way is to make it so complicated that there are no obvious deficiencies. The
> first method is far more difficult."
>
> Tony Hoare, 1980 ACM Turing Award Lecture.
>
>
>
>


-- 
Federico Mastellone
Computer Science Engineer - ITBA

".. there are two ways of constructing a software design: One way is to make
it so simple that there are obviously no deficiencies, and the other way is
to make it so complicated that there are no obvious deficiencies. The first
method is far more difficult."

Tony Hoare, 1980 ACM Turing Award Lecture.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110527/d929fcda/attachment-0001.htm>

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

Message: 2
Date: Fri, 27 May 2011 20:00:19 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] How to avoid repeating code
To: Federico Mastellone <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="utf-8"

On Friday 27 May 2011 19:23:54, Federico Mastellone wrote:
> Now I have a new problem, it's getting really difficult to program
> generically and to create highly parameterized libraries.

Yes.

> 
> So far so good with type families, but when I want to return a generic
> Set for the getValues function and provide a default implementation for
> getValuesCount function I don't know how to do it, I don't even know if
> it is possible.

You can't return a generic Set in getValues (unless you jump through a lot 
of hoops, IntMultiMap has IntSets, so you'd have to transform those to 
Sets, ... yuck) and default implementations wouldn't be possible [or at 
least rather horrible], but

> 
> newtype MultiMap k v = MultiMap (Map.Map k (Set.Set v))
> 
> newtype IntMultiMap = IntMultiMap (IntMap.IntMap IntSet.IntSet)

class (Ord (Elem c)) => Collection c where
  type Elem c
  emtpy :: c
  singleton :: Elem c -> c
  size :: c -> Int
  null :: c -> Bool
  member :: Elem c -> c -> Bool
  ...

> 
> class MultiMapClass m where

class (Collection (Coll m)) => MultiMapClass m where

>  type Key m
>  type Value m

  type Coll m

>  empty :: m
>  addValue :: Key m -> Value m -> m -> m
>  getValues :: Key m -> m -> Set (Value m)

  getValues :: Key m -> m -> Coll m

>  getValueCount :: Key m -> m -> Int
>  getValueCount k m = Set.size $ getValues k m

  getValueCount k m = size (getValues k m)

Should work or be possible to make working.
But things get more complicated the more generic functionality you want to 
add.

It would probably be possible to get a more elegant implementation if you 
designed the library to use a class-based interface from the ground up 
(take a look at the edison library [EdisonAPI and EdisonCore on hackage] 
for an idea of how to structure the classes - edison is old, it was created 
long before type families or functional dependencies were available, I 
don't know what new stuff was incorporated into it, I suspect not too much, 
so you could probably improve the design with the new powerful tools at 
your hands, but as a source of inspiration, it should still serve well).

The problem is that, as a rule of thumb, class based genericity costs 
performance. And the point of the containers package was to provide 
performant data structures, the genericity was wilfully forsaken.

So, perhaps writing a generic envelope for the containers types might not 
be the best option. It could be better to start from a clean sheet.
[disclaimer: I have not thought about how to do that, nor looked at the API 
or implementation from that point of view, so my hunch may be quite wrong.]

Cheers,
Daniel




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

Message: 3
Date: Fri, 27 May 2011 20:01:51 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] Simple UTF-8 output
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 05/26/2011 11:38 PM, Erik de Castro Lopo wrote:
> Elvio Toccalino wrote:
> 
>> I usually pull such values as ByteStrings. The utf8-strings package
>> provides printing functions for such data.
>> Also, if you'd like to work on it as Text, there's the
>> Data.Text.Encodings module, to decodeUTF8 or decodeASCII ByteString's
>> into Text.
> 
> I would recommend Data.Text because that is designed for Text
> encodings while ByteString is designed for handling arrays
> of bytes (eg things like data read from an network socket).
> 
> Cheers,
> Erik

Thank you everyone for your responses, but I'm still having trouble with
this issue. This is what I've learned so far:

- The problem is not my terminal: commands like putStrLn "\228" produce
the correct character.
- The problem doesn't seem to be my MySQL database. The character
encoding on the database and on the table are set to UTF-8. I can view
the characters just fine when in the MySQL client.
- I looked again at the character numbers on the special characters
being outputted by my program: They are not the correct Unicode numbers.
Apparently the special characters are somehow getting mangled when I
pull them from the database.
- The choice to put the query results into ByteStrings was not mine,
that is just what is in the results list that quickQuery gives me. I
tried converting to Text and then to String by using "unpack (decodeUtf8
byte_str)"; however, when the program goes to print its first line with
an extended character, it just throws the exception: "*** Exception:
Cannot decode byte '\xe6': Data.Text.Encoding.decodeUtf8: Invalid UTF-8
stream".

-- 
frigidcode.com
theologia.indicium.us



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

Message: 4
Date: Fri, 27 May 2011 20:35:52 -0800
From: Christopher Howard <[email protected]>
Subject: Re: [Haskell-beginners] Simple UTF-8 output
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 05/27/2011 08:01 PM, Christopher Howard wrote:
> On 05/26/2011 11:38 PM, Erik de Castro Lopo wrote:
>> Elvio Toccalino wrote:
>>
>>> I usually pull such values as ByteStrings. The utf8-strings package
>>> provides printing functions for such data.
>>> Also, if you'd like to work on it as Text, there's the
>>> Data.Text.Encodings module, to decodeUTF8 or decodeASCII ByteString's
>>> into Text.
>>
>> I would recommend Data.Text because that is designed for Text
>> encodings while ByteString is designed for handling arrays
>> of bytes (eg things like data read from an network socket).
>>
>> Cheers,
>> Erik
> 
> Thank you everyone for your responses, but I'm still having trouble with
> this issue. This is what I've learned so far:
> 
> - The problem is not my terminal: commands like putStrLn "\228" produce
> the correct character.
> - The problem doesn't seem to be my MySQL database. The character
> encoding on the database and on the table are set to UTF-8. I can view
> the characters just fine when in the MySQL client.
> - I looked again at the character numbers on the special characters
> being outputted by my program: They are not the correct Unicode numbers.
> Apparently the special characters are somehow getting mangled when I
> pull them from the database.
> - The choice to put the query results into ByteStrings was not mine,
> that is just what is in the results list that quickQuery gives me. I
> tried converting to Text and then to String by using "unpack (decodeUtf8
> byte_str)"; however, when the program goes to print its first line with
> an extended character, it just throws the exception: "*** Exception:
> Cannot decode byte '\xe6': Data.Text.Encoding.decodeUtf8: Invalid UTF-8
> stream".
> 

Update: I think this may be a problem with the myodbc5 driver. If I
connect with isql and then select the data, the results with extended
characters have the text either mangled or replaced with "***ERROR***".
So I am thinking either this MySQL driver for unixODBC either is not
able to handle the UTF-8, or (more likely) I don't have the correct
options in my .odbc.ini config file.

-- 
frigidcode.com
theologia.indicium.us



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

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


End of Beginners Digest, Vol 35, Issue 55
*****************************************

Reply via email to