ghc

What is a good way to debug haskell code?

纵然是瞬间 提交于 2019-12-03 04:45:47
问题 I have used the ghci debugger but would really prefer if it was somewhat integrated with a text editor to simplify the process of setting breakpoints. It should probably not strictly evaluate every visible variable but at least simplify the process of looking at the local state. I recently found the trace function which has been helpful by allowing debug printouts from otherwise hard places. 回答1: A good way to debug Haskell code is to write and test algebraic laws using QuickCheck and

How to cap memory usage of Haskell threads

我的未来我决定 提交于 2019-12-03 04:40:35
In a Haskell program compiled with GHC, is it possible to programmatically guard against excessive memory usage? That is, have it notify the program when memory usage reaches a specified limit, preferably indicating the offending thread. For example, suppose I want to write a server, hosting a scripting language interpreter, that users can connect to. It's Turing-complete, so programs could theoretically use unlimited memory or time. Suppose each client is handled with a separate thread. If a client writes an infinite loop that consumes memory very quickly, I want to ensure that the thread

Are newtypes faster than enumerations?

老子叫甜甜 提交于 2019-12-03 04:39:23
According to this article , Enumerations don't count as single-constructor types as far as GHC is concerned, so they don't benefit from unpacking when used as strict constructor fields, or strict function arguments. This is a deficiency in GHC, but it can be worked around. And instead the use of newtypes is recommended. However, I cannot verify this with the following code: {-# LANGUAGE MagicHash,BangPatterns #-} {-# OPTIONS_GHC -O2 -funbox-strict-fields -rtsopts -fllvm -optlc --x86-asm-syntax=intel #-} module Main(main,f,g) where import GHC.Base import Criterion.Main data D = A | B | C

Is there any reason not to use the INLINABLE pragma for a function?

北慕城南 提交于 2019-12-03 04:04:45
问题 The documentation states: An {-# INLINABLE f #-} pragma on a function f has the following behaviour: While INLINE says "please inline me", the INLINABLE says "feel free to inline me; use your discretion". In other words the choice is left to GHC, which uses the same rules as for pragma-free functions. Unlike INLINE, that decision is made at the call site, and will therefore be affected by the inlining threshold, optimisation level etc. Like INLINE, the INLINABLE pragma retains a copy of the

Understanding STG

 ̄綄美尐妖づ 提交于 2019-12-03 01:37:56
问题 The design of GHC is based on something called STG, which stands for "spineless, tagless G-machine". Now G-machine is apparently short for "graph reduction machine", which defines how laziness is implemented. Unevaluated thunks are stored as an expression tree, and executing the program involves reducing these down to normal form. (A tree is an acyclic graph, but Haskell's pervasive recursion means that Haskell expressions form general graphs , hence graph-reduction and not tree-reduction.)

Transitivity of Auto-Specialization in GHC

女生的网名这么多〃 提交于 2019-12-03 01:31:01
问题 From the docs for GHC 7.6: [Y]ou often don't even need the SPECIALIZE pragma in the first place. When compiling a module M, GHC's optimiser (with -O) automatically considers each top-level overloaded function declared in M, and specialises it for the different types at which it is called in M. The optimiser also considers each imported INLINABLE overloaded function, and specialises it for the different types at which it is called in M. and Moreover, given a SPECIALIZE pragma for a function f,

What are the pitfalls of using FlexibleContexts and FlexibleInstances?

隐身守侯 提交于 2019-12-03 01:03:33
Since these flexible contexts and instances aren't available in the Haskell standard, I assume there are potential problems when using them. What are they? Can they lead to some ambiguity, undecidability, overlapping instances, etc.? There is a similar question that asks only about FlexibleInstances , not FlexibleContexts , but the answer only says "that it's safe to use them". Will Ness I once stumbled upon the following. Answering this question , I first tried this code: {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FunctionalDependencies #-} class (Eq a, Show a) => Genome a where

Compiling very large constants with GHC

眉间皱痕 提交于 2019-12-02 22:11:26
Today I asked GHC to compile an 8MB Haskell source file. GHC thought about it for about 6 minutes, swallowing almost 2GB of RAM, and then finally gave up with an out-of-memory error. [As an aside, I'm glad GHC had the good sense to abort rather than floor my whole PC.] Basically I've got a program that reads a text file, does some fancy parsing, builds a data structure and then uses show to dump this into a file. Rather than include the whole parser and the source data in my final application, I'd like to include the generated data as a compile-time constant. By adding some extra stuff to the

Why does GHC think that this type variable is not injective?

流过昼夜 提交于 2019-12-02 22:06:40
I have this piece of code: {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, KindSignatures, GADTs, FlexibleInstances, FlexibleContexts #-} class Monad m => Effect p e r m | p e m -> r where fin :: p e m -> e -> m r data ErrorEff :: * -> (* -> *) -> * where CatchError :: (e -> m a) -> ErrorEff ((e -> m a) -> m a) m instance Monad m => Effect ErrorEff ((e -> m a) -> m a) a m where fin (CatchError h) = \f -> f h This doesn't compile, with this type error in the last line: Could not deduce (a1 ~ a) from the context (Monad m) [...] or from (((e -> m a) -> m a) ~ ((e1 -> m a1) -> m a1)) [

Restricting string literals to Text only

a 夏天 提交于 2019-12-02 21:46:31
I'm aware that the OverloadedStrings language pragma wraps an implicit fromString around all string literals. What I'd like to do is not actually overload strings, but merely change their meaning so that they are always turned into Text , and therefore, using a string literal as a list of characters should result in a type error. It appears to be impossible to import the IsString class without also importing the String instance for that class. Does ghc provide some way for me to restrict string literals to Text only? It's a little bit of overkill, but one solution is to combine