为什么 Idris 2 无法解决这个简单示例中函数组合的约束?
我尝试在 Idris 2 中编写的一些代码遇到了问题。我想解决这个问题,但更重要的是,我希望更深入地理解它并培养一些诊断此类问题的技能。
我将问题提炼为以下相对简单的示例:
data D : Nat -> Type where
V : (n : Nat) -> D n
d : (n : Nat) -> D n
d n = V n
f : D n -> String
f (V n) = show n
t : Nat -> String
t = f . d
t
的定义失败了类型检查,输出如下:
Error: While processing right hand side of t. Can't solve constraint between: ?n [no locals in scope] and n.
Test:11:9--11:10
07 | f : D n -> String
08 | f (V n) = show n
09 |
10 | t : Nat -> String
11 | t = f . d
一些实验表明 t
的以下替代定义也类型检查失败:
t : Nat -> String
t n = (f . d) n
t : Nat -> String
t = \n => (f . d) n
虽然这些替代方案类型检查成功:
t : Nat -> String
t n = f (d n)
t : Nat -> String
t = \n => f (d n)
我正在努力学习 Idris(第二次),因此虽然我可以继续不涉及函数组合的定义,但我想提高我的理解。
在我看来,通过类型检查的定义只是具有相同语义和行为的语法替代方案,并且我不明白为什么函数组合定义无法通过类型检查。我还想了解类型检查器报告的特定错误消息,以便我可以加深理解并解决将来类似的类型检查错误。
我有几个广泛的问题:
- 我应该如何解释此示例中类型检查器报告的错误,以及如何收集有关
的更多信息
?提到了
和n
类型?我特别欢迎任何有关如何理解和解决此类错误的建议或提示(俗话说授人以渔)。 - 为什么涉及函数组合的定义无法通过类型检查?
- 此示例的最佳解决方案是什么? 我应该使用不涉及函数组合的定义吗?有更好的选择吗?
I have encountered a problem with some code I am trying to write in Idris 2. I would like to resolve this issue, but more importantly, I wish to understand it more deeply and develop some skills in diagnosing such issues in general.
I have distilled the problem to the following relatively trivial example:
data D : Nat -> Type where
V : (n : Nat) -> D n
d : (n : Nat) -> D n
d n = V n
f : D n -> String
f (V n) = show n
t : Nat -> String
t = f . d
The definition of t
fails type checking with the following output:
Error: While processing right hand side of t. Can't solve constraint between: ?n [no locals in scope] and n.
Test:11:9--11:10
07 | f : D n -> String
08 | f (V n) = show n
09 |
10 | t : Nat -> String
11 | t = f . d
Some experimentation has revealed that the following alternative definitions for t
also fail type checking:
t : Nat -> String
t n = (f . d) n
t : Nat -> String
t = \n => (f . d) n
While these alternatives type check successfully:
t : Nat -> String
t n = f (d n)
t : Nat -> String
t = \n => f (d n)
I am endeavouring to learn Idris (for the second time), and so while I could move on with the definitions which don't involve function composition, I would like to improve my understanding.
It seems to me that the definitions which pass type checking are simply syntactic alternatives with identical semantics and behaviour, and I don't understand why the function composition definitions fail type checking. I would also like to understand the particular error message the type checker reports, so that I can deepen my understanding and resolve similar type checking errors in the future.
I have a few broad questions:
- How should I interpret the error reported by the type checker in this example, and how can I gather more information about the
?n
andn
types mentioned? I particularly welcome any advice or tips on how to go about understanding and resolving such an error (teach a man to fish, as the saying goes). - Why do the definitions involving function composition fail type checking?
- What is the best solution for this example? Should I just use a definition which does not involve function composition? Is there a better alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们看看涉及到的类型
问题是:
无法统一,因为
(a -> b)
不允许通过参数的值来决定返回值的类型。Let look at the types involved
The problem is:
cannot be unified because
(a -> b)
does not allow the value of the argument to determine the type of return value.