9 | module Data.Telescope.Segment
11 | import Data.Telescope.Telescope
13 | import Syntax.PreorderReasoning
23 | data Segment : (n : Nat) -> Left.Telescope k -> Type where
24 | Nil : Segment 0 gamma
25 | (::) : (ty : TypeIn gamma) -> (delta : Segment n (gamma -. ty)) -> Segment (S n) gamma
30 | tabulate : (n : Nat) -> (Left.Environment gamma -> Left.Telescope n) -> Segment n gamma
32 | tabulate (S n) tel = (sigma :: tabulate n (uncurry delta)) where
34 | sigma : TypeIn gamma
35 | sigma env = fst (uncons (tel env))
37 | delta : (env : Environment gamma) -> sigma env -> Left.Telescope n
38 | delta env v with (uncons (tel env))
39 | delta env v | (
sig ** delt ** _)
= delt v
44 | fromTelescope : {k : Nat} -> Left.Telescope k -> Segment k []
45 | fromTelescope gamma = tabulate _ (const gamma)
50 | untabulate : {n : Nat} -> Segment n gamma -> (Left.Environment gamma -> Left.Telescope n)
51 | untabulate [] _ = []
52 | untabulate (ty :: delta) env = cons (ty env) (untabulate delta . (\ v => (
env ** v)
))
56 | toTelescope : {k : Nat} -> Segment k [] -> Left.Telescope k
57 | toTelescope seg = untabulate seg ()
59 | %name Segment
delta,delta',delta1,delta2
61 | export infixl 3 |++
, :++
66 | succLemma : (lft, rgt : Nat) -> lft + (S rgt) = S (lft + rgt)
67 | succLemma x y = Calc $
69 | ~~ (x + 1)+ y ...(plusAssociative x 1 y)
70 | ~~ (1 + x)+ y ...(cong (+y) $
plusCommutative x 1)
71 | ~~ 1 + (x + y) ...(sym $
plusAssociative 1 x y)
75 | keep : (0 prf : a ~=~ b) -> a ~=~ b
87 | (|++) : (gamma : Left.Telescope k) -> {n : Nat} -> (delta : Segment n gamma) -> Left.Telescope (n + k)
88 | (|++) gamma {n = 0} delta = gamma
89 | (|++) gamma {n=S n} (ty :: delta) = rewrite sym $
succLemma n k in
90 | gamma -. ty |++ delta
94 | (++) : {gamma : Left.Telescope k}
96 | -> (lft : Segment n gamma )
97 | -> (rgt : Segment m (gamma |++ lft))
98 | -> Segment (n + m) gamma
99 | (++) {n = 0 } delta rgt = rgt
100 | (++) {n = S n} (ty :: lft) rgt = ty :: lft ++ rewrite succLemma n k in
104 | actSegmentAssociative : (gamma : Left.Telescope k)
105 | -> (lft : Segment n gamma)
106 | -> (rgt : Segment m (gamma |++ lft))
107 | -> (gamma |++ (lft ++ rgt)) ~=~ ((gamma |++ lft) |++ rgt)
108 | actSegmentAssociative gamma {n = 0} [] rgt = Refl
109 | actSegmentAssociative gamma {n = S n} (ty :: lft) rgt =
110 | let rgt' : Segment {k = n + (S k)} m (gamma -. ty |++ lft)
111 | rgt' = rewrite succLemma n k in
113 | rgt_eq_rgt' : rgt ~=~ rgt'
114 | rgt_eq_rgt' = rewrite succLemma n k in
117 | rewrite sym $
succLemma (n + m) k in
118 | rewrite sym $
succLemma n k in keep $
Calc $
119 | |~ ((gamma -. ty) |++ (lft ++ rgt'))
120 | ~~ ((gamma -. ty |++ lft) |++ rgt')
121 | ...(actSegmentAssociative (gamma -. ty) lft rgt')
124 | weaken : {0 gamma : Left.Telescope k}
125 | -> {delta : Segment n gamma} -> (sy : TypeIn gamma)
126 | -> TypeIn (gamma |++ delta)
127 | weaken {delta = [] } sy = sy
128 | weaken {n = S n} {delta = ty :: delta} sy = rewrite sym $
succLemma n k in
129 | weaken (weakenTypeIn sy)
132 | projection : {0 gamma : Left.Telescope k}
134 | -> {0 delta : Segment n gamma}
135 | -> Environment (gamma |++ delta)
136 | -> Environment gamma
137 | projection {n = 0 } {delta = [] } env = env
138 | projection {n = S n} {delta = ty :: delta} env
139 | = let (
env' ** _)
= projection {n} {delta}
140 | $
rewrite succLemma n k in env
146 | data Environment : (env : Left.Environment gamma)
147 | -> (delta : Segment n gamma) -> Type where
148 | Empty : Environment env []
149 | (.=) : {0 gamma : Left.Telescope k} -> {0 ty : TypeIn gamma}
150 | -> {0 env : Left.Environment gamma}
151 | -> {0 delta : Segment n (gamma -. ty)}
153 | -> (x : ty env) -> (xs : Segment.Environment (
env ** x)
delta)
154 | -> Environment env (ty :: delta)
157 | (:++) : {0 gamma : Left.Telescope k} -> {0 delta : Segment n gamma}
158 | -> (env : Left.Environment gamma)
159 | -> (ext : Segment.Environment env delta)
160 | -> Left.Environment (gamma |++ delta)
161 | (:++) env Empty = env
162 | (:++) {n = S n} env (x .= xs) = rewrite sym $
succLemma n k in