哈斯克尔的教会数字 [英] Church Numerals in haskell

查看:150
本文介绍了哈斯克尔的教会数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  0:=λfx.x
我试图用haskell打印教会数字1:=λfx.fx

Haskell代码:

  c0 = \ fx  - > x 
c1 = \ f x - > fx

当我在haskell控制台中输入时,出现一个错误,说

  test> c1 

< interactive>:1:0:
没有实例用于(Show((t - > t1) - > t - > t1))
在< interactive>:1:0-1
处使用`print'可能的修正:
添加一个实例声明(Show((t - > t1) - > t - > ; t1))
在一个交互式GHCi命令的脚本中:print it

我是无法准确找出错误的含义。



谢谢!

解决方案

这里的问题是,默认情况下,无法在Haskell中打印值。 print 函数和GHCi REPL使用的缺省方式是 show 函数,由类型类显示

定义。

然后,您收到的错误是通知你已经评估了一个没有 Show 定义实例的类型的表达式。 Modulo some verbiage,this is all the error message is saying:

 (Show((t  - > t1) - > t  - > t1))

类型 - > t1) - > t - > t1)是您评估的表达式的推断。这是教会数字1的有效类型,但教会数字的正确类型实际上应该是(a - > a) - > a - > a< / code>。

 在< interactive>使用'print'时产生:1:0 -1 

隐式使用 print 函数显示值。通常这会告诉你程序中哪里发现了错误,但是在这种情况下,它表示< interactive>:1:0-1 ,因为错误是由表达式在REPL中。

 可能的修正:
添加一个实例声明(Show((t - > t1 ) - > t - > t1))

这只是提示您可以修复错误通过定义它期望的实例。




现在,您可能想要打印您的教会数字,而不仅仅是知道为什么你不能。不幸的是,这不像添加要求的实例那么简单:如果为(a - > a) - >编写实例, a - >一个,Haskell将其解释为任何特定的 a 的实例,而Church数字的正确解释是可以在任何 a 中使用的多态功能。



换句话说,您希望您的显示函数是这样的:

  showChurch n = show $ n(+1)0 

如果您确实想要,您可以像这样实现Show实例:

  instance(Show a,Num a)=>显示((a  - > a) - > a  - > a)其中
显示n =显示$ n(+1)0
{ - #LANGUAGE FlexibleInstances# - }
添加到文件的第一行。或者你也可以实现类似的功能,将它们转换为常规数字。 churchToInt c1
1
> showChurch c1
1

等。


I am trying to print church numerals in haskell using the definions:

0 := λfx.x
1 := λfx.f x

Haskell code:

c0 = \f x -> x
c1 = \f x -> f x

When I enter it in the haskell console I get an error which says

    test> c1

    <interactive>:1:0:
    No instance for (Show ((t -> t1) -> t -> t1))
      arising from a use of `print' at <interactive>:1:0-1
    Possible fix:
      add an instance declaration for (Show ((t -> t1) -> t -> t1))
    In a stmt of an interactive GHCi command: print it

I am not able to exactly figure out what error says.

Thank you!

解决方案

The problem here is that, by default, it's not possible to print values in Haskell. The default way to print things--used by the print function and by the GHCi REPL, among others--is the show function, defined by the type class Show.

The error you're getting, then, is informing you that you've evaluated an expression of a type that doesn't have an instance of Show defined. Modulo some verbiage, this is all the error message is saying:

No instance for (Show ((t -> t1) -> t -> t1))

The type ((t -> t1) -> t -> t1) is what was inferred for the expression you evaluated. This is a valid type for the Church numeral 1, though the "correct" type for a Church numeral should actually be (a -> a) -> a -> a.

  arising from a use of `print' at <interactive>:1:0-1

It's implicitly using the print function to display the values. Normally this would tell you where in your program the error was found, but in this case it says <interactive>:1:0-1 because the error was caused by an expression in the REPL.

Possible fix:
  add an instance declaration for (Show ((t -> t1) -> t -> t1))

This is just suggesting that you could fix the error by defining the instance it was expecting.


Now, you probably want to actually print your Church numerals, not just know why you can't. Unfortunately, this isn't as simple as adding the instance it asked for: If you write an instance for (a -> a) -> a -> a, Haskell interprets this as an instance for any specific a, whereas the correct interpretation of a Church numeral is a polymorphic function that works on any arbitrary a.

In other words, you want your show function to be something like this:

showChurch n = show $ n (+1) 0

If you really want to, you may implement the Show instance like this:

instance (Show a, Num a) => Show ((a -> a) -> a -> a) where
    show n = show $ n (+1) 0

and add {-#LANGUAGE FlexibleInstances#-} to the first line of the file. Or you may implement something similar to convert them to a regular number

> churchToInt c1
1
> showChurch c1
"1"

etc.

这篇关于哈斯克尔的教会数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆