News aggregator

locale: decimal comma vs. point

libraries list - Wed, 01/21/2015 - 7:36pm
If I export a label track from Audacity the time stamps are formatted with decimal commas instead of decimal points. E.g. 1.5s is formatted as "1,5" not "1.5". I guess this is because I have German locale as default. I want to write numbers from Haskell compatible to this format and thus I want to know whether I have to format numbers with decimal commas or decimal points. However an according information or even better, a locale-aware printf seems to be missing in old-locale. :-(
Categories: Offsite Discussion

Magnus Therning: Combining inputs in conduit

Planet Haskell - Wed, 01/21/2015 - 6:00pm

The code the post on using my simplistic state machine together with conduit will happily wait forever for input, and the only way to terminate it is pressing Ctrl-C. In this series I’m writing a simple adder machine, but my actual use case for these ideas are protocols for communication (see the first post), and waiting forever isn’t such a good thing; I want my machine to time out if input doesn’t arrive in a timely fashion.

I can think of a few ways to achieve this:

  1. Use a watchdog thread that is signalled from the main thread, e.g. by a Conduit that sends a signal as each Char passes through it.
  2. As each Char is read kick off a “timeout thread” which throws an exception back to the main thread, unless the main thread kills it before the timeout expires.
  3. Run a thread that creates ticks that then can be combined with the Chars read and fed into the state machine itself.

Since this is all about state machines I’ve opted for option 3. Furthermore, since I’ve recently finished reading the excellent Parallel and Concurrent Programming in Haskell I decided to attempt writing the conduit code myself instead of using something like stm-conduit.

The idea is to write two functions:

  1. one Source to combine two Sources, and
  2. one Sink that writes its input into a TMVar.

The latter of the two is the easiest one. Given a TMVar it just awaits input, stores it in the TMVar and then calls itself:

sinkTMVar :: MonadIO m => TMVar a -> Sink a m () sinkTMVar tmv = forever $ do v <- await case v of Nothing -> return () Just v' -> liftIO (atomically $ putTMVar tmv v')

The other one is only slightly more involved:

whyTMVar :: MonadIO m => Source (ResourceT IO) a -> Source (ResourceT IO) a -> Source m a whyTMVar src1 src2 = do t1 <- liftIO newEmptyTMVarIO t2 <- liftIO newEmptyTMVarIO void $ liftIO $ async $ fstProc t1 void $ liftIO $ async $ sndProc t2 forever $ liftIO (atomically $ takeTMVar t1 `orElse` takeTMVar t2) >>= C.yield where fstProc t = runResourceT $ src1 $$ sinkTMVar t sndProc t = runResourceT $ src2 $$ sinkTMVar t

Rather short and sweet I think. However, there are a few things that I’m not completely sure of yet.

forkIO vs. async vs. resourceForkIO
There is a choice between at least three functions when it comes to creating the threads and I haven’t looked into which one is better to use. AFAIU there may be issues around exception handling and with resources. For now I’ve gone with async for no particular reason at all.
Using TMVar
In this example the input arrives rather slowly, which means having room for a single piece at a time is enough. If the use case changes and input arrives quicker then this decision has to be revisited. I’d probably choose to use stm-conduit in that case since it uses TMChan internally.
Combining only two Sources
Again, this use case doesn’t call for more than two Sources, at least at this point. If the need arises for using more Sources I’ll switch to stm-conduit since it already defines a function to combine a list of Sources.

The next step will be to modify the conduit process and the state machine.

Categories: Offsite Blogs

How to remove a cabal package from the local system?

glasgow-user - Wed, 01/21/2015 - 5:36pm
Hi! I have installed/registered a new version of a package with cabal by accident. How can I remove it again? There is something in ~/.cabal/packages/, but the defective version isn't included. bye V.W.
Categories: Offsite Discussion

CFP: UNIF 2015

General haskell list - Wed, 01/21/2015 - 4:35pm
========================================================== Call for Papers UNIF 2015 The 29th International Workshop on Unification June 28, 2015. Warsaw, Poland ********* part of the Federated Conference on Rewriting, Deduction, and Programming (RDP'15) ========================================================== UNIF 2015 is the 29th event in a series of international meetings devoted to unification theory and its applications. Unification is concerned with the problem of identifying terms, finding solutions for equations, or making formulas equivalent. It is a fundamental process used in a number of fields of computer science, including automated reasoning, term rewriting, logic programming, natural language processing, program analysis, types, etc. The International Workshop on Unification (UNIF) is a yearly forum for researchers in unificatio
Categories: Incoming News

AVoCS 2015: First Call for Papers

General haskell list - Wed, 01/21/2015 - 2:48pm
--------------------------------------------------------------------- FIRST CALL FOR PAPERS The 15th International Workshop on Automated Verification of Critical Systems AVoCS 2015 1-4 September 2015, Edinburgh, UK avocs2015< at > --------------------------------------------------------------------- IMPORTANT DATES Submission of abstract (full papers): 5th June 2015 Submission of full papers: 12th June 2015 Notification (full papers): 14th July 2015 Submission of research idea papers: 7th August 2015 Notification (research idea): 14th August 2015 Early registration: 18th August 2015 Submissions of final versions: 21st August 2015 INVITED SPEAKERS Colin O'Halloran (D-RisQ & the University of Oxford) Don Sannella (Contemplate & the University of Edinburgh) SPONSORS Formal Methods Europe (FME) The Scottish Informatics & Compute
Categories: Incoming News

Any fast octree in Haskell with a ray transversal feature?

Haskell on Reddit - Wed, 01/21/2015 - 1:20pm

Obviously for a ray-tracer.

submitted by SrPeixinho
[link] [10 comments]
Categories: Incoming News

mightybyte: LTMT Part 3: The Monad Cookbook

Planet Haskell - Wed, 01/21/2015 - 11:09am

The previous two posts in my Less Traveled Monad Tutorial series have not had much in the way of directly practical content. In other words, if you only read those posts and nothing else about monads, you probably wouldn't be able to use monads in real code. This was intentional because I felt that the practical stuff (like do notation) had adequate treatment in other resources. In this post I'm still not going to talk about the details of do notation--you should definitely read about that elsewhere--but I am going to talk about some of the most common things I have seen beginners struggle with and give you cookbook-style patterns that you can use to solve these issues.

Problem: Getting at the pure value inside the monad

This is perhaps the most common problem for Haskell newcomers. It usually manifests itself as something like this:

main = do lineList <- lines $ readFile "myfile.txt" -- ... do something with lineList here

That code generates the following error from GHC:

Couldn't match type `IO String' with `[Char]' Expected type: String Actual type: IO String In the return type of a call of `readFile'

Many newcomers seem puzzled by this error message, but it tells you EXACTLY what the problem is. The return type of readFile has type IO String, but the thing that is expected in that spot is a String. (Note: String is a synonym for [Char].) The problem is, this isn't very helpful. You could understand that error completely and still not know how to solve the problem. First, let's look at the types involved.

readFile :: FilePath -> IO String lines :: String -> [String]

Both of these functions are defined in Prelude. These two type signatures show the problem very clearly. readFile returns an IO String, but the lines function is expecting a String as its first argument. IO String != String. Somehow we need to extract the String out of the IO in order to pass it to the lines function. This is exactly what do notation was designed to help you with.

Solution #1 main :: IO () main = do contents <- readFile "myfile.txt" let lineList = lines contents -- ... do something with lineList here

This solution demonstrates two things about do notation. First, the left arrow lets you pull things out of the monad. Second, if you're not pulling something out of a monad, use "let foo =". One metaphor that might help you remember this is to think of "IO String" as a computation in the IO monad that returns a String. A do block lets you run these computations and assign names to the resulting pure values.

Solution #2

We could also attack the problem a different way. Instead of pulling the result of readFile out of the monad, we can lift the lines function into the monad. The function we use to do that is called liftM.

liftM :: Monad m => (a -> b) -> m a -> m b liftM :: Monad m => (a -> b) -> (m a -> m b)

The associativity of the -> operator is such that these two type signatures are equivalent. If you've ever heard Haskell people saying that all functions are single argument functions, this is what they are talking about. You can think of liftM as a function that takes one argument, a function (a -> b), and returns another function, a function (m a -> m b). When you think about it this way, you see that the liftM function converts a function of pure values into a function of monadic values. This is exactly what we were looking for.

main :: IO () main = do lineList <- liftM lines (readFile "myfile.txt") -- ... do something with lineList here

This is more concise than our previous solution, so in this simple example it is probably what we would use. But if we needed to use contents in more than one place, then the first solution would be better.

Problem: Making pure values monadic

Consider the following program:

import Control.Monad import System.Environment main :: IO () main = do args <- getArgs output <- case args of [] -> "cat: must specify some files" fs -> liftM concat (mapM readFile fs) putStrLn output

This program also has an error. GHC actually gives you three errors here because there's no way for it to know exactly what you meant. But the first error is the one we're interested in.

Couldn't match type `[]' with `IO' Expected type: IO Char Actual type: [Char] In the expression: "cat: must specify some files"

Just like before, this error tells us exactly what's wrong. We're supposed to have an IO something, but we only have a String (remember, String is the same as [Char]). It's not convenient for us to get the pure result out of the readFile functions like we did before because of the structure of what we're trying to do. The two patterns in the case statement must have the same type, so that means that we need to somehow convert our String into an IO String. This is exactly what the return function is for.

Solution: return return :: a -> m a

This type signature tells us that return takes any type a as input and returns "m a". So all we have to do is use the return function.

import Control.Monad import System.Environment main :: IO () main = do args <- getArgs output <- case args of [] -> return "cat: must specify some files" fs -> liftM concat (mapM readFile fs) putStrLn output

The 'm' that the return function wraps its argument in, is determined by the context. In this case, main is in the IO monad, so that's what return uses.

Problem: Chaining multiple monadic operations import System.Environment main :: IO () main = do [from,to] <- getArgs writeFile to $ readFile from

As you probably guessed, this function also has an error. Hopefully you have an idea of what it might be. It's the same problem of needing a pure value when we actually have a monadic one. You could solve it like we did in solution #1 on the first problem (you might want to go ahead and give that a try before reading further). But this particular case has a pattern that makes a different solution work nicely. Unlike the first problem, you can't use liftM here.

Solution: bind

When we used liftM, we had a pure function lines :: String -> [String]. But here we have writeFile :: FilePath -> String -> IO (). We've already supplied the first argument, so what we actually have is writeFile to :: String -> IO (). And again, readFile returns IO String instead of the pure String that we need. To solve this we can use another function that you've probably heard about when people talk about monads...the bind function.

(=<<) :: Monad m => (a -> m b) -> m a -> m b (=<<) :: Monad m => (a -> m b) -> (m a -> m b)

Notice how the pattern here is different from the first example. In that example we had (a -> b) and we needed to convert it to (m a -> m b). Here we have (a -> m b) and we need to convert it to (m a -> m b). In other words, we're only adding an 'm' onto the 'a', which is exactly the pattern we need here. Here are the two patterns next to each other to show the correspondence.

writeFile to :: String -> IO () a -> m b

From this we see that "writeFile to" is the first argument to the =<< function. readFile from :: IO String fits perfectly as the second argument to =<<, and then the return value is the result of the writeFile. It all fits together like this:

import System.Environment main :: IO () main = do [from,to] <- getArgs writeFile to =<< readFile from

Some might point out that this third problem is really the same as the first problem. That is true, but I think it's useful to see the varying patterns laid out in this cookbook style so you can figure out what you need to use when you encounter these patterns as you're writing code. Everything I've said here can be discovered by carefully studying the Control.Monad module. There are lots of other convenience functions there that make working with monads easier. In fact, I already used one of them: mapM.

When you're first learning Haskell, I would recommend that you keep the documentation for Control.Monad close by at all times. Whenever you need to do something new involving monadic values, odds are good that there's a function in there to help you. I would not recommend spending 10 hours studying Control.Monad all at once. You'll probably be better off writing lots of code and referring to it whenever you think there should be an easier way to do what you want to do. Over time the patterns will sink in as form new connections between different concepts in your brain.

It takes effort. Some people do pick these things up more quickly than others, but I don't know anyone who just read through Control.Monad and then immediately had a working knowledge of everything in there. The patterns you're grappling with here will almost definitely be foreign to you because no other mainstream language enforces this distinction between pure values and side effecting values. But I think the payoff of being able to separate pure and impure code is well worth the effort.

Categories: Offsite Blogs

Why are record field lenses the opposite of every other lens?

Haskell on Reddit - Wed, 01/21/2015 - 4:34am

After getting into lenses a bit one thing stuck out for me [1]: all the "built-in" lenses seem to start with _ (e.g. _1, _2, _left, etc.) but with records this is exactly reversed: the actual field starts with _ but its lens does not.

Is there any reason for this? Is it under consideration to flip this for consistency?

[1] I don't like the _ convention and would have preferred something like putting an L at the end, but it's probably far too prolific to think about now. Regardless, whatever the convention is, it would be nice if it were completely consistent.

submitted by nicheComicsProject
[link] [7 comments]
Categories: Incoming News - Wed, 01/21/2015 - 1:21am
Categories: Offsite Blogs - Wed, 01/21/2015 - 1:21am
Categories: Offsite Blogs

Question about understanding type inference rules.

Haskell on Reddit - Wed, 01/21/2015 - 12:28am

I've been rereading about type inference rules lately and on thing that has always puzzled me is the difference between an implication symbol and a comma when describing a type context Gamma. For example in some cases a rule will say:

Gamma ⊢ x : T

This makes sense as the context gamma implies x has type T. But in some cases you will see, for example:

Gamma, y : S ⊢ x : T Which I understand to mean that the context Gamma with the added assumption y has type S implies x has type T. My question is why not say: Gamma ⊢ x : T, y : S

Is it because the assumption for y is being provided by the rule, meaning S is a new type variable?


submitted by biglambda
[link] [13 comments]
Categories: Incoming News

Kevin Reid (kpreid): A Visual Introduction to DSP for SDR — now live in your browser!

Planet Haskell - Tue, 01/20/2015 - 10:51pm

My interactive presentation on digital signal processing (previous post with video) is now available on the web, at! More details, source code, etc. at the site.

(P.S. I'll also be at the next meetup, which is tomorrow, January 21, but I don’t have another talk planned. (Why yes, I did procrastinate getting this site set up until a convenient semi-deadline.))

Categories: Offsite Blogs

Unit test that a particular expression *does not* compile?

Haskell on Reddit - Tue, 01/20/2015 - 9:27pm

I just came across this D library that does compile-time unit checking similar to units or dimensional. However, if you look at the test cases on that page, there are tests that actually check whether certain sub-expressions compile:

// Dimensional correctness is check at compile-time unittest { Mass mass; static assert(!__traits(compiles, mass = 15 * meter)); static assert(!__traits(compiles, mass = 1.2)); }

Instead of just testing desired behaviour, this explicitly makes sure that bad behaviour is disallowed at compile-time.

Is this something we could implement in Haskell, possibly using TH? It seems like a good idea for libraries which intend to expose a typesafe API, and verify that bad behaviour is illegal at compile-time.

submitted by theonlycosmonaut
[link] [7 comments]
Categories: Incoming News

Why does that work but not this?

Haskell on Reddit - Tue, 01/20/2015 - 8:36pm

[x/4 | x <-- [0..100], x mod 4 == 0]

doesn't work

but this

[x*4 | x <-- [0..100], x mod 4 == 0]

works. Why?

submitted by SnakeNoir
[link] [3 comments]
Categories: Incoming News

"Found hole"

glasgow-user - Tue, 01/20/2015 - 8:36pm
Hello! What is a "hole"? This program fails to compile: main = _exit 0 I get this error message: ex.hs:1:8: Found hole ‘_exit’ with type: t Where: ‘t’ is a rigid type variable bound by the inferred type of main :: t at ex.hs:1:1 Relevant bindings include main :: t (bound at ex.hs:1:1) In the expression: _exit In an equation for ‘main’: main = _exit When I replace "_exit" with "foo", it produces a "not in scope" error, as expected. What is special about "_exit"? It doesn't occur in the Haskell Hierarchical Libraries. Bye Volker _______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users< at >
Categories: Offsite Discussion