ghc

Why is full-laziness a default optimization?

戏子无情 提交于 2019-12-04 17:47:03
问题 Full laziness has been repeatedly demonstrated to cause space leaks. Why is full laziness on from -O onwards? I find myself unconvinced by the reasoning in SPJ's The Implementation of Functional Programming Languages. The claim is that in f = \y -> y + sqrt 4 sqrt 4 is unnecessarily repeated each time f is entered so we should float it outside the lambda. I agree in the small, but since we've seen what problems this transformation causes in the large I don't believe it is worth it. It seems

Run-time exception when attempting to print a Unicode character

旧街凉风 提交于 2019-12-04 17:08:50
问题 Char is the type for Unicode characters in Haskell, and String is simply [Char] (i.e. a list of Char items). Here is some simple code: main = putStrLn "©" -- Unicode string This code compiles fine, but I get the runtime exception when I run it in the PowerShel.exe or cmd.exe : app.exe: : commitBuffer: invalid argument (invalid character) Why does this happen? Weirdly enough, when I do the same in C#, I get no exception: Console.WriteLine("©"); In .NET, chars are Unicode too. PowerShell or cmd

Catching Control-C exception in GHC (Haskell)

有些话、适合烂在心里 提交于 2019-12-04 16:50:04
问题 I built a really simple read-eval-print-loop in Haskell that catches Control-C (UserInterrupt). However, whenever I compile and run this program, it always catches the first Control-C and always aborts on the second Control-C with exit code 130. It doesn't matter how many lines of input I give it before and between the two Control-Cs, it always happens this way. I know I must be missing something simple... please help, thanks! Note: this is with base-4 exceptions, so Control.Exception and not

Adding context to rewrite rules

牧云@^-^@ 提交于 2019-12-04 11:39:27
In the following code, I want to rewrite g . f as h when possible. There may be cases h hasn't got the instance of class, but I'd like to do the rewrite when it's possible. I'm getting an error message suggesting this is achievable but I'm not sure exactly what I need to change. Here is some sample code: {-# LANGUAGE TypeFamilies #-} main = return () data D a f :: a -> D a f = undefined type family T a class G a where g :: D (T a) -> a class H a where h :: T a -> a {-# RULES "myrule" forall x. g (f x) = h x #-} An this is the error: • Could not deduce (H a) arising from a use of ‘h’ from the

How to force ghc's profiler to step deeper into the libraries?

☆樱花仙子☆ 提交于 2019-12-04 10:38:45
I'm trying to profile my program. So I compile it with -prof and -auto-all flags and run with -P to get detailed profiling report: $ ghc --make -prof -auto-all Test.hs $ ./Test +RTS -P Here is a piece of profiling report: COST CENTRE MODULE no. entries %time %alloc main Main 266 1 0.0 0.0 run Main 273 21845 99.3 99.7 sz Main 274 21844 0.0 0.0 size Main 268 21845 0.7 0.3 It seems that run consumes all time and memory. It calls a lot of functions from various libraries, and I'm quite sure that most time is spent in one of them, but I can't figure in which one. How can I get more detailed report?

Controlling memory allocation/GC in a simulation?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 08:16:20
I'm having a bit of trouble figuring out how to reduce memory usage and GC time in a simulation running in the State monad. Presently I have to run the compiled code with +RTS -K100M to avoid stack space overflow, and the GC stats are pretty hideous (see below). Here are relevant snippets of the code. Complete, working (GHC 7.4.1) code can be found at http://hpaste.org/68527 . -- Lone algebraic data type holding the simulation configuration. data SimConfig = SimConfig { numDimensions :: !Int -- strict , numWalkers :: !Int -- strict , simArray :: IntMap [Double] -- strict spine , logP :: Seq

ConstraintKinds explained on a super simple example

♀尐吖头ヾ 提交于 2019-12-04 07:50:08
问题 What is a Constraint kind? Why would someone use it (in practice)? What is it good for? Could you give a simple code example to illustrate the answers to the previous two questions? Why is it used in this code for example? 回答1: Well, I'll mention two practical things it allows you to do: Parametrize a type by a type class constraint Write type classes that allow their instances to specify constraints that they need. Maybe it's best to illustrate this with an example. One of the classic

Should I use GHC Haskell extensions or not?

烈酒焚心 提交于 2019-12-04 07:35:25
问题 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 towards using language extensions? And if use of extensions is OK, how can I distinguish extensions

Why is Haskell (GHC) so darn fast?

こ雲淡風輕ζ 提交于 2019-12-04 07:18:06
问题 Haskell (with the GHC compiler) is a lot faster than you'd expect. Used correctly, it can get close-ish to low-level languages. (A favorite thing for Haskellers to do is to try and get within 5% of C (or even beat it, but that means you are using an inefficient C program, since GHC compiles Haskell to C).) My question is, why? Haskell is declarative and based on lambda calculus. Machine architectures are clearly imperative, being based on turing machines, roughly. Indeed, Haskell doesn't even

Export Haskell lib as DLL

自闭症网瘾萝莉.ら 提交于 2019-12-04 07:00:31
I am working with GHC version 7.6.1, following the steps in the docs for creating a DLL from a Haskell lib for using it in VBA: GHC Docs 7.6.1 # Make a DLL The files I use are the exact same ones as in the Docs. When it comes to compiling, the following to commands work nearly as expected: ghc -c Adder.hs ghc -c StartEnd.c The first command returns this: Adder.hs:7:1: Warning: the 'stdcall' calling convention is unsupported on this platform, treating as ccall When checking declaration: foreign export stdcall "adder" adder :: Int -> Int -> IO Int Which I guess should be fine... However, the