实时持久队列总数 [英] Total real-time persistent queues
问题描述
Okasaki 描述了可以在 Haskell 中使用类型实现的持久实时队列
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]
}
增量旋转保持不变
length schedule = length front - length rear
更多详情
如果您熟悉所涉及的队列,则可以跳过此部分.
More details
If you're familiar with the queues involved, you can skip this section.
旋转函数的样子
rotate :: [a] -> [a] -> [a] -> [a]
rotate [] (y : _) a = y : a
rotate (x : xs) (y : ys) a =
x : rotate xs ys (y : a)
它被智能构造函数调用
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 []
在每个队列操作之后.当 length s = length f - length r + 1
时总是调用智能构造函数,确保 rotate
中的模式匹配成功.
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.
我讨厌偏函数!我很想找到一种方法来表达类型中的结构不变量.通常的依赖向量似乎是一个可能的选择:
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
然后(也许)
data Queue a = forall x rl sl . Queue
{ front :: Vec (sl :+ rl) a
, rear :: Vec rl a
, schedule :: Vec sl x
}
问题是我无法弄清楚如何处理这些类型.似乎极有可能需要 一些 数量的 unsafeCoerce
来提高效率.但是,我还没有想出一种甚至可以勉强管理的方法.是否有可能在 Haskell 中很好地做到这一点?
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?
推荐答案
这是我得到的:
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
是根据 grotate
定义的,原因与 reverse
是根据 foldl
(或 enumerate
genumerate
):因为 Vec A (suc n + m)
不是定义上的 Vec A (n + suc m)
,而(B ∘ suc) m
定义为 B (suc m)
.
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
与您提供的实现相同(以那些 subst
为模),但我不确定类型: r 是否可以
必须不为空?
exec
has the same implementation as you provided (modulo those subst
s), but I'm not sure about the types: is it OK that r
must be non-empty?
这篇关于实时持久队列总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!