简体   繁体   English

为什么是 `[1, "a"] :: [forall a. 不允许显示 a => a]`?

[英]Why is `[1, "a"] :: [forall a. Show a => a]` not allowed?

In my (might incorrect) understanding, following two lists should be equivalent:在我(可能不正确)的理解中,以下两个列表应该是等效的:

[1, "a"] :: [forall a. Show a => a]

data V = forall a. Show a => V a
[V 1, V "a"] :: [V]

However, the first one is not accepted but the second one works fine (with ExistentialQuantification ).但是,第一个不被接受,但第二个工作正常(使用ExistentialQuantification )。

If the first list doesn't exist, what would be the type in the blank of map V :: ??? -> [V]如果第一个列表不存在, map V :: ??? -> [V]的空白中的类型是什么map V :: ??? -> [V] map V :: ??? -> [V] ? map V :: ??? -> [V] ? What type mechanism enforces the existence of the wrapper?什么类型的机制强制包装器的存在?

Your understanding is not right.你的理解是不对的。 A big part of the problem is that the traditional existential quantification syntax you've used is pretty confusing to anyone who isn't thoroughly familiar with it.问题的很大一部分是您使用的传统存在量化语法对于不完全熟悉它的人来说非常混乱。 I therefore strongly recommend that you use GADT syntax instead, which also has the benefit of being strictly more powerful.因此,我强烈建议您改用 GADT 语法,这也有严格意义上更强大的好处。 The easy thing to do is just to enable {-# LANGUAGE GADTs #-} .最简单的事情就是启用{-# LANGUAGE GADTs #-} While we're at it, let's turn on {-# LANGUAGE ScopedTypeVariables #-} , because I hate wondering what forall means in any given spot.在此期间,让我们打开{-# LANGUAGE ScopedTypeVariables #-} ,因为我讨厌想知道forall在任何给定位置的含义。 Your V definition means exactly the same thing as您的V定义与

data V where
  V :: forall a . Show a => a -> V

We can actually drop the explicit forall if we like:如果我们愿意,我们实际上可以删除显式forall

data V where
  V :: Show a => a -> V

So the V data constructor is a function that takes something of any showable type and produces something of type V .所以V数据构造函数是一个函数,它接受任何可显示类型的东西并产生V类型的东西。 The type of map is pretty restrictive: map的类型非常严格:

map :: (a -> b) -> [a] -> [b]

All the elements of the list passed to map have to have the same type.传递给map的列表中的所有元素都必须具有相同的类型。 So the type of map V is just所以map V的类型就是

map V :: Show a => [a] -> [V]

Let's get back to your first expression now:现在让我们回到你的第一个表达式:

[1, "a"] :: [forall a. Show a => a]

Now what this actually says is that [1, "a"] is a list, each of whose elements has type forall a . Show a => a现在这实际上是说[1, "a"]是一个列表,其每个元素的类型为forall a . Show a => a forall a . Show a => a . forall a . Show a => a That is, if I provide any a that's an instance of Show , each element of the list should have that type.也就是说,如果我提供任何a Show实例,则列表中的每个元素都应该具有该类型。 This is simply not true.这是不正确的。 "a" does not, for example, have type Bool .例如, "a"没有类型Bool There's yet another problem here;这里还有一个问题; the type [forall a . Show a => a]类型[forall a . Show a => a] [forall a . Show a => a] is "impredicative". [forall a . Show a => a]是“不可预测的”。 I don't understand the details of what that means, but loosely speaking you've stuck a forall in the argument of a type constructor other than -> , and that's not allowed.我不明白这意味着什么的细节,但松散地说,你在->以外的类型构造函数的参数中坚持了forall ,这是不允许的。 GHC might suggest that you enable the ImpredicativeTypes extension, but this really doesn't work right, so you shouldn't. GHC 可能会建议您启用ImpredicativeTypes扩展,但这确实不起作用,因此您不应该这样做。 If you want a list of existentially quantified things, you need to wrap them up first in existential datatypes or use a specialized existential list type.如果你想要一个存在量化事物的列表,你需要首先将它们包装在存在数据类型中,或者使用专门的存在列表类型。 If you want a list of universally quantified things, you need to wrap them up first (probably in newtypes).如果你想要一个普遍量化的东西的列表,你需要先把它们包装起来(可能是新的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 forall a之间有什么区别? [a]和[forall a。一个]? - What is the difference between forall a. [a] and [forall a. a]? 为什么是forall a。 一个不被认为是Int的子类型,而我可以使用类型forall a的表达式。 预计会有任何一种类型的Int? - Why is forall a. a not considered a subtype of Int while I can use an expression of type forall a. a anywhere one of type Int is expected? 为什么id的类型不能专门用于(forall a.a - > a) - >(forall b.b - > b)? - Why can't the type of id be specialised to (forall a. a -> a) -> (forall b. b -> b)? 如何投射`forall a。 a -> a` 回到 `a -> a`? - How to cast `forall a. a -> a` back to `a -> a`? 翻译/编码Haskell的`data Obj = forall a。 (显示a)=> Scala中的Obj a` - Translate/encode Haskell's `data Obj = forall a. (Show a) => Obj a` in Scala 使用量化约束导出 Ord(forall a.Ord a => Ord (fa)) - Derive Ord with Quantified Constraints (forall a. Ord a => Ord (f a)) 什么是 AllowAmbiguousTypes 以及为什么在此“forall”示例中需要它? - What is AllowAmbiguousTypes and why is it needed in this "forall" example? 为什么`forall(a :: j)(b :: k)`与`forall(p ::(j,k))`的工作方式不同? - Why does `forall (a :: j) (b:: k)` work differently than `forall (p :: (j,k))`? 为什么rank-n类型需要明确的forall量词? - Why are explicit forall quantifiers necessary for rank-n types? 为什么`forall`需要在数据定义中具有多态类型? - Why is `forall` required to have a polymorphic type in data definition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM