【发布时间】:2017-08-13 16:19:26
【问题描述】:
最近我一直在探索 Idris 中的依赖类型。但是,我克服了一个非常烦人的问题,即在 Idris 中,我应该使用类型签名启动我的程序。那么问题来了,如何在 Idris 中编写简洁的类型签名?
例如,
get_member : (store : Vect n String) -> (idx : List (Fin n)) -> (m : Nat ** Vect m (Nat, String))
get_member store idx = Vect.mapMaybe maybe_member (Vect.fromList idx)
where
maybe_member : (x : Fin n) -> Maybe (Nat, String)
-- The below line should be type corrected
-- maybe_member x = Just (Data.Fin.finToNat x, Vect.index x store)
如果我注释最后一行,编译器将对上述函数进行类型检查, 但是如果我将最后一行作为表达式,编译会抱怨。
When checking right hand side of VecSort.get_member, maybe_member with expected type
Maybe (Nat, String)
When checking an application of function Data.Vect.index:
Type mismatch between
Vect n1 String (Type of store)
and
Vect n String (Expected type)
Specifically:
Type mismatch between
n1
and
n
但我是作为 lambda 函数来做的,
get_member : (store : Vect n String) -> (idx : List (Fin n)) -> (m : Nat ** Vect m (Nat, String))
get_member store idx = Vect.mapMaybe (\x => Just (Data.Fin.finToNat x, Vect.index x store)) (Vect.fromList idx)
它也将是类型检查。
所以问题是,我应该如何在类型签名中定义具有正确长度的 Vect 类型?
【问题讨论】:
标签: dependent-type idris purely-functional