项目作者: Kwasniok

项目描述 :
finite automata
高级语言: Haskell
项目地址: git://github.com/Kwasniok/haskell-finite-automata.git
创建时间: 2021-05-04T19:11:09Z
项目社区:https://github.com/Kwasniok/haskell-finite-automata

开源协议:BSD 3-Clause "New" or "Revised" License

下载


haskell-finite-automata

A cabal package for finite automata in haskell.

Dependencies

  • Haskell Compiler/Interactive Interpreter: GHC/GHCi
  • Package Manager for Haskell: Cabal

Usage

Use cabal repl to initiate the interactive REPL (Read Evaluate Print Loop).

Examples

There are two types of finite automata implemented:

  • deterministic finite automata (DFAs)
  • non-deterministic finite automata (NFAs)

Accepting Words

Determines if a word is accepted by the automaton or not.

Automaton accepts words ending with True:

  1. >>> -- Deterministic finite automaton with two states labeled `False` and `True`. It starts in state `False` and accepts a word if and only if it ends up in state `True`. Words are of the type [Bool].
  2. >>> -- transition function: Go from state `q` by reading a symbol `s` to state `s`.
  3. >>> -- t :: Bool -> Bool -> Bool
  4. >>> t q s = s
  5. >>> singleton creates a set with one element
  6. >>> dfa = MkDFA t False (singleton True)
  7. >>> -- accepts
  8. >>> accepts dfa [False]
  9. False
  10. >>> accepts dfa [False, True]
  11. True

Automaton accepts any word from the alphabet {False, True}:

  1. >>> -- Deterministic finite automaton with one state `()` accepting any word from the alphabet {False, True}.
  2. >>> -- transition function: Go from the unit state `q = ()` by reading a symbol `s` to the unit state.
  3. >>> -- t :: () -> Bool -> ()
  4. >>> t q s = ()
  5. >>> dfa :: DFA () Bool
  6. >>> dfa = MkDFA t () (singleton ())
  7. >>> -- accepts
  8. >>> accepts dfa [False]
  9. True
  10. >>> accepts dfa [False, True]
  11. True
  12. >>> -- empty word
  13. >>> e = [] :: [Bool]
  14. >>> accepts dfa e
  15. True

Reading Symbols

Gives an insight on the state transfer when reading a single symbol.

Automaton accepts words ending with True:

  1. >>> -- Deterministic finite automaton with two states labeled `False` and `True`. It starts in state `False` and accepts a word if and only if it ends up in state `True`. Words are of the type [Bool].
  2. >>> -- transition function: Go from state `q` by reading a symbol `s` to state `s`.
  3. >>> -- t :: Bool -> Bool -> Bool
  4. >>> t q s = s
  5. >>> singleton creates a set with one element
  6. >>> dfa = MkDFA t False (singleton True)
  7. >>> -- from a given initial state read a symbol and determine the new state
  8. >>> -- staring in state `False` read `True`
  9. >>> readSymbol dfa False True
  10. True
  11. >>> readSymbol dfa True True
  12. True
  13. >>> readSymbol dfa True False
  14. False

Reading (Partial) Words

Gives an insight on the state transfer when reading a word

Automaton accepts words ending with True:

  1. >>> -- GHC extension needed:
  2. >>> :set -XFlexibleContexts
  3. >>> -- Deterministic finite automaton with two states labeled `False` and `True`. It starts in state `False` and accepts a word if and only if it ends up in state `True`. Words are of the type [Bool].
  4. >>> -- transition function: Go from state `q` by reading a symbol `s` to state `s`.
  5. >>> -- t :: Bool -> Bool -> Bool
  6. >>> t q s = s
  7. >>> singleton creates a set with one element
  8. >>> dfa = MkDFA t False (singleton True)
  9. >>> -- from a given initial state read a symbol and determine the new state
  10. >>> -- staring in state `False` read `True`
  11. >>> readWord dfa [True] :: Bool
  12. True
  13. >>> readWord dfa [True, False] :: Bool
  14. False
  15. >>> -- empty word
  16. >>> e = [] :: [Bool]
  17. >>> readWord dfa e :: Bool
  18. False

DFA to NFA Conversion

Convert between equivalent DFAs and NFAs which accept exactly the same words.

Automaton accepts words ending with True:

  1. >>> t q s = s
  2. >>> dfa = MkDFA t False (singleton True)
  3. >>> :t dfa
  4. dfa :: DFA Bool Bool
  5. >>> nfa = dfaToNfa dfa
  6. >>> :t nfa
  7. nfa :: NFA Bool Bool
  8. >>> -- accepts
  9. >>> accepts nfa [False]
  10. False
  11. >>> accepts nfa [False, True]
  12. True

Use dfa' = nfaToDfa nfa for an equivalent inverse conversion.

NFAs behave like DFAs where the states become sets of states:

  1. >>> t q s = s
  2. >>> dfa = MkDFA t False (singleton True)
  3. >>> :t dfa
  4. dfa :: DFA Bool Bool
  5. >>> nfa = dfaToNfa dfa
  6. >>> :t nfa
  7. nfa :: NFA Bool Bool
  8. >>> -- read a symbol starting at the given initial states
  9. >>> -- fromList converts a list to a set
  10. >>> readSymbol nfa (fromList [False, True]) True
  11. {True}