Why can't GHC derive instances for Monoid?

GHC has a few language flags, such as DeriveFunctor , DeriveDataTypeable etc., which enable compiler generation of derived instances for type classes other than those allowed in Haskell 98. This especially makes sense for something like Functor , where the laws of that class dictate an obvious, "natural" derived instance. So why not for Monoid ? It seems like for any data type with a

为什么GHC不能为Monoid派生实例?

GHC有一些语言标志,例如DeriveFunctor , DeriveDataTypeable等,它们可以编译器生成除Haskell 98中允许的类型类的派生实例。这对于类似Functor东西特别Functor ,其中该类的法律规定明显的“自然”派生实例。 那么为什么不为Monoid ? 对于具有单个数据构造函数的任何数据类型来说似乎如此: data T = MkT a b c ... 人们可以机械地产生一个Monoid实例(借口伪代码): instance (Monoid a, Monoid b, Monoid c, ...) =>

What's the theoretical basis for existential types?

The Haskell Wiki does a good job of explaining how to use existential types, but I don't quite grok the theory behind them. Consider this example of an existential type: data S = forall a. Show a => S a -- (1) to define a type wrapper for things that we can convert to a String . The wiki mentions that what we really want to define is a type like data S = S (exists a. Show a =>

存在类型的理论基础是什么?

Haskell Wiki在解释如何使用存在类型方面做得很好,但我并不完全赞同它们背后的理论。 考虑这种存在类型的例子: data S = forall a. Show a => S a -- (1) 为我们可以转换为String东西定义一个类型包装器。 维基提到,我们真正想要定义的是类似的 data S = S (exists a. Show a => a) -- (2) 即一个真正的“存在”类型 - 我认为这是一个真正的“存在”类型:“数据构造函数S采用Show实例存在并包装它的任何类型

Difference between `data` and `newtype` in Haskell

我写这个有什么不同? data Book = Book Int Int newtype Book = Book(Int, Int) -- "Book Int Int" is syntactically invalid Great question! There are several key differences. Representation A newtype guarantees that your data will have exactly the same representation at runtime, as the type that you wrap. While data declares a brand new data structure at runtime. So the key point here is th

Haskell中`data`和`newtype`之间的区别

我写这个有什么不同? data Book = Book Int Int newtype Book = Book(Int, Int) -- "Book Int Int" is syntactically invalid 伟大的问题! 有几个关键的区别。 表示 一个newtype保证您的数据将有完全相同的表示在运行时,为你包的类型。 data在运行时声明一个全新的数据结构。 因此,这里的关键点是,对于构建newtype保证在编译时被删除。 例子: data Book = Book Int Int newtype Book = Book (Int, Int)

Couldn't match expected type ‘b’ with actual type ‘a’

Just started learning Haskell and Im trying to implement a max function to find the max of a list recursively max' :: (Num b) => [a] -> b max' [] = 0 max' (x:xs) | x > max' xs = x | otherwise = max' xs but am getting the error when trying to compile Couldn't match expected type 'b' with actual type 'a' 'a' is a rigid type variable bound by the type

无法与实际类型'a'匹配预期类型'b'

刚开始学习Haskell和Im试图实现一个最大函数来递归地查找列表的最大值 max' :: (Num b) => [a] -> b max' [] = 0 max' (x:xs) | x > max' xs = x | otherwise = max' xs 但在编译时遇到错误 无法与实际类型'a'匹配预期类型'b''a'是由类型签名绑定的刚性类型变量:max':: forall b a。 (数字b,数字a)=> [a] - > b在实现函数.hs:5:1-34'b'是一个刚性类型变

Method signature Haskell user defined type

I'm trying learning Haskell and I ended up reading this and I'm trying to figure out what some parts of the code do. At first, there's a data type definition: infixl 4 :+: infixl 5 :*:, :/: infixr 6 :^: data Expr a = Var Char | Const a | (Expr a) :+: (Expr a) | (Expr a) :*: (Expr a) | (Expr a) :^: (Expr a) | (Expr

方法签名Haskell用户定义的类型

我正在学习Haskell,并最终读到了这个,我试图找出代码的某些部分的作用。 首先,有一个数据类型定义: infixl 4 :+: infixl 5 :*:, :/: infixr 6 :^: data Expr a = Var Char | Const a | (Expr a) :+: (Expr a) | (Expr a) :*: (Expr a) | (Expr a) :^: (Expr a) | (Expr a) :/: (Expr a) deriving (Show, Eq) 接下来有一个功能: simp

Type erasure in Haskell?

I was reading a lecture note on Haskell when I came across this paragraph (http://www.seas.upenn.edu/~cis194/lectures/02-lists.html): This “not caring” is what the “parametric” in parametric polymorphism means. All Haskell functions must be parametric in their type parameters; the functions must not care or make decisions based on the choices for these parameters. A function can't do one

在Haskell中删除类型?

我在阅读本段(http://www.seas.upenn.edu/~cis194/lectures/02-lists.html)时正在阅读关于Haskell的讲稿: 这种“不关心”是参数多态性中“参数化”的含义。 所有的Haskell函数都必须是参数类型参数; 基于这些参数的选择,功能不必关心或做出决定。 当a是一个函数时,函数不能做一件事,而当a是Bool时,函数不能做一件事。 Haskell根本没有提供写这种操作的工具。 这种语言属性称为参数性。 参数化有许多深刻和深刻的后果

Memory layout of boxed and unboxed ints?

This question already has an answer here: Memory footprint of Haskell data types 2 answers The GHC documentation has some good information. But basically, you're correct in saying that an Int value is a pointer to a thunk. However, an unboxed value is not a pointer to the unboxed value, it is the unboxed value itself. Also, the Haskell standard report merely gives the lower limit on th

盒装和非盒装内存的内存布局?

这个问题在这里已经有了答案: Haskell数据类型的内存占用情况2个答案 GHC文件有一些很好的信息。 但基本上,你说的Int值是指向thunk的指针是正确的。 但是,unboxed值不是指向unboxed值的指针,而是unboxed值本身。 此外,Haskell标准报告仅给出Int范围的下限。 IIRC,GHC Int有超过30位。 我不认为GHC会使用额外的非装箱类型的位来存储任何元数据,但它确实使用了指针位来执行此操作。 请参阅此页面了解更多详情。

List of existentially quantified values in Haskell

I'm wondering why this piece of code doesn't type-check: {-# LANGUAGE ScopedTypeVariables, Rank2Types, RankNTypes #-} {-# OPTIONS -fglasgow-exts #-} module Main where foo :: [forall a. a] foo = [1] ghc complains: Could not deduce (Num a) from the context () arising from the literal `1' at exist5.hs:7:7 Given that: Prelude> :t 1 1 :: (Num t) => t Prelude> it seems that

Haskell中存在量化值列表

我想知道为什么这段代码没有进行类型检查: {-# LANGUAGE ScopedTypeVariables, Rank2Types, RankNTypes #-} {-# OPTIONS -fglasgow-exts #-} module Main where foo :: [forall a. a] foo = [1] ghc抱怨: Could not deduce (Num a) from the context () arising from the literal `1' at exist5.hs:7:7 鉴于: Prelude> :t 1 1 :: (Num t) => t Prelude> 似乎(Num t)上下文不能匹配arg的()上下文。

Ghc: partially compile Haskell code?

When I compile a Haskell file with ghci , typically with :load , and if there is no type error, all the expressions are loaded in the ghc interpreter. It's very nice: I can play around with :t to figure out the type of various expressions. My problem is: if there is a tiny error somewhere, ghci is not able to load anything (not even the imported modules!!), which makes finding the right ty

Ghc:部分编译Haskell代码?

当我使用ghci编译Haskell文件时,通常使用:load ,并且如果没有类型错误,则所有表达式都将加载到ghc解释器中。 这是非常好的:我可以玩弄:t找出各种表情的类型。 我的问题是:如果在某个地方有一个小错误,ghci无法加载任何东西(甚至不是导入的模块!!),这使得找到合适的类型更加困难。 我总是做同样的:注释掉不进行类型检查,发现室内用相关类型的所有位:t在ghci中,和去评论。 但这太乏味了! 是否有更好的“部分编

make module hierarchy

I'm trying to build a standalone program for the first time in Haskell, and am having trouble figuring out how to get ghc --make working with the directory organization of my liking. As the moment I have the following tree: readme.md src/ Main.hs Ciphers/ Affine.hs Shift.hs Substitution.hs Tests/ HCTests.hs With the following imports: Main.hs: module Main where

使模块层次结构

我试图在Haskell中第一次构建一个独立程序,并且无法弄清楚如何获得ghc - 与我所喜欢的目录组织一起工作。 正如我有以下树的那一刻: readme.md src/ Main.hs Ciphers/ Affine.hs Shift.hs Substitution.hs Tests/ HCTests.hs 通过以下导入: Main.hs: module Main where import Tests.HCTests import Ciphers.Affine import Ciphers.Shift import Ciphers.Substitution HCTests.hs module