Copyright | (c) Alice Rixte 2024 |
---|---|
License | BSD 3 |
Maintainer | alice.rixte@u-bordeaux.fr |
Stability | unstable |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Data.Memoizer.Commands
Description
A data structure to memoize the result of the execution of a sequence of commands.
As long as the sequence of commands is identical to the one memoized, we can avoid executing them. As soon as there is one command that differs from the memoized sequence, then we should discard all remaining memoized results.
Usage
Let's store the result of some commands (we alternate between memo
and
memo'
to avoid recursive definitions)
>>>
import Prelude hiding (lookup)
>>>
memo' = storeResult "x=1" "" empty :: CmdMemoizer String String
>>>
memo = storeResult "y=2" "" memo'
>>>
memo' = storeResult "x+y" "3" memo
Suppose there are no more commands in the sequence. Now we want to execute that sequence of commands again but some commands may have changed in between. We use memoized commands as long as all commands are equal to the previous ones:
>>>
memo = restart memo'
>>>
lookup "x=1" memo
Just ""
Since the command was memoized, we avoid executing is again. Now suppose the command "y=2"
was replaced by "y=3"
>>>
memo' = nextCmd memo
>>>
lookup "y=3" memo'
Nothing
Since the command was not memoized, we have to execute it:
>>>
memo = storeResult "y=3" "" memo'
Now none of the subsequent commands will use the memoized version:
>>>
memo' = nextCmd memo
>>>
lookup "x+y" memo'
Nothing
Synopsis
- data CmdMemoizer a b
- empty :: CmdMemoizer a b
- storeResult :: a -> b -> CmdMemoizer a b -> CmdMemoizer a b
- deleteResult :: CmdMemoizer a b -> CmdMemoizer a b
- lookup :: Eq a => a -> CmdMemoizer a b -> Maybe b
- restart :: CmdMemoizer a b -> CmdMemoizer a b
- nextCmd :: CmdMemoizer a b -> CmdMemoizer a b
Documentation
data CmdMemoizer a b Source #
A container for the memoized result of the execution of a sequence of commands
a
is the type of commandsb
is the type of results
Instances
(Show a, Show b) => Show (CmdMemoizer a b) Source # | |
Defined in Data.Memoizer.Commands Methods showsPrec :: Int -> CmdMemoizer a b -> ShowS # show :: CmdMemoizer a b -> String # showList :: [CmdMemoizer a b] -> ShowS # | |
(Eq a, Eq b) => Eq (CmdMemoizer a b) Source # | |
Defined in Data.Memoizer.Commands Methods (==) :: CmdMemoizer a b -> CmdMemoizer a b -> Bool # (/=) :: CmdMemoizer a b -> CmdMemoizer a b -> Bool # |
empty :: CmdMemoizer a b Source #
Memoizer of an empty sequence of commands
storeResult :: a -> b -> CmdMemoizer a b -> CmdMemoizer a b Source #
Store the result of the execution of a command.
This will override the current memoized command, and prevent access to
any memoized result until
is used.restart
deleteResult :: CmdMemoizer a b -> CmdMemoizer a b Source #
Delete the result of the current memoized command.
This will override the current memoized command, and prevent access to any
memoized result until
is used.restart
lookup :: Eq a => a -> CmdMemoizer a b -> Maybe b Source #
Access the memoized result of a command if it is equal to the current memoized command
restart :: CmdMemoizer a b -> CmdMemoizer a b Source #
Restart the sequence of commands
Memoized results will now be accessible until
or
storeResult
are used.deleteResult
nextCmd :: CmdMemoizer a b -> CmdMemoizer a b Source #
Move to the next memoized command