As I am learning Haskell, I see that there is a lot of language extensions used in real life code. As a beginner, should I learn to use them, or should I avoid them at all cost? I see that it breaks compatibility with Haskell 98 and limits code to pretty much GHC only. However, if I browse packages on Hackage, I see that most of them are GHC-only anyway. So, what is an attitude of community
在我学习Haskell时,我发现在现实生活中有很多语言扩展使用。 作为初学者,我应该学会使用它们,还是应该不惜一切代价避免它们? 我发现它破坏了与Haskell 98的兼容性,并且仅将代码限制在几乎GHC。 但是,如果我在Hackage上浏览软件包,我发现它们中的大多数只有GHC。 那么,社区对使用语言扩展的态度如何? 如果扩展的使用是可以的,那么我怎样才能区分我可以“安全”使用的扩展(可能成为下一个Haskell标准的一部分)和
I have been working on a pet language, which has Haskell-like syntax. One of the neat things which Haskell does, which I have been trying to replicate, is its insertion of {, } and ; tokens based on code layout, before the parsing step. I found http://www.haskell.org/onlinereport/syntax-iso.html, which includes a specification of how to implement the layout program, and have made a version of
我一直在研究一种带有Haskell语法的宠物语言。 Haskell做的其中一件我一直试图复制的东西是插入{,}和; 在解析步骤之前,基于代码布局的令牌。 我发现http://www.haskell.org/onlinereport/syntax-iso.html,其中包括如何实现布局程序的规范,并且已经制作了它的一个版本(当然修改了我的(更简单的) 语言)。 不幸的是,我得到了以下不正确的输出: f (do xyz) ab 它应该产生令牌流ID ( DO { ID ID ID } ) ID ID ,
I have a type family IfEq (n :: Nat) (m :: Nat) o that evaluates to o or Void depending on wether n :== m . Since Maybe Void is only inibited by Nothing I should be able to define a function Maybe (IfEq nmo) -> Maybe o but I can't figure it out. Follow up: Can this generalized from the Maybe monad to a more general type? (eg. all MonadPlus s) EDIT: I had initially rolled my own Nat b
我有一个类型系列IfEq (n :: Nat) (m :: Nat) o根据不同的n :== m计算为o或Void 。 由于Maybe Void只能被Nothing禁用,所以我应该能够定义一个函数Maybe (IfEq nmo) -> Maybe o但我无法弄清楚。 后续行动:这可以从Maybe monad扩展到更一般的类型吗? (例如所有MonadPlus ) 编辑:我最初推出了我自己的Nat但是(谢谢@chi)与GHC Nat类KnownNat constrait它很简单,做我上面描述的。 然而,GHC无法推断KnownNat a =&g
I am aware that you can add constraints on associated type families and data families. What this does is enforce the constraints on all instances of your class. But I can't figure out how to infer these constraints in instance derivation or function declarations . For example, this code fails to type check: {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE TypeFamilies #-} import Data.Prox
我知道你可以在关联的类型族和数据族上添加约束条件。 它所做的是强制对所有类的实例进行约束。 但我无法弄清楚如何在实例派生或函数声明中推断这些约束。 例如,这段代码无法输入check: {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE TypeFamilies #-} import Data.Proxy ( Proxy ) class Eq (FooT a) => Foo a where type FooT a :: * -- Can't infer it in an instance derivation data CantInferEq a = Ca
I'm exploring type families in Haskell, trying to establish the complexity of type-level functions I can define. I want to define a closed type-level version of mod , something like so: {-# LANGUAGE TypeFamilies, DataKinds, TypeOperators, UndecidableInstances #-} import GHC.TypeLits type family Mod (m :: Nat) (n :: Nat) :: Nat where n <= m => Mod m n = Mod (m - n) n Mod m n = m
我正在探索Haskell中的类型族,试图建立我可以定义的类型级函数的复杂性。 我想定义一个封闭的类型级别的mod ,如下所示: {-# LANGUAGE TypeFamilies, DataKinds, TypeOperators, UndecidableInstances #-} import GHC.TypeLits type family Mod (m :: Nat) (n :: Nat) :: Nat where n <= m => Mod m n = Mod (m - n) n Mod m n = m 然而,编译器(GHC 7.10.2)拒绝这样做,因为第一个等式中的约束是不允许的。
I've been working through some Haskell Tutorials introducing GADTs using the typesafe evaluator examples. A lot of the evaluators operate on both Boolean and Int type. So the GADT type for an evaluator which has the functions (Constant Integer, Constant Boolean, Add, Multiply, and Check Equality) has the form: data Expr a where I :: Int -> Expr Int B :: Bool -> Expr Bool A
我一直在通过一些使用类型安全评估器示例介绍GADT的Haskell教程。 许多评估人员都在布尔和Int类型上运行。 因此,具有函数(常量整数,常量布尔,加法,乘法和校验相等)的评估程序的GADT类型具有以下形式: data Expr a where I :: Int -> Expr Int B :: Bool -> Expr Bool Add :: Expr Int -> Expr Int -> Expr Int Mul :: Expr Int -> Expr Int -> Expr Int Eq :: Expr Int -> Expr I
I am trying to represent expressions with type families, but I cannot seem to figure out how to write the constraints that I want, and I'm starting to feel like it's just not possible. Here is my code: class Evaluable c where type Return c :: * evaluate :: c -> Return c data Negate n = Negate n instance (Evaluable n, Return n ~ Int) => Evaluable (Negate n) where type
我试图用类型族表示表达式,但我似乎无法弄清楚如何编写我想要的约束条件,并且我开始觉得这是不可能的。 这是我的代码: class Evaluable c where type Return c :: * evaluate :: c -> Return c data Negate n = Negate n instance (Evaluable n, Return n ~ Int) => Evaluable (Negate n) where type Return (Negate n) = Return n evaluate (Negate n) = negate (evaluate n) 这一切都编译好,但
Is it possible to specify a type class constraint that must be satisfied by all instances of a type family? For example, given the following declaration, how would I ensure that all instances are also instances of Eq : data family Channel c :: * Many thanks, Michael 这是你想要的? {-# LANGUAGE FlexibleContexts, TypeFamilies, FlexibleInstances #-} -- Data family inside a class so that we
是否可以指定类型族的所有实例必须满足的类型类约束? 例如,如果给出以下声明,我将如何确保所有实例都是Eq实例: data family Channel c :: * 非常感谢, 迈克尔 这是你想要的? {-# LANGUAGE FlexibleContexts, TypeFamilies, FlexibleInstances #-} -- Data family inside a class so that we can add an extra Eq constraint class Eq (Channel c) => MyClass c where data Channel c :: * -- A simple to
I'd like to write a horribly non-parametric version of a function of type pretty :: (Show a) => a -> Text such that pretty :: Text -> Text = id pretty :: String -> Text = T.pack pretty :: (Show a) => a -> Text = T.pack . show So the idea is that anything that already has a Show instance can be turned into a "pretty" Text by just show -ing it, except for Text an
我想写一个可怕的非参数版本的函数类型 pretty :: (Show a) => a -> Text 这样 pretty :: Text -> Text = id pretty :: String -> Text = T.pack pretty :: (Show a) => a -> Text = T.pack . show 所以我们的想法是,任何已经拥有Show实例的东西都可以通过show它变成一个“漂亮”的Text ,除了我们想要特殊情况的Text和String 。 以下代码工作: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE MultiParamT
I am trying to fill the hole in the following snippet import Data.Proxy import GHC.TypeLits import Data.Type.Equality import Data.Type.Bool import Unsafe.Coerce ifThenElse :: forall (a :: Nat) (b :: Nat) x l r. (KnownNat a, KnownNat b, x ~ If (a==b) l r) => Proxy a -> Proxy b -> Either (x :~: l) (x :~: r) ifThenElse pa pb = case sameNat pa pb of Just Refl -> Left Refl Nothin
我正试图填补下面的代码片段中的漏洞 import Data.Proxy import GHC.TypeLits import Data.Type.Equality import Data.Type.Bool import Unsafe.Coerce ifThenElse :: forall (a :: Nat) (b :: Nat) x l r. (KnownNat a, KnownNat b, x ~ If (a==b) l r) => Proxy a -> Proxy b -> Either (x :~: l) (x :~: r) ifThenElse pa pb = case sameNat pa pb of Just Refl -> Left Refl Nothing -> Right $ uns