Total real-time persistent queues

前端 未结 2 1003
夕颜
夕颜 2020-12-16 14:12

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         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 14:56

    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?

提交回复
热议问题