Is there a nice way to use `->` directly as a function in Idris?

好久不见. 提交于 2019-12-05 17:28:48

问题


One can return a type in a function in Idris, for example

t : Type -> Type -> Type
t a b = a -> b

But the situation came up (when experimenting with writing some parsers) that I wanted to use -> to fold a list of types, ie

typeFold : List Type -> Type
typeFold = foldr1 (->)

So that typeFold [String, Int] would give String -> Int : Type. This doesn't compile though:

error: no implicit arguments allowed
    here, expected: ")",
    dependent type signature,
    expression, name
typeFold = foldr1 (->)
                   ^

But this works fine:

t : Type -> Type -> Type
t a b = a -> b

typeFold : List Type -> Type
typeFold = foldr1 t

Is there a better way to work with ->, and if not is it worth raising as a feature request?


回答1:


The problem with using -> in this way is that it's not a type constructor but a binder, where the name bound for the domain is in scope in the range, so -> itself doesn't have a type directly. Your definition of t for example wouldn't capture a dependent type like (x : Nat) -> P x.

While it is a bit fiddly, what you're doing is the right way to do this. I'm not convinced we should make special syntax for (->) as a type constructor - partly because it really isn't one, and partly because it feels like it would lead to more confusion when it doesn't work with dependent types.




回答2:


The Data.Morphisms module provides something like this, except you have to do all the wrapping/unwrapping around the Morphism "newtype".



来源:https://stackoverflow.com/questions/27092080/is-there-a-nice-way-to-use-directly-as-a-function-in-idris

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!