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: Function definition order and performance
(Aleksandar Dimitrov)
2. List -> Map -> List, to add duplicates? (Tom Murphy)
3. Re: List -> Map -> List, to add duplicates?
(Arlen Christian Mart Cuss)
4. Re: List -> Map -> List, to add duplicates? (Ertugrul Soeylemez)
5. Re: Function definition order and performance
(Aleksandar Dimitrov)
6. Re: Confused by type constraints (Guy)
7. Re: Function definition order and performance (Daniel Fischer)
8. Re: Confused by type constraints (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Mon, 6 Jun 2011 22:28:25 +0200
From: Aleksandar Dimitrov <[email protected]>
Subject: Re: [Haskell-beginners] Function definition order and
performance
To: Zhi-Qiang Lei <[email protected]>
Cc: Haskell Beginer <[email protected]>
Message-ID: <20110606202825.GC6999@minsk>
Content-Type: text/plain; charset="us-ascii"
Hi,
> If the matching proceeds from top to bottom, will the same functions defined
> in different order have different performance strictly? Thanks.
You should only see a performance difference at all in the case of very
complicated (run-time-wise) guards on pattern matches.
Usually, your pattern matches will have to be placed in a certain order for
*semantic* purposes (i.e. for them to not be overlapping.)
I could imagine a scenario in which a complicated guard on a pattern match,
which could be otherwise rearranged without changing its semantics, on a
function that is itself in the oft-executed loop of an inner function of an
algorithm with high complexity *could* have a noticeable impact on performance.
In other words, premature optimization is the root of all evil.
Are you concerned about a particular case? Example code could help us determine
the reason for slowness.
Regards,
Aleks
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110606/dbd324f5/attachment-0001.pgp>
------------------------------
Message: 2
Date: Mon, 6 Jun 2011 21:43:13 -0400
From: Tom Murphy <[email protected]>
Subject: [Haskell-beginners] List -> Map -> List, to add duplicates?
To: beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi All,
This seems like an inefficient way to do what I'm trying to do.
I'd really appreciate any suggestions or comments:
import qualified Data.Map as Map
data Currency = Dollar
| Yen
| XP
| Health
| Street_Cred
| Peso
deriving (Show, Eq, Ord) -- why ord?
withDups = [(30, Dollar), (-20, Street_Cred), (-2, Dollar), (30, XP),
(15, Peso), (30, XP)]
flipAssoc (a, b) = (b, a)
noDups = Map.fromListWith (+) (map flipAssoc withDups)
final = map flipAssoc $ Map.toList noDups
Thanks for your time!
Tom
------------------------------
Message: 3
Date: Tue, 07 Jun 2011 16:25:23 +1000
From: Arlen Christian Mart Cuss <[email protected]>
Subject: Re: [Haskell-beginners] List -> Map -> List, to add
duplicates?
To: Tom Murphy <[email protected]>
Cc: beginners <[email protected]>
Message-ID: <1307427923.2943.11.camel@azayaka>
Content-Type: text/plain; charset="UTF-8"
On Mon, 2011-06-06 at 21:43 -0400, Tom Murphy wrote:
> Hi All,
> This seems like an inefficient way to do what I'm trying to do.
Intuition tells me it's inefficient, but on second thought:
You're trying to accumulate (and add) values for a matching given index
(in this case, the second argument of the tuple -- btw flipAssoc is
available as Data.Tuple.swap).
That necessarily means traversing the list, the whole time retaining
quick random access to update the value associated with a particular
index -- which is exactly what Maps are for, right? The code does
exactly what you want with the least complexity, AFAICS, though of
course it'd be nicer if there wasn't this List->Map->List.
I'd argue this isn't inefficient to do what you're doing, but I'd look
for a more general optimisation in the context this code comes from in
order to avoid such an operation from the start entirely. :) That may
involve keeping a single map, and updating it progressively as each pair
comes in (which you can do for the initial creation too, with a fold).
Cheers,
Arlen
------------------------------
Message: 4
Date: Tue, 7 Jun 2011 09:18:25 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] List -> Map -> List, to add
duplicates?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Tom Murphy <[email protected]> wrote:
> This seems like an inefficient way to do what I'm trying to do.
> I'd really appreciate any suggestions or comments:
>
> [...]
I don't see why it would be inefficient, although you don't exactly need
the map data structure. A set would totally suffice, and since tuples
have an Ord instance, the following should work (untested!):
import qualified Data.Set as S
newtype Humanism a = Human { animal :: a }
instance Eq (Humanism a) where _ == _ = True
instance Ord (Humanism a) where compare _ _ = EQ
swap :: (a,b) -> (b,a)
swap (x,y) = (y,x)
nubBySnd :: Ord b => [(a,b)] -> [(a,b)]
nubBySnd =
map (swap . first animal) .
S.toList . S.fromList .
map (first Human . swap)
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
------------------------------
Message: 5
Date: Tue, 7 Jun 2011 10:33:08 +0200
From: Aleksandar Dimitrov <[email protected]>
Subject: Re: [Haskell-beginners] Function definition order and
performance
To: Zhi-Qiang Lei <[email protected]>
Cc: [email protected]
Message-ID: <20110607083308.GA11919@minsk>
Content-Type: text/plain; charset="utf-8"
> I have not met a performance issue yet. Thanks. I'm just a beginner.
> I was just wondering how they will be different. For instance,
>
> safeHead [] = Nothing
> safeHead (x : xs) = Just x
>
> vesus
>
> safeHead (x : xs) = Just x
> safeHead [] = Nothing
There is no difference in this case. GHC compiles that down to the very same
thing.
Given this file:
> safeHead [] = Nothing
> safeHead (x:xs) = Just x
>
> safeHead' (x:xs) = Just x
> safeHead' [] = Nothing
You can look at the ghc core (the intermediate compiler output produced by GHC)
to find out what the difference is. Core is basically like machine-generated
Haskell with some syntactical oddities. Don Steward's ghc-core program on
hackage displays core nicely.
Here are the relevant core blocks:
> safeHead' =
> \ (@ a_acw) (ds_dcU :: [a_acw]) ->
> case ds_dcU of _ {
> [] -> Data.Maybe.Nothing @ a_acw;
> : x_abJ xs_abK -> Data.Maybe.Just @ a_acw x_abJ
> }
> safeHead =
> \ (@ a_acy) (ds_dcX :: [a_acy]) ->
> case ds_dcX of _ {
> [] -> Data.Maybe.Nothing @ a_acy;
> : x_abx xs_aby -> Data.Maybe.Just @ a_acy x_abx
> }
And, well, they're exactly the same (sans a few differences in variable naming,
which have no impact whatsoever.) Your pattern match gets compiled down to a
case pattern match, and the arguments are re-ordered. While you *can* look at
the assembly, from here on out, it doesn't make sense, because GHC uses Core as
a basis for compilation, *not* your code (it does your code as a basis for
Core.)
For completion's sake, the assembly, which is unexcitingly identical in both
cases, is below. Note that the only thing interesting here is that safeHead' got
translated to safeHeadzq, which is? well. Cute, and nothing else.
Really though, if you're still at RWH, chapter three, it's Ok to be interested
in these kinds of subtle performance differences, but you shouldn't concentrate
on them.
At this point, you can trust the compiler to optimize your code enough, and you
can worry about performance if your program is actually slow.
Happy Haskell hacking :-)
Aleks
Main_safeHeadzq_closure:
.quad Main_safeHeadzq_info
.text
.align 8
.quad 0
.quad 32
se2_info:
.Lcek:
movq %rbx,%rax
andq $7,%rax
cmpq $2,%rax
jae .Lcel
movl $base_DataziMaybe_Nothing_closure+1,%ebx
addq $8,%rbp
jmp *0(%rbp)
.Lcel:
addq $16,%r12
cmpq 144(%r13),%r12
ja .Lces
movq $base_DataziMaybe_Just_con_info,-8(%r12)
movq 6(%rbx),%rax
movq %rax,0(%r12)
leaq -6(%r12),%rbx
addq $8,%rbp
jmp *0(%rbp)
.Lces:
movq $16,184(%r13)
.Lceq:
jmp *-16(%r13)
.text
.align 8
.quad 4294967301
.quad 0
.quad 15
Main_safeHead_closure:
.quad Main_safeHead_info
.text
.align 8
.quad 0
.quad 32
seJ_info:
.Lcf1:
movq %rbx,%rax
andq $7,%rax
cmpq $2,%rax
jae .Lcf2
movl $base_DataziMaybe_Nothing_closure+1,%ebx
addq $8,%rbp
jmp *0(%rbp)
.Lcf2:
addq $16,%r12
cmpq 144(%r13),%r12
ja .Lcf9
movq $base_DataziMaybe_Just_con_info,-8(%r12)
movq 6(%rbx),%rax
movq %rax,0(%r12)
leaq -6(%r12),%rbx
addq $8,%rbp
jmp *0(%rbp)
.Lcf9:
movq $16,184(%r13)
.Lcf7:
jmp *-16(%r13)
.text
.align 8
.quad 4294967301
.quad 0
.quad 15
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: Digital signature
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110607/3e0bf923/attachment-0001.pgp>
------------------------------
Message: 6
Date: Tue, 07 Jun 2011 11:51:00 +0300
From: Guy <[email protected]>
Subject: Re: [Haskell-beginners] Confused by type constraints
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 06/06/2011 17:31, Daniel Schoepe wrote:
> On Sun, 05 Jun 2011 19:53:50 +0300, Guy<[email protected]> wrote:
>> So in my original example, why isn't "instance Foo (Bar f)" destructing Bar
>> - and making the constraint available?
>
> The "Bar" in "instance Foo (Bar f)" is a type constructor, not a data
> constructor like the X in "s (X x)". Hence "instance Foo (Bar f)" isn't
> really deconstructing anything, but applying the type constructor Bar to
> f, whereas "s (X x)" matches a _value_ of type X.
>
> This might be a bit clearer if you name type and data constructors
> differently, e.g.:
>
> data Bar f a = Foo f => MkBar { bar :: f a }
>
> Cheers,
> Daniel
Thanks, I (think I) understand your explanation, but ... why can't GHC infer
that Bar must always be a Show, seeing as
this is the only constructor?
------------------------------
Message: 7
Date: Tue, 7 Jun 2011 10:59:26 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Function definition order and
performance
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Tuesday 07 June 2011, 10:33:08, Aleksandar Dimitrov wrote:
> Note that the only thing interesting here is that safeHead' got
> translated to safeHeadzq, which is? well. Cute, and nothing else.
It's z-encoding,
http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/SymbolNames
Kind of irritating when you come across a z-encoded name containing a z
where you don't expect z-encoding.
------------------------------
Message: 8
Date: Tue, 7 Jun 2011 11:03:37 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Confused by type constraints
To: [email protected]
Cc: Guy <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="us-ascii"
On Tuesday 07 June 2011, 10:51:00, Guy wrote:
> On 06/06/2011 17:31, Daniel Schoepe wrote:
> > On Sun, 05 Jun 2011 19:53:50 +0300, Guy<[email protected]>
wrote:
> >> So in my original example, why isn't "instance Foo (Bar f)"
> >> destructing Bar - and making the constraint available?
> >
> > The "Bar" in "instance Foo (Bar f)" is a type constructor, not a data
> > constructor like the X in "s (X x)". Hence "instance Foo (Bar f)"
> > isn't really deconstructing anything, but applying the type
> > constructor Bar to f, whereas "s (X x)" matches a _value_ of type X.
> >
> > This might be a bit clearer if you name type and data constructors
> > differently, e.g.:
> >
> > data Bar f a = Foo f => MkBar { bar :: f a }
> >
> > Cheers,
> > Daniel
>
> Thanks, I (think I) understand your explanation, but ... why can't GHC
> infer that Bar must always be a Show, seeing as this is the only
> constructor?
undefined :: Bar f a
You get the Foo constraint from the constructor MkBar, but there is a value
which doesn't use the constructor, so you can't get it from the type alone.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 17
*****************************************