ghc

Are Functor instances unique?

天涯浪子 提交于 2019-11-27 21:16:21
I was wondering to what extent Functor instances in Haskell are determined (uniquely) by the functor laws. Since ghc can derive Functor instances for at least "run-of-the-mill" data types, it seems that they must be unique at least in a wide variety of cases. For convenience, the Functor definition and functor laws are: class Functor f where fmap :: (a -> b) -> f a -> f b fmap id = id fmap (g . h) = (fmap g) . (fmap h) Questions: Can one derive the definition of map starting from the assumption that it is a Functor instance for data List a = Nil | Cons a (List a) ? If so, what assumptions have

Extracting the exponent and mantissa of a Javascript Number

放肆的年华 提交于 2019-11-27 20:12:44
Is there a reasonably fast way to extract the exponent and mantissa from a Number in Javascript? AFAIK there's no way to get at the bits behind a Number in Javascript, which makes it seem to me that I'm looking at a factorization problem: finding m and n such that 2^n * m = k for a given k . Since integer factorization is in NP, I can only assume that this would be a fairly hard problem. I'm implementing a GHC plugin for generating Javascript and need to implement the decodeFloat_Int# and decodeDouble_2Int# primitive operations ; I guess I could just rewrite the parts of the base library that

Statically link GMP to an Haskell application using GHC (+ LLVM)

假如想象 提交于 2019-11-27 20:12:08
How can I drop dynamic dependency on libgmp and go from this: linux-vdso.so.1 => (0x00007fffdccb1000) libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007fb01afc1000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb01acc7000) librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fb01aabe000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb01a8ba000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb01a69d000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb01a2df000) /lib64/ld-linux-x86-64.so.2 (0x00007fb01b249000) to this (currently desired)

Why can I not make String an instance of a typeclass?

半世苍凉 提交于 2019-11-27 19:50:23
问题 Given : data Foo = FooString String … class Fooable a where --(is this a good way to name this?) toFoo :: a -> Foo I want to make String an instance of Fooable : instance Fooable String where toFoo = FooString GHC then complains: Illegal instance declaration for `Fooable String' (All instance types must be of the form (T t1 ... tn) where T is not a synonym. Use -XTypeSynonymInstances if you want to disable this.) In the instance declaration for `Fooable String' If instead I use [Char] :

How to run a haskell file in interpreted mode

霸气de小男生 提交于 2019-11-27 18:04:55
I've been told you can interpret haskell files (which I assume means they will work like Ruby/Python/Perl). Can't find the command line option on ghc to do this, though. It always wants to compile my file. Took a look at ghci as well, but it always dumps me into a repl. I'm basically wanting to just do ghc -i MyFile.hs (where -i is a made up flag that I'm pretending correllates to interpreted mode) and have it execute so that I can get quick feedback while I'm trying out ideas and learning. $ runhaskell MyFile.hs Alternatively, runghc (they're the same thing). ghci MyFile.hs will also start an

Under what circumstances could Common Subexpression Elimination affect the laziness of a Haskell program?

旧时模样 提交于 2019-11-27 17:58:27
问题 From wiki.haskell.org: First of all, common subexpression elimination (CSE) means that if an expression appears in several places, the code is rearranged so that the value of that expression is computed only once. For example: foo x = (bar x) * (bar x) might be transformed into foo x = let x' = bar x in x' * x' thus, the bar function is only called once. (And if bar is a particularly expensive function, this might save quite a lot of work.) GHC doesn't actually perform CSE as often as you

How does GHC/Haskell decide what character encoding it's going to decode/encode from/to?

梦想与她 提交于 2019-11-27 17:51:50
问题 It seems that GHC is at least inconsistent in the character encoding it decides to decode from. Consider a file, omatase-shimashita.txt , with the following content, encoded in UTF-8: お待たせしました readFile seems to read this in properly... Prelude> content <- readFile "/home/chris/Desktop/omatase-shimashita.txt" Prelude> length content 8 Prelude> putStrLn content お待たせしました However, if I write a simple "echo" server, it does not decode with a default of UTF-8. Consider the following code that

Is there a way to limit the memory, ghci can have?

血红的双手。 提交于 2019-11-27 17:45:28
问题 I'm used to debug my code using ghci. Often, something like this happens (not so obvious, of course): ghci> let f@(_:x) = 0:1:zipWith(+)f x ghci> length f Then, nothing happens for some time, and if I don't react fast enough, ghci has eaten maybe 2 GB of RAM, causing my system to freeze. If it's too late, the only way to solve this problem is [ALT] + [PRINT] + [K]. My question: Is there an easy way to limit the memory, which can be consumed by ghci to, let's say 1 GB? If limit is exceed, the

GHC rewrite rule specialising a function for a type class

余生长醉 提交于 2019-11-27 17:41:01
问题 Using the GHC RULES pragma, it is possible to specialise a polymorphic function for specific types. Example from the Haskell report: genericLookup :: Ord a => Table a b -> a -> b intLookup :: Table Int b -> Int -> b {-# RULES "genericLookup/Int" genericLookup = intLookup #-} This would make GHC use intLookup on an integer-indexed table and the generic version otherwise, where intLookup would probably be more efficient. I would like to accomplish something similar, using functions like the

GHC's RTS options for garbage collection

独自空忆成欢 提交于 2019-11-27 17:01:42
I have a Haskell program which processes a text file and builds a Map (with several million elements). The whole thing can run for 2-3 minutes. I found that tweaking the -H and -A options makes a big difference in running time. There is documentation about this functionality of the RTS, but it's a hard read for me since I don't know the algorithms and terms from GC theory. I'm looking for a less technical explanation, preferably specific to Haskell/GHC. Are there any references about choosing sensible values for these options? EDIT: That's the code, it builds a trie for a given list of words.