syntax - Idris 教程 - 中缀形式的命名实现的函数
问题描述
我正在阅读有关命名实现的教程,其中给出Semigroup Nat
了一个示例:
[PlusNatSemi] Semigroup Nat where
(<+>) x y = x + y
[MultNatSemi] Semigroup Nat where
(<+>) x y = x * y
如果我想使用 Plus 定义,(<+>) @{PlusNatSemi} Z (S Z)
可以。但是有没有办法更中缀地写这个?Z <+> S Z
抱怨缺乏实施,既不工作Z <+> @{PlusNatSemi} S Z
也不Z (<+> @{PlusNatSemi}) S Z
工作。
解决方案
在这种情况下,您需要明确定义要使用的命名实现。
但是Idris
默认情况下允许命名实现在带有using
符号的声明块中可用。因此,在您的示例中,它将类似于:
[PlusNatSemi] Semigroup Nat where
(<+>) x y = x + y
[MultNatSemi] Semigroup Nat where
(<+>) x y = x * y
using implementation PlusNatSemi
semiPlus : Nat -> Nat -> Nat
semiPlus x y = x <+> y
using implementation MultNatSemi
semiMul : Nat -> Nat -> Nat
semiMul x y = x <+> y
推荐阅读
- r - 如何检测以'abc'开头然后有'n'个数字然后再有字母的字符串?
- reactjs - 如何在 Javascript 中插入 HTML?
- css - Bobril - 关键帧规则
- c# - C# SmtpClient 谷歌邮件垃圾邮件文件夹
- java - 我有一个算法,但我没有修复。我该如何修复?我应该在java中做什么
- linux - 根据相应源中的内容在编辑器中打开头文件
- python - 如何根据神经网络预测改变pygame中的事件
- web-services - springboot rest api:内存和cpu始终处于高水平
- android - 无法在底部添加按钮
- android - 多窗口自由形式模式:限制垂直或水平调整大小的能力 oa 窗口