annotate nfa.agda @ 0:9615a94b18ca

new automaton in agda
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 12 Nov 2020 11:45:34 +0900
parents
children 7399aae907fc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module nfa where
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 open import Level renaming ( suc to Suc ; zero to Zero )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Relation.Binary.PropositionalEquality
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.List
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import automaton
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open import logic
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 record NAutomaton {n : Level} ( Q : Set n ) ( Σ : Set )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 : Set (Suc n) where
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 field
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 Nδ : Q → Σ → Q → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 NF : Q → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 Nexists : (Q → Set) → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 open NAutomaton
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 naccept : {n : Level} {Q : Set n} {Σ : Set} → NAutomaton Q Σ → (Q → Set) → List Σ → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 naccept {n} {Q} {Σ} nfa qs [] = Nexists nfa (λ q → qs q ∧ NF nfa q )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 naccept {n} {Q} {Σ} nfa qs (x ∷ input) =
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 Nexists nfa (λ q → qs q ∧ (naccept nfa (Nδ nfa q x) input ))
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 ntrace : {n : Level} {Q : Set n} {Σ : Set } → (NFA : NAutomaton Q Σ) → (start : Q → Set ) → (input : List Σ ) → naccept NFA start input
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 → ((Q → Set) → List Q)
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 → List (List Q)
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 ntrace {n} {Q} {Σ} A q [] a list = list q ∷ []
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 ntrace {n} {Q} {Σ} A q (x ∷ t) a list = list q ∷ ( ntrace A {!!} t {!!} list )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 data transition136 : Q3 → Σ2 → Q3 → Set where
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 d0 : transition136 q₁ s1 q₂
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 d1 : transition136 q₁ s0 q₁
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 d2 : transition136 q₂ s0 q₂
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 d3 : transition136 q₂ s0 q₃
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 d4 : transition136 q₂ s1 q₃
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 d5 : transition136 q₃ s0 q₁
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 start136 : Q3 → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 start136 q = q ≡ q₁
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 nfa136 : NAutomaton Q3 Σ2
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 nfa136 = record { Nδ = transition136 ; NF = λ q → q ≡ q₁ ; Nexists = exists-in-Q3 }
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 example136-1 = naccept nfa136 start136 ( s0 ∷ s1 ∷ s0 ∷ s0 ∷ [] )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 example136-0 = naccept nfa136 start136 ( s0 ∷ [] )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 example136-2 = naccept nfa136 start136 ( s1 ∷ s0 ∷ s1 ∷ s0 ∷ s1 ∷ [] )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 --
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 -- ((Q → Set) → Set) → ((Q → Set) → Set) → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 --
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 data Nexists→exits {n : Level} { Q : Set n } {Σ : Set} ( NFA : NAutomaton Q Σ ) : ((Q → Set) → Set) → Set where
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 one-of : Nexists→exits NFA (Nexists NFA)
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 subset-construction : {n : Level} { Q : Set n } { Σ : Set } →
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 (NAutomaton Q Σ ) → Automaton {Suc Zero ⊔ n} (Q → Set) Σ
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 subset-construction {n} {Q} { Σ} NFA = record {
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 δ = λ qs x qs → Nexists NFA (λ q → Nδ NFA q x qs )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 ; F = λ qs → Nexists NFA ( λ q → NF NFA q )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 ; exists = Nexists→exits NFA -- what this means?
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 }
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 subset-construction-lemma→ : { Q : Set } { Σ : Set } →
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 (NFA : NAutomaton Q Σ ) → (astart : Q → Set )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 → (x : List Σ)
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 → naccept NFA ( λ q1 → astart q1) x
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 → accept ( subset-construction NFA ) ( λ q1 → astart q1) x
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 subset-construction-lemma→ = {!!}
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 subset-construction-lemma← : { Q : Set } { Σ : Set } →
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 (NFA : NAutomaton Q Σ ) → (astart : Q → Set )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 → (x : List Σ)
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 → accept ( subset-construction NFA ) ( λ q1 → astart q1) x
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 → naccept NFA ( λ q1 → astart q1) x
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 subset-construction-lemma← = {!!}
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 open import regular-language
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 open RegularLanguage
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 open Automaton
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 open import Data.Empty
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 Concat-exists : {n : Level} {Σ : Set} → (A B : RegularLanguage {n} Σ ) → (states A ∨ states B → Set) → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 Concat-exists {n} {Σ} A B P = exists (automaton A) (λ q → P (case1 q)) ∨ exists (automaton B) (λ q → P (case2 q))
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 Concat-NFA : {n : Level} {Σ : Set} → (A B : RegularLanguage {n} Σ ) → NAutomaton (states A ∨ states B) Σ
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 Concat-NFA {n} {Σ} A B = record { Nδ = δnfa ; NF = nend ; Nexists = Concat-exists A B }
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 module Concat-NFA where
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 data δnfa : states A ∨ states B → Σ → states A ∨ states B → Set where
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 a-case : {q : states A} {i : Σ } → δnfa ( case1 q) i (case1 (δ (automaton A) q i))
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 ab-trans : {q : states A} {i : Σ } → F (automaton A) q → δnfa ( case1 q) i (case2 (δ (automaton B) (astart B) i))
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 b-case : {q : states B} {i : Σ } → δnfa ( case2 q) i (case2 (δ (automaton B) q i))
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 nend : states A ∨ states B → Set
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 nend (case2 q) = F (automaton B) q
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 nend (case1 q) = F (automaton A) q ∧ F (automaton B) (astart B) -- empty B case
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 data state-is {n : Level} {Σ : Set } (A : RegularLanguage {n} Σ ) : (a : states A ) → Set where
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 this : (a : states A) → state-is A a
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 closed-in-concat : {n : Level} {Σ : Set } → (A B : RegularLanguage {n} Σ ) → ( x : List Σ )
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 → isRegular (Concat {n} (contain A) (contain B)) x record {states = states A ∨ states B → Set ; astart = λ q → state-is A (astart A)
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 ; automaton = subset-construction ( Concat-NFA A B )}
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 closed-in-concat {Σ} A B x = {!!}
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
9615a94b18ca new automaton in agda
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109