annotate automaton-in-agda/src/regex1.agda @ 253:012f79b51dba

... prime version
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 29 Jun 2021 23:24:03 +0900
parents 3fa72793620b
children 1c8ed1220489
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)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
12 open import regex
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
13
36
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
14 -- postulate a b c d : Set
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
15
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
16 data In : Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
17 a : In
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
18 b : In
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
19 c : In
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
20 d : In
36
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
21
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
22 cmpi : (x y : In ) → Dec (x ≡ y)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
23 cmpi a a = yes refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
24 cmpi b b = yes refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
25 cmpi c c = yes refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
26 cmpi d d = yes refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
27 cmpi a b = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
28 cmpi a c = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
29 cmpi a d = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
30 cmpi b a = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
31 cmpi b c = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
32 cmpi b d = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
33 cmpi c a = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
34 cmpi c b = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
35 cmpi c d = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
36 cmpi d a = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
37 cmpi d b = no (λ ())
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
38 cmpi d c = no (λ ())
36
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
39
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
40 -- infixr 40 _&_ _||_
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
41
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
42 r1' = (< a > & < b >) & < c > --- abc
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
43 r1 = < a > & < b > & < c > --- abc
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
44 any = < a > || < b > || < c > --- a|b|c
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
45 r2 = ( any * ) & ( < a > & < b > & < c > ) -- .*abc
36
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
46 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
47 r4 = ( < a > & < b > & < c > ) || ( < b > & < c > & < d > )
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 35
diff changeset
48 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
49
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
50 open import nfa
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
51
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
52 -- former ++ later ≡ x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
53 split : {Σ : Set} → ((former : List Σ) → Bool) → ((later : List Σ) → Bool) → (x : List Σ ) → Bool
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
54 split x y [] = x [] ∧ y []
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
55 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
56 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
57
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
58 -- tt1 : {Σ : Set} → ( P Q : List In → Bool ) → split P Q ( a ∷ b ∷ c ∷ [] )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
59 -- tt1 P Q = ?
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
60
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
61 {-# TERMINATING #-}
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
62 repeat : {Σ : Set} → (List Σ → Bool) → List Σ → Bool
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
63 repeat x [] = true
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
64 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
65
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
66 regular-language : {Σ : Set} → Regex Σ → ((x y : Σ ) → Dec (x ≡ y)) → List Σ → Bool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
67 regular-language φ cmp _ = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
68 regular-language ε cmp [] = true
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
69 regular-language ε cmp (_ ∷ _) = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
70 regular-language (x *) cmp = repeat ( regular-language x cmp )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
71 regular-language (x & y) cmp = split ( λ z → (regular-language x cmp) z ) (λ z → regular-language y cmp z )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
72 regular-language (x || y) cmp = λ s → ( regular-language x cmp s ) ∨ ( regular-language y cmp s)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
73 regular-language < h > cmp [] = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
74 regular-language < h > cmp (h1 ∷ [] ) with cmp h h1
34
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
75 ... | yes _ = true
a904b6bc76af add regular language
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
76 ... | no _ = false
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
77 regular-language < h > _ (_ ∷ _ ∷ _) = false
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
78
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
79 test-regex : regular-language r1' cmpi ( a ∷ [] ) ≡ false
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
80 test-regex = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
81
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
82 test-regex1 : regular-language r2 cmpi ( a ∷ a ∷ b ∷ c ∷ [] ) ≡ true
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
83 test-regex1 = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
84
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
85
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
86 test-AB→split : {Σ : Set} → {A B : List In → Bool} → split A B ( a ∷ b ∷ a ∷ [] ) ≡ (
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
87 ( A [] ∧ B ( a ∷ b ∷ a ∷ [] ) ) ∨
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
88 ( A ( a ∷ [] ) ∧ B ( b ∷ a ∷ [] ) ) ∨
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
89 ( A ( a ∷ b ∷ [] ) ∧ B ( a ∷ [] ) ) ∨
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
90 ( A ( a ∷ b ∷ a ∷ [] ) ∧ B [] )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
91 )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
92 test-AB→split {_} {A} {B} = refl
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
93
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
94 -- from example 1.53 1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
95
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
96 ex53-1 : Set
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
97 ex53-1 = (s : List In ) → regular-language ( (< a > *) & < b > & (< a > *) ) cmpi s ≡ true → {!!} -- contains exact one b
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
98
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
99 ex53-2 : Set
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
100 ex53-2 = (s : List In ) → regular-language ( (any * ) & < b > & (any *) ) cmpi s ≡ true → {!!} -- contains at lease one b
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
101
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
102 evenp : {Σ : Set} → List Σ → Bool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
103 oddp : {Σ : Set} → List Σ → Bool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
104 oddp [] = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
105 oddp (_ ∷ t) = evenp t
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
106
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
107 evenp [] = true
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
108 evenp (_ ∷ t) = oddp t
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
109
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
110 -- from example 1.53 5
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
111 egex-even : Set
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
112 egex-even = (s : List In ) → regular-language ( ( any & any ) * ) cmpi s ≡ true → evenp s ≡ true
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
113
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
114 test11 = regular-language ( ( any & any ) * ) cmpi (a ∷ [])
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
115 test12 = regular-language ( ( any & any ) * ) cmpi (a ∷ b ∷ [])
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
116
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
117 -- proof-egex-even : egex-even
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
118 -- proof-egex-even [] _ = refl
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
119 -- proof-egex-even (a ∷ []) ()
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
120 -- proof-egex-even (b ∷ []) ()
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
121 -- proof-egex-even (c ∷ []) ()
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
122 -- proof-egex-even (x ∷ x₁ ∷ s) y = proof-egex-even s {!!}
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
123
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
124 open import derive In cmpi
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
125 open import automaton
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
126
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
127 ra-ex = trace (regex→automaton r2) (record { state = r2 ; is-derived = unit }) ( a ∷ b ∷ c ∷ [])
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
128