monad-memo-0.5.4: Memoization monad transformer

Copyright | (c) Eduard Sergeev 2013
License | BSD-style (see the file LICENSE)
Maintainer | eduard.sergeev@gmail.com
Stability | experimental
Portability | non-portable (multi-param classes, flexible instances)
Safe Haskell | None
Language | Haskell2010

Control.Monad.Memo.Vector

Contents

- VectorCache for boxed types
- UVectorCache for unboxed types
- Generic functions for VectorCache

Description

VectorCache - mutable-vector-based (`IO` and `ST` hosted) `MonadCache`

The fastest memoization cache, however it is even more limiting than Control.Monad.Memo.Array due to nature of Data.Vector.Mutable. Still if you can use this cache please do since it will give you dramatic calculation speed up in comparison to pure `Map`-based cache, especially when unboxed `UVectorCache` is used.

Limitations: Since `MVector` is used as `MonadCache` the key must be `Int` and the size of the cache's vector must be known beforehand with vector being allocated before the first call. In addition unboxed `UVectorCache` can only store `Unbox` values (but it does it very efficiently).

Synopsis

- type Vector = MVector
- type VectorCache s e = Cache Vector s e
- class MaybeLike e v => VectorMemo v e | v -> e
- evalVectorMemo :: (PrimMonad m, VectorMemo v e) => VectorCache (PrimState m) e m a -> Int -> m a
- runVectorMemo :: (PrimMonad m, VectorMemo v e) => VectorCache (PrimState m) e m a -> Int -> m (a, Vector (PrimState m) e)
- type UVector = MVector
- type UVectorCache s e = Cache UVector s e
- class MaybeLike e v => UVectorMemo v e | v -> e
- evalUVectorMemo :: (PrimMonad m, MVector UVector e, UVectorMemo v e) => UVectorCache (PrimState m) e m a -> Int -> m a
- runUVectorMemo :: (PrimMonad m, MVector UVector e, UVectorMemo v e) => UVectorCache (PrimState m) e m a -> Int -> m (a, UVector (PrimState m) e)
- newtype Container vec = Container {
  - toVector :: vec
  }
- type Cache (vec :: Type -> Type -> Type) s e = ReaderCache (Container (vec s e))
- genericEvalVectorMemo :: (MaybeLike e v, PrimMonad m, MVector c e) => Cache c (PrimState m) e m a -> Int -> m a
- genericRunVectorMemo :: (MaybeLike e v, PrimMonad m, MVector c e) => Cache c (PrimState m) e m a -> Int -> m (a, c (PrimState m) e)

# VectorCache for boxed types

type Vector = MVector Source #

Boxed vector

type VectorCache s e = Cache Vector s e Source #

`MonadCache` based on boxed vector

class MaybeLike e v => VectorMemo v e | v -> e Source #

This is just to be able to infer the type of the `VectorCache` element.

#### Instances

Instances details

MaybeLike (Maybe v) v => VectorMemo v (Maybe v) Source # |
Instance details Defined in Control.Monad.Memo.Vector.Instances

evalVectorMemo Source #

Arguments

:: (PrimMonad m, VectorMemo v e) |
=> VectorCache (PrimState m) e m a | memoized computation
-> Int | vector length
-> m a | result

Evaluate computation using mutable boxed vector

Vector length must covers all possible keys used in computation otherwise index out of bound error is generated by vector code

runVectorMemo Source #

Arguments

:: (PrimMonad m, VectorMemo v e) |
=> VectorCache (PrimState m) e m a | memoized computation
-> Int | vector length
-> m (a, Vector (PrimState m) e) | result and final vector cache

Evaluate computation using mutable boxed vector. It also returns the final content of the vector cache

Vector length must covers all possible keys used in computation otherwise index out of bound error is generated by vector code

# UVectorCache for unboxed types

type UVector = MVector Source #

Unboxed vector

type UVectorCache s e = Cache UVector s e Source #

`MonadCache` based on unboxed vector

class MaybeLike e v => UVectorMemo v e | v -> e Source #

This is just to be able to infer the type of the `UVectorCache` element.

#### Instances

Instances details

MaybeLike v v => UVectorMemo v v Source # |
Instance details Defined in Control.Monad.Memo.Vector.Instances

evalUVectorMemo Source #

Arguments

:: (PrimMonad m, MVector UVector e, UVectorMemo v e) |
=> UVectorCache (PrimState m) e m a | memoized computation
-> Int | vector length
-> m a | result

Evaluate computation using mutable unboxed vector

Vector length must covers all possible keys used in computation otherwise index out of bound error is generated by vector code

runUVectorMemo Source #

Arguments

:: (PrimMonad m, MVector UVector e, UVectorMemo v e) |
=> UVectorCache (PrimState m) e m a | memoized computation
-> Int | vector length
-> m (a, UVector (PrimState m) e) | result and final vector cache

Evaluate computation using mutable unboxed vector. It also returns the final content of the vector cache

Vector length must covers all possible keys used in computation otherwise index out of bound error is generated by vector code

# Generic functions for VectorCache

newtype Container vec Source #

Constructors

Container |

Fields

- toVector :: vec

#### Instances

Instances details

(PrimMonad m, PrimState m ~ s, MaybeLike e v, MVector c e) => MonadCache Int v (Cache c s e m) Source # |
Instance details Defined in Control.Monad.Memo.Vector Methods lookup :: Int -> Cache c s e m (Maybe v) Source # add :: Int -> v -> Cache c s e m () Source #
(PrimMonad m, PrimState m ~ s, MaybeLike e v, MVector c e) => MonadMemo Int v (Cache c s e m) Source # |
Instance details Defined in Control.Monad.Memo.Vector Methods memo :: (Int -> Cache c s e m v) -> Int -> Cache c s e m v Source #

type Cache (vec :: Type -> Type -> Type) s e = ReaderCache (Container (vec s e)) Source #

Generic Vector-based memo cache

genericEvalVectorMemo :: (MaybeLike e v, PrimMonad m, MVector c e) => Cache c (PrimState m) e m a -> Int -> m a Source #

genericRunVectorMemo :: (MaybeLike e v, PrimMonad m, MVector c e) => Cache c (PrimState m) e m a -> Int -> m (a, c (PrimState m) e) Source #