News aggregator

More concise code using phantom types

haskell-cafe - Sat, 08/08/2015 - 12:28am
Hello! Consider the following code: module Units where data Units a = U Double deriving Eq units :: Double -> a -> Units a units value _ = U value data Meters data Yards meters = undefined :: Meters yards = undefined :: Yards instance Show Meters where show _ = "meters" instance Show Yards where show _ = "yards" extractA :: Units a -> a extractA = undefined instance Show a => Show (Units a) where show u< at >(U value) = show value ++ " " ++ show $ extractA u main = (print $ units 5 yards) >> (print $ units 5 meters) Is it possible to use something instead extractA function here? For example, substitute "extractA u” with “undefined :: a”? GHC disallows it, so is there a way to explain that I only need a token with type a? Also, with highlighting on lpaste: With regards, Nikita Kartashov _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

ANN: happstack-server-tls-cryptonite

haskell-cafe - Sat, 08/08/2015 - 12:12am
Hi, I'd like to announce happstack-server-tls-cryptonite, package adding native TLS support to happstack, utilizing hs-tls and cryptonite packages by Vincent Hanquez. You can now run secure web server without using OpenSSL or other foreign libraries. This package is almost drop-in replacement for happstack-server-tls, just change Happstack.Server.SimpleHTTPS to Happstack.Server.SimpleTLS in imports. Regards, Andrey
Categories: Offsite Discussion

Writing fast Vector code.

Haskell on Reddit - Fri, 08/07/2015 - 7:20pm

dI seem to have a curse that whenever I rewrite code using lists to programs using vectors, the program runs in significantly less memory but is much slower (sample size of 3). Also my intuitions about refactoring code don't actually make code faster. For example, I also rewrote a function using a 2-d (V.Vector (U.Vector Float)) to use a 1-d (U.Vector Float) and suddenly things got really slow despite smaller cleaner code and I'm baffled.

So my question is are there any good references for writing good Vector code? I seem to suck at it.

EDIT: The code by popular demand. The original code using lists can be found here. The rewritten vector function in question is in this gist:

colorPixel1 is the first version of the function. colorPixel2 is the second version. Hopefully I have included enough explanation. I apologize in advance for the code.

EDIT2: Some of you have also asked about profiling information. Here is the profiling info on the original list implementation and vector implementation

and I didn't profile the second version because it was taking too long to run > 20 minutes. Interesting that many basicUnsafeIndex are happening because I use safeIndex (!) in the code. grep "unsafe" src/ -r returns nothing.

GC time for all versions have been ~1%.

EDIT3: After looking through the vector source code, I believe a big reason for the slowdown is I expected vector (a,b) to be represented as a and b unpacked next to each other in one vector. However it looks like from

data instance MVector s (a, b) = MV_2 {-# UNPACK #-} !Int !(MVector s a) !(MVector s b)

that it is instead represented as vector a and vector b. In retrospect this is probably why there is a O(1) bound on zip. This would absolutely destroy cache performance because I used vectors of tuples a fair bit, and whenever I access the first element I always access the second.

I guess the solution to this would be to wrap my tuples in a newtype and make it an instance of Unbox with

newtype Point = Point (Float,Float) derivingUnbox "Point" [t| Point -> (Float,Float) |] [| \(Point (x,y)) -> (x,y) |] [| \(x,y) -> Point (x,y) |]

but I'm not sure how to use derivingUnbox with anything other than (,).

btw if there is an easier way to make things an instance of Unbox I'd love to hear it because derivingUnbox really slows down compile times.

FINAL EDIT (probably): I found a stack overflow question which addresses my need by showing how to write vector instances for my datatypes to store the items sequentially :)

submitted by jura__
[link] [17 comments]
Categories: Incoming News

Serialize to Haskell

haskell-cafe - Fri, 08/07/2015 - 6:55pm
Hello! I'm wondering if it's possible to serialize some data to a format that is valid Haskell. In practice I would read a file like this: module Foo where myData :: MyData myData = MyData {name = "foo", descr = "test" ... } Reading this into a program is easily feasible with Hint, for example. Then the program would modify some data and serialize it back to: module Foo where myData :: MyData myData = MyData {name = "bar", descr = "test2" ... } In practice I think that the format should be a subset of Haskell that is not Turing-complete. A bit like JSON, which is a subset of Javascript but not Turing complete. Is it possible? Thanks, Corentin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

"cabal sdist" oddity

haskell-cafe - Fri, 08/07/2015 - 5:31pm
Cabal from HEAD started to issue the warning "Warning: Cannot run preprocessors. Run 'configure' command first." when running "cabal sdist" after a "cabal install", see e.g. Previous versions didn't do this, see e.g. Somehow the warning doesn't make sense, because the package has obviously been built before, so configuration must have happened, too. Is this a new bug in Cabal? Cheers, S. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Can't get function that should compile without issues to compile

Haskell on Reddit - Fri, 08/07/2015 - 4:29pm

I'm trying to compile the baby.hs function from LYAH but for some reason it's just not working.

When I use ghci and do :l baby.hs it gives me the output of

[1 of 1] Compiling Main ( baby.hs, interpreted ) baby.hs:1:16: Not in scope: ‘+’ Failed, modules loaded: none.

Anyone know what's up? Also earlier when I tried adding in type defs like Int, it didn't work.

Weirdly enough I can do addition just fine in the ghci (w/o trying to load in a function)

submitted by ThrowawayTartan
[link] [29 comments]
Categories: Incoming News

Follow up: safe lightweight checked exceptions

Haskell on Reddit - Fri, 08/07/2015 - 3:28am

So the thread on lightweight checked exceptions generated a good bit of interest, but there were two main concerns:

  • The use of unsafeCoerce to dynamically create a type class dictionary made /u/augustss and some other people wonder how this would work in other compilers.
  • /u/chrisdoner wondered if this could be generalized to things like MonadThrow.

Actually, as it turns out, we can address both of those concerns. We define Throws as an empty type class instead

class Throws e where type role Throws representational

(The use of roles is not essential; see below.) The definition of throwChecked is unchanged:

throwChecked :: (Exception e, Throws e) => e -> IO a throwChecked = Base.throwIO

But to catch the exception we somehow have to eliminate that Throws constraint, which we can do without unsafeCoerce as follows:

newtype Wrap e a = Wrap { unWrap :: Throws e => a } -- Catch is not exported, and the only type with a Throws instance newtype Catch a = Catch a instance Throws (Catch e) where coerceWrap :: Wrap e a -> Wrap (Catch e) a coerceWrap = coerce unthrow :: proxy e -> (Throws e => a) -> a unthrow _ = unWrap . coerceWrap . Wrap

At this point catch is just

catchChecked :: forall a e. Exception e => (Throws e => IO a) -> (e -> IO a) -> IO a catchChecked act = Base.catch (unthrow (Proxy :: Proxy e) act)

Two important points to note:

  • In compilers without support for roles/coerce, we can still use unsafeCoerce, BUT it would now be using unsafeCoerce between something of type Throw e => a and Throw (Catch e) => a, which doesn't require any dubious conversion between the runtime representation of dictionaries and other arguments. I would suspect that in all compilers this conversion will be just fine.

  • Since the Throws class now no longer mentions IO (is empty, in fact), we can very easily define variations using the exceptions or lifted-base libraries.

See CheckedRevisited.hs on GitHub for a version that has been tested across ghc 7.2, 7.4, 7.6, 7.8 and 7.10, and also provides definitions for exceptions and lifted-base.

submitted by edsko
[link] [8 comments]
Categories: Incoming News

FGL: The cost and propoer way of changing the labelat a node.

haskell-cafe - Thu, 08/06/2015 - 11:29pm
Is changing the label at a node O(N)? The only way I can think of to do it is with a map, like the following -- which works, but seems like its speed must be linear in the number of nodes of the graph: 'b', adjOut); _ -> c} mkGraph [(1,'a')] [(1,1,())] mkGraph [(1,'b')] [(1,1,())] Is that in fact the right way? Am I somehow missing the point of FGL if I have to do a lot of that? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Library for package preprocessing

haskell-cafe - Thu, 08/06/2015 - 9:33pm
Hi all, I'm writing a tool to analyze source code of a package, which uses haskell-src-extra. As copied from hackage, libraries may contain some extra stuff (for example, cpp directives). What is the modern way to preprocess the package (configuring for the environment, running the cpp ecc) to obtain code which is guaranteed to be parsed by haskell-src-extra? Ideally I'd like to leave most of the logic for this part outside of the package. Could you point me towards some libraries doing a similar thing? Best regards
Categories: Offsite Discussion

simultaneous ghc versions

glasgow-user - Fri, 07/31/2015 - 7:10pm
The recent release of ghc 7.10.2 reminded me of something I meant to ask about a long time ago. Most of the binaries ghc installs are versioned (x linked to x-7.10.2), with some exceptions (hpc and hsc2hs). Shouldn't they all be versioned? Also, 'haddock' is inconsistent with all the rest, in that it's haddock linked to haddock-ghc-7.10.2. I've long used a few shell scripts (recently upgraded to python) to manage ghc installs. A 'set' which creates symlinks to make a particular version current, and an 'rm' to remove all traces of a version. But due to the inconsistency, I have to remember to run "fix" first, which moves the unversioned binaries to versioned names. As an aside, I have three scripts I use all the time: set version, remove version, and remove library. Come to think of it, shouldn't ghc include this, instead of everyone creating their own shell scripts by hand?
Categories: Offsite Discussion

broken source link

glasgow-user - Fri, 07/24/2015 - 8:59am
Hi, when trying to look up the original definition for Data.List.transpose in I found that the source link does not work. Could this be fixed? Or should I look elsewhere for the sources? Cheers Christian P.S. my looking up transpose was inspired by
Categories: Offsite Discussion

New gtk2hs 0.12.4 release

gtk2hs - Wed, 11/21/2012 - 12:56pm

Thanks to John Lato and Duncan Coutts for the latest bugfix release! The latest packages should be buildable on GHC 7.6, and the cairo package should behave a bit nicer in ghci on Windows. Thanks to all!


Categories: Incoming News