Browse Source

Initial commit

Nicolas Lenz 6 months ago
commit
403a2d2179
No known key found for this signature in database
3 changed files with 95 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 89
    0
      Data/Automata.idr
  3. 4
    0
      Main.idr

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+*.ibc
2
+*.o

+ 89
- 0
Data/Automata.idr View File

@@ -0,0 +1,89 @@
1
+{-
2
+This file defines types for different types of automata and functions for analyzing them.
3
+-}
4
+
5
+module Data.Automata
6
+
7
+import Data.Fin
8
+
9
+%default total
10
+%access export
11
+
12
+||| Interface for things that accept (or reject) strings over an alphabet.
13
+interface Acceptor a alph where
14
+    decide : a -> List alph -> Bool
15
+
16
+
17
+||| A type for deterministic finite automata (DFA)
18
+|||
19
+||| @ state The type of the states
20
+||| @ alph The type of the input symbols
21
+data DFA : (state : Type) -> (alph : Type) -> Type where 
22
+    ||| Makes a DFA
23
+    |||
24
+    ||| @ delta The state transition function
25
+    ||| @ start The starting state
26
+    ||| @ accepting The states that accept
27
+    MkDFA : (delta : state -> alph -> state) -> (start : state) -> (accepting : List state) -> DFA state alph
28
+    
29
+||| Runs a DFA with an input string starting at a given state
30
+runDFAFrom : DFA state alph -> state -> List alph -> state
31
+runDFAFrom _ state [] = state
32
+runDFAFrom a@(MkDFA delta _ _) st (x::xs) = runDFAFrom a (delta st x) xs
33
+
34
+||| Runs a DFA with an input string
35
+runDFA : DFA state alph -> List alph -> state
36
+runDFA a@(MkDFA _ start _) xs = runDFAFrom a start xs
37
+
38
+Eq state => Acceptor (DFA state alph) alph where
39
+    decide a@(MkDFA _ _ accepting) xs = (runDFA a xs) `elem` accepting
40
+
41
+
42
+||| A type for non-deterministic finite automata (NFA)
43
+|||
44
+||| @ state The type of the states
45
+||| @ alph The type of the input symbols
46
+data NFA : (state : Type) -> (alph : Type) -> Type where
47
+    ||| Makes a NFA
48
+    |||
49
+    ||| @ delta The state transition function
50
+    ||| @ start The starting state
51
+    ||| @ accepting The states that accept
52
+    MkNFA : (delta : state -> alph -> List state) -> (start : state) -> (accepting : List state) -> NFA state alph
53
+    
54
+||| Runs a NFA with an input string starting at a given state
55
+runNFAFrom : NFA state alph -> state -> List alph -> List state
56
+runNFAFrom _ state [] = [state]
57
+runNFAFrom a@(MkNFA delta _ _) st (x::xs) = delta st x >>= (\y => runNFAFrom a y xs)
58
+
59
+||| Runs a NFA with an input string
60
+runNFA : NFA state alph -> List alph -> List state
61
+runNFA a@(MkNFA _ start _) xs = runNFAFrom a start xs
62
+
63
+Eq state => Acceptor (NFA state alph) alph where
64
+    decide a@(MkNFA _ _ accepting) xs = any (`elem` accepting) (runNFA a xs)
65
+    
66
+||| Transforms a DFA into a NFA
67
+DFAToNFA : DFA state alph -> NFA state alph
68
+DFAToNFA a@(MkDFA delta start accepting) = MkNFA (\st, x => [delta st x]) start accepting
69
+    
70
+
71
+||| A type for non-deterministic finite automata with ε-transitions (ε-NFA)
72
+|||
73
+||| @ state The type of the states
74
+||| @ alph The type of the input symbols
75
+data ENFA : (state : Type) -> (alph : Type) -> Type where
76
+    ||| Makes an ε-NFA
77
+    |||
78
+    ||| @ delta The state transition function
79
+    ||| @ start The starting state
80
+    ||| @ accepting The states that accept
81
+    MkENFA : (delta : state -> Maybe alph -> List state) -> (start : state) -> (accepting : List state) -> ENFA state alph
82
+
83
+-- TODO: make total (breadth-/depth-first-search?)
84
+partial runENFAEpsilon : ENFA state alph -> state -> List state
85
+runENFAEpsilon a@(MkENFA delta _ _) st = (delta st Nothing) >>= (\x => runENFAEpsilon a x)
86
+    
87
+partial runENFAFrom : ENFA state alph -> state -> List alph -> List state
88
+runENFAFrom _ state [] = [state]
89
+runENFAFrom a@(MkENFA delta _ _) st (x::xs) = (delta st (Just x) ++ runENFAEpsilon a st) >>= (\y => runENFAFrom a y xs)

+ 4
- 0
Main.idr View File

@@ -0,0 +1,4 @@
1
+module Main
2
+
3
+import Data.Automata
4
+import Data.Fin

Loading…
Cancel
Save