annotate automaton-in-agda/src/pushdown.agda @ 330:407684f806e4

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 16 Nov 2022 17:43:10 +0900
parents 91781b7c65a8
children 6f3636fbc481
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module pushdown where
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Level renaming ( suc to succ ; zero to Zero )
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Data.Nat
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.List
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import Data.Maybe
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
7 -- open import Data.Bool using ( Bool ; true ; false )
17
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Relation.Binary.PropositionalEquality hiding ( [_] )
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Relation.Nullary using (¬_; Dec; yes; no)
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 open import Level renaming ( suc to succ ; zero to Zero )
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
11 -- open import Data.Product
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
12 open import logic
318
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
13 open import automaton
17
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 data PushDown ( Γ : Set ) : Set where
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 pop : PushDown Γ
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 push : Γ → PushDown Γ
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
19 none : PushDown Γ
17
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 record PushDownAutomaton ( Q : Set ) ( Σ : Set ) ( Γ : Set )
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 : Set where
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 field
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
25 pδ : Q → Σ → Γ → Q ∧ ( PushDown Γ )
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
26 pok : Q → Bool
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
27 pempty : Γ
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
28 pmoves : Q → List Γ → Σ → ( Q ∧ List Γ )
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
29 pmoves q [] i with pδ q i pempty
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
30 pmoves q [] i | ⟪ qn , pop ⟫ = ⟪ qn , [] ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
31 pmoves q [] i | ⟪ qn , push x ⟫ = ⟪ qn , ( x ∷ [] ) ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
32 pmoves q [] i | ⟪ qn , none ⟫ = ⟪ qn , [] ⟫
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
33 pmoves q ( H ∷ T ) i with pδ q i H
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
34 pmoves q (H ∷ T) i | ⟪ qn , pop ⟫ = ⟪ qn , T ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
35 pmoves q (H ∷ T) i | ⟪ qn , none ⟫ = ⟪ qn , (H ∷ T) ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
36 pmoves q (H ∷ T) i | ⟪ qn , push x ⟫ = ⟪ qn , x ∷ H ∷ T ⟫
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
37
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
38 paccept : (q : Q ) ( In : List Σ ) ( sp : List Γ ) → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
39 paccept q [] [] = pok q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
40 paccept q ( H ∷ T) [] with pδ q H pempty
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
41 paccept q (H ∷ T) [] | ⟪ qn , pop ⟫ = paccept qn T []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
42 paccept q (H ∷ T) [] | ⟪ qn , none ⟫ = paccept qn T []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
43 paccept q (H ∷ T) [] | ⟪ qn , push x ⟫ = paccept qn T (x ∷ [] )
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
44 paccept q [] (_ ∷ _ ) = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
45 paccept q ( H ∷ T ) ( SH ∷ ST ) with pδ q H SH
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
46 ... | ⟪ nq , pop ⟫ = paccept nq T ST
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
47 ... | ⟪ nq , none ⟫ = paccept nq T (SH ∷ ST)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
48 ... | ⟪ nq , push ns ⟫ = paccept nq T ( ns ∷ SH ∷ ST )
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
49
318
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
50 record PDA ( Q : Set ) ( Σ : Set ) ( Γ : Set )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
51 : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
52 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
53 automaton : Automaton Q Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
54 pδ : Q → PushDown Γ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
55 open Automaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
56 paccept : (q : Q ) ( In : List Σ ) ( sp : List Γ ) → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
57 paccept q [] [] = aend automaton q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
58 paccept q (H ∷ T) [] with pδ (δ automaton q H)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
59 paccept q (H ∷ T) [] | pop = paccept (δ automaton q H) T []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
60 paccept q (H ∷ T) [] | none = paccept (δ automaton q H) T []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
61 paccept q (H ∷ T) [] | push x = paccept (δ automaton q H) T (x ∷ [] )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
62 paccept q [] (_ ∷ _ ) = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
63 paccept q ( H ∷ T ) ( SH ∷ ST ) with pδ (δ automaton q H)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
64 ... | pop = paccept (δ automaton q H) T ST
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
65 ... | none = paccept (δ automaton q H) T (SH ∷ ST)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
66 ... | push ns = paccept (δ automaton q H) T ( ns ∷ SH ∷ ST )
17
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
68 data States0 : Set where
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
69 sr : States0
17
08b589172493 add pushdown and turing
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
71 data In2 : Set where
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
72 i0 : In2
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
73 i1 : In2
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
74
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
75 pnn : PushDownAutomaton States0 In2 States0
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
76 pnn = record {
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
77 pδ = pδ
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
78 ; pempty = sr
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
79 ; pok = λ q → true
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
80 } where
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
81 pδ : States0 → In2 → States0 → States0 ∧ PushDown States0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
82 pδ sr i0 _ = ⟪ sr , push sr ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
83 pδ sr i1 _ = ⟪ sr , pop ⟫
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
84
318
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
85 data States2 : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
86 ph1 : States2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
87 ph2 : States2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
88 phf : States2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
89
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
90 pnnp : PDA States2 In2 States2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
91 pnnp = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
92 automaton = record { aend = aend ; δ = δ }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
93 ; pδ = pδ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
94 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
95 δ : States2 → In2 → States2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
96 δ ph1 i0 = ph1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
97 δ ph1 i1 = ph2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
98 δ ph2 i1 = ph2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
99 δ _ _ = phf
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
100 aend : States2 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
101 aend ph2 = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
102 aend _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
103 pδ : States2 → PushDown States2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
104 pδ ph1 = push ph1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
105 pδ ph2 = pop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
106 pδ phf = none
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
107
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
108 data States1 : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
109 ss : States1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
110 st : States1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
111 pn1 : PushDownAutomaton States1 In2 States1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
112 pn1 = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
113 pδ = pδ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
114 ; pempty = ss
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
115 ; pok = pok1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
116 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
117 pok1 : States1 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
118 pok1 ss = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
119 pok1 st = true
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
120 pδ : States1 → In2 → States1 → States1 ∧ PushDown States1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
121 pδ ss i0 _ = ⟪ ss , push ss ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
122 pδ ss i1 _ = ⟪ st , pop ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
123 pδ st i0 _ = ⟪ st , push ss ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
124 pδ st i1 _ = ⟪ st , pop ⟫
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
125
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
126 test1 = PushDownAutomaton.paccept pnn sr ( i0 ∷ i0 ∷ i1 ∷ i1 ∷ [] ) []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
127 test2 = PushDownAutomaton.paccept pnn sr ( i0 ∷ i0 ∷ i1 ∷ i0 ∷ [] ) []
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
128 test3 = PushDownAutomaton.pmoves pnn sr [] i0
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
129 test4 = PushDownAutomaton.paccept pnn sr ( i0 ∷ i0 ∷ i1 ∷ i1 ∷ i0 ∷ i1 ∷ [] ) []
38
ab265470c2d0 push down automaton example
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
130
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
131 test5 = PushDownAutomaton.paccept pn1 ss ( i0 ∷ i0 ∷ i1 ∷ i1 ∷ [] ) []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
132 test6 = PushDownAutomaton.paccept pn1 ss ( i0 ∷ i0 ∷ i1 ∷ i1 ∷ i0 ∷ i1 ∷ [] ) []
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
133