[Haskell] Views in Haskell

Brian Hulley brianh at metamilk.com
Mon Jan 29 04:45:54 EST 2007


Claus Reinke wrote:
>
>    mapA f (nilAP -> ()) = nilA
>    mapA f (consAP -> (h,t)) = consA (f h) (mapA f t)
>
>    foldA  f n (nilAP     -> ())    = n
>    foldA  f n (consAP -> (h,t)) = f h (foldA f n t)

To me this exactly illustrates why view patterns are a bad idea: you've 
taken some concrete type, abstracted it to replace the actual structure by a 
list structure, then defined map and fold over the list structure. This 
means that map and fold can't take advantage of the actual concrete 
structure and are therefore condemned to use the inefficient linear 
structure imposed by the list abstraction.

For example implementing map over a tree directly, gives the possibility of 
parallel execution since different subtrees can be mapped independently. But 
when you view the tree abstractly as a list, no such parallel execution can 
take place. Therefore surely it is better that map and fold are defined for 
each ADT separately, with the separate definitions hidden behind a type 
class, than to attempt to define them "outside" the definition of the ADT 
using view patterns?

Brian.
-- 
http://www.metamilk.com 



More information about the Haskell-prime mailing list