## Function Inheritance: Monadic Memoization Mixins

### BibTeX

@MISC{Brown_functioninheritance:,

author = {Daniel Brown and William R. Cook},

title = {Function Inheritance: Monadic Memoization Mixins},

year = {}

}

### OpenURL

### Abstract

Abstract. Inheritance is a mechanism for incrementally modifying recursive definitions. While inheritance is typically used in object-oriented languages, inheritance also has something to offer to functional programming. In this paper we illustrate the use of inheritance in a pure functional language by developing a small library for memoization. We define monadic memoization mixins that compose—via inheritance—with an ordinary monadic function to create a memoized version of the function. A comparison of the performance of different approaches shows that memoization mixins are efficient for a small example. 1.

### Citations

432 | Mixin-based inheritance
- Bracha, Cook
- 1990
(Show Context)
Citation Context ...the form fix (memo ◦ gFib) for an appropriate generator memo with gFib. This has the effect of binding selfreference in gFib to the memoized version of the function. In this context, memo is a mixin [=-=Bracha and Cook 1990-=-]. Object-oriented languages support open recursion implicitly: every recursive definition implicitly defines a generator which can be inherited using special syntax. The same thing could be supported... |

242 | Monad transformers and modular interpreters
- Liang, Hudak, et al.
- 1995
(Show Context)
Citation Context ...btained the monad parameter m in gmUnfringe by monadifying the list computation unfringe. A more elegant way to introduce a new monad into an already monadic computation is to use monad transformers [=-=Liang et al. 1995-=-]: lift unfringe from the list monad to the transformed monad ListT m, leaving m to be bound to the memoization monad when composed with an appropriate memo mixin. This produces the function gmUnfring... |

160 | Type classes with functional dependencies
- Jones
- 2000
(Show Context)
Citation Context ... Monad n) ⇒ TransForMonad t n | t → n, n → t where toTrans :: m (n a) → t m a fromTrans :: t m a → m (n a) The type class TransForMonad uses a common Haskell extension called functional dependencies [=-=Jones 2000-=-]: the clause t → n specifies that the type t uniquely determines n in instances of this class, which the type checker will use to infer types for functions like fromTrans where n does not occur in th... |

146 | A Denotational Semantics of Inheritance
- Cook
- 1989
(Show Context)
Citation Context ...ion Inheritance is usually understood as specific to object-oriented programming and classes. However, at its core inheritance is a general mechanism for incrementally modifying recursive structures [=-=Cook 1989-=-]. Classes are one common example of recursive definitions, but inheritance also applies to types [Canning et al. 1989], functions, and modules. In this paper we illustrate the use of inheritance for ... |

139 | A denotational semantics of inheritance and its correctness
- Cook, Palsberg
- 1989
(Show Context)
Citation Context ... to fib, the self-reference must be exposed, or opened, so that it can be rebound to refer to the memoized version of the function. This is exactly what inheritance does in object-oriented languages [=-=Cook and Palsberg 1989-=-]—and the same technique can be applied to functions [Cook 1989]. To do so, we abstract the self-reference in fib as an explicit self parameter, then reconstruct fib g using an explicit fixed point.F... |

71 | Composing monads
- Jones, Duponcheel
- 1993
(Show Context)
Citation Context ...lState (runListT (memoUnfringeT mapDict a)) empty Throughout this section we have been abusing the ListT “monad transformer” because the functor ListT m is only a monad when m is a commutative monad [=-=Jones and Duponcheel 1993-=-], but the particular instances we have in mind for m—a state-like monad—are usually not commutative. However, the “memoization effect” does not necessarily bring along the full functionality of state... |

69 | Packrat parsing:: simple, powerful, lazy, linear time, functional pearl - FORD - 2002 |

28 | Stretching the storage manager: Weak pointers and stable names in haskell
- Jones, Marlow, et al.
- 1999
(Show Context)
Citation Context ...itives which allow effects outside the normal semantics of functional languages. For example, memoization can be defined on top of an unsafe state monad [Cook and Launchbury 1997] or unsafe IO monad [=-=Jones et al. 1999-=-]. Hinze [2000] defines tabulation functions that store previous results in lazy datastructures. He also transforms functions, but only needs to open recursion, not apply monadification. As a result,... |

27 |
Memo" functions and machine learning. Nature
- Michie
- 1968
(Show Context)
Citation Context ...ort for details. We think that broadening our technique to monads beyond those mentioned in Section 3 is a good challenge problem for future research. 7. Related Work Memoization is an old technique [=-=Michie 1968-=-]. Most accounts introduce a higherorder function memo to perform memoization. This approach can be implemented in procedural languages that support higher-order functions [Hall and Mayfield 1993]. A ... |

18 | Reuse by Program Transformation
- Lämmel
(Show Context)
Citation Context ...which introduces a monad parameter into a recursive function. A comprehensive review of this problem and its solution was presented by Erwig and Ren [2004] although the problem was discussed earlier [=-=Lämmel 2000-=-]. Memoization by lazy data structures appears to be unique to lazy functional programming languages. The technique is powerful, but difficult to use because it is almost invisible in the resulting pr... |

14 | Memo functions, polytypically
- Hinze
- 2000
(Show Context)
Citation Context ...mixin as in Section 2 mixinFib/nofix mixinFib/fix with harded-coded fixed-point mixinFib/nofix/IO mixinFib/nofix with Data.Array.IO instead of ST tabFib/nofix Lazy tabulating function without mixins [=-=Hinze 2000-=-] The performance of these implementations is summarized in Figure 2. The vertical axis is the time to compute fib n where n increases linearly along the horizontal axis. Each computation is performed... |

10 | Disposable memo functions
- Cook, Launchbury
- 1997
(Show Context)
Citation Context ...an also be defined in terms of more basic primitives which allow effects outside the normal semantics of functional languages. For example, memoization can be defined on top of an unsafe state monad [=-=Cook and Launchbury 1997-=-] or unsafe IO monad [Jones et al. 1999]. Hinze [2000] defines tabulation functions that store previous results in lazy datastructures. He also transforms functions, but only needs to open recursion,... |

7 |
Probabilistic functional programming in
- Erwig, Kollmansberger
- 2006
(Show Context)
Citation Context ...ification Rather than rewrite fib to explicitly use a state monad, fib can first be rewritten in monadic style over an arbitrary monad parameter. This is an automatable process called monadification [=-=Erwig and Ren 2004-=-]. This monad “hole” can then be plugged with a state monad that carries a memo table through the recursive calls of the function. The monadic version mFib of fib computes over an arbitrary monad m: m... |

5 | Improving the performance of ai software: Payoffs and pitfalls in using automatic memoization
- Hall, Mayfield
- 1993
(Show Context)
Citation Context ... old technique [Michie 1968]. Most accounts introduce a higherorder function memo to perform memoization. This approach can be implemented in procedural languages that support higher-order functions [=-=Hall and Mayfield 1993-=-]. A memo function can also be implemented as a primitive within the implementation of a lazy functional language. For example, some versions of GHC included a memo function, but it appears to have be... |

4 | Functional pearl: Trouble shared is trouble halved
- Bird, Hinze
- 2003
(Show Context)
Citation Context ...ization by lazy data structures appears to be unique to lazy functional programming languages. The technique is powerful, but difficult to use because it is almost invisible in the resulting program [=-=Bird and Hinze 2003-=-]. There is no simple way to shrink the memo table in the canonical memoization of fib presented in the introduction. Cook and Lauchbury [1997] studied “disposable memo functions” where the memo table... |

4 | Monadic memoization mixins
- Brown, Cook
- 2007
(Show Context)
Citation Context ... goal of this work is to illustrate a novel use of inheritance, not to develop a high-performance memoization implementation. However, we do show that performance is acceptable. In an earlier report [=-=Brown and Cook 2007-=-] we scaled our technique up to memoize a parser combinator library, but the details are beyond the scope of the current paper.2. Memoizing Recursive Functions Memoizing a simple recursive function n... |