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 (James Cook)
   4. Re:  Return a Foldable instance (David McBride)
   5. Re:  Return a Foldable instance (James Cook)
   6. Re:  Haskell GUI library (Henk-Jan van Tuyl)
   7. Re:  Return a Foldable instance (Federico Mastellone)


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

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

On Wed, May 4, 2011 at 1:22 PM, Chadda? Fouch? <[email protected]> wrote:
> On Wed, May 4, 2011 at 2:21 PM, Federico Mastellone <[email protected]> wrote:
>> So, is it even possible to write a function with this type: getValues
>> :: Foldable f => k -> MultiMap k v -> f v ???
>>
>
> It isn't since there's nothing in the Foldable class that allows you
> to build one (only to consume one, do a fold over it, no "unfold").
>

As it's impossible to build a function like this one below:
getValues :: Foldable f => k -> MultiMap k v -> f v
I know that it may be too difficult, but wouldn't be great to make
this type illegal in Haskell ?
You can't make a function that returns a class that has no method that
builds an instance of that class unless another parameter of the
function is of that class or allows you to build one instance of that
class.

> But since this signature is _not_ the one you really want (it says you
> will deliver any Foldable your caller asks for), this isn't a problem,
> what you really want is :
>
>> data ExFoldable a = forall f . (Foldable f) => ExFoldable (f a)

This will do the trick and it is better than maintaining an
intermediate structure as I previously said.
Thank you and Alexander for this tip.

>>
>> instance Foldable ExFoldable where
>> ? foldr f z (Exfoldable xs) = foldr f z xs
>>
>> getValues :: k -> MultiMap k v -> Exfoldable v
>> getValues k mm = ExFoldable (yourLookupFunction mm k)
>>
>
> or something like that.
> With this signature you're really saying you will deliver a Foldable
> without telling which one, like in OO.
>
> Now I'm not sure if this is such a good idea since as other have
> pointed, delivering a list is a pretty good and mostly simple option
> where the list itself is often optimized away. But if you really want
> it, Haskell can do it.
>
> (You'll need to activate some extensions to allow this)
>
> --
> Jeda?
>



-- 
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: Wed, 4 May 2011 13:22:20 -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 Wed, May 4, 2011 at 1:08 PM, Federico Mastellone <[email protected]> wrote:
> On Wed, May 4, 2011 at 1:22 PM, Chadda? Fouch? <[email protected]> 
> wrote:
>> On Wed, May 4, 2011 at 2:21 PM, Federico Mastellone <[email protected]> wrote:
>>> So, is it even possible to write a function with this type: getValues
>>> :: Foldable f => k -> MultiMap k v -> f v ???
>>>
>>
>> It isn't since there's nothing in the Foldable class that allows you
>> to build one (only to consume one, do a fold over it, no "unfold").
>>
>
> As it's impossible to build a function like this one below:
> getValues :: Foldable f => k -> MultiMap k v -> f v
> I know that it may be too difficult, but wouldn't be great to make
> this type illegal in Haskell ?
> You can't make a function that returns a class that has no method that
> builds an instance of that class unless another parameter of the
> function is of that class or allows you to build one instance of that
> class.
>
>> But since this signature is _not_ the one you really want (it says you
>> will deliver any Foldable your caller asks for), this isn't a problem,
>> what you really want is :
>>
>>> data ExFoldable a = forall f . (Foldable f) => ExFoldable (f a)
>
> This will do the trick and it is better than maintaining an
> intermediate structure as I previously said.
> Thank you and Alexander for this tip.
>

I really think that this is just a more complicated version of making
your own wrapper type as discussed above.

Both types:

> data ExFoldable a = ...

and

> newtype ResultType a = Result [a]
>  deriving (Foldable)

offer up the exact same API to the caller. The second one is just
easier to understand and requires fewer advanced language extensions.

And if your goal is optimization, the 'ExFoldable' version is likely
to be slower for the caller to fold over than if you had converted the
result to a list and returned it, because with the 'ExFoldable' return
type the compiler has less information to work with at the call site.
Keep in mind I haven't benchmarked any of this :-)

Antoine



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

Message: 3
Date: Wed, 4 May 2011 14:51:41 -0400
From: James Cook <[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; format=flowed; delsp=yes

On May 4, 2011, at 2:22 PM, Antoine Latter wrote:

> On Wed, May 4, 2011 at 1:08 PM, Federico Mastellone  
> <[email protected]> wrote:
>> On Wed, May 4, 2011 at 1:22 PM, Chadda? Fouch? <[email protected] 
>> > wrote:
>>> On Wed, May 4, 2011 at 2:21 PM, Federico Mastellone <[email protected] 
>>> > wrote:
>>>> So, is it even possible to write a function with this type:  
>>>> getValues
>>>> :: Foldable f => k -> MultiMap k v -> f v ???
>>>>
>>>
>>> It isn't since there's nothing in the Foldable class that allows you
>>> to build one (only to consume one, do a fold over it, no "unfold").
>>>
>>
>> As it's impossible to build a function like this one below:
>> getValues :: Foldable f => k -> MultiMap k v -> f v
>> I know that it may be too difficult, but wouldn't be great to make
>> this type illegal in Haskell ?
>> You can't make a function that returns a class that has no method  
>> that
>> builds an instance of that class unless another parameter of the
>> function is of that class or allows you to build one instance of that
>> class.
>>
>>> But since this signature is _not_ the one you really want (it says  
>>> you
>>> will deliver any Foldable your caller asks for), this isn't a  
>>> problem,
>>> what you really want is :
>>>
>>>> data ExFoldable a = forall f . (Foldable f) => ExFoldable (f a)
>>
>> This will do the trick and it is better than maintaining an
>> intermediate structure as I previously said.
>> Thank you and Alexander for this tip.
>>
>
> I really think that this is just a more complicated version of making
> your own wrapper type as discussed above.
>
> Both types:
>
>> data ExFoldable a = ...
>
> and
>
>> newtype ResultType a = Result [a]
>> deriving (Foldable)
>
> offer up the exact same API to the caller. The second one is just
> easier to understand and requires fewer advanced language extensions.
>
> And if your goal is optimization, the 'ExFoldable' version is likely
> to be slower for the caller to fold over than if you had converted the
> result to a list and returned it, because with the 'ExFoldable' return
> type the compiler has less information to work with at the call site.
> Keep in mind I haven't benchmarked any of this :-)
>

If you're really dead-set on eliminating lists, I'd suggest that your  
'ResultType' just be a structure that contains a reference to a  
MultiMap and a key, or whatever other information you need to be able  
to implement foldMap.  For example:

 > data FoldableView k v = FoldableView k (MultiMap k v)
 > instance Foldable (FoldableView k) where {- ... -}
 >
 > getValues :: k -> MultiMap k v -> FoldableView k v
 > getValues k m = FoldableView k m

Then your users really can just fold over (a very lightweight view of)  
the MultiMap itself, with no intermediate structures of any kind.

-- James


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

Message: 4
Date: Wed, 4 May 2011 15:26:38 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I still have a question about this.  If you look at the
Text.Regex.Posix library you can do this:

> "asdf" =~ "asdf" :: String
"asdf"

> "asdf" =~ "asdf" :: Int
1
>:t "asdf" =~ "asdf"
"asdf" =~ "asdf" :: (RegexContext Regex [Char] target) => target

So, there is something similar to what the op was asking for.  Why is
this possible, but his is not?

(sorry james meant to reply to all)

On Wed, May 4, 2011 at 2:51 PM, James Cook <[email protected]> wrote:
> On May 4, 2011, at 2:22 PM, Antoine Latter wrote:
>
>> On Wed, May 4, 2011 at 1:08 PM, Federico Mastellone <[email protected]>
>> wrote:
>>>
>>> On Wed, May 4, 2011 at 1:22 PM, Chadda? Fouch? <[email protected]>
>>> wrote:
>>>>
>>>> On Wed, May 4, 2011 at 2:21 PM, Federico Mastellone <[email protected]>
>>>> wrote:
>>>>>
>>>>> So, is it even possible to write a function with this type: getValues
>>>>> :: Foldable f => k -> MultiMap k v -> f v ???
>>>>>
>>>>
>>>> It isn't since there's nothing in the Foldable class that allows you
>>>> to build one (only to consume one, do a fold over it, no "unfold").
>>>>
>>>
>>> As it's impossible to build a function like this one below:
>>> getValues :: Foldable f => k -> MultiMap k v -> f v
>>> I know that it may be too difficult, but wouldn't be great to make
>>> this type illegal in Haskell ?
>>> You can't make a function that returns a class that has no method that
>>> builds an instance of that class unless another parameter of the
>>> function is of that class or allows you to build one instance of that
>>> class.
>>>
>>>> But since this signature is _not_ the one you really want (it says you
>>>> will deliver any Foldable your caller asks for), this isn't a problem,
>>>> what you really want is :
>>>>
>>>>> data ExFoldable a = forall f . (Foldable f) => ExFoldable (f a)
>>>
>>> This will do the trick and it is better than maintaining an
>>> intermediate structure as I previously said.
>>> Thank you and Alexander for this tip.
>>>
>>
>> I really think that this is just a more complicated version of making
>> your own wrapper type as discussed above.
>>
>> Both types:
>>
>>> data ExFoldable a = ...
>>
>> and
>>
>>> newtype ResultType a = Result [a]
>>> deriving (Foldable)
>>
>> offer up the exact same API to the caller. The second one is just
>> easier to understand and requires fewer advanced language extensions.
>>
>> And if your goal is optimization, the 'ExFoldable' version is likely
>> to be slower for the caller to fold over than if you had converted the
>> result to a list and returned it, because with the 'ExFoldable' return
>> type the compiler has less information to work with at the call site.
>> Keep in mind I haven't benchmarked any of this :-)
>>
>
> If you're really dead-set on eliminating lists, I'd suggest that your
> 'ResultType' just be a structure that contains a reference to a MultiMap and
> a key, or whatever other information you need to be able to implement
> foldMap. ?For example:
>
>> data FoldableView k v = FoldableView k (MultiMap k v)
>> instance Foldable (FoldableView k) where {- ... -}
>>
>> getValues :: k -> MultiMap k v -> FoldableView k v
>> getValues k m = FoldableView k m
>
> Then your users really can just fold over (a very lightweight view of) the
> MultiMap itself, with no intermediate structures of any kind.
>
> -- James
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 5
Date: Wed, 4 May 2011 16:06:38 -0400
From: James Cook <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: David McBride <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

On May 4, 2011, at 3:26 PM, David McBride wrote:

> I still have a question about this.  If you look at the
> Text.Regex.Posix library you can do this:
>
>> "asdf" =~ "asdf" :: String
> "asdf"
>
>> "asdf" =~ "asdf" :: Int
> 1
>> :t "asdf" =~ "asdf"
> "asdf" =~ "asdf" :: (RegexContext Regex [Char] target) => target
>
> So, there is something similar to what the op was asking for.  Why is
> this possible, but his is not?

Because unlike the Foldable class, the RegexContext class does provide  
functions that return values of its type parameters.    RegexContext  
is defined as follows:

 > class RegexLike regex source => RegexContext regex source target  
where
 >   match :: regex -> source -> target
 >   matchM :: Monad m => regex -> source -> m target

The 'match' function returns a value of type 'target'.  For Foldable,  
though:

 > class Foldable t where
 >   fold :: Monoid m => t m -> m
 >   foldMap :: Monoid m => (a -> m) -> t a -> m
 >   foldr :: (a -> b -> b) -> b -> t a -> b
 >   foldl :: (a -> b -> a) -> a -> t b -> a
 >   foldr1 :: (a -> a -> a) -> t a -> a
 >   foldl1 :: (a -> a -> a) -> t a -> a

None of those functions return anything that mentions "t", so the  
Foldable class is not able to construct values of an unknown Foldable  
type.

For a simpler example, the same ideas applies to more-familiar  
typeclasses defined in the Prelude.  For example, you can have  
expressions of type "Num a => a", because the Num class defines a  
function "fromInteger :: Integer -> a" - so if you have an Integer,  
you can make a value of any type that is an instance of Num.  But you  
cannot have a useful expression of type "Show a => a", because Show  
doesn't define any functions that produce values.

-- James



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

Message: 6
Date: Wed, 04 May 2011 23:50:55 +0200
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] Haskell GUI library
To: [email protected], Nathan H?sken        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
        delsp=yes

On Wed, 04 May 2011 16:06:33 +0200, Nathan H?sken  
<[email protected]> wrote:

> Hey,
>
> looking on the haskell homepage, there are a lot of gui libraries (or at
> least bindings) available.
> One big difference seems to be that some are based on "Functional
> Reactive Programming" (I do not know what that is, just read it).
>
> So if I want to write a gui in haskell, which library should I use?
> Which is stable enough? Has anyone experience with some of these gui
> libraries?
>

I tried Gtk2Hs a couple of years ago on my Windows PC and found a lot of  
serious bugs. Recently there was some discussion about problems on Mac OS  
X. The Gtk2Hs license is not suitable if you want to sell your software.

WxHaskell works fine for me and the license is very liberal.

Regards,
Henk-Jan van Tuyl


-- 
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--



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

Message: 7
Date: Thu, 5 May 2011 01:01:12 -0300
From: Federico Mastellone <[email protected]>
Subject: Re: [Haskell-beginners] Return a Foldable instance
To: James Cook <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Thanks James and Antoine, making a new type like ResultType or
FoldableView seems to be an elegant solution!

On Wed, May 4, 2011 at 3:51 PM, James Cook <[email protected]> wrote:
> On May 4, 2011, at 2:22 PM, Antoine Latter wrote:
>
>> On Wed, May 4, 2011 at 1:08 PM, Federico Mastellone <[email protected]>
>> wrote:
>>>
>>> On Wed, May 4, 2011 at 1:22 PM, Chadda? Fouch? <[email protected]>
>>> wrote:
>>>>
>>>> On Wed, May 4, 2011 at 2:21 PM, Federico Mastellone <[email protected]>
>>>> wrote:
>>>>>
>>>>> So, is it even possible to write a function with this type: getValues
>>>>> :: Foldable f => k -> MultiMap k v -> f v ???
>>>>>
>>>>
>>>> It isn't since there's nothing in the Foldable class that allows you
>>>> to build one (only to consume one, do a fold over it, no "unfold").
>>>>
>>>
>>> As it's impossible to build a function like this one below:
>>> getValues :: Foldable f => k -> MultiMap k v -> f v
>>> I know that it may be too difficult, but wouldn't be great to make
>>> this type illegal in Haskell ?
>>> You can't make a function that returns a class that has no method that
>>> builds an instance of that class unless another parameter of the
>>> function is of that class or allows you to build one instance of that
>>> class.
>>>
>>>> But since this signature is _not_ the one you really want (it says you
>>>> will deliver any Foldable your caller asks for), this isn't a problem,
>>>> what you really want is :
>>>>
>>>>> data ExFoldable a = forall f . (Foldable f) => ExFoldable (f a)
>>>
>>> This will do the trick and it is better than maintaining an
>>> intermediate structure as I previously said.
>>> Thank you and Alexander for this tip.
>>>
>>
>> I really think that this is just a more complicated version of making
>> your own wrapper type as discussed above.
>>
>> Both types:
>>
>>> data ExFoldable a = ...
>>
>> and
>>
>>> newtype ResultType a = Result [a]
>>> deriving (Foldable)
>>
>> offer up the exact same API to the caller. The second one is just
>> easier to understand and requires fewer advanced language extensions.
>>
>> And if your goal is optimization, the 'ExFoldable' version is likely
>> to be slower for the caller to fold over than if you had converted the
>> result to a list and returned it, because with the 'ExFoldable' return
>> type the compiler has less information to work with at the call site.
>> Keep in mind I haven't benchmarked any of this :-)
>>
>
> If you're really dead-set on eliminating lists, I'd suggest that your
> 'ResultType' just be a structure that contains a reference to a MultiMap and
> a key, or whatever other information you need to be able to implement
> foldMap. ?For example:
>
>> data FoldableView k v = FoldableView k (MultiMap k v)
>> instance Foldable (FoldableView k) where {- ... -}
>>
>> getValues :: k -> MultiMap k v -> FoldableView k v
>> getValues k m = FoldableView k m
>
> Then your users really can just fold over (a very lightweight view of) the
> MultiMap itself, with no intermediate structures of any kind.
>
> -- James



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


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

Reply via email to