view agda/nfa.agda @ 89:e919e82e95a2

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 10 Nov 2019 12:21:44 +0900
parents f124fceba460
children b3f05cd08d24
line wrap: on
line source

module nfa where

-- open import Level renaming ( suc to succ ; zero to Zero )
open import Data.Nat
open import Data.List
open import Data.Fin hiding ( _<_ )
open import Data.Maybe
open import Relation.Nullary
open import Data.Empty
-- open import Data.Bool using ( Bool ; true ; false ; _∧_ ; _∨_ )
open import  Relation.Binary.PropositionalEquality hiding ( [_] )
open import Relation.Nullary using (¬_; Dec; yes; no)
open import finiteSet
open import logic

data  States1   : Set  where
   sr : States1
   ss : States1
   st : States1

data  In2   : Set  where
   i0 : In2
   i1 : In2


record NAutomaton ( Q : Set ) ( Σ : Set  )
       : Set  where
    field
          Nδ : Q → Σ → Q → Bool
          Nend  :  Q → Bool

open NAutomaton

finState1 : FiniteSet States1 
finState1 = record {
        Q←F = Q←F
      ; F←Q  = F←Q
      ; finiso→ = finiso→
      ; finiso← = finiso←
   } where
       Q←F : Fin 3 → States1
       Q←F zero = sr
       Q←F (suc zero) = ss
       Q←F (suc (suc zero)) = st
       F←Q : States1 → Fin 3
       F←Q sr = zero
       F←Q ss = suc zero
       F←Q st = suc (suc zero)
       finiso→ : (q : States1) → Q←F (F←Q q) ≡ q
       finiso→ sr = refl
       finiso→ ss = refl
       finiso→ st = refl
       finiso← : (f : Fin 3) → F←Q (Q←F f) ≡ f
       finiso← zero = refl
       finiso← (suc zero) = refl
       finiso← (suc (suc zero)) = refl
       finiso← (suc (suc (suc ()))) 


open FiniteSet

Nmoves : { Q : Set } { Σ : Set  }
    → NAutomaton Q  Σ
    → {n : ℕ } → FiniteSet Q  {n}
    →  ( Qs : Q → Bool )  → (s : Σ ) → Q → Bool
Nmoves {Q} { Σ} M fin  Qs  s q  =
      exists fin ( λ qn → (Qs qn /\ ( Nδ M qn s q )  ))


Naccept : { Q : Set } { Σ : Set  } 
    → NAutomaton Q  Σ
    → {n : ℕ } → FiniteSet Q {n}
    → (Nstart : Q → Bool) → List  Σ → Bool
Naccept M fin sb []  = exists fin ( λ q → sb q /\ Nend M q )
Naccept M fin sb (i ∷ t ) = Naccept M fin ( Nmoves M  fin sb i ) t


transition3 : States1  → In2  → States1 → Bool
transition3 sr i0 sr = true
transition3 sr i1 ss = true
transition3 sr i1 sr = true
transition3 ss i0 sr = true
transition3 ss i1 st = true
transition3 st i0 sr = true
transition3 st i1 st = true
transition3 _ _ _ = false

fin1 :  States1  → Bool
fin1 st = true
fin1 ss = false
fin1 sr = false

start1 : States1 → Bool
start1 sr = true
start1 _ = false

am2  :  NAutomaton  States1 In2
am2  =  record { Nδ = transition3 ; Nend = fin1}

example2-1 = Naccept am2 finState1 start1 ( i0  ∷ i1  ∷ i0  ∷ [] ) 
example2-2 = Naccept am2 finState1 start1 ( i1  ∷ i1  ∷ i1  ∷ [] ) 

transition4 : States1  → In2  → States1 → Bool
transition4 sr i0 sr = true
transition4 sr i1 ss = true
transition4 sr i1 sr = true
transition4 ss i0 ss = true
transition4 ss i1 st = true
transition4 st i0 st = true
transition4 st i1 st = true
transition4 _ _ _ = false

fin4 :  States1  → Bool
fin4 st = true
fin4 _ = false

start4 : States1 → Bool
start4 ss = true
start4 _ = false

am4  :  NAutomaton  States1 In2
am4  =  record { Nδ = transition4 ; Nend = fin4}

example4-1 = Naccept am4 finState1 start4 ( i0  ∷ i1  ∷ i1  ∷ i0 ∷ [] ) 
example4-2 = Naccept am4 finState1 start4 ( i0  ∷ i1  ∷ i1  ∷ i1 ∷ [] ) 

fin0 :  States1  → Bool
fin0 st = false
fin0 ss = false
fin0 sr = false

test0 : Bool
test0 = exists finState1 fin0

test1 : Bool
test1 = exists finState1 fin1

test2 = Nmoves am2 finState1 start1 

-- test4 : Fin 2 → Bool
-- test4 zero = {!!}
-- test4 (suc zero) = {!!}
-- test4 (suc (suc ()))

--  0011
--  00000111111
inputnn : { n :  ℕ }  →  { Σ : Set  } → ( x y : Σ )  → List  Σ → List  Σ
inputnn {zero} {_} _ _ s = s
inputnn {suc n} x y s = x  ∷ ( inputnn {n} x y ( y  ∷ s ) )                                                                                                         

-- lemmaNN :  { Q : Set } { Σ : Set  } → ( x y : Σ ) → (M : NAutomaton Q  Σ)
--     → ( n :  ℕ )  → (fin :  FiniteSet Q  {n} ) 
--     →  Naccept M fin ( inputnn {n} x y [] )  ≡ true  
--     →  Naccept M fin ( inputnn {suc n} x y [] )  ≡ false  
-- lemmaNN {Q} {Σ} x y M n fin nac = {!!}