我是Irdis的新手。是否可以撤销HVect
?如果我在HVect [String, Int]
上拨打反向,则应返回HVect [Int, String]
,如果我在HVect [String, Int, Day]
上拨打反向,则应返回HVect [Day, Int, String]
。
我尝试重复使用Data.Vect.reverse
(Gist)
module reverse_hvect
import Data.HVect
reverse : HVect ts -> HVect (reverse ts)
reverse [] = []
reverse (x::xs) = (reverse xs) ++ [x]
但我得到
Type checking .\reverse_hvect.idr
reverse_hvect.idr:7:9:When checking right hand side of reverse_hvect.reverse with expected type
HVect (reverse (t :: ts))
Type mismatch between
HVect (Data.Vect.reverse, go Type k ts [] ts ++ [t]) (Type of reverse xs ++ [x])
and
HVect (Data.Vect.reverse, go Type (S k) (t :: ts) [t] ts) (Expected type)
Specifically:
Type mismatch between
Data.Vect.reverse, go Type k ts [] ts ++ [t]
and
Data.Vect.reverse, go Type (S k) (t :: ts) [t] ts
Holes: reverse_hvect.reverse
由于Data.Vect.reverse
使用带有累加器的内部函数go
,我编写了自己的Vect.reverse
,其结构与我的HVect.reverse
(Gist)< / p>
module reverse_hvect
import Data.Vect
import Data.HVect
myReverse : Vect n a -> Vect n a
myReverse [] = []
myReverse {n = S k} (x::xs) = rewrite plusCommutative 1 k in (myReverse xs) ++ [x]
reverse : HVect ts -> HVect (myReverse ts)
reverse [] = []
reverse (x::xs) = (reverse xs) ++ [x]
但我收到了另一个错误
Type checking .\reverse_hvect.idr
reverse_hvect.idr:12:9:When checking right hand side of reverse_hvect.reverse with expected type
HVect (myReverse (t :: ts))
Type mismatch between
HVect (myReverse ts ++ [t]) (Type of reverse xs ++ [x])
and
HVect (replace (sym (replace (sym (replace (sym (plusZeroRightNeutral k)) Refl)) (replace (sym (plusSuccRightSucc k 0)) Refl))) (myReverse ts ++ [t])) (Expected type)
Specifically:
Type mismatch between
myReverse ts ++ [t]
and
replace (sym (replace (sym (replace (sym (plusZeroRightNeutral k)) Refl)) (replace (sym (plusSuccRightSucc k 0)) Refl))) (myReverse ts ++ [t])
Holes: reverse_hvect.reverse
答案 0 :(得分:3)
user3237465在Idris的回答:
import Data.Vect
import Data.HVect
nreverse : Nat -> Nat
nreverse Z = Z
nreverse (S n) = nreverse n + 1
vreverse : Vect n a -> Vect (nreverse n) a
vreverse [] = []
vreverse (x :: xs) = vreverse xs ++ [x]
hreverse : HVect ts -> HVect (vreverse ts)
hreverse [] = []
hreverse (x :: xs) = hreverse xs ++ [x]
你在不同的逆转中拥有相同结构的正确想法,但尝试了更难的结构。
答案 1 :(得分:2)
在类型级别使用rewrite
是一个坏主意。我没有Idris类型检查程序,但应该直接调整这个Agda代码:
open import Data.Nat.Base
open import Data.Vec hiding (reverse)
infixr 5 _∷ₕ_ _++ₕ_
nreverse : ℕ -> ℕ
nreverse 0 = 0
nreverse (suc n) = nreverse n + 1
vreverse : ∀ {n α} {A : Set α} -> Vec A n -> Vec A (nreverse n)
vreverse [] = []
vreverse (x ∷ xs) = vreverse xs ++ (x ∷ [])
data HList : ∀ {n} -> Vec Set n -> Set where
[]ₕ : HList []
_∷ₕ_ : ∀ {n A} {As : Vec Set n} -> A -> HList As -> HList (A ∷ As)
_++ₕ_ : ∀ {n m} {As : Vec Set n} {Bs : Vec Set m} -> HList As -> HList Bs -> HList (As ++ Bs)
[]ₕ ++ₕ ys = ys
(x ∷ₕ xs) ++ₕ ys = x ∷ₕ xs ++ₕ ys
reverseₕ : ∀ {n} {As : Vec Set n} -> HList As -> HList (vreverse As)
reverseₕ []ₕ = []ₕ
reverseₕ (x ∷ₕ xs) = reverseₕ xs ++ₕ (x ∷ₕ []ₕ)
即。你只需要在逆转塔中另一个reverse
,即“反转”一个数字。
Vect
对于Idris库中的import
来说,它应该比它应该更多。 reverse
是一个无重写版本。