News aggregator

Haskell code running slowly (taking like a minute to solve the problem) (follow up to an earlier post)

Haskell on Reddit - Wed, 08/12/2015 - 1:47pm
isPrime :: Int -> Bool isPrime x | x <= 1 = False | x == 2 = True | x `mod` 2 == 0 = False | otherwise = dummy where posRoots= map floor [1..sqrt(fromIntegral x)] posRootsOdd = filter (odd) posRoots posRootsOddOne = filter (>1) posRootsOdd wasRoot = map ((==0). (x `mod` )) posRootsOddOne dummy = and $ map (==False) wasRoot primeFactors :: Int -> Int primeFactors x = head $ filter ((==0). (x `mod`)) $ reverse $ filter (isPrime) $ map floor [1..sqrt(fromIntegral x)]

I cleaned up my variable names Yay me! So, I got it to work and accoridng to Project Euler I got the right answer, but it's running a lot slower than I'd like and I'm not exactly sure why that is? Any suggestions?

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

Haskell code running slowly (taking like a minute to solve the problem) (follow up to an earlier post)

Haskell on Reddit - Wed, 08/12/2015 - 1:47pm
isPrime :: Int -> Bool isPrime x | x <= 1 = False | x == 2 = True | x `mod` 2 == 0 = False | otherwise = dummy where posRoots= map floor [1..sqrt(fromIntegral x)] posRootsOdd = filter (odd) posRoots posRootsOddOne = filter (>1) posRootsOdd wasRoot = map ((==0). (x `mod` )) posRootsOddOne dummy = and $ map (==False) wasRoot primeFactors :: Int -> Int primeFactors x = head $ filter ((==0). (x `mod`)) $ reverse $ filter (isPrime) $ map floor [1..sqrt(fromIntegral x)]

I cleaned up my variable names Yay me! So, I got it to work and accoridng to Project Euler I got the right answer, but it's running a lot slower than I'd like and I'm not exactly sure why that is? Any suggestions?

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

(InfoQ) Frege: a Haskell for the JVM

Haskell on Reddit - Wed, 08/12/2015 - 12:56pm

article with a very interesting part about the history of this effort, current state, and future plans.

The Frege Day is coming up Sept. 11th.

submitted by codevise
[link] [9 comments]
Categories: Incoming News

(InfoQ) Frege: a Haskell for the JVM

Haskell on Reddit - Wed, 08/12/2015 - 12:56pm

article with a very interesting part about the history of this effort, current state, and future plans.

The Frege Day is coming up Sept. 11th.

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

Depending on both mtl-2.1.3.1 for stackage LTS-2.* and mtl-2.2.1 for stackage nightly

Haskell on Reddit - Wed, 08/12/2015 - 11:00am

At some point I moved my code to use the new ExceptT introduced in mtl-2.2. It's great, I can use GHC 7.10.2 and stackage nightly. But I can no longer use 7.8.4 and stackage LTS-2.22, because it has the old mtl-2.1.3.1 with the now deprecated ErrorT. Also, Hackage still uses 7.8.3 and I am getting build failures and no documentation.

So I came up with the following way of depending on both mtls using CPP (of course... :-( ).

First, extension

{-# LANGUAGE CPP #-}

Then, for imports

#if MIN_VERSION_mtl(2,2,0) import Control.Monad.Except #else import Control.Monad.Error #endif

Then, because my code was rewritten to use ExceptT everywhere, I introduced the type synonym and following functions:

#if !MIN_VERSION_mtl(2,2,0) type ExceptT = ErrorT -- so that I don't have to fix all type signatures runExceptT :: forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a) runExceptT = runErrorT -- so that I don't have to fix code mkExceptT = ErrorT #else mkExceptT = ExceptT #endif

Now, the problem starts with trying to give a type signature for runExceptT. That required adding {-# LANGUAGE RankNTypes, KindSignatures #-}, which is ok.

Another problem is with constructor. Wherever the constructor was used to create a valueExceptT ....., I had to replace it with mkExceptT. That's not too bad. However, wherever the constructor was used in a pattern match position, mkExceptT cannot be used.

So, the main question is: How can I give an alias to the constructor the same way as I gave an alias to the type? I cannot declare a top-level constructor outside of the data definition like this:

ExceptT = ErrorT

Are there any better ways of dealing with such a problem?

submitted by mallai
[link] [14 comments]
Categories: Incoming News

Depending on both mtl-2.1.3.1 for stackage LTS-2.* and mtl-2.2.1 for stackage nightly

Haskell on Reddit - Wed, 08/12/2015 - 11:00am

At some point I moved my code to use the new ExceptT introduced in mtl-2.2. It's great, I can use GHC 7.10.2 and stackage nightly. But I can no longer use 7.8.4 and stackage LTS-2.22, because it has the old mtl-2.1.3.1 with the now deprecated ErrorT. Also, Hackage still uses 7.8.3 and I am getting build failures and no documentation.

So I came up with the following way of depending on both mtls using CPP (of course... :-( ).

First, extension

{-# LANGUAGE CPP #-}

Then, for imports

#if MIN_VERSION_mtl(2,2,0) import Control.Monad.Except #else import Control.Monad.Error #endif

Then, because my code was rewritten to use ExceptT everywhere, I introduced the type synonym and following functions:

#if !MIN_VERSION_mtl(2,2,0) type ExceptT = ErrorT -- so that I don't have to fix all type signatures runExceptT :: forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a) runExceptT = runErrorT -- so that I don't have to fix code mkExceptT = ErrorT #else mkExceptT = ExceptT #endif

Now, the problem starts with trying to give a type signature for runExceptT. That required adding {-# LANGUAGE RankNTypes, KindSignatures #-}, which is ok.

Another problem is with constructor. Wherever the constructor was used to create a valueExceptT ....., I had to replace it with mkExceptT. That's not too bad. However, wherever the constructor was used in a pattern match position, mkExceptT cannot be used.

So, the main question is: How can I give an alias to the constructor the same way as I gave an alias to the type? I cannot declare a top-level constructor outside of the data definition like this:

ExceptT = ErrorT

Are there any better ways of dealing with such a problem?

submitted by mallai
[link] [14 comments]
Categories: Incoming News

Haskell Code critique and help debugging?

Haskell on Reddit - Wed, 08/12/2015 - 10:02am

Hey everyone! So I tried making a program to find if a number is a prime but I'm getting some errors and I'd like some comments on it. It's sort of to solve project Euler problem 3. I'm kind of familiar with SML but these errors seem like it might be a problem with my type declarations? Basically it's not something that I know how to fix, unfortunately.

isPrime :: Int -> Bool isPrime x | x <= 1 = False | x == 2 = True | x `mod` 2 == 0 = False | dummy = True where haskell = map floor [1..sqrt(x)] isSo = filter (odd) haskell effing = filter (>1) isSo cool = map ((==0). (x `mod` )) effing srsly = and $ map (==True) cool

The errors that I'm getting are

test.hs:68:23: No instance for (RealFrac Int) arising from a use of ‘floor’ In the first argument of ‘map’, namely ‘floor’ In the expression: map floor [1 .. sqrt (x)] In an equation for ‘haskell’: haskell = map floor [1 .. sqrt (x)] test.hs:68:33: No instance for (Floating Int) arising from a use of ‘sqrt’ In the expression: sqrt (x) In the second argument of ‘map’, namely ‘[1 .. sqrt (x)]’ In the expression: map floor [1 .. sqrt (x)] Failed, modules loaded: none. submitted by ThrowawayTartan
[link] [12 comments]
Categories: Incoming News

Haskell Code critique and help debugging?

Haskell on Reddit - Wed, 08/12/2015 - 10:02am

Hey everyone! So I tried making a program to find if a number is a prime but I'm getting some errors and I'd like some comments on it. It's sort of to solve project Euler problem 3. I'm kind of familiar with SML but these errors seem like it might be a problem with my type declarations? Basically it's not something that I know how to fix, unfortunately.

isPrime :: Int -> Bool isPrime x | x <= 1 = False | x == 2 = True | x `mod` 2 == 0 = False | dummy = True where haskell = map floor [1..sqrt(x)] isSo = filter (odd) haskell effing = filter (>1) isSo cool = map ((==0). (x `mod` )) effing srsly = and $ map (==True) cool

The errors that I'm getting are

test.hs:68:23: No instance for (RealFrac Int) arising from a use of ‘floor’ In the first argument of ‘map’, namely ‘floor’ In the expression: map floor [1 .. sqrt (x)] In an equation for ‘haskell’: haskell = map floor [1 .. sqrt (x)] test.hs:68:33: No instance for (Floating Int) arising from a use of ‘sqrt’ In the expression: sqrt (x) In the second argument of ‘map’, namely ‘[1 .. sqrt (x)]’ In the expression: map floor [1 .. sqrt (x)] Failed, modules loaded: none. submitted by ThrowawayTartan
[link] [12 comments]
Categories: Incoming News

Mark Jason Dominus: A message to the aliens, part 2/23 (arithmetic)

Planet Haskell - Wed, 08/12/2015 - 7:21am

Earlier articles: Introduction Common features Page 1 (numerals)

This is page 2 of the Cosmic Call message. An explanation follows.

Reminder: the previous page explained the ten digits:


0
1
2
3
4
5
6
7
8
9

This page, headed with the glyph for “mathematics” , explains the arithmetic operations on numbers.

The page is in five sections, three on top and two below.
The first four sections explain addition , subtraction , multiplication , and division . Each is explained with a series of five typical arithmetic equalities. For example, :

The subtraction sign actually appeared back on page 1 in the Mersenne prime .

The negative sign is introduced in connection with subtraction, since :

Note that the negative-number sign is not the same as the subtraction sign.

The decimal point is introduced in connection with division. For example, :

There is also an attempt to divide by zero:

It's not clear what the authors mean by this; the mysterious glyph does not appear anywhere else in the document. What did they think it meant? Infinity? Indeterminate? Well, I found out later they published a cheat sheet, which assigns the meaning “undetermined” to this glyph. Not a great choice, in my opinion, because is not numerically equal to anything.

For some reason, perhaps because of space limitations, the authors have stuck the equation at the bottom of the division section.

The fifth section, at lower right, displays some nonterminating decimal fractions and introduces the ellipsis or ‘…’ symbol. For example, :

I would have put here instead of , which I think is too similar to the other examples.

The next article, to appear 2015-08-14, will discuss page 3, shown at right. (Click to enlarge.) Try to figure it out before then.

Categories: Offsite Blogs

Mark Jason Dominus: A message to the aliens, part 1/23 (numbers)

Planet Haskell - Wed, 08/12/2015 - 7:21am

Earlier articles: Introduction Common features

This is page 1 of the Cosmic Call message. An explanation follows.

This page, headed with the glyph for “mathematics” , explains the numeral symbols that will be used throughout the rest of the document. I should warn you that these first few pages are a little dull, establishing basic mathematical notions. The good stuff comes a little later.

The page is in three sections. The first section explains the individual digit symbols. A typical portion looks like this:


•••• ••• = 0111 = 7

Here the number 7 is written in three ways: first, as seven dots, probably unmistakeable. Second, as a 4-bit binary number, using the same bit symbols that are used in the page numbers. The three forms are separated by the glyph , which means “equals”. The ten digits, in order from 0 to 9, are represented by the glyphs


0
1
2
3
4
5
6
7
8
9

The authors did a great job selecting glyphs that resemble the numerals they represent. All have some resemblance except for 4, which has 4 horizontal strokes. Watch out for 4; it's easy to confuse with 3.

The second section serves two purposes. It confirms the meaning of the ten digits, and it also informs the aliens that the rest of the message will write numerals in base ten. For example, the number 14:


••••• ••••• •••• = 14

Again, there are 14 dots, an equal sign, and the numeral 14, this time written with the two glyphs (1) and (4). The base-2 version is omitted this time, to save space. The aliens know from this that we are using base 10; had it been, say, base 8, the glyphs would have been .

People often ask why the numbers are written in base 10, rather than say in base 2. One good answer is: why not? We write numbers in base 10; is there a reason to hide that from the aliens? The whole point of the message is to tell the aliens a little bit about ourselves, so why disguise the fact that we use base-10 numerals? Another reason is that base-10 numbers are easier to proofread for the humans sending the message.

The third section of the page is a list of prime numbers from 2 to 89:


67, 71, 73, 79, 83

and finally the number

,

which was the largest prime number known to humans at the time. (The minus sign and exponentiation notation are explained on later pages.) Why? Again. to tell the aliens about ourselves: here's a glimpse of the limits of our mathematical knowledge.

I often wonder what the aliens will think of the . Will they laugh at how cute we are, boasting about the sweet little prime number we found? Or will they be astounded and wonder why we think we know that such a big number is prime?

The next article, to appear 2015-08-12, will discuss page 2, shown at right. (Click to enlarge.) Try to figure it out before then.

Categories: Offsite Blogs

Bug in Template Haskell?

Haskell on Reddit - Wed, 08/12/2015 - 4:18am

Works.hs

{-# LANGUAGE TemplateHaskell #-} module Works where import Language.Haskell.TH stringNewtype :: String -> DecsQ stringNewtype name = do let typeN = mkName name let accN = mkName ("un" ++ name) stringT <- [t| String |] return [NewtypeD [] typeN [] (RecC typeN [(accN, NotStrict, stringT)]) [''Show]]

DoesNotWork.hs

{-# LANGUAGE TemplateHaskell #-} module DoesNotWork where import Language.Haskell.TH stringNewtype :: String -> DecsQ stringNewtype name = do [d| newtype $typeN = $typeN { $accN :: String } |] where typeN = mkName name accN = mkName ("un" ++ name)

Main.hs

{-# LANGUAGE TemplateHaskell #-} module Main where import Works -- import DoesNotWork $(stringNewtype "Abc") main :: IO () main = putStrLn $ unAbc $ Abc "Hello, World!"

The code works with the Works module, but with the DoesNotWork module it refuses to compile:

DoesNotWork.hs:9:33: parse error on input ‘$accN’

Why does that happen?

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

Bug in Template Haskell?

Haskell on Reddit - Wed, 08/12/2015 - 4:18am

Works.hs

{-# LANGUAGE TemplateHaskell #-} module Works where import Language.Haskell.TH stringNewtype :: String -> DecsQ stringNewtype name = do let typeN = mkName name let accN = mkName ("un" ++ name) stringT <- [t| String |] return [NewtypeD [] typeN [] (RecC typeN [(accN, NotStrict, stringT)]) [''Show]]

DoesNotWork.hs

{-# LANGUAGE TemplateHaskell #-} module DoesNotWork where import Language.Haskell.TH stringNewtype :: String -> DecsQ stringNewtype name = do [d| newtype $typeN = $typeN { $accN :: String } |] where typeN = mkName name accN = mkName ("un" ++ name)

Main.hs

{-# LANGUAGE TemplateHaskell #-} module Main where import Works -- import DoesNotWork $(stringNewtype "Abc") main :: IO () main = putStrLn $ unAbc $ Abc "Hello, World!"

The code works with the Works module, but with the DoesNotWork module it refuses to compile:

DoesNotWork.hs:9:33: parse error on input ‘$accN’

Why does that happen?

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

ANNOUNCE: brick 0.1 released (deprecates vty-ui)

General haskell list - Wed, 08/12/2015 - 2:33am
Hi, I'm very excited to announce the first public release of brick, a new high-level library for making terminal user interfaces based on vty. This library deprecates vty-ui. Folks depending heavily on vty-ui should contact me! brick exposes a declarative interface for specifying terminal user interfaces. If you have used gloss, you know what to expect: you can specify your UI in a purely-functional style by defining a drawing function and an event handler and you're off and running! A feature overview can be found at https://github.com/jtdaugherty/brick#feature-overview Check out the (many) demonstration programs and user guide at https://github.com/jtdaugherty/brick or see the Haddock at http://hackage.haskell.org/package/brick Enjoy!
Categories: Incoming News

Dominic Steinitz: Stochastic Integration

Planet Haskell - Wed, 08/12/2015 - 1:14am
Introduction

Suppose we wish to model a process described by a differential equation and initial condition

But we wish to do this in the presence of noise. It’s not clear how do to this but maybe we can model the process discretely, add noise and somehow take limits.

Let be a partition of then we can discretise the above, allow the state to be random and add in some noise which we model as samples of Brownian motion at the selected times multiplied by so that we can vary the amount noise depending on the state. We change the notation from to to indicate that the variable is now random over some probability space.

We can suppress explicit mention of and use subscripts to avoid clutter.

We can make this depend continuously on time specifying that

and then telescoping to obtain

In the limit, the second term on the right looks like an ordinary integral with respect to time albeit the integrand is stochastic but what are we to make of the the third term? We know that Brownian motion is nowhere differentiable so it would seem the task is impossible. However, let us see what progress we can make with so-called simple proceses.

Simple Processes

Let

where is -measurable. We call such a process simple. We can then define

So if we can produce a sequence of simple processes, that converge in some norm to then we can define

Of course we need to put some conditions of the particular class of stochastic processes for which this is possible and check that the limit exists and is unique.

We consider the , the space of square integrable functions with respect to the product measure where is Lesbegue measure on and is some given probability measure. We further restrict ourselves to progressively measurable functions. More explicitly, we consider the latter class of stochastic processes such that

Less Simple Processes Bounded, Almost Surely Continuous and Progressively Adapted

Let be a bounded, almost surely continuous and progressively measurable process which is (almost surely) for for some positive constant . Define

These processes are cleary progressively measurable and by bounded convergence ( is bounded by hypothesis and is uniformly bounded by the same bound).

Bounded and Progressively Measurable

Let be a bounded and progressively measurable process which is (almost surely) for for some positive constant . Define

Then is bounded, continuous and progressively measurable and it is well known that as . Again by bounded convergence

Progressively Measurable

Firstly, let be a progressively measurable process which is (almost surely) for for some positive constant . Define . Then is bounded and by dominated convergence

Finally let be a progressively measurable process. Define

Clearly

The Itô Isometry

Let be a simple process such that

then

Now suppose that is a Cauchy sequence of progressively measurable simple functions in then since the difference of two simple processes is again a simple process we can apply the Itô Isometry to deduce that

In other words, is also Cauchy in and since this is complete, we can conclude that

exists (in ). Uniqueness follows using the triangle inequality and the Itô isometry.

Notes
  1. We defer proving the definition also makes sense almost surely to another blog post.

  2. This approach seems fairly standard see for example Handel (2007) and Mörters et al. (2010).

  3. Rogers and Williams (2000) takes a more general approach.

  4. Protter (2004) takes a different approach by defining stochastic processes which are good integrators, a more abstract motivation than the one we give here.

  5. The requirement of progressive measurability can be relaxed.

Bibliography

Handel, Ramon von. 2007. “Stochastic Calculus, Filtering, and Stochastic Control (Lecture Notes).”

Mörters, P, Y Peres, O Schramm, and W Werner. 2010. Brownian motion. Cambridge Series on Statistical and Probabilistic Mathematics. Cambridge University Press. http://books.google.co.uk/books?id=e-TbA-dSrzYC.

Protter, P.E. 2004. Stochastic Integration and Differential Equations: Version 2.1. Applications of Mathematics. Springer. http://books.google.co.uk/books?id=mJkFuqwr5xgC.

Rogers, L.C.G., and D. Williams. 2000. Diffusions, Markov Processes and Martingales: Volume 2, Itô Calculus. Cambridge Mathematical Library. Cambridge University Press. https://books.google.co.uk/books?id=bDQy-zoHWfcC.


Categories: Offsite Blogs