diff options
Diffstat (limited to 'edit-lens/src/Data/String/DFST.hs')
-rw-r--r-- | edit-lens/src/Data/String/DFST.hs | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/edit-lens/src/Data/String/DFST.hs b/edit-lens/src/Data/String/DFST.hs index 8a22dd3..75ca1cd 100644 --- a/edit-lens/src/Data/String/DFST.hs +++ b/edit-lens/src/Data/String/DFST.hs | |||
@@ -8,7 +8,7 @@ Description: Deterministic finite state transducers | |||
8 | -} | 8 | -} |
9 | module Data.String.DFST | 9 | module Data.String.DFST |
10 | ( DFST(..) | 10 | ( DFST(..) |
11 | , runDFST | 11 | , runDFST, runDFST' |
12 | ) where | 12 | ) where |
13 | 13 | ||
14 | import Data.Map.Strict (Map, (!?)) | 14 | import Data.Map.Strict (Map, (!?)) |
@@ -17,6 +17,8 @@ import qualified Data.Map.Strict as Map | |||
17 | import Data.Set (Set) | 17 | import Data.Set (Set) |
18 | import qualified Data.Set as Set | 18 | import qualified Data.Set as Set |
19 | 19 | ||
20 | import Control.Monad | ||
21 | |||
20 | data DFST state = DFST | 22 | data DFST state = DFST |
21 | { stInitial :: state | 23 | { stInitial :: state |
22 | , stTransition :: Map (state, Char) (state, Maybe Char) | 24 | , stTransition :: Map (state, Char) (state, Maybe Char) |
@@ -25,14 +27,13 @@ data DFST state = DFST | |||
25 | } | 27 | } |
26 | 28 | ||
27 | runDFST :: forall state. Ord state => DFST state -> String -> Maybe String | 29 | runDFST :: forall state. Ord state => DFST state -> String -> Maybe String |
28 | runDFST DFST{..} str = ($ []) <$> go stInitial str id | 30 | runDFST dfst@DFST{..} str = let (finalState, str') = runDFST' dfst stInitial str id |
29 | where | 31 | in str' "" <$ guard (finalState `Set.member` stAccept) |
30 | go :: state -> String -> (String -> String) -> Maybe (String -> String) | 32 | |
31 | go st [] acc | 33 | runDFST' :: forall state. Ord state => DFST state -> state -> String -> (String -> String) -> (state, (String -> String)) |
32 | | st `Set.member` stAccept = Just acc | 34 | runDFST' _ st [] acc = (st, acc) |
33 | | otherwise = Nothing | 35 | runDFST' dfst@DFST{..} st (c:cs) acc |
34 | go st (c:cs) acc | 36 | | Just (st', mc') <- stTransition !? (st, c) |
35 | | Just (st', mc') <- stTransition !? (st, c) | 37 | = runDFST' dfst st' cs $ acc . maybe id (:) mc' |
36 | = go st' cs $ acc . maybe id (:) mc' | 38 | | otherwise |
37 | | otherwise | 39 | = runDFST' dfst st cs acc |
38 | = go st cs acc | ||