ParenDepTac.agda 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. -- Author: Makoto Takeyama
  2. module ParenDepTac where
  3. ----------------------------------------------------------------------
  4. -- Preliminary
  5. ----------------------------------------------------------------------
  6. infix 3 _≡_
  7. data _≡_ {A : Set}(x : A) : A -> Set where
  8. refl : x ≡ x
  9. subst : {A : Set}(C : A -> Set){x y : A} -> x ≡ y -> C y -> C x
  10. subst C refl c = c
  11. sym : {A : Set}{x y : A} -> x ≡ y -> y ≡ x
  12. sym refl = refl
  13. cong : {A B : Set}(f : A -> B){x y : A} -> x ≡ y -> f x ≡ f y
  14. cong f refl = refl
  15. infixl 2 _`tran`_
  16. _`tran`_ : {A : Set}{x y z : A} -> x ≡ y -> y ≡ z -> x ≡ z
  17. refl `tran` refl = refl
  18. data FALSE : Set where
  19. data TRUE : Set where tt : TRUE
  20. data Nat : Set where
  21. zer : Nat
  22. suc : Nat -> Nat
  23. _+_ : Nat -> Nat -> Nat
  24. zer + n = n
  25. suc m + n = suc ( m + n )
  26. ----------------------------------------------------------------------
  27. -- Strings of parentheses
  28. ----------------------------------------------------------------------
  29. infix 5 ≪_ ≫_
  30. data Parens : Set where
  31. ε : Parens
  32. ≪_ : Parens -> Parens
  33. ≫_ : Parens -> Parens
  34. infixr 5 _·_
  35. _·_ : Parens -> Parens -> Parens
  36. ε · ys = ys
  37. (≪ xs) · ys = ≪ (xs · ys)
  38. (≫ xs) · ys = ≫ (xs · ys)
  39. ·ass : (xs : Parens){ys zs : Parens} -> xs · (ys · zs) ≡ (xs · ys) · zs
  40. ·ass ε = refl
  41. ·ass (≪ xs) = cong ≪_ (·ass xs)
  42. ·ass (≫ xs) = cong ≫_ (·ass xs)
  43. ·unitR : {xs : Parens} -> xs · ε ≡ xs
  44. ·unitR {ε} = refl
  45. ·unitR {≪ xs} = cong ≪_ ·unitR
  46. ·unitR {≫ xs} = cong ≫_ ·unitR
  47. infix 6 _≫' _≪'
  48. _≫' : Parens -> Parens
  49. xs ≫' = xs · ≫ ε
  50. _≪' : Parens -> Parens
  51. xs ≪' = xs · ≪ ε
  52. ----------------------------------------------------------------------
  53. -- A poorman's tactics for equational reasoning
  54. ----------------------------------------------------------------------
  55. infixr 5 _⊙_
  56. data Exp : Set where
  57. Var : Nat -> Exp
  58. Lit : Parens -> Exp
  59. _⊙_ : Exp -> Exp -> Exp
  60. nf0 : Exp -> Exp -> Exp
  61. nf0 (Var x) e0 = Var x ⊙ e0
  62. nf0 (Lit a) e0 = Lit a ⊙ e0
  63. nf0 (e1 ⊙ e2) e0 = nf0 e1 (nf0 e2 e0)
  64. nf : Exp -> Exp
  65. nf e = nf0 e (Lit ε)
  66. Env = Nat -> Parens
  67. module withEnv(ρ : Env) where
  68. ⟦_⟧ : Exp -> Parens
  69. ⟦ Var x ⟧ = ρ x
  70. ⟦ Lit a ⟧ = a
  71. ⟦ e1 ⊙ e2 ⟧ = ⟦ e1 ⟧ · ⟦ e2 ⟧
  72. nfSound0 : (e e0 : Exp) -> ⟦ e ⊙ e0 ⟧ ≡ ⟦ nf0 e e0 ⟧
  73. nfSound0 (Var x) e0 = refl
  74. nfSound0 (Lit a) e0 = refl
  75. nfSound0 (e1 ⊙ e2) e0 = sym (·ass ⟦ e1 ⟧) `tran`
  76. cong (_·_ ⟦ e1 ⟧) (nfSound0 e2 e0) `tran`
  77. nfSound0 e1 (nf0 e2 e0)
  78. nfSound : (e : Exp) -> ⟦ e ⟧ ≡ ⟦ nf e ⟧
  79. nfSound e = sym ·unitR `tran` nfSound0 e (Lit ε)
  80. tac : (e1 e2 : Exp) -> nf e1 ≡ nf e2 -> ⟦ e1 ⟧ ≡ ⟦ e2 ⟧
  81. tac e1 e2 p = nfSound e1 `tran` subst (\x -> ⟦ x ⟧ ≡ ⟦ nf e2 ⟧) p refl `tran`
  82. sym (nfSound e2)
  83. module tac4 (a0 a1 a2 a3 : Parens) where
  84. ρ : Env
  85. ρ zer = a0
  86. ρ (suc zer) = a1
  87. ρ (suc (suc zer)) = a2
  88. ρ (suc (suc (suc zer))) = a3
  89. ρ (suc (suc (suc (suc _ )))) = ε
  90. open module tac4' = withEnv ρ public using (tac)
  91. v0 = Var zer
  92. v1 = Var (suc zer)
  93. v2 = Var (suc (suc zer))
  94. v3 = Var (suc (suc (suc zer)))
  95. [≪] = Lit (≪ ε)
  96. [≫] = Lit (≫ ε)
  97. ----------------------------------------------------------------------
  98. -- Derivations of S and T grammars
  99. -- indexed by their underlying strings
  100. ----------------------------------------------------------------------
  101. infix 3 _∈S _∈T
  102. infix 4 <_> _⟨_⟩
  103. infixl 4 _•_
  104. data _∈S : Parens -> Set where
  105. εS : ε ∈S
  106. <_> : {xs : Parens} -> xs ∈S -> ≪ xs ≫' ∈S
  107. _•_ : {xs ys : Parens} -> xs ∈S -> ys ∈S -> xs · ys ∈S
  108. data _∈T : Parens -> Set where
  109. εT : ε ∈T
  110. _⟨_⟩ : {xs ys : Parens} -> xs ∈T -> ys ∈T -> xs · ≪ ys ≫' ∈T
  111. ----------------------------------------------------------------------
  112. -- Equivalence of S and T grammars
  113. ----------------------------------------------------------------------
  114. infixl 3 _○_
  115. _○_ : {xs ys : Parens} -> xs ∈T -> ys ∈T -> xs · ys ∈T
  116. t ○ εT = subst _∈T ·unitR t
  117. _○_ {xs} t (t1 ⟨ t2 ⟩) = subst _∈T (·ass xs) ((t ○ t1) ⟨ t2 ⟩)
  118. S⊂T : {xs : Parens} -> xs ∈S -> xs ∈T
  119. S⊂T εS = εT
  120. S⊂T (< s >) = εT ⟨ S⊂T s ⟩
  121. S⊂T (s1 • s2) = S⊂T s1 ○ S⊂T s2
  122. T⊂S : {xs : Parens} -> xs ∈T -> xs ∈S
  123. T⊂S εT = εS
  124. T⊂S (t1 ⟨ t2 ⟩) = T⊂S t1 • < T⊂S t2 >
  125. ----------------------------------------------------------------------
  126. -- Recursively defined test function
  127. ----------------------------------------------------------------------
  128. Test : Nat -> Parens -> Set
  129. Test n (≪ xs) = Test (suc n) xs
  130. Test (suc n) (≫ xs) = Test n xs
  131. Test zer (≫ xs) = FALSE
  132. Test (suc n) ε = FALSE
  133. Test zer ε = TRUE
  134. ----------------------------------------------------------------------
  135. -- Soundness of Test
  136. ----------------------------------------------------------------------
  137. lemTest : (m : Nat)(xs : Parens) -> Test m xs ->
  138. (n : Nat)(ys : Parens) -> Test n ys ->
  139. Test (m + n) (xs · ys)
  140. lemTest m (≪ xs) p = lemTest (suc m) xs p
  141. lemTest (suc m) (≫ xs) p = lemTest m xs p
  142. lemTest zer (≫ xs) ()
  143. lemTest (suc m) ε ()
  144. lemTest zer ε tt = \ n ys q -> q
  145. sound : {xs : Parens} -> xs ∈S -> Test zer xs
  146. sound εS = tt
  147. sound (<_>{xs} s) = lemTest zer xs (sound s) (suc zer) (≫ ε) tt
  148. sound (_•_{xs}{ys} s1 s2) = lemTest zer xs (sound s1) zer ys (sound s2)
  149. ----------------------------------------------------------------------
  150. -- Completeness of Test
  151. ----------------------------------------------------------------------
  152. complete : (xs : Parens) -> Test zer xs -> xs ∈S
  153. complete xs0 p0 = parse init εS xs0 p0
  154. where
  155. data St : Nat -> Parens -> Set where
  156. init : St zer ε
  157. _*_≪ : {n : Nat} ->
  158. {xs : Parens} -> St n xs ->
  159. {ys : Parens} -> ys ∈S ->
  160. St (suc n) (xs · ys ≪')
  161. stPar : forall {n xs} -> St n xs -> Parens
  162. stPar {xs = xs} _ = xs
  163. ∈SPar : forall {xs} -> xs ∈S -> Parens
  164. ∈SPar {xs} _ = xs
  165. parse : {n : Nat} ->
  166. {xs : Parens} -> St n xs ->
  167. {ys : Parens} -> ys ∈S ->
  168. (zs : Parens) -> Test n zs ->
  169. xs · ys · zs ∈S
  170. -- <SHIFT> (st , s , ≪ zs ) ↦ (st * s ≪ , εS , zs)
  171. -- <REDUCE> (st * s3 ≪ , s , ≫ zs ) ↦ (st , s3 • < s > , zs)
  172. -- <FINISH> (init , s , ε ) ↦ s
  173. parse {_} {xs} st {ys} s (≪ zs) p = subst _∈S eq (parse (st * s ≪) εS zs p)
  174. where open module foo = tac4 xs ys zs ε
  175. eq = tac (v0 ⊙ v1 ⊙ [≪] ⊙ v2) ((v0 ⊙ v1 ⊙ [≪]) ⊙ v2) refl
  176. parse (st * s3 ≪) {ys} s (≫ zs) p
  177. = subst _∈S eq (parse st (s3 • < s >) zs p)
  178. where open module foo = tac4 (stPar st) (∈SPar s3) ys zs
  179. eq = tac ((v0 ⊙ v1 ⊙ [≪]) ⊙ v2 ⊙ [≫] ⊙ v3)
  180. ( v0 ⊙ (v1 ⊙ [≪] ⊙ v2 ⊙ [≫]) ⊙ v3) refl
  181. parse ( _ * _ ≪) _ ε ()
  182. parse init _ (≫ zs) ()
  183. parse init s ε tt = subst _∈S ·unitR s