- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定这两个程序(用 JavaScript 编写)......
// comp :: (b -> c) -> (a -> b) -> (a -> c)
const comp = f=> g=> x=> f (g (x))
// comp2 :: (c -> d) -> (a -> b -> c) -> (a -> b -> d)
const comp2 = comp (comp) (comp)
我的问题是如何导出comp2
的Hindley-Milner Type 没有引用comp
的实现
如果我们知道 comp
的实现,就很容易了……我们可以在整个求值过程中使用替换模型来得到扩展表达式……
comp (comp) (comp)
= (f => g => x => f (g (x))) (comp) (comp)
= x => comp (comp (x))
= y => comp (comp (y))
= y => (f => g => x => f (g (x))) (comp (y))
<em>... keep going until ...</em>
<b>= f=> g=> x=> y=> f (g (x) (y))</b>
铃声响起。扩展的评估匹配 comp2
的类型。没有人留下深刻印象。
// comp2 :: (c -> d) -> (a -> b -> c) -> (a -> b -> d)
const comp2 = f=> g=> x=> y=> f (g (x) (y))
但是,如果我们只知道 comp
的类型而不知道它的实现会怎么样?除了评估代码以确定类型,我是否可以对 comp
的类型执行某种替换/评估以最终得到 comp2
的类型?
仅鉴于此,问题就变得更加困难……(至少对我而言)
// comp :: (b -> c) -> (a -> b) -> (a -> c)
<b>// comp2 :: ???</b>
const comp2 = comp (comp) (comp)
总有办法吧?这不就是algebraic data types吗都是关于什么?
让我们看一个简化的例子来澄清我的问题:如果我们有像 add
和 map
这样的函数......
// add :: Number -> Number -> Number
// map :: (a -> b) -> [a] -> [b]
如果我们想使用 map
和 add
定义一个函数,我们可以在不知道 的情况下系统地 找出类型添加
或map
的实现
// add :: Number -> Number -> Number
// map :: (a -> b) -> [a] -> [b]
// add6 :: Number -> Number
let add6 = add (6)
// mapAdd6 :: [Number] -> [Number]
let mapAdd6 = map(add6)
这真的很强大,因为它允许你推理你没有编写的代码,而不必深入研究实现(尽可能多)
但是,当尝试使用 comp2
示例执行此操作时,我很快就卡住了
// comp :: (b -> c) -> (a -> b) -> (a -> c)
// comp2 :: ??
const comp2 = comp (comp) (comp)
// initial type
(b -> c) -> (a -> b) -> (a -> c)
// apply to comp once ... ???
[(b -> c) -> (a -> b) -> (a -> c)] -> (a -> b) -> (a -> c)
// apply the second time ... ???
[(b -> c) -> (a -> b) -> (a -> c)] -> [(b -> c) -> (a -> b) -> (a -> c)] -> (a -> c)
// no... this can't be right
如何去亨德利·米尔纳
最佳答案
让我们看看我们知道什么。让我们单独看一下 comp2
的实现:
comp2 = comp comp comp
让我们考虑一下 comp
的类型签名:
comp :: (b -> c) -> (a -> b) -> (a -> c)
现在,comp2
的结果将是 comp
应用于两个参数的结果,这是 comp< 的最右侧
类型签名。因此,我们知道comp2
的类型是a -> c
类型,只是不知道a
和是什么c
还没有。
但是,我们可以弄清楚。我们可以通过手动统一类型(通过知道两种类型需要相同),然后替换已知类型变量为其具体类型来手动解决这个问题。这两个参数都是comp
,但它们应该有不同的类型:分别是b -> c
和a -> b
。让我们添加一些类型注释以使其更加清晰:
comp2 = (comp (comp :: b -> c)
(comp :: a -> b))
我们可以先尝试统一b -> c
和comp
的类型来确定b
和c
是,但是我们需要做一些字母重命名,这样我们的变量名就不会冲突:
b -> c
(b1 -> c1) -> (a1 -> b1) -> (a1 -> c1)
b = b1 -> c1
c = (a1 -> b1) -> (a1 -> c1)
接下来,我们可以对第二个参数做同样的事情,统一为 a -> b
类型:
a -> b
(b2 -> c2) -> (a2 -> b2) -> (a2 -> c2)
a = b2 -> c2
b = (a2 -> b2) -> (a2 -> c2)
但是等等!我们现在对同一类型变量 b
有两个不同的定义,因此它们也必须统一。让我们对这两种类型执行相同的过程:
b1 -> c1
(a2 -> b2) -> (a2 -> c2)
b1 = a2 -> b2
c1 = a2 -> c2
现在,回到我们为 comp2
提供的原始类型,我们可以执行一系列替换以得到一个完整的类型:
a -> c | type of comp2, from the return type of comp
(b2 -> c2) -> c | substituting the definition of a
(b2 -> c2) -> (a1 -> b1) -> (a1 -> c1) | substituting the definition of c
(b2 -> c2) -> (a1 -> (a2 -> b2)) -> (a1 -> c1) | substituting the definition of b1
(b2 -> c2) -> (a1 -> (a2 -> b2)) -> (a1 -> (a2 -> c2)) | substituting the definition of c1
(b2 -> c2) -> (a1 -> a2 -> b2) -> a1 -> a2 -> c2 | removing unnecessary parentheses
(c -> d) -> (a -> b -> c) -> a -> b -> d | alpha renaming
您会注意到这与您手动指定的类型相同。
关于javascript - 如何根据其实现派生过程的 HM 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38682768/
我有一个无法理解的奇怪编译问题。 //I know, you should never derive from the STL Library template class SharedClass :
我是一个刚开始学习 Haskell 的菜鸟,所以如果我问愚蠢的问题,请耐心等待。 最近我在 SO 中遇到了演示如何导出函数和表达式的类型和实现的问题(诸如 How can I understand "
如何自动派生此 GADT 的 Read 实例: {-# LANGUAGE GADTs, StandaloneDeriving #-} data TypeDec a where TypeDecInt
我遇到了我想要的情况 Deal class要注意它DealDetail type反之亦然,我想 DealDetail注意Deal type .将来我想有很多 Deal 的后代和 DealDetails
我是 C# 新手,所以请多多包涵。 好的,所以我在不同的程序集中有两个类需要相互引用: namespace AssemblyA { class A { private B MyB {
简而言之,我已经实现了一个派生自 SynchronizationContext 的类,以便 GUI 应用程序可以轻松地使用在 GUI 线程以外的线程上引发的事件。我非常感谢对我的实现的评论。具体来说,
我正在设计一个小型系统,想知道如何为派生类分配内存的细微差别。 如果我有两个类(class) class foo { public: int a; Foo(): a(0) {}; }; class
我正在尝试编写一个派生 PartialEq 的枚举,其中包含一个手动执行此操作的特征对象。我使用了解决方案 here为了强制 Trait 的实现者编写相等方法。这无法编译: trait Trait {
以下代码可以编译(特别是 MyError 被识别为具有调试特性): use std::str; use std::fmt; #[derive(Debug)] enum MyError where F:
是否有一种简单的方法来注释结构中的字段,以便在派生 PartialEq 特征时忽略它们?例如: #[derive(PartialEq,Eq)] pub struct UndirectedGraph {
我正在编写代码来处理“Foo”类型的对象。 foo 是一种容器,为了提供对其元素的高效和抽象访问,它提供了 Element 类型的嵌套类。 Element 包装对象在容器中的位置。 现在,“Foo”可
假设如下: class child : public parent { public: fun1(parent * obj); //somewhere on the child class
我有几个模板类 template class Transition { public: virtual Cost getCost() = 0; }; template class St
我正在尝试使用自定义 QSortFilterProxyModel . 这是我的标题: #include class QSortFilterProxyModel_NumbersLast : publi
我正在使用 C# 和 mvc3。我在解决方案中添加了一个项目。我想创建一个新 Controller 并让它从我添加的项目中的 Controller 派生。我该怎么做? 最佳答案 在 Visual St
我在 python 中有一个对象,它派生自 QtGui.QGraphicsPixmapItem,具有一些基本属性和方法。在对此对象的引用上调用 deepcopy 后,当我尝试使用该副本时收到一条错误消
由于只能给FixedDocument添加页面,所以我写了一个派生类: public class CustomFixedDocument : FixedDocument { public voi
我在自定义 QMainWindow 时遇到了很大的问题,因为我不知道如何实现以下内容: 在 QMainWindow 文档中,QMainWindow 有一些用于工具栏、停靠小部件、状态栏和其他的特殊区域
我想感受一下QT,决定写一个小的十六进制编辑器。为此,我需要一个允许滚动的小部件。经过一番研究,我发现 QTextEdit 为此目的派生自 QAbstractScrollArea。在阅读 QAbstr
我正在寻找一种可以从已经发生的洗牌过程中派生出 key 的算法。 假设我们有被打乱的字符串“Hello”: "hello" -> "loelh" 现在我想从中导出一个 key k,我可以用它来撤销洗牌
我是一名优秀的程序员,十分优秀!