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:  From Functional Dependencies to Type Families
      (Daniel Fischer)
   2. Re:  Eq a => [a]->[a] (I. J. Kennedy)
   3. Re:  Eq a => [a]->[a] (Patrick LeBoutillier)
   4. Re: Re: [Haskell-beginners] From Functional Dependencies to
      Type      Families ([email protected])
   5. Re:  Eq a => [a]->[a] (Daniel Fischer)
   6. Re:  From Functional Dependencies to Type Families
      (Daniel Fischer)


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

Message: 1
Date: Thu, 3 Dec 2009 12:30:08 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] From Functional Dependencies to Type
        Families
To: [email protected]
Cc: Adolfo Builes <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-15"

Am Donnerstag 03 Dezember 2009 04:06:43 schrieb Adolfo Builes:
> > {-# OPTIONS -fglasgow-exts #-}
> >
> > module VarArg where
> > import Data.FiniteMap -- for an example below
> >
> > class BuildList a r | r-> a where
> > build' :: [a] -> a -> r
> >
> > instance BuildList a [a] where
> > build' l x = reverse$ x:l
> >
> > instance BuildList a r => BuildList a (a->r) where
> > build' l x y = build'(x:l) y
> >
> > --build :: forall r a. (BuildList a r) => a -> r
> > build x = build' [] x
>
> I'm trying to replace the code below to work with type families, I started
> out replacing the definition of class with :
>
> class BuildList r where
>     type Build r
>     build' :: [Build r] -> Build r -> r
>
> follow by the instance for [a] resulting in
>
> instance BuildList [a] where
>     type Build [a] = a
>     build' l x = reverse $ x:l
>
> Until here, everything is working, and I'm able to do
>
> > build' [2,3,4] 1 :: [Integer]
> > [4,3,2,1]
>
> then I move on to the next instance (a -> r) with
>
> instance BuildList r => BuildList (a-> r) where
>      type Build (a -> r) =  a
>      build' l x =  \ y -> build'(x:l) y
>
>
> But I get the following error
>
>     Couldn't match expected type `Build r' against inferred type `a'
>       `a' is a rigid type variable bound by
>           the instance declaration at /home/adolfo/foo.hs:347:35
>     In the first argument of `(:)', namely `x'
>     In the first argument of `build'', namely `(x : l)'
>     In the expression: build' (x : l) y
>
> then I try with :
>
> instance BuildList r => BuildList (a-> r) where
>      type Build (a -> r) =  Build r
>      build' l x =  \ y -> build'(x:l) y
>
>
> And I get
>
>       Couldn't match expected type `a' against inferred type `Build r'
>       `a' is a rigid type variable bound by
>           the instance declaration at /home/adolfo/foo.hs:347:35
>       Expected type: [Build r]
>       Inferred type: [Build (a -> r)]
>     In the second argument of `(:)', namely `l'
>     In the first argument of `build'', namely `(x : l)'
>
> I have been trying to figure out, which type should it be, but I haven't
> found the correct one, any ideas ?

I think

instance (BuildList r, Build r ~ a) => BuildList (a -> r) where
    type Build (a -> r) = a
    build' l x = \y -> build' (x:l) y

should work.

You need to tell the compiler explicitly that a and Build r should be the same 
type.

>
>
> Thanks
>
> -
> Adolfo Builes



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

Message: 2
Date: Thu, 3 Dec 2009 10:38:50 -0600
From: "I. J. Kennedy" <[email protected]>
Subject: Re: [Haskell-beginners] Eq a => [a]->[a]
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Thanks for the response and thanks for the implementation.

 f xs = map fst $ takeWhile (uncurry S.notMember) (zip xs cums)
   where cums = scanl (flip S.insert) S.empty xs

I absolutely love (what I know so far) Haskell, but I must say
when I see this kind of function, or write it myself, I am strongly
reminded of programming in Forth thirty years ago.


On Wed, Dec 2, 2009 at 8:46 PM, Brent Yorgey <[email protected]> wrote:
>
> On Wed, Dec 02, 2009 at 06:47:54PM -0600, I. J. Kennedy wrote:
> > I am looking for a function
> >  f::Eq a => [a]->[a]
> > that takes a list and returns the longest
> > initial segment of the list for which all
> > the elements are distinct.
> >
> > For example f [2,3,6,4,3,5] = [2,3,6,4].
> >
> > I didn't see anything that matched using Hoogle,
> > but I thought this might be a common enough
> > operation that this function might exists somewhere
> > in the standard packages.
>
> Not that I know of.  Here's how I would implement it (although you may
> enjoy trying to implement it yourself):
>
>  import qualified Data.Set as S
>
>  f xs = map fst $ takeWhile (uncurry S.notMember) (zip xs cums)
>    where cums = scanl (flip S.insert) S.empty xs
>
> It works by incrementally building up a list of sets of the elements
> found in prefixes of the list (with scanl), then goes down the list
> (takeWhile) checking that each element isn't already in the
> corresponding set of elements.
>
> -Brent
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners


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

Message: 3
Date: Thu, 3 Dec 2009 13:16:56 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Eq a => [a]->[a]
To: "I. J. Kennedy" <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Dec 3, 2009 at 11:38 AM, I. J. Kennedy <[email protected]> wrote:
> Thanks for the response and thanks for the implementation.
>
>  f xs = map fst $ takeWhile (uncurry S.notMember) (zip xs cums)
>    where cums = scanl (flip S.insert) S.empty xs

When I first saw this function I thought it looked complicated, and in
a naive attempt
to simplify it I came up with this:

import Data.List

f :: (Eq a) => [a] -> [a]
f = last . takeWhile (\l -> nub l == l) . inits

This worked well for short lists, but started to drag for large lists,
especially if the result was long, i.e. for input like ([1 .. 1000] ++
[1]).

Brent's version seems very fast no matter the list length. Maybe
someone can provide a O() analysis?

Patrick

>
> I absolutely love (what I know so far) Haskell, but I must say
> when I see this kind of function, or write it myself, I am strongly
> reminded of programming in Forth thirty years ago.
>
>
> On Wed, Dec 2, 2009 at 8:46 PM, Brent Yorgey <[email protected]> wrote:
>>
>> On Wed, Dec 02, 2009 at 06:47:54PM -0600, I. J. Kennedy wrote:
>> > I am looking for a function
>> >  f::Eq a => [a]->[a]
>> > that takes a list and returns the longest
>> > initial segment of the list for which all
>> > the elements are distinct.
>> >
>> > For example f [2,3,6,4,3,5] = [2,3,6,4].
>> >
>> > I didn't see anything that matched using Hoogle,
>> > but I thought this might be a common enough
>> > operation that this function might exists somewhere
>> > in the standard packages.
>>
>> Not that I know of.  Here's how I would implement it (although you may
>> enjoy trying to implement it yourself):
>>
>>  import qualified Data.Set as S
>>
>>  f xs = map fst $ takeWhile (uncurry S.notMember) (zip xs cums)
>>    where cums = scanl (flip S.insert) S.empty xs
>>
>> It works by incrementally building up a list of sets of the elements
>> found in prefixes of the list (with scanl), then goes down the list
>> (takeWhile) checking that each element isn't already in the
>> corresponding set of elements.
>>
>> -Brent
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 4
Date: Thu, 03 Dec 2009 18:51:31 +0000
From: [email protected]
Subject: Re: Re: [Haskell-beginners] From Functional Dependencies to
        Type    Families
To: Daniel Fischer <[email protected]>,
        [email protected],       Adolfo Builes 
<[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

> You need to tell the compiler explicitly that a and Build r should be the  
> same type.

Thanks Daniel :). That was the trick .

It's the first time that I see "~", is that from -XUndecidableInstances ? .

Also, thanks to Stephen.

-
Adolfo

On Dec 3, 2009 6:30am, Daniel Fischer <[email protected]> wrote:
> Am Donnerstag 03 Dezember 2009 04:06:43 schrieb Adolfo Builes:

> > > {-# OPTIONS -fglasgow-exts #-}

> > >

> > > module VarArg where

> > > import Data.FiniteMap -- for an example below

> > >

> > > class BuildList ar | r-> a where

> > > build' :: [a] -> a -> r

> > >

> > > instance BuildList a [a] where

> > > build' lx = reverse$ x:l

> > >

> > > instance BuildList ar => BuildList a (a->r) where

> > > build' lxy = build'(x:l) y

> > >

> > > --build :: forall r a. (BuildList ar) => a -> r

> > > build x = build' [] x

> >

> > I'm trying to replace the code below to work with type families, I  
> started

> > out replacing the definition of class with :

> >

> > class BuildList r where

> > type Build r

> > build' :: [Build r] -> Build r -> r

> >

> > follow by the instance for [a] resulting in

> >

> > instance BuildList [a] where

> > type Build [a] = a

> > build' lx = reverse $ x:l

> >

> > Until here, everything is working, and I'm able to do

> >

> > > build' [2,3,4] 1 :: [Integer]

> > > [4,3,2,1]

> >

> > then I move on to the next instance (a -> r) with

> >

> > instance BuildList r => BuildList (a-> r) where

> > type Build (a -> r) = a

> > build' lx = \ y -> build'(x:l) y

> >

> >

> > But I get the following error

> >

> > Couldn't match expected type `Build r' against inferred type `a'

> > `a' is a rigid type variable bound by

> > the instance declaration at /home/adolfo/foo.hs:347:35

> > In the first argument of `(:)', namely `x'

> > In the first argument of `build'', namely `(x : l)'

> > In the expression: build' (x : l) y

> >

> > then I try with :

> >

> > instance BuildList r => BuildList (a-> r) where

> > type Build (a -> r) = Build r

> > build' lx = \ y -> build'(x:l) y

> >

> >

> > And I get

> >

> > Couldn't match expected type `a' against inferred type `Build r'

> > `a' is a rigid type variable bound by

> > the instance declaration at /home/adolfo/foo.hs:347:35

> > Expected type: [Build r]

> > Inferred type: [Build (a -> r)]

> > In the second argument of `(:)', namely `l'

> > In the first argument of `build'', namely `(x : l)'

> >

> > I have been trying to figure out, which type should it be, but I haven't

> > found the correct one, any ideas ?



> I think



> instance (BuildList r, Build r ~ a) => BuildList (a -> r) where

> type Build (a -> r) = a

> build' lx = \y -> build' (x:l) y



> should work.



> You need to tell the compiler explicitly that a and Build r should be the  
> same type.



> >

> >

> > Thanks

> >

> > -

> > Adolfo Builes



-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091203/1838b533/attachment-0001.html

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

Message: 5
Date: Thu, 3 Dec 2009 20:00:00 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Eq a => [a]->[a]
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag 03 Dezember 2009 19:16:56 schrieb Patrick LeBoutillier:
> On Thu, Dec 3, 2009 at 11:38 AM, I. J. Kennedy <[email protected]> wrote:
> > Thanks for the response and thanks for the implementation.
> >
> >  f xs = map fst $ takeWhile (uncurry S.notMember) (zip xs cums)
> >    where cums = scanl (flip S.insert) S.empty xs
>
> When I first saw this function I thought it looked complicated, and in
> a naive attempt
> to simplify it I came up with this:
>
> import Data.List
>
> f :: (Eq a) => [a] -> [a]
> f = last . takeWhile (\l -> nub l == l) . inits
>
> This worked well for short lists, but started to drag for large lists,
> especially if the result was long, i.e. for input like ([1 .. 1000] ++
> [1]).
>
> Brent's version seems very fast no matter the list length. Maybe
> someone can provide a O() analysis?

nub is O((length l)^2), thus nubbing all initial segments of l up to length k 
is O(k^3).
Data.Set.insert is O(log size), so calculating the first k elements of
scanl (flip insert) empty l is O(log (k!)) = O(k log k). The membership test is 
again 
O(log size), zipping is O(k), so altogether O(k*(log k)^2).

You can remove one factor (log k) by checking the size instead of membership:

f xs = map fst . takeWhile snd . zip xs . zipWith ((. S.size) . (==)) [1 .. ] $ 
cums
    where
      cums = tail $ scanl (flip S.insert) S.empty xs

I think for *short* nubbed prefixes, Brent's version is faster, but I've no 
idea yet when 
short stops (10, 100, 1000?).

A quadratic (I think) version if the type of elements doesn't belong to Ord:

f (x:xs) = x:f (takeWhile (/= x) xs)
f [] = []

>
> Patrick
>
> > I absolutely love (what I know so far) Haskell, but I must say
> > when I see this kind of function, or write it myself, I am strongly
> > reminded of programming in Forth thirty years ago.
> >
> > On Wed, Dec 2, 2009 at 8:46 PM, Brent Yorgey <[email protected]> wrote:
> >> On Wed, Dec 02, 2009 at 06:47:54PM -0600, I. J. Kennedy wrote:
> >> > I am looking for a function
> >> >  f::Eq a => [a]->[a]
> >> > that takes a list and returns the longest
> >> > initial segment of the list for which all
> >> > the elements are distinct.
> >> >
> >> > For example f [2,3,6,4,3,5] = [2,3,6,4].
> >> >
> >> > I didn't see anything that matched using Hoogle,
> >> > but I thought this might be a common enough
> >> > operation that this function might exists somewhere
> >> > in the standard packages.
> >>
> >> Not that I know of.  Here's how I would implement it (although you may
> >> enjoy trying to implement it yourself):
> >>
> >>  import qualified Data.Set as S
> >>
> >>  f xs = map fst $ takeWhile (uncurry S.notMember) (zip xs cums)
> >>    where cums = scanl (flip S.insert) S.empty xs
> >>
> >> It works by incrementally building up a list of sets of the elements
> >> found in prefixes of the list (with scanl), then goes down the list
> >> (takeWhile) checking that each element isn't already in the
> >> corresponding set of elements.
> >>
> >> -Brent




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

Message: 6
Date: Thu, 3 Dec 2009 20:13:18 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] From Functional Dependencies to Type
        Families
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-15"

Am Donnerstag 03 Dezember 2009 19:51:31 schrieb [email protected]:
> > You need to tell the compiler explicitly that a and Build r should be the
> > same type.
>
> Thanks Daniel :). That was the trick .
>
> It's the first time that I see "~", is that from -XUndecidableInstances ? .

No, it's part of the TypeFamilies extension.

http://www.haskell.org/haskellwiki/GHC/Type_families#Equality_constraints

and http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html 
(much 
overlap).

It requires UndecidableInstances ( 
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-
extensions.html#undecidable-instances ) because both type variables from the 
instance head 
appear in the constraints, so the compiler can't prove that type inference will 
terminate. 
You tell it that you think it will terminate, and it goes forth.

Which reminds me:

 > {-# OPTIONS -fglasgow-exts #-}

is deprecated. Use LANGUAGE pragmas, in this case

{-# LANGUAGE TypeFamilies, UndecidableInstances #-}

({-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-} for the FunDep 
version).


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

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


End of Beginners Digest, Vol 18, Issue 3
****************************************

Reply via email to