The two Haskell functions below seems to differ only by whether the index variable is implicit or explicit but the difference in performance is by two orders of magnitude. This function takes about 0.03 seconds to calculate mfib 30: let mfib = (map fib [0..] !!) where fib 0 = 0 fib 1 = 1 fib x = mfib (x-1) + mfib (x-2) This function takes about 3 seconds for mfib 30: let mfib i
下面的两个Haskell函数似乎只是根据索引变量是隐式的还是显式的而有所不同,但性能差异是两个数量级。 此功能需要大约0.03秒来计算mfib 30: let mfib = (map fib [0..] !!) where fib 0 = 0 fib 1 = 1 fib x = mfib (x-1) + mfib (x-2) mfib 30需要大约3秒的时间: let mfib i = map fib [0..] !! i where fib 0 = 0 fib 1 = 1 fib x = mfib (x-1) + mfib (x-2) 我猜测它与GHC内联规则有关,
Because I can see it coming: this is a different question than What optimizations can GHC be expected to perform reliably? because I'm not asking for the most reliable optimizations, just the most clever/powerful. I'm specifically looking for non-intuitive optimizations that GHC makes that can have serious impacts on performance and demonstrate the power of compiler optimizations relat
因为我可以看到它的到来:这是一个与GHC预计可以可靠执行哪些优化不同的问题? 因为我不是要求最可靠的优化,而是最聪明/最强大的优化。 我专门寻找GHC所做的非直观优化,这会对性能产生严重影响,并展示与延迟评估或纯度相关的编译器优化的能力。 并直接解释如何解决这些问题。 最好的答案将有: 优化的解释以及它为什么如此聪明或强大 为什么优化可以提高性能 GHC如何识别何时可以使用此优化 实际上,优化将代码
Occasionally, a piece of code I want to write isn't legal without at least one language extension. This is particularly true when trying to implement ideas in research papers, which tend to use whichever spiffy, super-extended version of GHC was available at the time the paper was written, without making it clear which extensions are actually required. The result is that I often end up wit
偶尔,如果没有至少一种语言扩展名,我想写的一段代码是不合法的。 当试图在研究论文中实现想法时尤其如此,在撰写论文时可以使用GHC的任何漂亮的超扩展版本,而没有明确说明哪些扩展实际需要。 结果是,我经常在我的.hs文件的顶部得到类似这样的东西: {-# LANGUAGE TypeFamilies , MultiParamTypeClasses , FunctionalDependencies , FlexibleContexts , FlexibleInstances
So, purely functional languages have their own class of potentials due to the clear separation between pure and impure code. I have seen several features that are somewhat simpler to implement in Haskell like Nested Data Parallelism or Stream Fusion. My question is, what are other improvements/optimizations that are more or less unique to Haskell in terms of feasibility/simplicity but not yet
因此,由于纯代码和不纯代码之间的明确分离,纯功能语言有其自身的潜能。 我看到了几个在Haskell中实现起来比较简单的特性,比如嵌套数据并行或者流融合。 我的问题是,在可行性/简单性方面,Haskell或多或少具有独特性但尚未实现的其他改进/优化是什么? (我主要关心GHC,但也喜欢听到其他人) 我希望在GHC中看到的一种优化是超级编译。 然而,在GHC的不远的将来,这似乎不大可能,因为它是整个程序的优化,GHC非常专注
I have a bunch of complicated type level functions that evaluate to things like: (If (EqNat n 2) 1 (If (EqNat n 1) 2 (If (EqNat n 0) 3 0))) Now obviously in this case this expression is a KnownNat . More generally we may say: forall (c :: * -> Constraint) (p :: Bool) a b . (c a, c b) => c (If p a b) Is there a way to teach GHC to infer this? Edit: @chi noted that in some
我有一堆复杂的类型级别的函数,其计算如下: (If (EqNat n 2) 1 (If (EqNat n 1) 2 (If (EqNat n 0) 3 0))) 现在显然在这种情况下,这个表达式是一个KnownNat 。 更一般地说,我们可以说: forall (c :: * -> Constraint) (p :: Bool) a b . (c a, c b) => c (If p a b) 有没有办法教GHC推断这一点? 编辑:@chi指出,在某些情况下,这是可以与GADTs解决,但我的具体情况是这样的: module M1 (C(..))
I'm puzzled by how the haskell compiler sometimes infers types that are less polymorphic than what I'd expect, for example when using point-free definitions. It seems like the issue is the "monomorphism restriction", which is on by default on older versions of the compiler. Consider the following haskell program: {-# LANGUAGE MonomorphismRestriction #-} import Data.List(so
我很困惑haskell编译器如何推断比我期望的多态性更少的类型,例如使用无点定义时。 看起来问题是“单态限制”,它在默认情况下在编译器的旧版本上开启。 考虑下面的haskell程序: {-# LANGUAGE MonomorphismRestriction #-} import Data.List(sortBy) plus = (+) plus' x = (+ x) sort = sortBy compare main = do print $ plus' 1.0 2.0 print $ plus 1.0 2.0 print $ sort [3, 1, 2] 如果我用ghc编译,我不会获
I have the following data type: data PValue = IV Int | BV Bool | SV String deriving (Show, Eq) I want to write a function that generates PValue from an Int, a Bool or a String like: > loadVal 3 IV 3 > loadVal True BV Bool > loadVal "Ha" SV "Ha" Since the argument to loadVal is polymorphic, I tried to create a class: class PValues v where loadVal :: v -> PValue in
我有以下数据类型: data PValue = IV Int | BV Bool | SV String deriving (Show, Eq) 我想写一个从Int,Bool或String生成PValue的函数,如下所示: > loadVal 3 IV 3 > loadVal True BV Bool > loadVal "Ha" SV "Ha" 由于loadVal的参数是多态的,我尝试创建一个类: class PValues v where loadVal :: v -> PValue instance PValues Int where loadVal v = IV v instance PValues Bool wh
In the example below, I'm trying to make foo return its "expected" polymorphic output type. The idea is that foo returns a polymorphic value and an existential type, and then bar specifies the type of the tuple to be the hidden type. (Of course this only works if the type in bar is also existential, which is true in my case.) The following example compiles: {-# LANGUAGE GADTs, Sc
在下面的例子中,我试图让foo返回它的“预期”多态输出类型。 这个想法是, foo返回一个多态值和一个存在类型,然后bar将元组的类型指定为隐藏类型。 (当然这只有在bar的类型也是存在的时才有效,在我的例子中是这样。)下面的例子编译: {-# LANGUAGE GADTs, ScopedTypeVariables #-} module Foo where import Data.Proxy import Data.Typeable data HiddenType where Hidden :: (Typeable a) => Proxy a -> Hidden
I would like to write a GHC plugin which "adds a hook" to each function. Say I want to apply a function addHook of type Ord a => (a -> b) -> a -> b to the right-hand side of each function binding, transforming foo = [RHS] into foo = addHook [RHS] This works fine if I'm only interested in adding hooks to functions of type Int -> Int , in which case I make addHook
我想写一个GHC插件,为每个函数“添加一个钩子”。 假设我想在每个函数绑定的右侧应用一个类型为Ord a => (a -> b) -> a -> b addHook函数, foo = [RHS] 成 foo = addHook [RHS] 这工作正常,如果我只是有兴趣添加钩子函数的Int -> Int ,在这种情况下,我让addHook也有类型(Int -> Int) -> Int -> Int ,并调用mkCoreApp [AddHook] [RHS]并将其绑定到我的GHC插件中的foo 。 然而,如果我想让addH
Is it possible to provide type signatures to Haskell values that contain "blanks" for the type inference algorithm to fill in? Extremely contrived example for context: m = return ('I', (("don't", "really"), "care", ["what", "this"], "type"), "is") b = isJust m This works. The use of isJust m in b constrains the type of m to be Maybe <something> , and m 's definition con
是否可以提供类型签名给Haskell值,其中包含用于填充类型推断算法的“空白”? 上下文极其人为的例子: m = return ('I', (("don't", "really"), "care", ["what", "this"], "type"), "is") b = isJust m 这工作。 使用isJust m in b将m的类型约束为Maybe <something> ,并且m的定义将m的类型约束为<something> (Char, ((String, String), String, [String], String), String) ,并且编译器将这两条信息放在一