# News aggregator

### Combine `StateT` and `InputT` to maintain state onCtrl-C interrupt

### Douglas M. Auclair (geophf): Recipe: Getting Haskell running on AWS Linux EC2 instance

So, you’ve configured your EC-2 with the standard AWS-Linux image, and now you want to compile your sources and have running executables to demo, including in not only Java and Perl but possibly C, C++ and, for my prototypes and graph-systems, Haskell. How to do this?

Unfortunately, the AMI for Haskell is rather unforthcoming, as it seems to compile the GHC from sources, an endeavor that takes hours of AWS-time. Unnecessary. So, I’ve cut out those middle steps and have a Haskell-image on my S3 that can be downloaded and used once the dev-tools are in place.

Let’s do this.

1. Get the ghc image from my public-S3-bucket: $ wget https://s3-us-west-2.amazonaws.com/haskell-7-10-2/haskell-platform-7.10.2-a-unknown-linux-deb7.tar.gz

(you can either get this from my S3 bucket and be stuck with that version of haskell on a vanilla linux instance, or you can go to haskell.org and get the most recent version for your particular flavor of linux)

2. install the dev-tools from Amazon (this takes a bit)

$ sudo yum groupinstall “Development Tools”

3. Now you need to provide a soft link to libgmp.so:

$ cd /usr/lib64 $ sudo ln -s libgmp.so.3 libgmp.so $ sudo ln -s libgmp.so.3 libgmp.so.10 $ cd

Yes, you need both of those soft links.

4. Once you have all that done, unzip ghc:

$ sudo tar xzf haskell-platform-7.10.2-a-unknown-linux-deb7.tar.gz

5. And now ghci will work for you:

$ ghci Prelude> 3 + 4 ~> 7

YAY! Do a happy dance!

### Douglas M. Auclair (geophf): (Pure) Functional Programming Claims IRL

*THIS*happened:

– question from a young programmer

So, does functional programming stack up in the real world? In industry?

Yes. Check my linkin profile. I have been programming, in industry, as long as you have been alive.

Here are some (pure) functional programming examples to back up this claim, because academics can talk all they want, but they are not in there, in the trenches, with you where it counts.

I am, because I happened to have dug a few of those trenches. You're welcome.

__Case study 1: ATS-L__Worked on a project in DHS called 'ATS' ('Automated Targeting System'). The existing system ATS-C was a 100,000-line Prolog behemoth that used pure dynamic types (no type hints, nor boxed types) and every rule started with an assert and ended with a retract. And 100,000 lines.

It was

*impossible*to know what was going on in that system, without running the code in the debugger and pulling from the dynamic environment. Consequently, the ATS-C guy had (still has) job security. Not his aim, but that is a nice plus.

It took us 72-hours to go through every line of his code to correct the Int-rollover problem when the primary key exceeded two billion for the index.

So, I was called in to 'help.' HA! But then eventually I built ATS-L. I wrote it in 10,000 lines of purely functional Prolog (yes, that is possible to do, and remain authentic to logic programming in Prolog), so every rule called gave the same truth-verification from the same set of arguments, every time.

Shocker! I know.

I had the same level of functionality of ATS-C and handled 1,000x the number of transactions per hour. And as it was purely functional Prolog, I could reason about my program in the large and in the small. Importantly, so could others, as I passed on that work after maintaining it for three years.

In short: 1/10th the SLOC with the same level of functionality with increased real-time responsiveness and a vastly reduced level of maintenance.

Oh, and I also wrote 726 unit tests and put them on an automated midnight run, generating a report every single day. If my system broke, or something changed, I knew it, and management knew it when the report was automatically emailed to them.

__Case Study 2: CDDS__Worked three years in Fannie Mae devising within a team an appraisal review process, CDDS. We had a good team of five engineers and I was given the 'Sales Comparison Approach' which had 600 elements out of 2,100 data elements in over 100 data tables, one of the tables ingested 100

*million*elements per month.

*All*the elements were optional. All of them, so primary key dependencies were an ...

*interesting*problem. The upshot was that Sales Comparison Approach was an impossible task to code, as we coded it in Java, of course.

What did I do? I coded it in Java.

After I implemented the Maybe type, then the Monad type-class ... in Java.

After I completed the system and tuned it, storing

*only*the values that were present in the submitted forms, my manager reported up the chain that SCA and CDDS would have failed if I had not been there to implement it.

How did I implement it? In Java. I didn't use one for-loop and my if-statements were not there. I used the Maybe-Monad to model semi-determinism, lifting the present data to Just x and the absent data ('null') to Nothing, and then I executed action against the monadic data.

Simple. Provable. Implemented. Done.

Oh, and I had written 1,000 of the 1,100 unit test cases. SCA had 1,000 unit test cases, the rest of the system had a total of 100 unit test cases.

My code coverage was

*fiiiiiiine.*

**Case Study 3: Sunset Dates**__This one was interesting.__

I worked at Freddy Mac for a year, and they had a problem, and that problem was to calculate the sunset date for a mortgage based on the most recent date from one of possibly five indicators, that changed with each possible mortgage transaction.

Three different software teams tackled this problem over a period of six months and

*none*of them implemented a system that passed UAT.

I sat down with the UATester and kept getting part of the story. I lifted our conversations up into the categorical domain, and then dropped that into a Java-implementation (I used both monads and comonads which I had implemented).

It took me two solid months working with this tester and a front-end developer, but we passed UAT and we got the customer and their SMA to sign off on it.

Three person team, purely functional programming ...

*in Java*won that work where standard imperative approaches failed, over and over again.

Funny story. I was seriously asked on that project: "What's a tuple?"

__Case Study 4: Dependency Graphs of Program Requirements ('TMQER')__**I can't compare what I wrote, in Haskell, to an alternative system, because the alternative, traditional imperative approach was never essayed. We had a set of 521 requirements for a program with many (multiple) parent and child dependencies, so it wasn't a tree, it was a graph. So, I parsed the requirements document into a Haskell Data.Graph and provided not only a distance matrix, as requested (which is not what the customer wanted at all: it was just what they**

*said*and

*thought*they wanted), but also clustering reports of which requirements were the 'heaviest' having the most dependencies and which requirements were show-stoppers to how many follow-on requirements.

Then I uploaded my Haskell Graph into Neo4J, making heavily-clustered requirements an obvious visual cue. And we won that contract.

The project wasn't attempted in Java. The project was attempted in R, and it couldn't be done. They estimated the graph manipulation algorithm would be 200-lines of code in R, that they couldn't get working.

With comonads, I did it in one line of Haskell. One line for a graph deforestation algorithm to get to the bare essentials of what was important to the project. Wanna see it?

How hard was that? In Haskell, a pure functional programming language, not hard at all.

Not only that, that we won a contract that our competing companies said was impossible, but our VP got wind of this and started vetting my tech to other companies.

We have a contract in the works, right now, using Haskell and Neo4J on AWS that is answering questions about fuzzy relations in social networks that a company that is expert in social engineering needs us to answer.

And I can answer these questions using graph theory and purely functional programming.

__Case study 5: the one that got away__**Oh, and then there was the one that got away. It had to do with a neural network I built in Mercury, a purely functional logic programming language with Prolog-like syntax that was able to classify images into 'interesting' and (mostly) 'not-interesting' but 'interesting' had very specific, different meanings, and it was able to classify these images, using a pulse-coupled neural network, in ways that eliminated 99% of waste images quickly so that analysts could concentrate on doing work, as opposed to sieving through the deluge of useless images to get the the ones they needed to see.**

I build a working prototype and demoed it.

This had never been done before. Ever.

Then, a Big Six came in and said, 'we can do that for you with 250 programmers and Java' and stole the project. After ten years and

*billions*of dollars, they were unable to reproduce my work.

__Pure Functional Programming Claims IRL__So, let's do a real-money tally.

__ATS-L__in one month, in the three years I maintained it (it is

*still*up and running ten years later, ladies and gentlemen) made $26

*million*dollars in seizures and

*rescued three teens*being human-trafficked over the border.

__CDDS__has been in production since the year 2010 and has verified appraisals helping Fannie Mae to make

*62 Billion*dollars in

*net*

*profit*in one quarter the year it went live, actually contributing to the rescue of Fannie Mae from insolvency.

__TMQER__has rescued a government run program from failure that has the funding price-tag of over

*100 Million*dollars of Government (your) taxpayer (your) money. You're welcome.

__Sunset dates__I wish I had a dollar amount, but you can estimate for me: three teams of business analysts and software engineers over a six month period said it couldn't be done (or tried it and failed). I scrapped

*all*that code, wrote the system from first principals (Category Theory) and got it working and approved in two months. You do the math.

... Oh, and then there's my current project. I might actually be able to own this thing. Hmmmm.

So, yes, Virginia,

1. there is a Santa Clause

2. those academics are actually onto something. (Pure) functional programming actually does matter. It actually does allow you to program better, faster and more cleanly, and with these enhanced skill-sets you become the one they turn to when other teams throw up their hands at an

*'impossible'*task. And then you deliver, ahead of expectations on both time to deliver and budget costs.

Hm.

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

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]

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

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]

### (InfoQ) Frege: a Haskell for the JVM

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]

### (InfoQ) Frege: a Haskell for the JVM

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]

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

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 #endifThen, 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 #endifNow, 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]

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

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 #endifThen, 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 #endifNow, 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]

### Haskell Code critique and help debugging?

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) coolThe 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]

### Haskell Code critique and help debugging?

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) coolThe 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]

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

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.### Mark Jason Dominus: A message to the aliens, part 1/23 (numbers)

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

,

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.### Bug in Template Haskell?

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]

### Bug in Template Haskell?

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]