diff --git a/ContainsSequence.fs b/ContainsSequence.fs new file mode 100644 index 0000000..cb9b34d --- /dev/null +++ b/ContainsSequence.fs @@ -0,0 +1,41 @@ +namespace Tests + +open System +open NFA +open DFA + +module ContainsSequence = + + let contains0101:NFA = { + sigma = Seq.toList "01"; + states = [ + {name = "q0"}; + {name = "q1"}; + {name = "q2"}; + {name = "q3"} + {name = "yes"} + ]; + delta = (fun x y -> + match (x, y) with + | ({name = "q0"}, '0') -> [{name = "q0"}; {name = "q1"}] + | ({name = "q1"}, '1') -> [{name = "q0"}; {name = "q2"}] + | ({name = "q2"}, '0') -> [{name = "q0"}; {name = "q3"}] + | ({name = "q3"}, '1') -> [{name = "yes"}] + | ({name = "yes"}, _) -> [{name = "yes"}] + | _ -> [] + ); + beginState = {name = "q0"}; + acceptingStates = [{name = "yes"}] + + } + + let test = + printfn "Testing contains sequence NFA" + printfn "Contains '0101' ?" + printfn "0101: %b" (NFA.acceptsWord contains0101 "0101") + printfn "0000: %b" (NFA.acceptsWord contains0101 "0000") + printfn "1010: %b" (NFA.acceptsWord contains0101 "1010") + printfn "0100: %b" (NFA.acceptsWord contains0101 "0100") + printfn "00101: %b" (NFA.acceptsWord contains0101 "00101") + printfn "01011: %b" (NFA.acceptsWord contains0101 "01011") + 0 \ No newline at end of file diff --git a/IsNumericChecker.fs b/IsNumericChecker.fs index 5247b45..fb752c2 100644 --- a/IsNumericChecker.fs +++ b/IsNumericChecker.fs @@ -1,46 +1,50 @@ -module Tests +namespace Tests open System open DFA open NFA -let numericDFA:DFA = { - sigma = Seq.toList "01ab"; - states = [ - {name = "yes"}; - {name = "no"} - ]; - delta = (fun x y -> - match (x, y) with - | ({name = "yes"}, '0') -> {name = "yes"} - | ({name = "yes"}, '1') -> {name = "yes"} - | _ -> {name = "no"} - ); - beginState = {name = "yes"}; - acceptingStates = [{name = "yes"}] -} +module IsNumeric = -let numericNFA:NFA = { - sigma = Seq.toList "01ab"; - states = [ - {name = "yes"} - ]; - delta = (fun x y -> - match (x, y) with - | ({name = "yes"}, '0') -> [{name = "yes"}] - | ({name = "yes"}, '1') -> [{name = "yes"}] - | _ -> [] - ); - beginState = {name = "yes"}; - acceptingStates = [{name = "yes"}] -} + let numericDFA:DFA = { + sigma = Seq.toList "01ab"; + states = [ + {name = "yes"}; + {name = "no"} + ]; + delta = (fun x y -> + match (x, y) with + | ({name = "yes"}, '0') -> {name = "yes"} + | ({name = "yes"}, '1') -> {name = "yes"} + | _ -> {name = "no"} + ); + beginState = {name = "yes"}; + acceptingStates = [{name = "yes"}] + } -let test = - printfn "Testing numeric checker DFA" - printfn "Is valid DFA: %b" (DFA.validateDFA numericDFA) - printfn "0101: %b" (DFA.acceptsWord numericDFA "0101") - printfn "01a1: %b" (DFA.acceptsWord numericDFA "01a1") - printfn "Testing numeric checker NFA" - printfn "0101: %A" (NFA.acceptsWord numericNFA "0101") - printfn "01a1: %b" (NFA.acceptsWord numericNFA "01a1") - 0 \ No newline at end of file + let numericNFA:NFA = { + sigma = Seq.toList "01ab"; + states = [ + {name = "yes"} + ]; + delta = (fun x y -> + match (x, y) with + | ({name = "yes"}, '0') -> [{name = "yes"}] + | ({name = "yes"}, '1') -> [{name = "yes"}] + | _ -> [] + ); + beginState = {name = "yes"}; + acceptingStates = [{name = "yes"}] + } + + let test = + printfn "Testing numeric checker DFA" + printfn "Is valid DFA: %b" (DFA.validateDFA numericDFA) + printfn "0101: %b" (DFA.acceptsWord numericDFA "0101") + printfn "01a1: %b" (DFA.acceptsWord numericDFA "01a1") + printfn "Empty: %b" (DFA.acceptsWord numericDFA "") + printfn "Testing numeric checker NFA" + printfn "0101: %A" (NFA.acceptsWord numericNFA "0101") + printfn "01a1: %b" (NFA.acceptsWord numericNFA "01a1") + printfn "Empty: %b" (NFA.acceptsWord numericNFA "") + 0 \ No newline at end of file diff --git a/Program.fs b/Program.fs index 8024b2d..552d739 100644 --- a/Program.fs +++ b/Program.fs @@ -4,5 +4,8 @@ open System [] let main argv = printfn "Finite Automata in F#" - Tests.test |> ignore + + Tests.IsNumeric.test |> ignore + Tests.ContainsSequence.test |> ignore + 0 // return an integer exit code diff --git a/fsharp-finite-automata.fsproj b/fsharp-finite-automata.fsproj index bc769fd..dee832d 100644 --- a/fsharp-finite-automata.fsproj +++ b/fsharp-finite-automata.fsproj @@ -8,6 +8,7 @@ + \ No newline at end of file