Mercurial > hg > Members > kono > Proof > automaton
view automaton-in-agda/src/derive.agda @ 270:dd98e7e5d4a5
derive worked but finiteness is difficult
add regular star
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 26 Nov 2021 20:02:06 +0900 |
parents | 3fa72793620b |
children | 5e066b730d73 |
line wrap: on
line source
{-# OPTIONS --allow-unsolved-metas #-} open import Relation.Binary.PropositionalEquality hiding ( [_] ) open import Relation.Nullary using (¬_; Dec; yes; no) open import Data.List hiding ( [_] ) module derive ( Σ : Set) ( eq? : (x y : Σ) → Dec (x ≡ y)) where -- open import nfa open import Data.Nat -- open import Data.Nat hiding ( _<_ ; _>_ ) -- open import Data.Fin hiding ( _<_ ) open import finiteSet open import finiteSetUtil open import automaton open import logic open import regex empty? : Regex Σ → Bool empty? ε = true empty? φ = false empty? (x *) = true empty? (x & y) = empty? x /\ empty? y empty? (x || y) = empty? x \/ empty? y empty? < x > = false derivative : Regex Σ → Σ → Regex Σ derivative ε s = φ derivative φ s = φ derivative (x *) s with derivative x s ... | ε = x * ... | φ = φ ... | t = t & (x *) derivative (x & y) s with empty? x ... | true with derivative x s | derivative y s ... | ε | φ = φ ... | ε | t = y || t ... | φ | t = t ... | x1 | φ = x1 & y ... | x1 | y1 = (x1 & y) || y1 derivative (x & y) s | false with derivative x s ... | ε = y ... | φ = φ ... | t = t & y derivative (x || y) s with derivative x s | derivative y s ... | φ | y1 = y1 ... | x1 | φ = x1 ... | x1 | y1 = x1 || y1 derivative < x > s with eq? x s ... | yes _ = ε ... | no _ = φ data regex-states (x : Regex Σ ) : Regex Σ → Set where unit : regex-states x x derive : { y : Regex Σ } → regex-states x y → (s : Σ) → regex-states x ( derivative y s ) record Derivative (x : Regex Σ ) : Set where field state : Regex Σ is-derived : regex-states x state open Derivative open import Data.Fin hiding (_<_) -- derivative generates (x & y) || ... form. y and x part is a substerm of original regex -- since subterm is finite, only finite number of state is generated for each operator -- this does not work, becuase it depends on input sequences -- finite-derivative : (r : Regex Σ) → FiniteSet Σ → FiniteSet (Derivative r) -- order : Regex Σ → ℕ -- decline-derive : (x : Regex Σ ) (i : Σ ) → 0 < order x → order (derivative x i) < order x -- is not so easy -- in case of automaton, number of derivative is limited by iteration of input length, so it is finite. -- so we cannot say derived automaton is finite i.e. regex-match is regular language now regex→automaton : (r : Regex Σ) → Automaton (Derivative r) Σ regex→automaton r = record { δ = λ d s → record { state = derivative (state d) s ; is-derived = derive-step d s} ; aend = λ d → empty? (state d) } where derive-step : (d0 : Derivative r) → (s : Σ) → regex-states r (derivative (state d0) s) derive-step d0 s = derive (is-derived d0) s regex-match : (r : Regex Σ) → (List Σ) → Bool regex-match ex is = accept ( regex→automaton ex ) record { state = ex ; is-derived = unit } is