Functional pearl: a smart view on datatypes

Left-nested list concatenations, left-nested binds on the free monad, and left-nested choices in many non-determinism monads have an algorithmically bad performance. Can we solve this problem without losing the ability to pattern-match on the computation? Surprisingly, there is a deceptively simple solution: use a smart view to pattern-match on the datatype. We introduce the notion of smart view and show how it solves the problem of slow left-nested operations. In particular, we use the technique to obtain fast and simple implementations of lists, of free monads, and of two non-determinism monads.

[1]  Amr Sabry,et al.  Backtracking, interleaving, and terminating monad transformers: (functional pearl) , 2005, ICFP '05.

[2]  Janis Voigtländer,et al.  Asymptotic Improvement of Computations over Free Monads , 2008, MPC.

[3]  Graham Hutton,et al.  Factorising folds for faster functions , 2010, J. Funct. Program..

[4]  M. R. Sleep,et al.  A short note concerning lazy reduction rules for append , 1982 .

[5]  Ralf Hinze,et al.  Deriving backtracking monad transformers , 2000, ICFP '00.

[6]  KiselyovOleg,et al.  Backtracking, interleaving, and terminating monad transformers , 2005 .

[7]  Philip Wadler The Concatenate Vanishes , 1987 .

[8]  Oleg Kiselyov,et al.  Reflection without remorse: revealing a hidden sequence to speed up monadic reflection , 2014, Haskell '14.

[9]  Chris Okasaki Views for Standard ML , 2011 .

[10]  Ralf Hinze,et al.  Finger trees: a simple general-purpose data structure , 2005, Journal of Functional Programming.

[11]  Eugenio Moggi,et al.  Monad transformers as monoid transformers , 2010, Theor. Comput. Sci..

[12]  Tarmo Uustalu Explicit Binds: Effortless Efficiency with and without Trees , 2012, FLOPS.

[13]  John Hughes,et al.  A Novel Representation of Lists and its Application to the Function "reverse" , 1986, Inf. Process. Lett..

[14]  Chris Okasaki,et al.  Purely functional data structures , 1998 .

[15]  Exequiel Rivas,et al.  Notions of computation as monoids* , 2014, Journal of Functional Programming.

[16]  Janis Voigtländer,et al.  Concatenate, reverse and map vanish for free , 2002, ICFP '02.