ghc

Extent of GHC's optimization

六月ゝ 毕业季﹏ 提交于 2019-12-05 15:17:17
问题 I am not very familiar with the degree that Haskell/GHC can optimize code. Below I have a pretty "brute-force" (in the declarative sense) implementation of the n queens problem. I know it can be written more efficiently, but thats not my question. Its that this got me thinking about the GHC optimizations capabilities and limits. I have expressed it in what I consider a pretty straightforward declarative sense. Filter permutations of [1..n] that fulfill the predicate For all indices i,j s.t j

Going through the source code for the prelude brings up weirdness

霸气de小男生 提交于 2019-12-05 14:36:06
问题 I was looking for the definition of seq and came across this weirdness. Why do all these functions have the same/similar definitions? seq :: a -> b -> b seq = let x = x in x inline :: a -> a inline = let x = x in x lazy :: a -> a lazy = let x = x in x There are many more with this definition in the source code. What's going on? 回答1: What's going on is that these functions cannot be implemented in Haskell, but they should appear in the docs. Since haddock needs a syntactically correct (and

Replace record projection function with lenses

早过忘川 提交于 2019-12-05 14:04:39
Almost every time I make a record, I find myself adding makeLenses ''Record (from lens ) immediately afterwards, and I never end up actually using the projection functions that the record gives me. In fact, looking at what makeLenses produces (with the GHC -ddump-splices flag), it looks like not even it uses those projection functions, except to choose a name for the lenses it produces. Is there some way, either through TemplateHaskell , or through a preprocessor, or frankly any other magic, that I could get record projection functions to be straight up Van Laarhoven lenses instead? To be

why pipes defines inner functions

試著忘記壹切 提交于 2019-12-05 12:47:45
I'm looking at the pipes library source code and for instance in the Core module I don't understand why the author is all over the place using the pattern of defining functions like that: runEffect = go where go p = ... Or: pull = go where go a' = ... Or: reflect = go where go p = ... Is this some trick to enable some optimizations? I find it ugly, if it's some optimization trick I really wish the compiler could do it without things like that. But maybe there's another reason? GHC will only inline non-recursive functions, and only when they are "fully applied" from a syntactic point of view (i

Installed parsec in sandbox, but can't find libraries when trying to load file in ghci

浪子不回头ぞ 提交于 2019-12-05 12:37:40
tl;dr: installed library with cabal sandbox, ghci still complains that the library is missing I have a directory where I am developing some Haskell stuff. This used to work fine on another computer, with ghc 7.6, but now that I'm working on another computer with ghc 7.8.2 (I don't know if the version would matter) I get errors like this: Prelude> :l Interpreter.hs Parser.hs:9:8: Could not find module ‘Text.ParserCombinators.Parsec’ Perhaps you meant Text.ParserCombinators.ReadPrec (from base) Text.ParserCombinators.ReadP (from base) Use -v to see a list of the files searched for. when trying

Searching for rewrite rules

耗尽温柔 提交于 2019-12-05 12:30:49
Is there any way to browse or search rewrite rules? When I use flags like -ddump-rule-firings or -ddump-rule-rewrites I just get the name of the rule that fired and the rewrite that it caused, but not the actual rule itself... Ideally I'd like to see what rewrite rules are in scope via GHCi, but realistically I'd be willing to settle for just an exhaustive list of the rewrite rules present in base. Alright, still hoping for a good answer to this, but if there isn't, I went ahead and did what pdexter suggested and grep'd base for rules. Here are the rules in base 4.9. For anyone interested in

Pattern matching on rank-2 type

 ̄綄美尐妖づ 提交于 2019-12-05 12:05:50
问题 I'm trying to understand why one version of this code compiles, and one version does not. {-# LANGUAGE RankNTypes, FlexibleContexts #-} module Foo where import Data.Vector.Generic.Mutable as M import Data.Vector.Generic as V import Control.Monad.ST import Control.Monad.Primitive data DimFun v m r = DimFun {dim::Int, func :: v (PrimState m) r -> m ()} runFun1 :: (Vector v r, MVector (Mutable v) r) => (forall m . (PrimMonad m) => DimFun (Mutable v) m r) -> v r -> v r runFun1 (DimFun dim t) x |

Haskell's type inference strangeness

十年热恋 提交于 2019-12-05 09:35:43
Look at this output from ghci: Prelude> :t Data.Map.lookup Data.Map.lookup :: Ord k => k -> Data.Map.Map k a -> Maybe a Prelude> :t flip Data.Map.lookup flip Data.Map.lookup :: Ord a => Data.Map.Map a a1 -> a -> Maybe a1 Prelude> let look = flip Data.Map.lookup Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. Prelude> :t look look :: Data.Map.Map () a -> () -> Maybe a Why look 's inferred type differs from type of flip Data.Map.lookup ? To give you some context. Initially I had small program and was trying to figure out why it

Playing with DataKinds - Kind mis-match errors

你说的曾经没有我的故事 提交于 2019-12-05 09:32:10
I've been teaching myself about type-level programming and wanted to write a simple natural number addition type function. My first version which works is as follows: data Z data S n type One = S Z type Two = S (S Z) type family Plus m n :: * type instance Plus Z n = n type instance Plus (S m) n = S (Plus m n) So in GHCi I can do: ghci> :t undefined :: Plus One Two undefined :: Plus One Two :: S * (S * (S * Z)) Which works as expected. I then decided to try out the DataKinds extension by modifying the Z and S types to: data Nat = Z | S Nat And the Plus family now returns a Nat kind: type

Monomorphism restriction triggered when generic instance defined

ε祈祈猫儿з 提交于 2019-12-05 08:50:22
Consider the following: {-# LANGUAGE TypeFamilies, FlexibleContexts, GADTs, MultiParamTypeClasses #-} type family F r class (Functor t) => T t r where fromScalar :: r -> t r data Foo t r where Foo :: t (F r) -> Foo t r Scalar :: r -> Foo t r toF :: r -> F r toF = undefined convert :: (T t (F r)) => Foo t r -> Foo t r convert (Scalar c) = let fromScalar' = fromScalar in Foo $ fromScalar' $ toF c This code compiles with GHC 7.8.4. When I add a generic instance for T (which requires FlexibleInstances ): instance (Functor t, Num r) => T t r GHC complains: Could not deduce (Num (F r)) arising from