Search code examples
time-complexityagdadependent-typeidris

How to enumerate the elements of a list by `Fin`s in linear time?


We can enumerate the elements of a list like this:

-- enumerate-ℕ = zip [0..]
enumerate-ℕ : ∀ {α} {A : Set α} -> List A -> List (ℕ × A)
enumerate-ℕ = go 0 where
  go : ∀ {α} {A : Set α} -> ℕ -> List A -> List (ℕ × A)
  go n  []      = []
  go n (x ∷ xs) = (n , x) ∷ go (ℕ.suc n) xs

E.g. enumerate-ℕ (1 ∷ 3 ∷ 2 ∷ 5 ∷ []) is equal to (0 , 1) ∷ (1 , 3) ∷ (2 , 2) ∷ (3 , 5) ∷ []. Assuming there is sharing in Agda, the function is linear.

However if we try to enumerate the elements of a list by Fins rather than s, the function becomes quadratic:

enumerate-Fin : ∀ {α} {A : Set α} -> (xs : List A) -> List (Fin (length xs) × A)
enumerate-Fin  []      = []
enumerate-Fin (x ∷ xs) = (zero , x) ∷ map (pmap suc id) (enumerate-Fin xs)

Is it possible to enumerate by Fins in linear time?


Solution

  • Consider this as a first attempt:

    go : ∀ {m α} {A : Set α} -> Fin m -> (xs : List A) -> List (Fin (m + length xs) × A)
    go i  []      = []
    go i (x ∷ xs) = (inject+ _ i , x) ∷ {!go (suc i) xs!}
    

    i grows at each recursive call as it should, but there is a mismatch:

    the type of the goal is List (Fin (.m + suc (length xs)) × .A)

    the type of the expression insise the hole is List (Fin (suc (.m + length xs)) × .A)

    It's easy to prove that two types are equal, but it's also dirty. This is a common problem: one argument grows and the other lowers, so we need definitionally commutative _+_ to handle both the cases, but there is no way to define it. The solution is to use CPS:

    go : ∀ {α} {A : Set α} -> (k : ℕ -> ℕ) -> (xs : List A) -> List (Fin (k (length xs)) × A)
    go k  []      = []
    go k (x ∷ xs) = ({!!} , x) ∷ go (k ∘ suc) xs
    

    (k ∘ suc) (length xs) is the same thing as k (length (x ∷ xs)), hence the mismatch is fixed, but what is i now? The type of the hole is Fin (k (suc (length xs))) and it's uninhabited in the current context, so let's introduce some inhabitant:

    go : ∀ {α} {A : Set α}
       -> (k : ℕ -> ℕ)
       -> (∀ {n} -> Fin (k (suc n)))
       -> (xs : List A)
       -> List (Fin (k (length xs)) × A)
    go k i  []      = []
    go k i (x ∷ xs) = (i , x) ∷ go (k ∘ suc) {!!} xs
    

    The type of the new hole is {n : ℕ} → Fin (k (suc (suc n))). We can fill it with i, but i must grow at each recursive call. However suc and k doesn't commute, so suc i is Fin (suc (k (suc (_n_65 k i x xs)))). So we add another argument that sucs under k, and the final definition is

    enumerate-Fin : ∀ {α} {A : Set α} -> (xs : List A) -> List (Fin (length xs) × A)
    enumerate-Fin = go id suc zero where
      go : ∀ {α} {A : Set α}
         -> (k : ℕ -> ℕ)
         -> (∀ {n} -> Fin (k n) -> Fin (k (suc n)))
         -> (∀ {n} -> Fin (k (suc n)))
         -> (xs : List A)
         -> List (Fin (k (length xs)) × A)
      go k s i  []      = []
      go k s i (x ∷ xs) = (i , x) ∷ go (k ∘ suc) s (s i) xs
    

    which works, because s : {n : ℕ} → Fin (k n) → Fin (k (suc n)) can be treated as {n : ℕ} → Fin (k (suc n)) → Fin (k (suc (suc n))).

    A test: C-c C-n enumerate-Fin (1 ∷ 3 ∷ 2 ∷ 5 ∷ []) gives

    (zero , 1) ∷
    (suc zero , 3) ∷
    (suc (suc zero) , 2) ∷ (suc (suc (suc zero)) , 5) ∷ []
    

    Now note that in enumerate-Fin k always follows Fin in the types. Hence we can abstract Fin ∘ k and get a generic version of the function that works with both and Fin:

    genumerate : ∀ {α β} {A : Set α}
               -> (B : ℕ -> Set β)
               -> (∀ {n} -> B n -> B (suc n))
               -> (∀ {n} -> B (suc n))
               -> (xs : List A)
               -> List (B (length xs) × A)
    genumerate B s i  []      = []
    genumerate B s i (x ∷ xs) = (i , x) ∷ genumerate (B ∘ suc) s (s i) xs
    
    enumerate-ℕ : ∀ {α} {A : Set α} -> List A -> List (ℕ × A)
    enumerate-ℕ = genumerate _ suc 0
    
    enumerate-Fin : ∀ {α} {A : Set α} -> (xs : List A) -> List (Fin (length xs) × A)
    enumerate-Fin = genumerate Fin suc zero