playground/clash/Nfa.hs.orig

59 lines
1.3 KiB
Haskell

import Clash.Prelude
import qualified Prelude
import qualified Data.List
-------------------------------------------------------------
lshift :: [a] -> [Either a b]
lshift = Prelude.map Left
rshift :: [b] -> [Either a b]
rshift = Prelude.map Right
-------------------------------------------------------------
data Nfa a = Nfa {
start :: [a]
, final :: [a]
, tfun :: a -> Maybe Char -> a -> Bool
}
-------------------------------------------------------------
eps :: Nfa ()
eps = Nfa {
start = [()]
, final = [()]
, tfun = \ s c d -> case (c, s, d) of
(Nothing, (), ()) -> True
otherwise -> False
-- () to () possible but not by consuming `c'
}
char :: (Char -> Bool) -> Nfa Bool
char f = Nfa {
start = [False]
, final = [True]
, tfun = \ s c d -> case (c, s, d) of
(Just c, False, True) -> f c
(Nothing, False, False) -> True
(Nothing, True, True) -> True
otherwise -> False
}
cat :: Nfa a -> Nfa b -> Nfa (Either a b)
cat a1 a2 = Nfa {
start = lshift (start a1)
, final = rshift (final a2)
, tfun = \ s c d -> case (c, s, d) of
(Just c, Left s, Left d) -> (tfun a1) s c d
(Just c, Right s, Right d) -> (tfun a2) s c d
(Nothing, Left s, Right d) ->
if (final a1) s && (sta
}
-- if ∃st ∈ (start a1) and st ∈ (final a1) then
-- (start a1) ++
-- else
-- start a1