Change from State struct to just strings
This commit is contained in:
parent
fa48bc49d1
commit
30949252c0
4 changed files with 36 additions and 40 deletions
|
|
@ -9,23 +9,23 @@ module ContainsSequence =
|
||||||
let contains0101:NFA = {
|
let contains0101:NFA = {
|
||||||
sigma = Seq.toList "01";
|
sigma = Seq.toList "01";
|
||||||
states = [
|
states = [
|
||||||
{name = "q0"};
|
"q0";
|
||||||
{name = "q1"};
|
"q1";
|
||||||
{name = "q2"};
|
"q2";
|
||||||
{name = "q3"}
|
"q3"
|
||||||
{name = "yes"}
|
"yes"
|
||||||
];
|
];
|
||||||
delta = (fun x y ->
|
delta = (fun x y ->
|
||||||
match (x, y) with
|
match (x, y) with
|
||||||
| ({name = "q0"}, '0') -> [{name = "q0"}; {name = "q1"}]
|
| ("q0", '0') -> ["q0"; "q1"]
|
||||||
| ({name = "q1"}, '1') -> [{name = "q0"}; {name = "q2"}]
|
| ("q1", '1') -> ["q0"; "q2"]
|
||||||
| ({name = "q2"}, '0') -> [{name = "q0"}; {name = "q3"}]
|
| ("q2", '0') -> ["q0"; "q3"]
|
||||||
| ({name = "q3"}, '1') -> [{name = "yes"}]
|
| ("q3", '1') -> ["yes"]
|
||||||
| ({name = "yes"}, _) -> [{name = "yes"}]
|
| ("yes", _) -> ["yes"]
|
||||||
| _ -> []
|
| _ -> []
|
||||||
);
|
);
|
||||||
beginState = {name = "q0"};
|
beginState = "q0";
|
||||||
acceptingStates = [{name = "yes"}]
|
acceptingStates = ["yes"]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
14
DFA.fs
14
DFA.fs
|
|
@ -2,8 +2,6 @@ module DFA
|
||||||
|
|
||||||
open System
|
open System
|
||||||
|
|
||||||
type State = { name: String }
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// A deterministic finite automaton
|
/// A deterministic finite automaton
|
||||||
///
|
///
|
||||||
|
|
@ -11,14 +9,14 @@ type DFA = {
|
||||||
/// All possible characters
|
/// All possible characters
|
||||||
sigma: Char list
|
sigma: Char list
|
||||||
/// All possible states
|
/// All possible states
|
||||||
states: State list
|
states: String list
|
||||||
/// All possible transitions
|
/// All possible transitions
|
||||||
delta: State -> Char -> State
|
delta: String -> Char -> String
|
||||||
/// The state this DFA begins in
|
/// The state this DFA begins in
|
||||||
beginState: State
|
beginState: String
|
||||||
/// The states in which this DFA accepts
|
/// The states in which this DFA accepts
|
||||||
/// the word
|
/// the word
|
||||||
acceptingStates: State list
|
acceptingStates: String list
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the DFA is valid, false otherwise
|
/// Returns true if the DFA is valid, false otherwise
|
||||||
|
|
@ -51,12 +49,12 @@ let validateDFA (dfa:DFA) =
|
||||||
|
|
||||||
/// Returns the state the DFA is in after
|
/// Returns the state the DFA is in after
|
||||||
/// reading this character
|
/// reading this character
|
||||||
let processChar (dfa: DFA) (state: State) (char: Char) =
|
let processChar (dfa: DFA) (state: String) (char: Char) =
|
||||||
dfa.delta state char
|
dfa.delta state char
|
||||||
|
|
||||||
/// Returns the state the DFA is in after
|
/// Returns the state the DFA is in after
|
||||||
/// reading this character array
|
/// reading this character array
|
||||||
let rec processCharArray (dfa: DFA) (state: State) (charArr: Char list) =
|
let rec processCharArray (dfa: DFA) (state: String) (charArr: Char list) =
|
||||||
match charArr with
|
match charArr with
|
||||||
| [] -> state
|
| [] -> state
|
||||||
| head :: tail -> processCharArray dfa (processChar dfa state head) tail
|
| head :: tail -> processCharArray dfa (processChar dfa state head) tail
|
||||||
|
|
|
||||||
|
|
@ -9,32 +9,32 @@ module IsNumeric =
|
||||||
let numericDFA:DFA = {
|
let numericDFA:DFA = {
|
||||||
sigma = Seq.toList "01ab";
|
sigma = Seq.toList "01ab";
|
||||||
states = [
|
states = [
|
||||||
{name = "yes"};
|
"yes";
|
||||||
{name = "no"}
|
"no"
|
||||||
];
|
];
|
||||||
delta = (fun x y ->
|
delta = (fun x y ->
|
||||||
match (x, y) with
|
match (x, y) with
|
||||||
| ({name = "yes"}, '0') -> {name = "yes"}
|
| ("yes", '0') -> "yes"
|
||||||
| ({name = "yes"}, '1') -> {name = "yes"}
|
| ("yes", '1') -> "yes"
|
||||||
| _ -> {name = "no"}
|
| _ -> "no"
|
||||||
);
|
);
|
||||||
beginState = {name = "yes"};
|
beginState = "yes";
|
||||||
acceptingStates = [{name = "yes"}]
|
acceptingStates = ["yes"]
|
||||||
}
|
}
|
||||||
|
|
||||||
let numericNFA:NFA = {
|
let numericNFA:NFA = {
|
||||||
sigma = Seq.toList "01ab";
|
sigma = Seq.toList "01ab";
|
||||||
states = [
|
states = [
|
||||||
{name = "yes"}
|
"yes"
|
||||||
];
|
];
|
||||||
delta = (fun x y ->
|
delta = (fun x y ->
|
||||||
match (x, y) with
|
match (x, y) with
|
||||||
| ({name = "yes"}, '0') -> [{name = "yes"}]
|
| ("yes", '0') -> ["yes"]
|
||||||
| ({name = "yes"}, '1') -> [{name = "yes"}]
|
| ("yes", '1') -> ["yes"]
|
||||||
| _ -> []
|
| _ -> []
|
||||||
);
|
);
|
||||||
beginState = {name = "yes"};
|
beginState = "yes";
|
||||||
acceptingStates = [{name = "yes"}]
|
acceptingStates = ["yes"]
|
||||||
}
|
}
|
||||||
|
|
||||||
let test =
|
let test =
|
||||||
|
|
|
||||||
14
NFA.fs
14
NFA.fs
|
|
@ -2,31 +2,29 @@ module NFA
|
||||||
|
|
||||||
open System
|
open System
|
||||||
|
|
||||||
type State = { name: String }
|
|
||||||
|
|
||||||
/// A non-deterministic finite automaton
|
/// A non-deterministic finite automaton
|
||||||
type NFA = {
|
type NFA = {
|
||||||
/// All possible characters
|
/// All possible characters
|
||||||
sigma: Char list
|
sigma: Char list
|
||||||
/// All possible states
|
/// All possible states
|
||||||
states: State list
|
states: String list
|
||||||
/// All possible transitions
|
/// All possible transitions
|
||||||
delta: State -> Char -> State list
|
delta: String -> Char -> String list
|
||||||
/// The state this NFA begins in
|
/// The state this NFA begins in
|
||||||
beginState: State
|
beginState: String
|
||||||
/// The states in which this NFA accepts
|
/// The states in which this NFA accepts
|
||||||
/// the word
|
/// the word
|
||||||
acceptingStates: State list
|
acceptingStates: String list
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a list of possible states after this charater
|
/// Returns a list of possible states after this charater
|
||||||
/// was read
|
/// was read
|
||||||
let processChar (nfa: NFA) (state: State) (char: Char) =
|
let processChar (nfa: NFA) state (char: Char) =
|
||||||
nfa.delta state char
|
nfa.delta state char
|
||||||
|
|
||||||
/// Returns a list of possible states after this character
|
/// Returns a list of possible states after this character
|
||||||
/// list was read
|
/// list was read
|
||||||
let rec processCharArray (nfa: NFA) (states: State list) (charArr: Char list) : State list =
|
let rec processCharArray (nfa: NFA) (states: String list) (charArr: Char list) : String list =
|
||||||
match charArr with
|
match charArr with
|
||||||
| [] -> states
|
| [] -> states
|
||||||
| head :: tail ->
|
| head :: tail ->
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue