- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个高阶列表 GADT 定义如下:
data HList as where
HNil :: HList '[]
HCons :: a -> HList as -> HList (a ': as)
我可以编写一个函数,让我从该列表中获取第一项,前提是它是非空的:
first :: HList (a ': as) -> HList '[a]
first (HCons a _) = HCons a HNil
但是,如果我创建一个新类型 Cat
和一个仅将类型级别列表中的每个类型应用于 Cat
的类型族(有点像类型级别 map ):
newtype Cat a = Cat a
type family MapCat (as :: [*]) :: [*] where
MapCat '[] = '[]
MapCat (a ': as) = Cat a ': MapCat as
还有一个类型 CatList
,它将类型列表转换为 HList
,其中包含应用于 Cat
的那些类型:
type CatList cs = HList (MapCat cs)
我无法编写适用于 CatList
的类似函数。
first' :: CatList (a ': as) -> CatList '[a]
first' (HCons a _) = HCons a HNil
它出错了:
Couldn't match type ‘MapCat as0’ with ‘MapCat as’
NB: ‘MapCat’ is a type function, and may not be injective
The type variable ‘as0’ is ambiguous
Expected type: CatList (a : as) -> CatList '[a]
Actual type: CatList (a : as0) -> CatList '[a]
In the ambiguity check for:
forall a (as :: [*]). CatList (a : as) -> CatList '[a]
To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
In the type signature:
Testing.fst :: CatList (a : as) -> CatList '[a]
最佳答案
让我们从一个更简单的例子开始:
type family F a
err :: F a -> ()
err _ = ()
抛出
Couldn't match type ‘F a0’ with ‘F a’ NB:
F is a type function, and may not be injective
The type variable a0 is ambiguous
Expected type: F a -> ()
Actual type: F a0 -> () …
虽然编译正常:
ok :: (a ~ Int) => F a -> ()
ok _ = ()
还有这个:
type family F a where
F a = a
ok :: F a -> ()
ok _ = ()
这是另一个例子,更接近你的问题:
data D = C
data Id (d :: D) where
Id :: Id d
type family TF_Id (d :: D) :: D where
TF_Id C = C
err :: Id (TF_Id d) -> ()
err _ = ()
错误是
Couldn't match type ‘TF_Id d0’ with ‘TF_Id d’ NB:
TF_Id is a type function, and may not be injective
The type variable d0 is ambiguous
Expected type: Id (TF_Id d) -> ()
Actual type: Id (TF_Id d0) -> () …
但这行得通:
ok :: Id (TF_Id C) -> ()
ok _ = ()
还有这个:
type family TF_Id (d :: D) :: D where
TF_Id x = x
ok :: Id (TF_Id d) -> ()
ok _ = ()
因为 TF_Id a
在这两种情况下都会立即减少到 a
。
因此,编译器每次无法减少 SomeTypeFamily a
时都会抛出错误,其中 a
是某个类型变量,之前未确定。
所以如果我们想解决这个问题:
type family TF_Id (d :: D) :: D where
TF_Id C = C
err :: Id (TF_Id d) -> ()
err _ = ()
在不重新定义类型族的情况下,我们需要确定err
类型签名中的d
。最简单的方法是
type family TF_Id (d :: D) :: D where
TF_Id C = C
ok :: Id d -> Id (TF_Id d) -> ()
ok _ _ = ()
或者我们可以为此定义一个数据类型:
data Proxy (d :: D) = Proxy
ok :: Proxy d -> Id (TF_Id d) -> ()
ok _ _ = ()
现在回到 first'
函数:
data Proxy (a :: [*]) = Proxy
first' :: Proxy (a ': as) -> CatList (a ': as) -> CatList '[a]
first' _ (HCons a _) = HCons a HNil
这编译得很好。你可以像这样使用它:
main = print $ first' (Proxy :: Proxy '[Int, Bool]) (HCons (Cat 3) $ HCons (Cat True) HNil)
打印 Cat3:[]
。
但编译器唯一必须知道的要减少 MapCat as
的是 as
的弱头部范式,所以我们实际上不需要提供这些额外的类型信息在 Proxy::Proxy '[Int, Bool]
中。这是一个更好的方法:
data ListWHNF (as :: [*]) where
LZ :: ListWHNF '[]
LS :: ListWHNF as -> ListWHNF (a ': as)
first' :: ListWHNF (a ': as) -> CatList (a ': as) -> CatList '[a]
first' _ (HCons a _) = HCons a HNil
main = print $ first' (LS $ LS LZ) (HCons (Cat 3) $ HCons (Cat True) HNil)
所以 first'
现在收到一些东西,看起来像列表的长度。但是我们可以静态地做到这一点吗?没那么快:
data ListWHNF (as :: [*]) where
LZ :: ListWHNF '[]
LS :: ListWHNF as -> ListWHNF (a ': as)
data HList as ln where
HNil :: HList '[] LZ
HCons :: a -> HList as ln -> HList (a ': as) (LS ln)
throws 数据构造函数 LZ 来自不可提升的类型 ListWHNF ...
。我们不能在 Haskell 中使用索引数据类型作为索引。
具有某种功能依赖性,这将允许将一种类型的术语与另一种术语的弱头部范式联系起来,我们可能可以做到这一点,但我不知道 GHC 中有类似的东西(但我不是专家)。在 Agda 中,这很简单
_^_ : ∀ {α} -> Set α -> ℕ -> Set α
A ^ 0 = Lift ⊤
A ^ suc n = A × A ^ n
因此,如果我们唯一需要推断列表的是它的长度,那么我们可以用 n
嵌套元组替换列表。滥用符号,[?, ?]::[*]
变成 (?::*, (?::*, Lift ⊤))
和所有这些 ?
现在可以自动推断出 ⊤
的成员(Agda 的 ()
等价物)。但是在 Agda 中,没有值级和类型级编程的区别,所以根本不存在这样的问题。
关于haskell - 高阶列表和类型族的歧义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218403/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!