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,10 +1,12 @@
module Tests
namespace Tests
open System
open DFA
open NFA
let numericDFA:DFA = {
module IsNumeric =
let numericDFA:DFA = {
sigma = Seq.toList "01ab";
states = [
{name = "yes"};
@ -18,9 +20,9 @@ let numericDFA:DFA = {
);
beginState = {name = "yes"};
acceptingStates = [{name = "yes"}]
}
}
let numericNFA:NFA = {
let numericNFA:NFA = {
sigma = Seq.toList "01ab";
states = [
{name = "yes"}
@ -33,14 +35,16 @@ let numericNFA:NFA = {
);
beginState = {name = "yes"};
acceptingStates = [{name = "yes"}]
}
}
let test =
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>]
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

View file

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