- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在阅读existential section维基教科书中的内容如下:
Firstly, forall really does mean 'for all'. One way of thinking about types is as sets of values with that type, for example, Bool is the set {True, False, ⊥} (remember that bottom, ⊥, is a member of every type!), Integer is the set of integers (and bottom), String is the set of all possible strings (and bottom), and so on. forall serves as an intersection over those sets. For example, forall a. a is the intersection over all types, which must be {⊥}, that is, the type (i.e. set) whose only value (i.e. element) is bottom.
forall
如何充当这些集合的交集?
forall
在形式逻辑中意味着它可以是论域中的任何值。在 Haskell 中如何将其转换为交集?
最佳答案
Haskell 的 forall
-s 可以被视为受限制的依赖函数类型,我认为这是概念上最具启发性的方法,也最适合集合论或逻辑解释。
在依赖语言中,我们可以绑定(bind)函数类型中的参数值,并在返回类型中提及这些值。
-- Idris
id : (a : Type) -> (a -> a)
id _ x = x
-- Can also leave arguments implicit (to be inferred)
id : a -> a
id x = x
-- Generally, an Idris function type has the form "(x : A) -> F x"
-- where A is a type (or kind/sort, or any level really) and F is
-- a function of type "A -> Type"
-- Haskell
id :: forall (a : *). (a -> a)
id x = x
关键的区别在于 Haskell 只能使用 forall
绑定(bind)类型、提升类型和类型构造函数。 ,而依赖语言可以绑定(bind)任何东西。
在文献中,相关函数称为相关产品。当它们是函数时,为什么要这样调用它们呢?事实证明,我们可以仅使用依赖函数来实现 Haskell 的代数积类型。
一般来说,任何函数a -> b
可以被视为某些产品的查找功能,其中键的类型为 a
并且元素的类型为 b
。 Bool -> Int
可以解释为一对Int
-s。对于非依赖函数来说,这种解释并不是很有趣,因为所有产品字段都必须是同一类型。通过依赖函数,我们的对可以适本地多态:
Pair : Type -> Type -> Type
Pair a b = (index : Bool) -> (if index then a else b)
fst : Pair a b -> a
fst pair = pair True
snd : Pair a b -> b
snd pair = pair False
setFst : c -> Pair a b -> Pair c b
setFst c pair = \index -> if index then c else pair False
setSnd : c -> Pair a b -> Pair a c
setSnd c pair = \index -> if index then pair True else c
我们已经在这里恢复了配对的所有基本功能。另外,使用 Pair
我们可以构建任意数量的产品。
那么,它如何与 forall
的解释联系起来? -s?好吧,我们可以解读普通产品并为它们建立一些直觉,然后尝试将其转移到 forall
-s。
那么,让我们先看一下普通乘积的代数。代数类型之所以被称为代数,是因为我们可以通过代数来确定其值的个数。 Link to detailed explanation.如果A
有|A|
值的数量和 B
有|B|
值的数量,则 Pair A B
有|A| * |B|
可能值的数量。使用 sum 类型,我们对居民数量进行求和。自 A -> B
可以被视为具有 |A|
的产品字段,全部具有类型 B
, A -> B
的居民数量是 |A|
数量|B|
-s 相乘,等于 |B|^|A|
。因此,有时使用“指数类型”这个名称来表示函数。当函数是相关的时,我们会回到“一定数量的不同类型的乘积”解释,因为指数公式不再适合。
有了这个理解,我们就可以解释 forall (a :: *). t
作为索引类型为 *
的产品类型和类型为 t
的字段,其中a
t
里面可能会提到,因此字段类型可能会根据 a
的选择而变化。 。我们可以通过让 Haskell 推断 forall
的某些特定类型来查找字段。 ,有效地将函数应用于类型参数。
请注意,该乘积具有与索引值一样多的字段,考虑到 Haskell 类型的潜在数量,这里几乎是无限的。
关于haskell - forall 作为这些集合的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25344032/
我有一个现有站点,其数据库设计不正确并且包含大量记录,因此我们无法更改数据库结构。 本期数据库主要包含用户、问题、选项、答案4个表。有一组标准的问题和选项,但对于每个用户,每组问题和选项在答案表中都有
有没有办法找出两个 CGPathRef 是否相交。就我而言,所有 CGPath 都有 closePath。 例如,我有两条路径。一条路径是旋转一定角度的矩形,另一条路径是弯曲路径。两条路径的原点会经常
我目前正在使用 JavaFX 研究不同形状之间的边界相交。我想检测两个多边形在它们的点上而不是在它们的边界上的碰撞(即 2 个多边形)。 请参见图 1:不需要的行为和图 2:需要的行为。 是否有任何现
在我的three.js 场景中,我有一些立方体。我想为用户提供用鼠标选择框的可能性。 这是重要的代码(我使用 Three.js 版本 69。): function init() { [...]
我有一个问题。我想将四边形与四边形相交。 int main(){ typedef boost::geometry::model::point_xy TBoostPoint; typedef b
在 MongoDB 中我们找到了交集的方法,但我们也想实现日期范围排除。让我解释一下。 我们有每个支持团队的每日轮值表。每个支持团队可以每 15 分钟预订一次,持续 5-25 分钟(大约)。每个团队有
目录 1、列表求并集 1. union_by 2、列表求交集 1. intersection_by
我有以下查询: select id from t1 intersect select id from t2 intersect select id from t3 id 在某些表中可能不是唯一的,所以
需要完成此实现才能使用 UseSet 类。不确定我所实现的是否100%正确。 但是我需要 Union 和 SysDiff 方面的帮助。 public class Set { private Ar
我的程序打印主构造函数,但不返回 3 个底部函数,而是返回其编译结果。我哪里出错了? import java.util.*; public class Main { public static v
我正在尝试找到两个不同列表列表的交集。换句话说,找出 list1 中的所有列表是否与列表 2 中的任何列表相交。 列表1: [[1,4],[1,6],[6,8],[8,10]] 列表2: [[],
我正在尝试从 2 个已知 BST 的交集创建一个新的 BST。我在第二种情况下的 intersect2 方法中收到 NullPointerException,位于“cur3.item.set_acco
这个问题已经有答案了: self referential struct definition? (9 个回答) 已关闭 7 年前。 我有一个脚本 a.h #include b.h type
我在 user_profile 表上运行搜索,其中包含单个索引和复合索引: SELECT ••• FROM user_profile up JOIN auth_user
我正在尝试为(公寓)列表创建过滤器,通过 apartsments_features 表与 apartment features 建立多对多关系。 我只想包括具有所有某些功能(在表格上标记为"is")的
我想从两个给定的嵌套列表中创建一个新的嵌套列表(每个列表中都有唯一的项目),以便新的嵌套列表是两个列表的最大公共(public)交集。 一个例子希望能帮助阐明我的问题: old1 = [[1,
我在 Django 中有两个模型,我不确定如何编写它们(是否有一个抽象模型并继承等等......或者有两个不同的模型)但通常我有两种类型的对象 A 和 B。 A 和 B 完全相同,因为它们只是项目。它
我有一个像这样的数组 arrays = [ ['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'g'], ['a',
我正在通过向 Mario Zechner 的开源跳线游戏添加更多功能来学习 libgdx。我正在尝试制作一些带有角度的平台并遇到旋转矩形的碰撞检测问题。 我关注了this解决方案并使用多边形和我的矩形
我有一个包含对象的数组: let data = [[{a:0}, {b:1}], [{a:1}, {b:1}]] 现在我想制作一个 lodash intersection这两个数组,返回 [{b:1}
我是一名优秀的程序员,十分优秀!