Search code examples
haskellqueuedependent-typegadt

Total real-time persistent queues


Okasaki describes persistent real-time queues which can be realized in Haskell using the type

data Queue a = forall x . Queue
  { front :: [a]
  , rear :: [a]
  , schedule :: [x]
  }

where incremental rotations maintain the invariant

length schedule = length front - length rear

More details

If you're familiar with the queues involved, you can skip this section.

The rotation function looks like

rotate :: [a] -> [a] -> [a] -> [a]
rotate [] (y : _) a = y : a
rotate (x : xs) (y : ys) a =
  x : rotate xs ys (y : a)

and it's called by a smart constructor

exec :: [a] -> [a] -> [x] -> Queue a
exec f r (_ : s) = Queue f r s
exec f r [] = Queue f' [] f' where
  f' = rotate f r []

after each queue operation. The smart constructor is always called when length s = length f - length r + 1, ensuring that the pattern match in rotate will succeed.

The problem

I hate partial functions! I'd love to find a way to express the structural invariant in the types. The usual dependent vector seems a likely choice:

data Nat = Z | S Nat

data Vec n a where
  Nil :: Vec 'Z a
  Cons :: a -> Vec n a -> Vec ('S n) a

and then (perhaps)

data Queue a = forall x rl sl . Queue
  { front :: Vec (sl :+ rl) a
  , rear :: Vec rl a
  , schedule :: Vec sl x
  }

The trouble is that I haven't been able to figure out how to juggle the types. It seems extremely likely that some amount of unsafeCoerce will be needed to make this efficient. However, I haven't been able to come up with an approach that's even vaguely manageable. Is it possible to do this nicely in Haskell?


Solution

  • Here is what I got:

    open import Function
    open import Data.Nat.Base
    open import Data.Vec
    
    grotate : ∀ {n m} {A : Set}
            -> (B : ℕ -> Set)
            -> (∀ {n} -> A -> B n -> B (suc n))
            -> Vec A n
            -> Vec A (suc n + m)
            -> B m
            -> B (suc n + m)
    grotate B cons  []      (y ∷ ys) a = cons y a
    grotate B cons (x ∷ xs) (y ∷ ys) a = grotate (B ∘ suc) cons xs ys (cons y a)
    
    rotate : ∀ {n m} {A : Set} -> Vec A n -> Vec A (suc n + m) -> Vec A m -> Vec A (suc n + m)
    rotate = grotate (Vec _) _∷_
    
    record Queue (A : Set) : Set₁ where
      constructor queue
      field
        {X}      : Set
        {n m}    : ℕ
        front    : Vec A (n + m)
        rear     : Vec A m
        schedule : Vec X n
    
    open import Relation.Binary.PropositionalEquality
    open import Data.Nat.Properties.Simple
    
    exec : ∀ {m n A} -> Vec A (n + m) -> Vec A (suc m) -> Vec A n -> Queue A
    exec {m} {suc n} f r (_ ∷ s) = queue (subst (Vec _) (sym (+-suc n m)) f) r s
    exec {m}         f r  []     = queue (with-zero f') [] f' where
     with-zero    = subst (Vec _ ∘ suc) (sym (+-right-identity m))
     without-zero = subst (Vec _ ∘ suc) (+-right-identity m)
    
     f' = without-zero (rotate f (with-zero r) [])
    

    rotate is defined in terms of grotate for the same reason reverse is defined in terms of foldl (or enumerate in terms of genumerate): because Vec A (suc n + m) is not definitionally Vec A (n + suc m), while (B ∘ suc) m is definitionally B (suc m).

    exec has the same implementation as you provided (modulo those substs), but I'm not sure about the types: is it OK that r must be non-empty?