diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2018-05-21 16:14:26 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2018-05-21 16:14:26 +0200 |
commit | d3f6fdc3ea71386c2b9a9cd5a686455dbee3e60e (patch) | |
tree | b62e4748b8e058a5ab4122accf6b33e15bdd9b49 /edit-lens/src/Data/String/DFST.hs | |
parent | eb599b2394e62842423cc0bbee2432a9cae95f4b (diff) | |
download | incremental-dfsts-d3f6fdc3ea71386c2b9a9cd5a686455dbee3e60e.tar incremental-dfsts-d3f6fdc3ea71386c2b9a9cd5a686455dbee3e60e.tar.gz incremental-dfsts-d3f6fdc3ea71386c2b9a9cd5a686455dbee3e60e.tar.bz2 incremental-dfsts-d3f6fdc3ea71386c2b9a9cd5a686455dbee3e60e.tar.xz incremental-dfsts-d3f6fdc3ea71386c2b9a9cd5a686455dbee3e60e.zip |
Introduce FSTs & Generalize input/output
`toFST` is currently invalid
Diffstat (limited to 'edit-lens/src/Data/String/DFST.hs')
-rw-r--r-- | edit-lens/src/Data/String/DFST.hs | 42 |
1 files changed, 0 insertions, 42 deletions
diff --git a/edit-lens/src/Data/String/DFST.hs b/edit-lens/src/Data/String/DFST.hs deleted file mode 100644 index 54a1336..0000000 --- a/edit-lens/src/Data/String/DFST.hs +++ /dev/null | |||
@@ -1,42 +0,0 @@ | |||
1 | {-# LANGUAGE ScopedTypeVariables | ||
2 | #-} | ||
3 | |||
4 | {-| | ||
5 | Description: Deterministic finite state transducers | ||
6 | -} | ||
7 | module Data.String.DFST | ||
8 | ( DFST(..) | ||
9 | , runDFST, runDFST' | ||
10 | ) where | ||
11 | |||
12 | import Data.Map.Strict (Map, (!?)) | ||
13 | import qualified Data.Map.Strict as Map | ||
14 | |||
15 | import Data.Set (Set) | ||
16 | import qualified Data.Set as Set | ||
17 | |||
18 | import Control.Monad | ||
19 | |||
20 | data DFST state = DFST | ||
21 | { stInitial :: state | ||
22 | , stTransition :: Map (state, Char) (state, String) | ||
23 | -- ^ All @(s, c)@-combinations not mapped are assumed to map to @(s, Nothing)@ | ||
24 | , stAccept :: Set state | ||
25 | } | ||
26 | |||
27 | runDFST :: forall state. Ord state => DFST state -> String -> Maybe String | ||
28 | runDFST dfst@DFST{..} str = let (finalState, str') = runDFST' dfst stInitial str id | ||
29 | in str' "" <$ guard (finalState `Set.member` stAccept) | ||
30 | |||
31 | runDFST' :: forall state. Ord state | ||
32 | => DFST state | ||
33 | -> state -- ^ Current state | ||
34 | -> String -- ^ Remaining input | ||
35 | -> (String -> String) -- ^ Output as difference list | ||
36 | -> (state, (String -> String)) -- ^ Next state, altered output | ||
37 | runDFST' _ st [] acc = (st, acc) | ||
38 | runDFST' dfst@DFST{..} st (c:cs) acc | ||
39 | | Just (st', mc') <- stTransition !? (st, c) | ||
40 | = runDFST' dfst st' cs $ acc . (mc' ++) | ||
41 | | otherwise | ||
42 | = runDFST' dfst st cs acc | ||