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:  Return a Foldable instance (Federico Mastellone)
   2. Re:  Return a Foldable instance (Antoine Latter)
   3. Re:  Return a Foldable instance (Alexander Dunlap)
   4. Re:  Return a Foldable instance (Federico Mastellone)
   5. Re:  Return a Foldable instance (Brent Yorgey)
   6. Re:  Return a Foldable instance (Chadda? Fouch?)


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

Message: 1
Date: Wed, 4 May 2011 01:08:25 -0300
From: Federico Mastellone <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: Antoine Latter <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Thank you both!

So, how can I return something that implements Foldable, so the caller
can fold without knowing if it is a [] or a Set and also be able to
change the underlying implementation without breaking the code?

With this I also want to avoid the extra overhead of the conversion to
or from lists, so the user will be folding the [] or Set directly,
depending on the implementation.

Creating an intermediate data type like this:
Temp a = TempList [a] | TempSet (Set a) | ...
That implements Foldable could be one solution, but is it a good one?
A cons of this is that every new implementation will have to alter
this type as well as creating the implementation.

Thanks again!

On Wed, May 4, 2011 at 12:27 AM, Antoine Latter <[email protected]> wrote:
> On Tue, May 3, 2011 at 8:31 PM, Federico Mastellone <[email protected]> wrote:
>> Hi,
>>
>> I want to make a function that returns a Foldable instance, briefly,
>> something like this:
>>
>> import Data.Foldable
>>
>> test :: Foldable f => f Int
>> test = [1,2,3,4]
>>
>
> One point that helped me figure things out:
>
> The signature:
>
>> test :: Foldable f => f Int
>
> does NOT mean "the value test satisfies the 'Foldable' contract'
>
> It means "the value test is of ANY 'Foldable' type". As in, it is of
> whatever type the caller picks, so long as it is 'Foldable'. And a
> list is not 'whatever type the caller picks.'
>
> Type classes are used in Haskell to write polymorphic functions, not
> for implementation hiding:
>
>> myNumericalCalculation :: Fractional n => n -> n -> Bool -> n
>
> The caller can pick if they want to use limited precision Floats or
> Doubles, or if they want to pay the price for infinite precision Ratio
> types, or some type that I hadn't even thought of when I wrote the
> functions.
>
> Antoine
>
>> But I get this error:
>>
>> ? ?Couldn't match expected type `f' against inferred type `[]'
>> ? ? ? ?`f' is a rigid type variable bound by
>> ? ? ? ? ? ?the type signature for `test' at Test.hs:3:17
>> ? ?In the expression: [1, 2, 3, 4]
>> ? ?In the definition of `test': test = [1, 2, 3, ....]
>>
>> How can I make a function like the one above?
>>
>> I'm creating different implementations of a multimap, using a Set and
>> a [], and instead of providing functions getValuesList and
>> getValuesSet that return a [] and a Set respectively I want to provide
>> a more generic one, getValues that returns a Foldable instance.
>>
>> Thanks!!!
>>
>> --
>> 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.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>



-- 
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.



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

Message: 2
Date: Tue, 3 May 2011 23:18:44 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: Federico Mastellone <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Tue, May 3, 2011 at 11:08 PM, Federico Mastellone <[email protected]> wrote:
> Thank you both!
>
> So, how can I return something that implements Foldable, so the caller
> can fold without knowing if it is a [] or a Set and also be able to
> change the underlying implementation without breaking the code?
>

Well again, this is not what typeclasses are - in spite of the name,
they are nothing like OO-style classes.

If you want to hide your implementation, then (as you suggest bellow)
you need to create a new type and not expose the details of the type.
If you want the caller to be able to use the 'Foldable' family of
functions then your new type needs to implement the 'Foldable'
typeclass (or export equivalent functionality in some other way.)

> With this I also want to avoid the extra overhead of the conversion to
> or from lists, so the user will be folding the [] or Set directly,
> depending on the implementation.
>
> Creating an intermediate data type like this:
> Temp a = TempList [a] | TempSet (Set a) | ...
> That implements Foldable could be one solution, but is it a good one?

> A cons of this is that every new implementation will have to alter
> this type as well as creating the implementation.

I really don't know enough about what you're trying to do to answer
this - going with our current example the value:

> test :: SomeType Int

only has one implementation.

Feel free to respond back with more details about your problem.

Antoine



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

Message: 3
Date: Tue, 3 May 2011 22:06:41 -0700
From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: Federico Mastellone <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

This probably isn't a good solution to your problem, but you could use
an existential type to hide what the type actually was. You need a GHC
extension (ExistentialQuantification, I think) for this to work. The
following code is completely untested and the syntax might be wrong,
but the idea should be right:

data SomethingFoldable = SomethingFoldable (forall f. Foldable f => f)

Now your function can return a value of type SomethingFoldable. Notice
that the type of SomethingFoldable is not parameterized by the type of
f: the "forall" keyword inside the parentheses means that the value
could be *any* type that satisfies the Foldable constraint.

Note that this hugely limits what the caller can do with the value.
They don't know what type the value is, so they can't use list
functions (because the value might be a Set or something else
Foldable) or Set functions (because the value might be a list or
something else Foldable). All they know is that the value is a member
of the Foldable class, so the only way the value can be used is in a
function that takes an arbitrary member of the Foldable class as a
value. (function :: Foldable f => ... -> f -> ...)

Because of this, I've found that this technique's usefulness is
limited to situations where you really want to be leveraging the type
system to do something interesting. In other cases, there is probably
just one function (since type classes generally don't have a lot of
member functions) that your caller would be applying to the returned
value, and you could just call that function within your function and
return the result of that. (You might have to add additional arguments
to your function, like a function to fold with or an initial value,
etc.)

Hope that helps you a bit.

Alex

On 3 May 2011 21:08, Federico Mastellone <[email protected]> wrote:
> Thank you both!
>
> So, how can I return something that implements Foldable, so the caller
> can fold without knowing if it is a [] or a Set and also be able to
> change the underlying implementation without breaking the code?
>
> With this I also want to avoid the extra overhead of the conversion to
> or from lists, so the user will be folding the [] or Set directly,
> depending on the implementation.
>
> Creating an intermediate data type like this:
> Temp a = TempList [a] | TempSet (Set a) | ...
> That implements Foldable could be one solution, but is it a good one?
> A cons of this is that every new implementation will have to alter
> this type as well as creating the implementation.
>
> Thanks again!
>
> On Wed, May 4, 2011 at 12:27 AM, Antoine Latter <[email protected]> wrote:
>> On Tue, May 3, 2011 at 8:31 PM, Federico Mastellone <[email protected]> wrote:
>>> Hi,
>>>
>>> I want to make a function that returns a Foldable instance, briefly,
>>> something like this:
>>>
>>> import Data.Foldable
>>>
>>> test :: Foldable f => f Int
>>> test = [1,2,3,4]
>>>
>>
>> One point that helped me figure things out:
>>
>> The signature:
>>
>>> test :: Foldable f => f Int
>>
>> does NOT mean "the value test satisfies the 'Foldable' contract'
>>
>> It means "the value test is of ANY 'Foldable' type". As in, it is of
>> whatever type the caller picks, so long as it is 'Foldable'. And a
>> list is not 'whatever type the caller picks.'
>>
>> Type classes are used in Haskell to write polymorphic functions, not
>> for implementation hiding:
>>
>>> myNumericalCalculation :: Fractional n => n -> n -> Bool -> n
>>
>> The caller can pick if they want to use limited precision Floats or
>> Doubles, or if they want to pay the price for infinite precision Ratio
>> types, or some type that I hadn't even thought of when I wrote the
>> functions.
>>
>> Antoine
>>
>>> But I get this error:
>>>
>>> ? ?Couldn't match expected type `f' against inferred type `[]'
>>> ? ? ? ?`f' is a rigid type variable bound by
>>> ? ? ? ? ? ?the type signature for `test' at Test.hs:3:17
>>> ? ?In the expression: [1, 2, 3, 4]
>>> ? ?In the definition of `test': test = [1, 2, 3, ....]
>>>
>>> How can I make a function like the one above?
>>>
>>> I'm creating different implementations of a multimap, using a Set and
>>> a [], and instead of providing functions getValuesList and
>>> getValuesSet that return a [] and a Set respectively I want to provide
>>> a more generic one, getValues that returns a Foldable instance.
>>>
>>> Thanks!!!
>>>
>>> --
>>> 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.
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>
>
>
> --
> 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.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 4
Date: Wed, 4 May 2011 02:18:14 -0300
From: Federico Mastellone <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: Antoine Latter <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Yes, I got confused between haskell type classes and OO classes.

I want to create different implementations of a multimap, for example
using lists and using Data.Set, and instead of providing functions
getValuesList and getValuesSet that return a [] and a Set respectively
I want to provide a more generic one, getValues that returns a
Foldable and avoid unnecessary conversions to and from lists. This way
the user folds directly the underlying structure, without having to
worry about which function is the best to fold the values. But I don't
know how to do this without an extra intermediate data type.

One implementation returns a list and the other a set, both Foldables.
But I can't make a function like the one below for this two
getValues :: Foldable f => MultiMap k v -> f v

On Wed, May 4, 2011 at 1:18 AM, Antoine Latter <[email protected]> wrote:
> On Tue, May 3, 2011 at 11:08 PM, Federico Mastellone <[email protected]> wrote:
>> Thank you both!
>>
>> So, how can I return something that implements Foldable, so the caller
>> can fold without knowing if it is a [] or a Set and also be able to
>> change the underlying implementation without breaking the code?
>>
>
> Well again, this is not what typeclasses are - in spite of the name,
> they are nothing like OO-style classes.
>
> If you want to hide your implementation, then (as you suggest bellow)
> you need to create a new type and not expose the details of the type.
> If you want the caller to be able to use the 'Foldable' family of
> functions then your new type needs to implement the 'Foldable'
> typeclass (or export equivalent functionality in some other way.)
>
>> With this I also want to avoid the extra overhead of the conversion to
>> or from lists, so the user will be folding the [] or Set directly,
>> depending on the implementation.
>>
>> Creating an intermediate data type like this:
>> Temp a = TempList [a] | TempSet (Set a) | ...
>> That implements Foldable could be one solution, but is it a good one?
>
>> A cons of this is that every new implementation will have to alter
>> this type as well as creating the implementation.
>
> I really don't know enough about what you're trying to do to answer
> this - going with our current example the value:
>
>> test :: SomeType Int
>
> only has one implementation.
>
> Feel free to respond back with more details about your problem.
>
> Antoine
>



-- 
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.



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

Message: 5
Date: Wed, 4 May 2011 01:36:51 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Wed, May 04, 2011 at 02:18:14AM -0300, Federico Mastellone wrote:
> I want to create different implementations of a multimap, for example
> using lists and using Data.Set, and instead of providing functions
> getValuesList and getValuesSet that return a [] and a Set respectively
> I want to provide a more generic one, getValues that returns a
> Foldable and avoid unnecessary conversions to and from lists. This way
> the user folds directly the underlying structure, without having to
> worry about which function is the best to fold the values. But I don't
> know how to do this without an extra intermediate data type.

A couple thoughts:

First, I am not sure why you are so worried about avoiding an
intermediate list.  It smells like premature optimization to me.  I
doubt it will really make that big of a difference; and depending on
how the result is used, the intermediate list might be optimized away
anyway.

Second, if you really want to *only* allow the caller of getValues to
be able to fold the result, then you can just "inline" the fold, like
so:

  getValues :: Monoid m => MultiMap k v -> (v -> m) -> m

That is, instead of getting a Foldable thing and doing the fold
themselves, they provide a mapping from values to some monoid and you
do the fold for them.

-Brent



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

Message: 6
Date: Wed, 4 May 2011 07:51:48 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: Federico Mastellone <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Wed, May 4, 2011 at 7:18 AM, Federico Mastellone <[email protected]> wrote:
> Yes, I got confused between haskell type classes and OO classes.
>
> I want to create different implementations of a multimap, for example
> using lists and using Data.Set, and instead of providing functions
> getValuesList and getValuesSet that return a [] and a Set respectively
> I want to provide a more generic one, getValues that returns a
> Foldable and avoid unnecessary conversions to and from lists. This way
> the user folds directly the underlying structure, without having to
> worry about which function is the best to fold the values. But I don't
> know how to do this without an extra intermediate data type.
>
> One implementation returns a list and the other a set, both Foldables.
> But I can't make a function like the one below for this two
> getValues :: Foldable f => MultiMap k v -> f v
>

Writing this function given your intention (doing different thing
depending on the type) is equivalent to writing an instance of
Foldable for your (MultiMap k). If the underlying structure of your
multimaps is always foldable, it doesn't make sense to make a function
like this, the multimaps themselves should be Foldable. This is not
hard to do, refer to the doc (
http://www.haskell.org/ghc/docs/7.0-latest/html/libraries/base-4.3.1.0/Data-Foldable.html
) to see how to write a minimal instance of Foldable.

-- 
Jeda?



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

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


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

Reply via email to