This module defines functions for memoizing functions, i.e. creating functions which remember their results.

This module works by turning a function into a lazily-evaluated data structure depending on its domain type.

#Tabulate Source

class Tabulate a  where

The Tabulate class identifies those types which can be used as the domain of a memoized function, i.e. those for which the results can be tabulated.



#memoize Source

memoize :: forall b a. Tabulate a => (a -> b) -> a -> b

Memoize a function of one argument

#memoize2 Source

memoize2 :: forall c b a. Tabulate a => Tabulate b => (a -> b -> c) -> a -> b -> c

Memoize a function of two arguments

#memoize3 Source

memoize3 :: forall d c b a. Tabulate a => Tabulate b => Tabulate c => (a -> b -> c -> d) -> a -> b -> c -> d

Memoize a function of three arguments

#genericTabulate Source

genericTabulate :: forall rep r a. Generic a rep => Tabulate rep => (a -> r) -> a -> Lazy r

A default implementation of Tabulate for Generic types.

Given a data type made up of data types with Tabulate instances:

data MyDataType
  = A Int
  | B String

First, derive an instance of Data.Generics.Rep.Generic:

derive instance genericMyDataType :: Generic MyDataType _

Now, Tabulate can be defined in terms of genericTabulate:

instance tabulateMyDataType :: Tabulate MyDataType where
  tabulate = genericTabulate

Note: this function should not be used to derive instances for recursive data types, and attempting to do so will lead to stack overflow errors at runtime.