{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
module Agda.Utils.IndexedList where
import Data.Kind ( Type )
import Agda.Utils.Lens
data Some :: (k -> Type) -> Type where
Some :: f i -> Some f
withSome :: Some b -> (forall i. b i -> a) -> a
withSome (Some x) f = f x
data All :: (x -> Type) -> [x] -> Type where
Nil :: All p '[]
Cons :: p x -> All p xs -> All p (x ': xs)
makeAll :: (a -> Some b) -> [a] -> Some (All b)
makeAll f [] = Some Nil
makeAll f (x : xs) =
case (f x, makeAll f xs) of
(Some y, Some ys) -> Some (Cons y ys)
forgetAll :: (forall x. b x -> a) -> All b xs -> [a]
forgetAll f Nil = []
forgetAll f (Cons x xs) = f x : forgetAll f xs
data Index :: [x] -> x -> Type where
Zero :: Index (x ': xs) x
Suc :: Index xs x -> Index (y ': xs) x
forgetIndex :: Index xs x -> Int
forgetIndex Zero = 0
forgetIndex (Suc i) = 1 + forgetIndex i
mapWithIndex :: (forall x. Index xs x -> p x -> q x) -> All p xs -> All q xs
mapWithIndex f Nil = Nil
mapWithIndex f (Cons p ps) = Cons (f Zero p) $ mapWithIndex (f . Suc) ps
lIndex :: Index xs x -> Lens' (p x) (All p xs)
lIndex Zero f (Cons x xs) = f x <&> \ x -> Cons x xs
lIndex (Suc i) f (Cons x xs) = lIndex i f xs <&> \ xs -> Cons x xs
lookupIndex :: All p xs -> Index xs x -> p x
lookupIndex = flip ix
where
ix :: Index xs x -> All p xs -> p x
ix Zero (Cons x xs) = x
ix (Suc i) (Cons x xs) = ix i xs
allIndices :: All p xs -> All (Index xs) xs
allIndices = mapWithIndex const