summaryrefslogtreecommitdiff
path: root/edit-lens/src/Data/String/DFST.hs
diff options
context:
space:
mode:
Diffstat (limited to 'edit-lens/src/Data/String/DFST.hs')
-rw-r--r--edit-lens/src/Data/String/DFST.hs42
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{-|
5Description: Deterministic finite state transducers
6-}
7module Data.String.DFST
8 ( DFST(..)
9 , runDFST, runDFST'
10 ) where
11
12import Data.Map.Strict (Map, (!?))
13import qualified Data.Map.Strict as Map
14
15import Data.Set (Set)
16import qualified Data.Set as Set
17
18import Control.Monad
19
20data 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
27runDFST :: forall state. Ord state => DFST state -> String -> Maybe String
28runDFST dfst@DFST{..} str = let (finalState, str') = runDFST' dfst stInitial str id
29 in str' "" <$ guard (finalState `Set.member` stAccept)
30
31runDFST' :: 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
37runDFST' _ st [] acc = (st, acc)
38runDFST' 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