ghc

Future of roles for GADT-like type variables?

泄露秘密 提交于 2019-12-03 08:42:36
问题 A question from yesterday had a definition of HList (from the HList package) that uses data families. Basically: data family HList (l :: [*]) data instance HList '[] = HNil newtype instance HList (x ': xs) = HCons1 (x, HList xs) pattern HCons x xs = HCons1 (x, xs) instead of the usual (IMO more elegant and intuitive) GADT definition data HList (l :: [*]) where HNil :: HList '[] HCons :: x -> HList xs -> HList (x ': xs) This is because the data family version lets us coerce (we only get to

Scoped type variables require explicit foralls. Why?

房东的猫 提交于 2019-12-03 07:32:51
问题 If you want to use GHC's lexically scoped type variables, you also have to use explicit universal quantification. That is, you have to add forall declarations to your functions' type signatures: {-# LANGUAGE ExplicitForAll, ScopedTypeVariables #-} f :: forall a . [a] -> [a] -- The `forall` is required here ... f (x:xs) = xs ++ [x :: a] -- ... to relate this `a` to the ones above. Does this actually have anything to do with quantification, or did the extension writers just coopt the forall

Multiple folds in one pass using generic tuple function

末鹿安然 提交于 2019-12-03 06:52:26
How can I write a function which takes a tuple of functions of type ai -> b -> ai and returns a function which takes a tuple of elements of type ai , one element of type b , and combines each of the elements into a new tuple of ai : That is the signature should be like f :: (a1 -> b -> a1, a2 -> b -> a2, ... , an -> b -> an) -> (a1, a2, ... , an) -> b -> (a1, a2, ... , an) Such that: f (min, max, (+), (*)) (1,2,3,4) 5 = (1, 5, 8, 20) The point of this is so I can write: foldlMult' t = foldl' (f t) And then do something like: foldlMult' (min, max, (+), (*)) (head x, head x, 0, 0) x to do

Why does Haskell's “do nothing” function, id, consume tons of memory?

可紊 提交于 2019-12-03 06:08:25
问题 Haskell has an identity function which returns the input unchanged. The definition is simple: id :: a -> a id x = x So for fun, this should output 8 : f = id id id id id id id id id id id id id id id id id id id id id id id id id id id main = print $ f 8 After a few seconds (and about 2 gb of memory according to Task Manager), compilation fails with ghc: out of memory . Similarly, the interpreter says ghci: out of memory . Since id is a pretty simple function, I wouldn't expect it to be a

Can I provide the type-checker with proofs about inductive naturals in GHC 7.6?

…衆ロ難τιáo~ 提交于 2019-12-03 05:53:13
GHC 7.6.1 comes with new features for programming at the type level, including datatype promotion . Taking the example about type-level naturals and vectors from there, I'd like to be able to write functions on vectors that rely on basic laws of arithmetic. Unfortunately, even though the laws I want are typically easy to prove on inductive naturals by case analysis and induction, I'm doubt I can convince the type-checker of this. As a simple example, type-checking the naive reverse function below requires a proof that n + Su Ze ~ Su n . Is there any way I can supply that proof, or am I really

Rewriting as a practical optimization technique in GHC: Is it really needed?

心不动则不痛 提交于 2019-12-03 05:39:48
问题 I was reading the paper authored by Simon Peyton Jones, et al. named “Playing by the Rules: Rewriting as a practical optimization technique in GHC”. In the second section, namely “The basic idea” they write: Consider the familiar map function, that applies a function to each element of a list. Written in Haskell, map looks like this: map f [] = [] map f (x:xs) = f x : map f xs Now suppose that the compiler encounters the following call of map : map f (map g xs) We know that this expression is

Undefined at the type level

你。 提交于 2019-12-03 05:34:31
Often when I'm playing with Haskell code, I stub things out with a type annotation and undefined . foo :: String -> Int foo = undefined Is there a type-level "undefined" that I could use in a similar way? (Ideally, in conjunction with a kind annotation) type Foo :: * -> * type Foo = Undefined Further thought on the same thread: is there a way for me to stub out typeclass instances for types created this way? An even easier way than the following theoretical way? instance Monad Foo where return = undefined (>>=) = undefined You can use EmptyDataDecls to stub out a type, and with KindSignatures

How to prevent common sub-expression elimination (CSE) with GHC

你说的曾经没有我的故事 提交于 2019-12-03 05:29:13
Given the program: import Debug.Trace main = print $ trace "hit" 1 + trace "hit" 1 If I compile with ghc -O (7.0.1 or higher) I get the output: hit 2 i.e. GHC has used common sub-expression elimination (CSE) to rewrite my program as: main = print $ let x = trace "hit" 1 in x + x If I compile with -fno-cse then I see hit appearing twice. Is it possible to avoid CSE by modifying the program? Is there any sub-expression e for which I can guarantee e + e will not be CSE'd? I know about lazy , but can't find anything designed to inhibit CSE. The background of this question is the cmdargs library,

Are GHC's Type Famlies An Example of System F-omega?

て烟熏妆下的殇ゞ 提交于 2019-12-03 05:11:31
问题 I'm reading up about the Lambda-Cube, and I'm particularly interested in System F-omega, which allows for "type operators" i.e. types depending on types. This sounds a lot like GHC's type families. For example type family Foo a type instance Foo Int = Int type instance Foo Float = ... ... where the actual type depends on the type parameter a . Am I right in thinking that type families are an example of the type operators ala system F-omega? Or am I out in left field? 回答1: System F-omega

What is the relationship between ghc-pkg and cabal?

六月ゝ 毕业季﹏ 提交于 2019-12-03 04:46:02
问题 With respect to how packages are created, installed and used in Haskell, what is the relationship between ghc-pkg and cabal? What are their roles - when would you use one, over the other, or use both? Are they complementary tools, competitive tools, or simply tools that do different jobs? 回答1: Graphically, the dependencies are: Packages GHC can use | Are registered with "ghc-pkg register" | And (almost always) built with Cabal | With build dependencies resolved by cabal-install | From Hackage