functional-programming - Do pure functions have only one possible implementation?
问题描述
In the image below there is a quick explanation, why pure functions appear to have only one possible implementation.
I don't really get the idea because (++) : ('a -> 'b) -> ('a -> 'b) -> 'a -> 'b
for example can clearly be implemented by let (++) (f: ('a -> 'b)) (g: ('a -> 'b)) x = f x
orlet (++) (f: ('a -> 'b)) (g: ('a -> 'b)) x = g x
Is that image just wrong or do I miss something here?
解决方案
You are right. The attached image is incorrect even without type annotations.
At first, it's important to consider what kind of "equality" on implementations is assumed here. Let's consider the following examples.
Is
(@@)
equal to(@@+)
?let ( @@ ) f x = f x let ( @@+ ) f x = let _ = 42 in f x
Is
(|>)
equal to(|>+)
?let ( |> ) x f = f x let ( |>+ ) x f = f @@ x
Is
(%)
equal to(%+)
?let ( % ) f g x = f (g x) let ( %+ ) p q r = p (q r)
If (@@)
is not equal to (@@+)
, then we can construct the 5th implementation of a function bool -> bool
, such as (fun x -> let _ = 42 in true)
.
Therefore, the author of the image would have wanted to distinguish functions not by its implementation (or codes), but by some other element such as its behavior (like duck test or the equality on mathematical functions).
Still, the image is incorrect. The image claims "for pure functions that don't have any concrete type in the signature, there is only one possible implementation", but no. For example, there is no pure function 'a -> 'b
. This can be shown through the Curry–Howard correspondence.
推荐阅读
- xml - Gson 无法序列化 - java.lang.UnsupportedOperationException:尝试序列化。忘记注册类型适配器?
- javascript - stringify match result
- facebook-graph-api - 无法获取 Instagram 轮播儿童媒体
- regex - 正则表达式匹配字符周围的字符,正则表达式中的AND运算符?
- python - 安装 librec python
- discord.js - 我正在编写一个不和谐的机器人并制作一个验证系统,但发生了一些奇怪的事情
- c# - 如何在 web config 中设置我的网站 iis 应用程序池设置?
- c# - 如何在 OpenTK (C#) 中简约地实现 2D 图像?
- ms-word - 通过 include_text 在 Word 中包含 HTML 时左对齐
- google-cloud-platform - 提供嵌套 VM、静态内部 IP 或内部 IP