Functions as instances of typeclasses?

{-# LANGUAGE LambdaCase #-} I have a bunch of functions which encode failure in various ways. For example: f :: A -> Bool returns False on failure g :: B -> Maybe B' returns Nothing on failure h :: C -> Either Error C' returns Left ... on failure I want to chain these operations in the same way as the Maybe monad, so the chaining function needs to know whether each fun

函数作为类型类的实例?

{-# LANGUAGE LambdaCase #-} 我有很多以各种方式编码失败的函数。 例如: f :: A -> Bool在失败时返回False g :: B -> Maybe B'在失败时返回Nothing h :: C -> Either Error C'在失败时返回Left ... 我想以与Maybe monad相同的方式链接这些操作,因此链接函数需要知道每个函数在进入下一个函数之前是否失败。 为此我写了这个类: class Fail a where isFail :: a -> Bool instance Fail () w

How is the instance of Show for String written?

I have a basic question about definining instances of typeclasses. I am using the Show typeclass as an example, and I am considering only the function show within the class. The Show instance for a concrete type like Bool is simple instance Show Bool where show x = {function of x here} but for String it is not: instance Show String where show x = {function of x here} produces understand

Show for String实例如何写入?

我有一个关于定义类型类的基本问题。 我使用Show类型类作为示例,并且我正在考虑仅在类中显示函数。 像Bool这样的具体类型的Show实例很简单 instance Show Bool where show x = {function of x here} 但对于字符串它不是: instance Show String where show x = {function of x here} 可以理解地产生一个错误 Illegal instance declaration for ‘Formatter String’ (All instance types must be of the form (T t1 .

Using custom instance when deriving an instance via GeneralizedNewtypeDeriving

Suppose that we have a typeclass class (A a, B a) => C a where . Using newtype will allow us to clone a data type and then automatically derive the instances via the GeneralizedNewtypeDeriving language extension (See how to write a derivable class? and Handling multiple types with the same internal representation and minimal boilerplate?). QUESTION : Is it possible to get ghc to automatical

通过GeneralizedNewtypeDeriving获取实例时使用自定义实例

假设我们有一个类型class (A a, B a) => C a where 。 使用newtype将允许我们克隆一个数据类型,然后通过GeneralizedNewtypeDeriving语言扩展自动派生实例(请参阅如何编写可派生类?以及使用相同的内部表示形式处理多个类型以及最少的样板?)。 问题 :是否有可能让ghc自动派生A和C ,而是使用我们自己的指定实现B来派生C ? 例如下面的代码(其中A = Planet , B = Lives , C = Description )不能按预期工作: {-#

Are typeclasses essential?

I once asked a question on haskell beginners, whether to use data/newtype or a typeclass. In my particular case it turned out that no typeclass was required. Additionally Tom Ellis gave me a brilliant advice, what to do when in doubt: The simplest way of answering this which is mostly correct is: use data I know that typeclasses can make a few things a bit prettier, but not much AFIK. It

typeclasses是必不可少的?

我曾经问过一个关于haskell初学者的问题,不管是使用data / newtype还是一个typeclass。 在我的特殊情况下,事实证明不需要类型类。 此外汤姆埃利斯给了我一个明智的建议,如果有疑问应该怎么做: 回答这个问题最简单的方法是: 使用数据 我知道typeclasses可以使一些事情变得更漂亮,但AFIK并不多。 这也让我觉得typeclasses主要用于脑干的东西,wheras更新的东西,新的typeclasses几乎没有被引入,一切都是用数据/新

Even more generalized newtype deriving

Newtypes are often used to change the behavior of certain types when used in certain class contexts. For example, one would use the Data.Monoid.All wrapper to change the behavior of Bool when used as a Monoid . I'm currently writing such a newtype wrapper that would apply to a large range of different types. The wrapper is supposed to change the behavior of one specific class instance. I

更广泛的新型派生

当在特定的类上下文中使用时,Newtypes通常用于改变某些类型的行为。 例如,可以使用Data.Monoid.All包装来改变Bool在用作Monoid时的行为。 我目前正在撰写这样一种适用于大量不同类型的新型包装。 包装应该改变一个特定类实例的行为。 它可能看起来像这样: newtype Wrapper a = Wrapper a instance Special a => Special (Wrapper a) where -- ... 但是,添加这个包装器通常会改变包装类型的可用性。 例如,如果

Is there a way to define an existentially quantified newtype in GHC Haskell?

Is it possible in (GHC) Haskell to define an existentially-quantified newtype? I understand that if type classes are involved it can't be done in a dictionary-passing implementation, but for my purposes type-classes are not needed. What I'd really like to define is this: newtype Key t where Key :: t a -> Key t But GHC does not seem to like it. Currently I'm using data Key t wh

有没有办法在GHC Haskell中定义一种存在量化的新类型?

(GHC)Haskell有可能定义一种存在量化的新类型吗? 我明白,如果涉及类型类,它不能在字典传递的实现中完成,但为了我的目的,不需要类型类。 我真正想定义的是这样的: newtype Key t where Key :: t a -> Key t 但GHC似乎并不喜欢它。 目前我正在使用data Key t where Key :: !(ta) -> Key t 。 有没有什么办法(可能只是使用-funbox-strict-fields ?)来定义一个与上面的newtype版本具有相同语义和开销的类型?

Understanding Haskell's RankNTypes

While working my way through GHC extensions, I came across RankNTypes at the School of Haskell, which had the following example: main = print $ rankN (+1) rankN :: (forall n. Num n => n -> n) -> (Int, Double) rankN f = (f 1, f 1.0) The article offered an alternative type for rankN : rankN :: forall n. Num n => (n -> n) -> (Int, Double) The explanation of the difference is

了解Haskell的RankNTypes

在通过GHC扩展工作的过程中,我遇到了哈斯克尔学院的RankNTypes ,它有以下例子: main = print $ rankN (+1) rankN :: (forall n. Num n => n -> n) -> (Int, Double) rankN f = (f 1, f 1.0) 该文章为rankN提供了另一种类型: rankN :: forall n. Num n => (n -> n) -> (Int, Double) 对不同点的解释是:“后一个签名需要从n到n的函数,对于某些Num n;前一个签名需要从n到n的函数,用于每个Num n。”

Haskell, GHC 8: dynamically load/import module

I need to have something like -- Main.hs module Main where main :: IO () main = do <import Plugin> print Plugin.computation With a Plugin like -- Plugin.hs module Plugin where computation :: Int computation = 4 However, I need the plugin to be compiled alongside the main application. They need to be deployed together. Only the import (not the compilation) of the module should ha

Haskell,GHC 8:动态加载/导入模块

我需要有类似的东西 -- Main.hs module Main where main :: IO () main = do <import Plugin> print Plugin.computation 带有一个插件 -- Plugin.hs module Plugin where computation :: Int computation = 4 但是,我需要将该插件与主应用程序一起编译。 他们需要一起部署。 只有模块的导入(不是编译)应该动态发生。 我发现动态加载编译好的Haskell模块--HGHC 7.6,GHC 8.0.2工作得很好,除了在执行应用

get the renamed (with fully qualified import) haskell AST using ghc api

I can get the following ghc compiler working using ghc api to compile a single file.I would like to get the renamed AST of haskell source (AST with all function calls fully qualified) ghcmake = defaultErrorHandler defaultFatalMessager defaultFlushOut $ do runGhc (Just GP.libdir) $ do dflags <- getSessionDynFlags setSessionDynFlags dflags target <- guessTarget targetFile Nothi

使用ghc api获取重命名的(具有完全限定的导入)haskell AST

我可以得到下面的ghc编译器使用ghc api来编译单个文件。我想获得haskell源代码的AST(所有函数调用完全限定)的AST, ghcmake = defaultErrorHandler defaultFatalMessager defaultFlushOut $ do runGhc (Just GP.libdir) $ do dflags <- getSessionDynFlags setSessionDynFlags dflags target <- guessTarget targetFile Nothing setTargets [target] load LoadAllTargets 从这个简单的演示中,

Using GHC API to compile Haskell sources to CORE and CORE to binary

The Idea Hello! I want to create a program, that will generate Haskell Core and will use GHC API to compile it further into an executable. But before I will do it I want to construct a very basic example, showing how can we just compile Haskell sources into CORE and then into the binary file. The problem I have read a lot of documentation and tried many methods from GHC Api, but for now w

使用GHC API将Haskell源代码编译为CORE和CORE

理念 你好! 我想创建一个程序,它将生成Haskell Core,并使用GHC API将其进一步编译为可执行文件。 但在我做之前,我想构建一个非常基本的例子,展示如何将Haskell源代码编译到CORE中,然后编译成二进制文件。 问题 我已经阅读了很多文档,并尝试了很多来自GHC Api的方法,但现在没有成功。 我从官方GHC Api入门开始并成功编译了这些示例。 实例显示下列函数的用法: parseModule , typecheckModule , desugarModul