monad-memo-0.4.0: Memoization monad transformer

Portabilitynon-portable (multi-param classes, flexible instances)
Stabilityexperimental
Maintainereduard.sergeev@gmail.com
Safe HaskellNone

Control.Monad.Memo.Array

Contents

Description

ArrayCache - mutable-array-based (IO and ST hosted) MonadCache

Very fast memoization cache. Unfortunatelly it cannot suit every case (see limitations), but if you can use it, please do: it is generally an order of magnitude faster than Map-based Memo, especially unboxed version - try to use it whenever you can.

Limitations: Since MArray is used as MonadCache the key range must be known beforehand and the array is allocated before the first call. It is therefore most suitable for the cases when the distribution of possible key values is within reasonable range and is rather dense (the best case: all values withing some range will be used). If this is the case then MArray has O(1) for both lookup and update operations. In addition unboxed UArrayCache can only store unboxed types (but it does it very efficiently).

Synopsis

ArrayCache for boxed types

type family Array m :: * -> * -> *Source

A family of boxed arrays

type ArrayCache k e m = Cache (Array m) k e mSource

Memoization monad based on mutable boxed array

class MaybeLike e v => ArrayMemo v e | v -> eSource

This is just to be able to infer the type of the ArrayCache element

Type families could be used instead but due to the bug in 7.4.* we cannot use them here

Instances

MaybeLike (Maybe v) v => ArrayMemo v (Maybe v) 

evalArrayMemoSource

Arguments

:: (Ix k, MArray (Array m) e m, ArrayMemo v e) 
=> ArrayCache k e m a

memoized computation to be evaluated

-> (k, k)

array key range

-> m a

computation result

Evaluate computation using boxed array

Key range should cover all possible keys used in computation otherwise not in range error is generated by array

runArrayMemoSource

Arguments

:: (Ix k, MArray (Array m) e m, ArrayMemo v e) 
=> ArrayCache k e m a

memoized computation to be evaluated

-> (k, k)

array key range

-> m (a, Array m k e)

computation result and final array cache

Evaluate computation and the final content of array cache using boxed array

Key range should cover all possible keys used in computation otherwise not in range error is generated by array

ArrayCache for unboxed types

type family UArray m :: * -> * -> *Source

A family of unboxed arrays

type UArrayCache k e m = Cache (UArray m) k e mSource

Memoization monad based on mutable unboxed array

class MaybeLike e v => UArrayMemo v e | v -> eSource

This is just to be able to infer the type of the UArrayCache element

Type families could be used instead but due to the bug in 7.4.* we cannot use them here

Instances

MaybeLike v v => UArrayMemo v v 

evalUArrayMemoSource

Arguments

:: (Ix k, MArray (UArray m) e m, UArrayMemo v e) 
=> UArrayCache k e m a

memoized computation to be evaluated

-> (k, k)

array key range

-> m a

computation result

Evaluate computation using unboxed array

Key range should cover all possible keys used in computation otherwise not in range error is generated by array

runUArrayMemoSource

Arguments

:: (Ix k, MArray (UArray m) e m, UArrayMemo v e) 
=> UArrayCache k e m a

memoized computation to be evaluated

-> (k, k)

array key range

-> m (a, UArray m k e)

computation result and final array cache

Evaluate computation and the final content of array cache using unboxed array

Key range should cover all possible keys used in computation otherwise not in range error is generated by array

Generic function for ArrayCache

newtype Container arr Source

Constructors

Container 

Fields

toArray :: arr
 

Instances

(Monad m, Ix k, MaybeLike e v, MArray c e m) => MonadMemo k v (Cache c k e m) 
(Monad m, Ix k, MaybeLike e v, MArray c e m) => MonadCache k v (Cache c k e m) 

type Cache arr k e = ReaderCache (Container (arr k e))Source

Generic Array-based memo cache

genericEvalArrayMemo :: (Ix k, MaybeLike e v, MArray arr e m) => Cache arr k e m a -> (k, k) -> m aSource

genericRunArrayMemo :: (Ix k, MaybeLike e v, MArray arr e m) => Cache arr k e m a -> (k, k) -> m (a, arr k e)Source