Add more examples

This commit is contained in:
joachimschmidt557 2019-02-20 19:18:48 +01:00
parent 0f5d632388
commit fa48bc49d1
4 changed files with 89 additions and 40 deletions

41
ContainsSequence.fs Normal file
View file

@ -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

View file

@ -1,46 +1,50 @@
module Tests namespace Tests
open System open System
open DFA open DFA
open NFA open NFA
let numericDFA:DFA = { module IsNumeric =
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 numericNFA:NFA = { let numericDFA:DFA = {
sigma = Seq.toList "01ab"; sigma = Seq.toList "01ab";
states = [ states = [
{name = "yes"} {name = "yes"};
]; {name = "no"}
delta = (fun x y -> ];
match (x, y) with delta = (fun x y ->
| ({name = "yes"}, '0') -> [{name = "yes"}] match (x, y) with
| ({name = "yes"}, '1') -> [{name = "yes"}] | ({name = "yes"}, '0') -> {name = "yes"}
| _ -> [] | ({name = "yes"}, '1') -> {name = "yes"}
); | _ -> {name = "no"}
beginState = {name = "yes"}; );
acceptingStates = [{name = "yes"}] beginState = {name = "yes"};
} acceptingStates = [{name = "yes"}]
}
let test = let numericNFA:NFA = {
printfn "Testing numeric checker DFA" sigma = Seq.toList "01ab";
printfn "Is valid DFA: %b" (DFA.validateDFA numericDFA) states = [
printfn "0101: %b" (DFA.acceptsWord numericDFA "0101") {name = "yes"}
printfn "01a1: %b" (DFA.acceptsWord numericDFA "01a1") ];
printfn "Testing numeric checker NFA" delta = (fun x y ->
printfn "0101: %A" (NFA.acceptsWord numericNFA "0101") match (x, y) with
printfn "01a1: %b" (NFA.acceptsWord numericNFA "01a1") | ({name = "yes"}, '0') -> [{name = "yes"}]
0 | ({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

View file

@ -4,5 +4,8 @@ open System
[<EntryPoint>] [<EntryPoint>]
let main argv = let main argv =
printfn "Finite Automata in F#" printfn "Finite Automata in F#"
Tests.test |> ignore
Tests.IsNumeric.test |> ignore
Tests.ContainsSequence.test |> ignore
0 // return an integer exit code 0 // return an integer exit code

View file

@ -8,6 +8,7 @@
<Compile Include="DFA.fs" /> <Compile Include="DFA.fs" />
<Compile Include="NFA.fs" /> <Compile Include="NFA.fs" />
<Compile Include="IsNumericChecker.fs" /> <Compile Include="IsNumericChecker.fs" />
<Compile Include="ContainsSequence.fs" />
<Compile Include="Program.fs" /> <Compile Include="Program.fs" />
</ItemGroup> </ItemGroup>
</Project> </Project>