module Agda.Utils.Memo where
import Control.Applicative
import Control.Monad.State
import Agda.Utils.Lens
memo
#if __GLASGOW_HASKELL__ <= 708
:: (Functor m, MonadState s m)
#else
:: MonadState s m
#endif
=> Lens' (Maybe a) s -> m a -> m a
memo tbl compute = do
mv <- use tbl
case mv of
Just x -> return x
Nothing -> do
x <- compute
x <$ (tbl .= Just x)
memoRec
#if __GLASGOW_HASKELL__ <= 708
:: (Functor m, MonadState s m)
#else
:: MonadState s m
#endif
=> Lens' (Maybe a) s -> a -> m a -> m a
memoRec tbl ih compute = do
mv <- use tbl
case mv of
Just x -> return x
Nothing -> do
tbl .= Just ih
x <- compute
x <$ (tbl .= Just x)