Advertisement
JoelSjogren

Snake2.agda

Dec 19th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. -- See also closed meanders, https://en.wikipedia.org/wiki/Meander_(mathematics).
  2.  
  3. {-# OPTIONS --cubical --type-in-type #-}
  4. module Snake2 where
  5.  
  6. open import Cubical.Core.Everything
  7. open import Cubical.Foundations.Prelude
  8. open import Cubical.Data.Sigma
  9.  
  10. data List (A : Type) : Type where
  11. nil : List A
  12. cons : A → List A → List A
  13.  
  14. snoc : {A : Type} → List A → A → List A
  15. snoc nil a = cons a nil
  16. snoc (cons a' as) a = cons a' (snoc as a)
  17.  
  18. data Index {A : Type} : List A → Type where
  19. here : {a : A} {as : List A} → Index (cons a as)
  20. there : {a : A} {as : List A} → Index as → Index (cons a as)
  21.  
  22. split-at-index : {A : Type} {as : List A} → Index as → List A × A × List A
  23. split-at-index {A} {cons a as} here = nil , a , as
  24. split-at-index {A} {cons a as} (there i) =
  25. let as₀ , a' , as₁ = split-at-index i in cons a as₀ , a' , as₁
  26.  
  27. _[_] : {A : Type} → (as : List A) → Index as → A
  28. cons a as [ here ] = a
  29. cons a as [ there i ] = as [ i ]
  30.  
  31. record Tree (A : Type) : Type where
  32. inductive
  33. constructor node
  34. field
  35. label : A
  36. children : List (Tree A)
  37. open Tree
  38.  
  39. data Node {A : Type} : Tree A → Type where
  40. root : {t : Tree A} → Node t
  41. child : {a : A} {ts : List (Tree A)} (i : Index ts) → Node (ts [ i ]) → Node (node a ts)
  42.  
  43. _++_ : {A : Type} → List A → List A → List A
  44. nil ++ as = as
  45. cons a as ++ as' = cons a (as ++ as')
  46.  
  47. data Concat {A : Type} : (as₀ as₁ as : List A) → Type where
  48. left-of : {as : List A} → Concat nil as as
  49. right-of : {a : A} {as₀ as₁ as : List A} → Concat as₀ as₁ as →
  50. Concat (cons a as₀) as₁ (cons a as)
  51.  
  52. record Cut' {A : Type} (as : List A) : Type where
  53. constructor cut'
  54. field
  55. as₀ : List A
  56. as₁ : List A
  57. iden : Concat as₀ as₁ as
  58.  
  59. nil-cut : {A : Type} {as : List A} → Cut' as
  60. nil-cut {A} {as} = cut' nil as left-of
  61.  
  62. cons-cut : {A : Type} (a : A) {as : List A} (c : Cut' as) → Cut' (cons a as)
  63. cons-cut a (cut' as₀ as₁ iden) = cut' (cons a as₀) as₁ (right-of iden)
  64.  
  65. append-cut : {A : Type} (as₀ : List A) {as₁ : List A} (c : Cut' as₁) → Cut' (as₀ ++ as₁)
  66. append-cut nil c = c
  67. append-cut (cons a as₀) c = cons-cut a (append-cut as₀ c)
  68.  
  69. end-cut : {A : Type} {as : List A} → Cut' as
  70. end-cut {A} {nil} = nil-cut
  71. end-cut {A} {cons a as} = cons-cut a end-cut
  72.  
  73. _%_ : {A : Type} (t : Tree A) → Node t → Tree A
  74. t % root = t
  75. node a ts % child i n = (ts [ i ]) % n
  76.  
  77. Wedge : {A : Type} (t : Tree A) → Node t → Type
  78. Wedge t n = Cut' (children (t % n))
  79.  
  80. record Tree-Node-Wedge (A : Type) : Type where
  81. constructor ⟨_,_,_⟩
  82. field
  83. t : Tree A
  84. n : Node t
  85. w : Wedge t n
  86.  
  87. INIT : {A : Type} (a₀ a₁ : A) → Tree-Node-Wedge A -- (a₀) root
  88. INIT a₀ a₁ = -- ↓
  89. record { -- (a₁)< wedge
  90. t = (node a₀ (cons (node a₁ nil) nil));
  91. n = child here root;
  92. w = cut' nil nil left-of
  93. }
  94.  
  95. data Neighbour {A : Type} : (t : Tree A) → Node t → Type where
  96. parent : {t : Tree A} (i : Index (children t))
  97. → Neighbour t (child i root)
  98. child : {t : Tree A} (i : Index (children t))
  99. → Neighbour t root
  100. lift : {t : Tree A} (i : Index (children t)) (n : Node (children t [ i ]))
  101. → Neighbour (children t [ i ]) n
  102. → Neighbour t (child i n)
  103.  
  104. Neighbour' : {A : Type} → Tree-Node-Wedge A → Type
  105. Neighbour' tnw = let ⟨ t , n , w ⟩ = tnw in Neighbour t n
  106.  
  107. SPLIT-downwards' : {A : Type}
  108. (a : A)
  109. (ts : List (Tree A))
  110. (i₁ : Index ts)
  111. (c₁ : Cut' ts)
  112. → Σ[ ts' ∈ List (Tree A) ] Σ[ i' ∈ Index ts' ] Cut' (children (ts' [ i' ]))
  113. SPLIT-downwards' a _ i₁ (cut' nil ts left-of) with split-at-index i₁
  114. ... | ts₀ , node a* ts* , ts₁ =
  115. cons t' ts₁ , here , nil-cut
  116. where
  117. t' = node a* (cons (node a ts₀) ts₁)
  118. SPLIT-downwards' a _ here (cut' (cons (node a* ts*) ts₀) ts₁ (right-of iden)) =
  119. cons t' ts₁ , here , end-cut
  120. where
  121. t' = node a* (snoc ts* (node a ts₀))
  122. SPLIT-downwards' a (cons t ts) (there i) (cut' _ _ (right-of iden)) =
  123. let ts' , i' , c' = SPLIT-downwards' a ts i (cut' _ _ iden)
  124. in cons t ts' , there i' , c'
  125.  
  126. SPLIT-upwards' : {A : Type}
  127. (ts : List (Tree A))
  128. (i₀ : Index ts)
  129. (c₁ : Cut' (children (ts [ i₀ ])))
  130. → Σ[ ts' ∈ List (Tree A) ] Cut' ts'
  131. SPLIT-upwards' (cons (node a₁ a₁ts) a₀ts) here (cut' tsL tsR iden) =
  132. cons tL (cons tR a₀ts) , cut' _ _ (right-of left-of)
  133. where
  134. tL = node a₁ tsL
  135. tR = node a₁ tsR
  136. SPLIT-upwards' (cons a₀t a₀ts) (there i₀) c₁ =
  137. let ts , c = SPLIT-upwards' a₀ts i₀ c₁
  138. in cons a₀t ts , cons-cut a₀t c
  139.  
  140. modify-tnw : {A : Type} (ts : List (Tree A)) (i : Index ts)
  141. → Tree-Node-Wedge A → Σ[ ts ∈ List (Tree A) ] Σ[ i ∈ Index ts ]
  142. let t = ts [ i ] in Σ[ n ∈ Node t ] Wedge t n
  143. modify-tnw (cons t ts) here ⟨ t₁ , n , w ⟩ = cons t₁ ts , here , n , w
  144. modify-tnw (cons t ts) (there i) tnw =
  145. let ts^ , i^ , n^ , w^ = modify-tnw ts i tnw in cons t ts^ , there i^ , n^ , w^
  146.  
  147. SPLIT : {A : Type} (tnw : Tree-Node-Wedge A) → (o : Neighbour' tnw) → Tree-Node-Wedge A
  148. SPLIT ⟨ node a ts , .(child i root) , w ⟩ (parent i) =
  149. let ts' , c' = SPLIT-upwards' ts i w
  150. in ⟨ node a ts' , root , c' ⟩
  151. SPLIT ⟨ node a ts , .root , w ⟩ (child i) =
  152. let ts' , i' , c' = SPLIT-downwards' a ts i w
  153. in ⟨ node a ts' , child i' root , c' ⟩
  154. SPLIT ⟨ node a ts , .(child i n) , w ⟩ (lift i n o) =
  155. let ⟨ t' , n' , w' ⟩ = SPLIT ⟨ ts [ i ] , n , w ⟩ o
  156. ts^ , i^ , n^ , w^ = modify-tnw ts i ⟨ t' , n' , w' ⟩
  157. in ⟨ node a ts^ , child i^ n^ , w^ ⟩
  158.  
  159. data Snake {A : Type} : (t : Tree A) (n : Node t) (w : Wedge t n) → Type where
  160. init : {a₀ a₁ : A} →
  161. let open Tree-Node-Wedge (INIT a₀ a₁) in Snake t n w
  162. split : (t' : Tree A) (n' : Node t') (w' : Wedge t' n') (o : Neighbour t' n') →
  163. let open Tree-Node-Wedge (SPLIT ⟨ t' , n' , w' ⟩ o) in Snake t n w
  164.  
  165. data Snake₀ {A : Type} : Type where
  166. snake : (t : Tree A) (w : Wedge t root) → Snake₀
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement