我正在Haskell中实现一个非确定性有限自动机,我正在尝试实现计算epsilon闭包的函数。为此目的,NFA实现为:
data Transaction = Transaction {
start_state :: Int,
symbol :: Maybe Char,
end_state :: Int
} deriving Show
data Automaton = Automaton {
initial_state :: Int,
states :: Set.Set Int,
transactions :: [Transaction],
final_states :: Set.Set Int,
language :: Set.Set Char
} deriving Show
关闭时间:
--Perform the computations necessary to eclosure
getClosure :: [Transaction] -> [Int]
getClosure [] = []
getClosure [tr] = [end_state tr]
getClosure (tr:trs) = end_state tr : getClosure trs
--Get the ε-closure of a given state
eclosure :: Automaton -> Int -> [Int]
eclosure a s
= s : List.concat
[ eclosure a st
| st <- getClosure . List.filter (equal_transaction Nothing s)
$ transactions a ]
问题是如果闭包中有一个循环,代码将永远运行。我理解这种行为背后的原因,但我不知道如何解决它。你能帮我吗?