Creating many similar newtypes/typeclass instances in Haskell

I am a beginner in Haskell and trying to learn about typeclasses and types. I have the following example (which represents a real problem in algebra that I am working on), in which I define a type which just wraps Num instances, and a typeclass which defines a binary operation baz . newtype Foo i = B i deriving (Eq, Ord, Show) class Bar k where baz :: k -> k -> k instance Num k =>

在Haskell中创建许多类似的newtypes / typeclass实例

我是Haskell的初学者,并试图了解类型和类型。 我有下面的例子(它代表了我正在处理的代数中的一个真正的问题),其中我定义了一个只包装Num实例的类型和一个定义二进制运算baz的类型类。 newtype Foo i = B i deriving (Eq, Ord, Show) class Bar k where baz :: k -> k -> k instance Num k => Bar (Foo k) where baz (B a) (B b) = B (f a b) f :: Num a => a -> a -> a f a b = a + b 在将B

A typecheck errors in deriving wrapper for Linear.V

I am trying to make a newtype wrapper for the Linear.V type and derive useful instances. I was trying this: {-# LANGUAGE DataKinds, PolyKinds, ScopedTypeVariables, StandaloneDeriving, FlexibleContexts, UndecidableInstances, GeneralizedNewtypeDeriving, PartialTypeSignatures, TypeFamilies #-} import Linear.V import Control.Lens.At data Foo = Foo1 | Foo2 deriving (Show, Eq) Attempt 1 - I wou

Linear.V派生封装器中的类型检查错误

我正在尝试为Linear.V类型创建一个新类型的包装并派生有用的实例。 我正在尝试这个: {-# LANGUAGE DataKinds, PolyKinds, ScopedTypeVariables, StandaloneDeriving, FlexibleContexts, UndecidableInstances, GeneralizedNewtypeDeriving, PartialTypeSignatures, TypeFamilies #-} import Linear.V import Control.Lens.At data Foo = Foo1 | Foo2 deriving (Show, Eq) 尝试1 - 我会认为GeneralizedNewtypeDeriving

Can I implement this newtype as a composition of other types?

I've written a newtype Const3 that's very similar to Const , but contains the first of three given type arguments: newtype Const3 a b c = Const3 { getConst3 :: a } I can define very many useful instances for this newtype, but I'd have to do it all myself. However, the function I'm applying on the type level resembles the function a b c -> a which @pl tells me is equivalen

我是否可以将这种新类型实现为其他类型的组合?

我写了一个与Const类似的新类型Const3 ,但包含三个给定类型参数中的第一个: newtype Const3 a b c = Const3 { getConst3 :: a } 我可以为这种新类型定义很多有用的实例,但我必须自己完成。 但是,我在类型级别上应用的函数类似于函数 a b c -> a @pl告诉我等价于const . const const . const 。 (.)和const都有匹配的newtype包装: Compose和Const 。 所以我想我可以写: type Const3 = Compose Const Const

creating MonadBaseControl instance for newtype

Suppose I have simple newtype declaration newtype Foo a = Foo { unFoo :: ReaderT Int IO a } I want to make Foo instance of MonadBaseControl IO. It should be easy, since ReaderT Int IO is already an instance of MonadBaseControl IO. However, automatically deriving it using GeneralizedNewtypeDeriving doesn't work, because MonadBaseControl class has an associated type. How can one write a M

为newtype创建MonadBaseControl实例

假设我有简单的newtype声明 newtype Foo a = Foo { unFoo :: ReaderT Int IO a } 我想制作MonadBaseControl IO的Foo实例。 它应该很容易,因为ReaderT Int IO已经是MonadBaseControl IO的一个实例。 但是,使用GeneralizedNewtypeDeriving自动派生它不起作用,因为MonadBaseControl类具有关联的类型。 如何为Foo编写MonadBaseControl IO实例? defaultLiftBaseWith和defaultRestoreM应该很有帮助,但解密它们的类型有点困

Why is there no `

GHC has several useful language extensions for mechanically deriving various common Haskell typeclasses ( -XDeriveFunctor , -XDeriveFoldable , -XDeriveTraversable ). It seems that Applicative is another class which is often needed and frequently easily derived. For a simple record containing slots of type a , eg, data SimpleRecord a = Simple a a a the Applicative instance is trivially derived

为什么没有`

GHC有几种有用的语言扩展,用于机械派生各种常见的Haskell类型类( -XDeriveFunctor , -XDeriveFoldable , -XDeriveTraversable )。 看起来Applicative是另一类经常需要且经常容易派生的类。 对于包含类型a插槽的简单记录,例如, data SimpleRecord a = Simple a a a Applicative实例是平凡派生的, instance Applicative SimpleRecord where pure x = Simple x x x Simple a1 b1 c1 <*> Simple a2 b2 c2

Existential data types with a single strict field

So I have an existential data type with a single strict field: data Uncurry (a :: i -> j -> *) (z :: (i,j)) = forall x y. z ~ '(x,y) => Uncurry !(a x y) Experimentation using unsafeSizeof (stolen from this answer) leads me to believe that it can be zero memory-overhead: λ p = (0, '') :: (Int, Char) λ q = Uncurry p λ unsafeSizeof p 10 λ unsafeSizeof q 10 So it seems like Uncurry is

具有单一严格字段的​​存在性数据类型

所以我有一个严格的字段存在的数据类型: data Uncurry (a :: i -> j -> *) (z :: (i,j)) = forall x y. z ~ '(x,y) => Uncurry !(a x y) 使用unsafeSizeof (从这个答案中窃取)的实验使我相信它可以是零内存开销: λ p = (0, '') :: (Int, Char) λ q = Uncurry p λ unsafeSizeof p 10 λ unsafeSizeof q 10 所以好像Uncurry是有点像个newtype ,只有在编译时使用。 这对我来说很有意义,因为平等主张并不需要

GHC existentially quantified singleton

I'm using GHC 7.10 and the singletons package. There's a GADT named SomeSing in this library that allows you to existentially quantify a singleton. It's definition looks like this: data SomeSing (kproxy :: KProxy k) where SomeSing :: Sing (a :: k) -> SomeSing ('KProxy :: KProxy k) And it works great. We can wrap up a singleton with this so that only the kind is known, not th

GHC存在量化的单身人士

我正在使用GHC 7.10和singletons软件包。 在这个库中有一个名为SomeSing的GADT,它允许你对单例进行存在量化。 它的定义如下所示: data SomeSing (kproxy :: KProxy k) where SomeSing :: Sing (a :: k) -> SomeSing ('KProxy :: KProxy k) 它效果很好。 我们可以用这个来包装一个单例,这样只有类型是已知的,而不是类型。 我想创建一个变体,一个类似的包装器,它可以让我包装一个单例,并将类型函数的结果应用于

Haskell function returning existential type

Is it possible to write a Haskell function that yields a parameterised type where the exact type parameter is hidden? Ie something like f :: T -> (exists a. U a) ? The obvious attempt: {-# LANGUAGE ExistentialQuantification #-} data D a = D a data Wrap = forall a. Wrap (D a) unwrap :: Wrap -> D a unwrap (Wrap d) = d fails to compile with: Couldn't match type `a1' with `a' `a1' is

Haskell函数返回存在类型

是否有可能编写一个Haskell函数生成参数化类型,其中隐藏了确切的类型参数? 即像f :: T -> (exists a. U a) ? 显而易见的尝试: {-# LANGUAGE ExistentialQuantification #-} data D a = D a data Wrap = forall a. Wrap (D a) unwrap :: Wrap -> D a unwrap (Wrap d) = d 无法编译: Couldn't match type `a1' with `a' `a1' is a rigid type variable bound by a pattern with constructor W

Why aren't there existentially quantified type variables in GHC Haskell

There are universally quantified type variables, and there are existentially quantified data types. However, despite that people give pseudocode of the form exists a. Int -> a exists a. Int -> a to help explain concepts sometimes, it doesn't seem like a compiler extension that there's any real interest in. Is this just a "there isn't much value in adding this" kind o

为什么GHC Haskell中没有存在量化类型变量

有普遍量化的类型变量,并且有存在量化的数据类型。 但是,尽管人们给出了伪代码的形式exists a. Int -> a exists a. Int -> a有时可以帮助解释概念,它似乎并不像编译器扩展那样有真正的兴趣。这仅仅是“添加这种类型的东西没有多大价值”(因为它看起来很有价值对我来说),还是有一个问题,如不可判定性,这使得它是不可能的。 编辑:我已经标记viorior的答案是正确的,因为它似乎可能是为什么这不包括在内的实际原因

Existential vs. Universally quantified types in Haskell

What exactly is the difference between these? I think I understand how existential types work, they are like having a base class in OO without a way to down cast. How are universal types different? The terms "universal" and "existential" here come from the similarly-named quantifiers in predicate logic. Universal quantification is normally written as ∀, which you can rea

Haskell中的存在与量化类型

这些差别究竟是什么? 我想我明白存在类型是如何工作的,他们就像在OO中有一个基类,而没有下沉的方法。 通用类型有何不同? 这里的术语“通用”和“存在”来自谓词逻辑中类似命名的量词。 通用量化通常写成∀,你可以读作“for all”,并且大致意味着它听起来像:在一个类似于“∀x...”的逻辑语句中,无论代替“...”对于所有可能的“x”都是正确的,你可以从任何一组量化的事物中进行选择。 存在量化通常被写为∃,你可以把它看作“存