fsharp-finite-automata/DFA.fs
joachimschmidt557 857ef08ace WIP
2019-02-06 22:05:21 +01:00

44 lines
No EOL
1.2 KiB
FSharp

module DFA
open System
type State = { name: String }
type DFA = {
sigma: Char list
states: State list
delta: State -> Char -> State
beginState: State
acceptingStates: State list
}
let validateDFA (dfa:DFA) =
let allAcceptingStatesAreStates =
dfa.acceptingStates
|> List.map (fun x -> List.contains x dfa.states)
|> List.filter not
|> List.length < 1
let deltaIsComplete =
dfa.states
|> List.collect (fun x -> dfa.sigma |> List.map (fun y -> (x, y)))
|> List.map (fun x -> dfa.delta (fst x) (snd x))
|> List.filter (fun x -> not (List.contains x dfa.states))
|> List.length < 1
List.contains dfa.beginState dfa.states ||
allAcceptingStatesAreStates ||
deltaIsComplete
let processChar (dfa: DFA) (state: State) (char: Char) =
dfa.delta state char
let rec processCharArray (dfa: DFA) (state: State) (charArr: Char list) =
match charArr with
| [] -> state
| _ -> processCharArray dfa (processChar dfa state charArr.Head) charArr.Tail
let acceptsWord (dfa: DFA) (word: String) =
dfa.acceptingStates
|> List.contains (processCharArray dfa dfa.beginState (Seq.toList word))