Albert Y. C. Lai | 22 Apr 09:51 2013
Picon

conditional branching vs pattern matching: pwn3d by GHC

When I was writing
http://www.vex.net/~trebla/haskell/crossroad.xhtml
I wanted to write: branching on predicates and then using selectors is 
less efficient than pattern matching, since selectors repeat the tests 
already done by predicates.

It is only ethical to verify this claim before writing it. So here it 
goes, eval uses pattern matching, fval uses predicates and selectors:

module E where

data E = Val{fromVal::Integer} | Neg{fromNeg::E}
   | Add{fromAdd0, fromAdd1 :: E}
isVal Val{} = True
isVal _ = False
isNeg Neg{} = True
isNeg _ = False
isAdd Add{} = True
isAdd _ = False

eval (Val n) = n
eval (Neg e0) = - eval e0
eval (Add e0 e1) = eval e0 + eval e1

fval e | isVal e = fromVal e
        | isNeg e = - fval (fromNeg e)
        | isAdd e = fval (fromAdd0 e) + fval (fromAdd1 e)

Simple and clear. What could possibly go wrong!

(Continue reading)

Edward Z. Yang | 22 Apr 10:10 2013
Picon

Re: conditional branching vs pattern matching: pwn3d by GHC

Note that, unfortunately, GHC's exhaustiveness checker is *not* good
enough to figure out that your predicates are covering. :o)  Perhaps
there is an improvement to be had here.

Edward

Excerpts from Albert Y. C. Lai's message of Mon Apr 22 00:51:46 -0700 2013:
> When I was writing
> http://www.vex.net/~trebla/haskell/crossroad.xhtml
> I wanted to write: branching on predicates and then using selectors is 
> less efficient than pattern matching, since selectors repeat the tests 
> already done by predicates.
> 
> It is only ethical to verify this claim before writing it. So here it 
> goes, eval uses pattern matching, fval uses predicates and selectors:
> 
> module E where
> 
> data E = Val{fromVal::Integer} | Neg{fromNeg::E}
>    | Add{fromAdd0, fromAdd1 :: E}
> isVal Val{} = True
> isVal _ = False
> isNeg Neg{} = True
> isNeg _ = False
> isAdd Add{} = True
> isAdd _ = False
> 
> eval (Val n) = n
> eval (Neg e0) = - eval e0
> eval (Add e0 e1) = eval e0 + eval e1
(Continue reading)


Gmane