[Haskell-cafe] CPS versus Pattern Matching performance

Jonathan Cast jcast at ou.edu
Tue Jul 10 11:22:51 EDT 2007


On Tuesday 10 July 2007, Tony Morris wrote:
> Thanks Don,
> Is your explanation specific to maybe? Or does that apply to all functions?
>
> Suppose the following function for lists:
>
> f :: [a] -> b -> (a -> [a] -> b) -> b
>
> ...instead of pattern matching [] and (x:xs)

Of course.  GHC doesn't know anything about maybe; all it sees is:

1. One-liner:

maybe f x mb = case mb of { Just x' -> f x'; Nothing -> x }

2. Non-recursive

And it inlines like crazy.  If you had quite a large data type, the number of 
case alternatives /might/ put you over GHC's inlining threshold, but that 
(ridiculous) scenario is the only thing that'll keep the argument from 
generalizing to your use case.

<snip>

Jonathan Cast
http://sourceforge.net/projects/fid-core
http://sourceforge.net/projects/fid-emacs


More information about the Haskell-Cafe mailing list