ghc

Laziness and tail recursion in Haskell, why is this crashing?

孤街浪徒 提交于 2019-12-29 03:10:37
问题 I have this fairly simple function to compute the mean of elements of a big list, using two accumulators to hold the sum so far and the count so far: mean = go 0 0 where go s l [] = s / fromIntegral l go s l (x:xs) = go (s+x) (l+1) xs main = do putStrLn (show (mean [0..10000000])) Now, in a strict language, this would be tail-recursive, and there would be no problem. However, as Haskell is lazy, my googling has led me to understand that (s+x) and (l+1) will be passed down the recursion as

Curious about the HashTable performance issues

一曲冷凌霜 提交于 2019-12-29 02:45:23
问题 I read that hash tables in Haskell had performance issues (on the Haskell-Cafe in 2006 and Flying Frog Consultancy's blog in 2009), and since I like Haskell it worried me. That was a year ago, what is the status now (June 2010)? Has the "hash table problem" been fixed in GHC? 回答1: The problem was that the garbage collector is required to traverse mutable arrays of pointers ("boxed arrays") looking for pointers to data that might be ready to deallocate. Boxed, mutable arrays are the main

Is the gdiff library obsolete?

一笑奈何 提交于 2019-12-24 15:06:05
问题 I just had a closer look at the gdiff library and wondered if some of the work there could be shifted into GHC Generics and/or Typeables. To make this a solid question: could the library be improved and deflated by utilizing features available in current ghc versions? 来源: https://stackoverflow.com/questions/28707547/is-the-gdiff-library-obsolete

“Could not deduce” error on multi-parameter type class

与世无争的帅哥 提交于 2019-12-24 05:45:35
问题 I have this code: {-# LANGUAGE MultiParamTypeClasses #-} import System.Random (RandomGen(..)) class RandomGen gen => Shadow gen light where shadowRay :: gen -> light -> Float eval :: light -> Float and I get that error: [1 of 1] Compiling Main ( problem.hs, problem.o ) problem.hs:6:5: error: * Could not deduce (Shadow gen0 light) from the context: Shadow gen light bound by the type signature for: eval :: Shadow gen light => light -> float -> float at problem.hs:6:5-40 The type variable `gen0'

GHC Generics: How to write an implementation of (:+:) that converts sum types from/to integers?

醉酒当歌 提交于 2019-12-24 02:57:08
问题 I would like to write an implementation of instance (GMySerialize a, GMySerialize b) => GMySerialize (a :+: b) Where GMySerialize is defined as: class GMySerialize f where gtoMyS :: f a -> MySerialize gfromMyS :: MySerialize -> Maybe (f a) That will, for any sum type consisting solely of nullary data constructors (such as data MyType = A | B | C | D | E | f ), convert it to and from MySerializeInt , where MySerializeInt is a constructor for MySerialize that takes one int parameter. I started

What is the format of GHC hp profile?

天大地大妈咪最大 提交于 2019-12-24 00:48:47
问题 I have a program that exhausts memory. If I were to trust hp2pretty , this program barely uses 1% of the heap at peak. So, I'd like to verify the contents of "suspect" .hp lines like: (39987)listDirectoryCerts.getDirContents/listDirector... 179632 How else can I determine what is consuming all my heap? 来源: https://stackoverflow.com/questions/47127085/what-is-the-format-of-ghc-hp-profile

Associated data families and overlapping instances

余生长醉 提交于 2019-12-23 16:49:25
问题 I'd like a 'generic' map data structure that can be efficiently specialized by providing custom instances, much like in the GHC manual section on type families. {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} module MapKey where import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map class MapKey k where data MMap k :: * -> * instance {-# OVERLAPPING #-} MapKey () where newtype MMap () v = UnitMap (Maybe v) instance {-#

System.Directory.getDirectoryContents unicode support

心不动则不痛 提交于 2019-12-23 12:53:33
问题 The following code prints something like °Ð½Ð´Ð¸Ñ-ÐÑÐ¿Ð°Ð½Ð¸Ñ getDirectoryContents "path/to/directory/that/contains/files/with/nonASCII/names" >>= mapM_ putStrLn Looks like it is a ghc bug and it is fixed already in repository. But what to do until everybody upgrade ghc? The last time I encountered such the problem (it was few years ago, btw), I used utf8-string package to convert strings, but I don't remember how I did it, and ghc unicode support was changed visibly last years. So, what is

Forked IORef reader function seems to stall main thread

最后都变了- 提交于 2019-12-23 12:24:09
问题 I was doing some experiments with concurrency and memory visibility and ran into this strange behavior (see comments inline): module Main where import Data.IORef import Control.Concurrent import System.CPUTime import System.IO main = do hSetBuffering stdout NoBuffering r <- newIORef False putStrLn "forking..." -- PRINTED forkIO $ f r threadDelay 1000000 putStrLn "writeIORef" -- NEVER PRINTED writeIORef r True threadDelay maxBound f :: IORef Bool -> IO () f r = readIORef r >>= \b-> if b then

Type synonyms “not in scope” when using Template Haskell

对着背影说爱祢 提交于 2019-12-23 10:49:31
问题 I am getting a strange error about a data type being "not in scope" when using Template Haskell. Here is my Main.hs file: {-# LANGUAGE TemplateHaskell #-} module Main where import Control.Lens import Data.Aeson import Data.Aeson.TH type Foo = Bar data Baz = Baz $(deriveJSON defaultOptions ''Baz) -- $(makeLenses ''Baz) data Bar = Bar main :: IO () main = print "hello" When trying to compile it, I get the following error: test-0.1.0.0: configure Configuring test-0.1.0.0... test-0.1.0.0: build