annotate agda/regex1.agda @ 89:e919e82e95a2

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 10 Nov 2019 12:21:44 +0900
parents aa15eff1aeb3
children 7a0634a7c25a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
1 {-# OPTIONS --allow-unsolved-metas #-}
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
2 module regex1 where
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Level renaming ( suc to succ ; zero to Zero )
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
5 open import Data.Fin
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
6 open import Data.Nat hiding ( _≟_ )
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
7 open import Data.List hiding ( any ; [_] )
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
8 import Data.Bool using ( Bool ; true ; false ; _∧_ )
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
9 open import Data.Bool using ( Bool ; true ; false ; _∧_ ; _∨_ )
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
10 open import Relation.Binary.PropositionalEquality as RBF hiding ( [_] )
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
11 open import Relation.Nullary using (¬_; Dec; yes; no)
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
12
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 data Regex ( Σ : Set ) : Set where
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 _* : Regex Σ → Regex Σ
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 _&_ : Regex Σ → Regex Σ → Regex Σ
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
17 _||_ : Regex Σ → Regex Σ → Regex Σ
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
18 <_> : Σ → Regex Σ
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
19
36
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
20 -- postulate a b c d : Set
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
21
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
22 data hoge : Set where
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
23 a : hoge
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
24 b : hoge
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
25 c : hoge
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
26 d : hoge
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
27
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
28 infixr 40 _&_ _||_
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
29
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
30 r1' = (< a > & < b >) & < c >
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
31 r1 = < a > & < b > & < c >
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
32 any = < a > || < b > || < c >
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
33 r2 = ( any * ) & ( < a > & < b > & < c > )
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
34 r3 = ( any * ) & ( < a > & < b > & < c > & < a > & < b > & < c > )
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
35 r4 = ( < a > & < b > & < c > ) || ( < b > & < c > & < d > )
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
36 r5 = ( any * ) & ( < a > & < b > & < c > || < b > & < c > & < d > )
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
37
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
38 open import nfa
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
39
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
40 split : {Σ : Set} → (List Σ → Bool)
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
41 → ( List Σ → Bool) → List Σ → Bool
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
42 split x y [] = x [] ∧ y []
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
43 split x y (h ∷ t) = (x [] ∧ y (h ∷ t)) ∨
41
ae69102153a9 cfg done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
44 split (λ t1 → x ( h ∷ t1 )) (λ t2 → y t2 ) t
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
45
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
46 {-# TERMINATING #-}
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
47 repeat : {Σ : Set} → (List Σ → Bool) → List Σ → Bool
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
48 repeat x [] = true
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
49 repeat {Σ} x ( h ∷ t ) = split x (repeat {Σ} x) ( h ∷ t )
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
50
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
51 open import finiteSet
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
52 open FiniteSet
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
53
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
54 cmpi : {Σ : Set} → {n : ℕ } (fin : FiniteSet Σ {n}) → (x y : Σ ) → Dec (F←Q fin x ≡ F←Q fin y)
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
55 cmpi fin x y = F←Q fin x ≟ F←Q fin y
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
56
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
57 regular-language : {Σ : Set} → Regex Σ → {n : ℕ } (fin : FiniteSet Σ {n}) → List Σ → Bool
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
58 regular-language (x *) f = repeat ( regular-language x f )
39
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
59 regular-language (x & y) f = split ( regular-language x f ) ( regular-language y f )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
60 regular-language (x || y) f = λ s → ( regular-language x f s ) ∨ ( regular-language y f s)
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
61 regular-language < h > f [] = false
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
62 regular-language < h > f (h1 ∷ [] ) with cmpi f h h1
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
63 ... | yes _ = true
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
64 ... | no _ = false
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
65 regular-language < h > f _ = false
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
66