annotate automaton-in-agda/src/cfg1.agda @ 278:e89957b99662

dup in finiteSet in long list
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 26 Dec 2021 12:38:37 +0900
parents 42563cc6afdf
children 91781b7c65a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module cfg1 where
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Level renaming ( suc to succ ; zero to Zero )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Data.Nat hiding ( _≟_ )
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
5 -- open import Data.Fin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
6 -- open import Data.Product
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 open import Data.List
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Data.Maybe
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
9 -- open import Data.Bool using ( Bool ; true ; false ; _∧_ ; _∨_ )
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 open import Relation.Binary.PropositionalEquality hiding ( [_] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 open import Relation.Nullary using (¬_; Dec; yes; no)
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
12 open import logic
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
163
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
14 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
15 -- Java → Java Byte Code
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
16 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
17 -- CFG Stack Machine (PDA)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
18 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
19
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
20
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 data Node (Symbol : Set) : Set where
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 T : Symbol → Node Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 N : Symbol → Node Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 data Seq (Symbol : Set) : Set where
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 _,_ : Symbol → Seq Symbol → Seq Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 _. : Symbol → Seq Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 Error : Seq Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 data Body (Symbol : Set) : Set where
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 _|_ : Seq Symbol → Body Symbol → Body Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 _; : Seq Symbol → Body Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 record CFGGrammer (Symbol : Set) : Set where
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 field
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 cfg : Symbol → Body Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 top : Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 eq? : Symbol → Symbol → Bool
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 typeof : Symbol → Node Symbol
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 infixr 80 _|_
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 infixr 90 _;
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 infixr 100 _,_
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 infixr 110 _.
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 open CFGGrammer
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 -----------------
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 --
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 -- CGF language
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 --
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 -----------------
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 split : {Σ : Set} → (List Σ → Bool)
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 → ( List Σ → Bool) → List Σ → Bool
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
56 split x y [] = x [] /\ y []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
57 split x y (h ∷ t) = (x [] /\ y (h ∷ t)) \/
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 split (λ t1 → x ( h ∷ t1 )) (λ t2 → y t2 ) t
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 cfg-language0 : {Symbol : Set} → CFGGrammer Symbol → Body Symbol → List Symbol → Bool
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 {-# TERMINATING #-}
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 cfg-language1 : {Symbol : Set} → CFGGrammer Symbol → Seq Symbol → List Symbol → Bool
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 cfg-language1 cg Error x = false
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 cfg-language1 cg (S , seq) x with typeof cg S
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
67 cfg-language1 cg (_ , seq) (x' ∷ t) | T x = eq? cg x x' /\ cfg-language1 cg seq t
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 cfg-language1 cg (_ , seq) [] | T x = false
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 cfg-language1 cg (_ , seq) x | N nonTerminal = split (cfg-language0 cg (cfg cg nonTerminal) )(cfg-language1 cg seq ) x
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 cfg-language1 cg (S .) x with typeof cg S
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 cfg-language1 cg (_ .) (x' ∷ []) | T x = eq? cg x x'
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 cfg-language1 cg (_ .) _ | T x = false
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 cfg-language1 cg (_ .) x | N nonTerminal = cfg-language0 cg (cfg cg nonTerminal) x
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 cfg-language0 cg _ [] = false
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 cfg-language0 cg (rule | b) x =
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
77 cfg-language1 cg rule x \/ cfg-language0 cg b x
46
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 cfg-language0 cg (rule ;) x = cfg-language1 cg rule x
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 cfg-language : {Symbol : Set} → CFGGrammer Symbol → List Symbol → Bool
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 cfg-language cg = cfg-language0 cg (cfg cg (top cg ))
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 data IFToken : Set where
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 EA : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 EB : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 EC : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 IF : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 THEN : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 ELSE : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 SA : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 SB : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 SC : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 expr : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 statement : IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 token-eq? : IFToken → IFToken → Bool
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 token-eq? EA EA = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 token-eq? EB EB = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 token-eq? EC EC = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 token-eq? IF IF = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 token-eq? THEN THEN = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 token-eq? ELSE ELSE = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 token-eq? SA SA = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 token-eq? SB SB = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 token-eq? SC SC = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 token-eq? expr expr = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 token-eq? statement statement = true
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 token-eq? _ _ = false
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 typeof-IFG : IFToken → Node IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 typeof-IFG expr = N expr
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 typeof-IFG statement = N statement
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 typeof-IFG x = T x
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 IFGrammer : CFGGrammer IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 IFGrammer = record {
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 cfg = cfg'
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 ; top = statement
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 ; eq? = token-eq?
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 ; typeof = typeof-IFG
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 } where
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 cfg' : IFToken → Body IFToken
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 cfg' expr = EA . | EB . | EC . ;
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 cfg' statement =
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 SA . | SB . | SC .
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 | IF , expr , THEN , statement .
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 | IF , expr , THEN , statement , ELSE , statement .
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 cfg' x = Error ;
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 cfgtest1 = cfg-language IFGrammer ( SA ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 cfgtest2 = cfg-language1 IFGrammer ( SA .) ( SA ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 cfgtest3 = cfg-language1 IFGrammer ( SA . ) ( SA ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 cfgtest4 = cfg-language IFGrammer (IF ∷ EA ∷ THEN ∷ SA ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 cfgtest5 = cfg-language1 IFGrammer ( IF , expr , THEN , statement . ) (IF ∷ EA ∷ THEN ∷ SA ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 cfgtest6 = cfg-language1 IFGrammer ( statement .)(IF ∷ EA ∷ SA ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 cfgtest7 = cfg-language1 IFGrammer ( IF , expr , THEN , statement , ELSE , statement . )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 (IF ∷ EA ∷ THEN ∷ SA ∷ ELSE ∷ SB ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 cfgtest8 = cfg-language IFGrammer (IF ∷ EA ∷ THEN ∷ IF ∷ EB ∷ THEN ∷ SA ∷ ELSE ∷ SB ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 cfgtest9 = cfg-language IFGrammer (IF ∷ EB ∷ THEN ∷ SA ∷ ELSE ∷ SB ∷ [] )
964e4bd0272a add coinduction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
147 data E1Token : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
148 e1 : E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
149 e[ : E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
150 e] : E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
151 expr : E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
152 term : E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
153
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
154 E1-token-eq? : E1Token → E1Token → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
155 E1-token-eq? e1 e1 = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
156 E1-token-eq? e[ e] = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
157 E1-token-eq? e] e] = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
158 E1-token-eq? expr expr = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
159 E1-token-eq? term term = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
160 E1-token-eq? _ _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
161
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
162 typeof-E1 : E1Token → Node E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
163 typeof-E1 expr = N expr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
164 typeof-E1 term = N term
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
165 typeof-E1 x = T x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
166
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
167 E1Grammer : CFGGrammer E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
168 E1Grammer = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
169 cfg = cfgE
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
170 ; top = expr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
171 ; eq? = E1-token-eq?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
172 ; typeof = typeof-E1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
173 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
174 cfgE : E1Token → Body E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
175 cfgE expr = term .
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
176
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
177 cfgE term = e1 .
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
178 | e[ , expr , e] .
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
179
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
180 cfgE x = Error ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
181
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
182 ecfgtest1 = cfg-language E1Grammer ( e1 ∷ [] )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
183 ecfgtest2 = cfg-language E1Grammer ( e[ ∷ e1 ∷ e] ∷ [] )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
184 ecfgtest3 = cfg-language E1Grammer ( e[ ∷ e[ ∷ e1 ∷ e] ∷ e] ∷ [] )
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
185 ecfgtest4 = cfg-language E1Grammer ( e[ ∷ e1 ∷ [] )
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
186
274
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
187 open import Function
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
188
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
189 left : {t : Set } → List E1Token → ( fail next : List E1Token → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
190 left ( e[ ∷ t ) fail next = next t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
191 left t fail next = fail t
274
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
192
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
193 right : {t : Set } → List E1Token → ( fail next : List E1Token → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
194 right ( e] ∷ t ) fail next = next t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
195 right t fail next = fail t
274
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
196
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
197
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
198 {-# TERMINATING #-}
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
199 expr1 : {t : Set } → List E1Token → ( fail next : List E1Token → t ) → t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
200 expr1 ( e1 ∷ t ) fail next = next t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
201 expr1 ( expr ∷ t ) fail next = next t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
202 expr1 ( term ∷ t ) fail next = next t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
203 expr1 x fail next = left x fail $ λ x → expr1 x fail $ λ x → right x fail $ next
277
42563cc6afdf non-regular
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 276
diff changeset
204 -- expr1 x fail next = left x fail ( λ x → expr1 x fail ( λ x → right x fail ( next )))
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
205
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
206 cfgtest01 = expr1 ( e[ ∷ e[ ∷ e1 ∷ e] ∷ e] ∷ [] ) (λ x → ⟪ false , x ⟫ ) (λ x → ⟪ true , x ⟫ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
207 cfgtest02 = expr1 ( e[ ∷ e[ ∷ e1 ∷ e] ∷ [] ) (λ x → ⟪ false , x ⟫ ) (λ x → ⟪ true , x ⟫ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
208 cfgtest03 = expr1 ( e[ ∷ e[ ∷ e1 ∷ e] ∷ e] ∷ e] ∷ [] ) (λ x → ⟪ false , x ⟫ ) (λ x → ⟪ true , x ⟫ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
209
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
210 open import pushdown
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
211
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
212 data CG1 : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
213 ce : CG1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
214 c1 : CG1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
216 pd : CG1 → E1Token → CG1 → CG1 ∧ PushDown CG1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
217 pd c1 e[ s = ⟪ c1 , push c1 ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
218 pd c1 e] c1 = ⟪ c1 , pop ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
219 pd c1 e1 c1 = ⟪ c1 , none ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
220 pd s expr s1 = ⟪ c1 , none ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
221 pd s term s1 = ⟪ c1 , none ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
222 pd s _ s1 = ⟪ ce , none ⟫
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
223
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
224 pok : CG1 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
225 pok c1 = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
226 pok s = false
274
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
227
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
228 pnc : PushDownAutomaton CG1 E1Token CG1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
229 pnc = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
230 pδ = pd
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
231 ; pempty = ce
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
232 ; pok = pok
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
233 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
234
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
235 pda-ce-test1 = PushDownAutomaton.paccept pnc c1 ( e[ ∷ e[ ∷ e1 ∷ e] ∷ e] ∷ [] ) []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
236 pda-ce-test2 = PushDownAutomaton.paccept pnc c1 ( e[ ∷ e[ ∷ e1 ∷ e] ∷ [] ) []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
237 pda-ce-test3 = PushDownAutomaton.paccept pnc c1 ( e[ ∷ e1 ∷ e] ∷ e1 ∷ [] ) []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
238
276
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
239 record PNC (accept : Bool ) : Set where
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
240 field
276
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
241 orig-x : List E1Token
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
242 pnc-q : CG1
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
243 pnc-st : List CG1
276
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
244 pnc-p : CG1 → List CG1 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
245 success : accept ≡ true → pnc-p pnc-q pnc-st ≡ true → PushDownAutomaton.paccept pnc c1 orig-x [] ≡ true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
246 failure : accept ≡ false → pnc-p pnc-q pnc-st ≡ false → PushDownAutomaton.paccept pnc c1 orig-x [] ≡ false
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
248 open import Data.Unit
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
249
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
250 {-# TERMINATING #-}
276
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
251 expr1P : {n : Level } {t : Set n } → (x : List E1Token ) → PNC true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
252 → ( fail : List E1Token → PNC false → t ) → ( next : List E1Token → PNC true → t ) → t
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
253 expr1P x _ _ _ = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
254
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
255 expr1P-test : (x : List E1Token ) → ⊤
276
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
256 expr1P-test x = expr1P x record { orig-x = x ; pnc-q = c1 ; pnc-st = []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
257 ; pnc-p = λ q st → PushDownAutomaton.paccept pnc q x st ; success = λ _ p → p ; failure = λ _ p → p }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 275
diff changeset
258 (λ x p → {!!} ) (λ x p → {!!} )
275
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
259
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
260
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 274
diff changeset
261