News aggregator

BWK on "How to succeed in language design without really trying"

Lambda the Ultimate - Fri, 11/20/2015 - 9:50am

A talk by Brian Kernighan at The University of Nottingham.

Describes all the usual suspects: AWK, EQN, PIC. Even AMPL. I was wondering which languages he had in mind when he mentioned that some of his creations were total flops. I'd love to learn from those!

The talk is a fun way to spend an hour, and the audio would be good for commuters. For real aficionados I would recommend reading Jon Bentley's articles on the design of these languages (as well as CHEM and others) instead.

Categories: Offsite Discussion

List comprehension with boolean guard not terminating

Haskell on Reddit - Fri, 11/20/2015 - 5:41am


I'm only a beginning Haskeller so I might be getting tripped up with infinite lists here, but I can't figure out what's causing the problem. I have the following code:

posTuple :: [a] -> [([a],Integer)]

posTuple list = [(list,toInteger i) | i <- [1..], i <= length list]

But when I evaluate: posTuple "hello"

ghci prints: [("test",1),("test",2),("test",3),("test",4)

and does not terminate (notice the lack of right bracket). Is anyone able to shed some light? Thanks!

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

Compile Shared Library with Stack

Haskell on Reddit - Fri, 11/20/2015 - 1:07am

I am currently trying to build a shared library and get the following error: /usr/bin/ld: /home/roman/.stack/programs/x86_64-linux/ghc-7.10.2/lib/ghc-7.10.2/rts/libHSrts.a(Linker.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC

If i understand this right it is because ghc was not compiled with the -fPIC flag. Do i have to manually build ghc from source or is there some easier way in achieving what i want?

The solutions was to use "extra-libraries: HSrts-ghc7.10.2" instead of "extra-libraries: HSrts" as only the former is a .so library the later is a static .a library which will result in this error.

submitted by lordmord319
[link] [2 comments]
Categories: Incoming News

AMP - how do you motivate this in teaching?

haskell-cafe - Thu, 11/19/2015 - 11:27pm
Because of AMP, I have to rewrite slides and example code for my lectures, and I don't like it. In fact I probably won't do it, and will advise students to return to ghc-7.8 - but then, how does that look? Really, my answer to [1] 3.5 Beginner friendliness How often did you say ... "A Monad is always an Applicative" is: never. (for "is a Functor" - often. In fact, always) Now, I don't want to bring on another general discussion of AMP - instead I'd like to hear from people who use monads in teaching (e.g., to define semantic domains) about how they sell "Applicative m =>" to their students. (The intersection of AMPers and teachers is non-empty?) - Johannes [1]
Categories: Offsite Discussion

Compilers as Assistants

Lambda the Ultimate - Thu, 11/19/2015 - 11:11pm

Designers of Elm want their compiler to produce friendly error messages. They show some examples of helpful error messages from their newer compiler on the blog post.

Compilers as Assistants

One of Elm’s goals is to change our relationship with compilers. Compilers should be assistants, not adversaries. A compiler should not just detect bugs, it should then help you understand why there is a bug. It should not berate you in a robot voice, it should give you specific hints that help you write better code. Ultimately, a compiler should make programming faster and more fun!

Categories: Offsite Discussion

Why is my mappend so slow?

haskell-cafe - Thu, 11/19/2015 - 7:18pm
Hello all, I wrote a Logger which, under certain conditions, prepends log-entries to a log and a Monoid instance of it. But as soon as I mappend two Loggers my performance drops by 50%. This even happens when I mappend mempty as shown below in --<2--. I understand that the system has to do *something*, but it seems to cost a bit much. Without the strictness annotation in --<1-- the performance degradation is even more dramatic (orders of magnitude). The profile tells me that more that 50% of the time is spent in mappend. COST CENTRE MODULE %time %alloc mappend.\ Logger 50.6 35.8 logCount'.f Logger 18.7 40.3 logCount' Logger 5.4 0.0 Why is that so, and can I do anything about it? I am willing to change the overall design if required. This is the code
Categories: Offsite Discussion

Low-Level Haskell

Haskell on Reddit - Thu, 11/19/2015 - 4:19pm

Hello! I'm working a lot with numerical code, and sometimes I feel that I really need to learn how to get closer to the machine and memory. I know that Haskell has ways to make it possible through the ST monad, etc...

Are there good tutorials on this topic? Or the best is just to try to learn more C and just try to reproduce it in Haskell? Thanks!

EDIT: I should have been more precise. The goal is to do low level work inside Haskell, pointers, memory management, perhaps safe mutability, etc. And I'm looking for tutorials and references.

submitted by guaraqe
[link] [18 comments]
Categories: Incoming News

How to read a file from another folder

Haskell on Reddit - Thu, 11/19/2015 - 2:07pm

Hi /r/haskell, got a really basic question today, I'm trying to write a small program which reads and writes to files, it works fine so far if the files are located in the same folder as my source code or inside subfolders of the same folder, but what I do not know (and don't find examples of) is this:

If my folder structure looks like this:

myProg/source/mycode.hs myProg/files/text.txt

How do I write the filepath? I tried

"../files/text.txt" "./../files/text.txt"

both do not work.

At the same time this made me wonder on a whole, how do you lay out file paths in a project, relative to the exe which is going to be compiled? Relative to the source file (modules) or to the main.hs file? Or maybe only using absolute paths?

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

FP Complete: Kubernetes for Haskell Services

Planet Haskell - Thu, 11/19/2015 - 1:00pm
Kubernetes (AKA "k8s")

From Kubernetes is an open source orchestration system for Docker containers. It handles scheduling onto nodes in a compute cluster and actively manages workloads to ensure that their state matches the users declared intentions. Using the concepts of "labels" and "pods", it groups the containers which make up an application into logical units for easy management and discovery.

We use it to host some of our web applications at FP Complete. In this article we will show you how to deploy a Kubernetes cluster to Amazon's AWS cloud, create a Haskell web application, build the application with Stack and finally deploy the application with Docker & Kubernetes. The whole process should take about 10 minutes.

Download the command line interface kubectl here (the binary CLI for your OS will be found in the tarball). You'll need this executable in order to interact with your deployed Kubernetes cluster. Download it now & make sure it's in your PATH.

If you are curious about all the things you can do with Kubernetes, you can find the documentation online. There's also an active mail-list and IRC channel.

kube-aws Deployment Tool

The CoreOS team has created a nice AWS CloudFormation tool for deploying working clusters of Kubernetes on AWS with ease. This is much simpler than my blog post from last winter (shows the guts of a CloudFormation deployment with CoreOS & Kubernetes.) These days all we need is one command line tool & a tiny yaml manifest file.

Download the command line tool here and put it in your PATH.

Deploy KubernetesSetup your AWS environment variables

Make sure you have at least the following environment variables set:

Create a folder for the new cluster

This will house the TLS certificates & configuration for communicating with Kubernetes after launch.

mkdir -p ~/.kube/clusters/kube/ cd ~/.kube/Create a ~/.kube/clusters/kube/cluster.yaml manifest file

This file will be used by kube-aws when launching a cluster (or destroying a cluster. See kube-aws --help for more options.)

# cluster name is whatever you want it to be clusterName: kube # the key name is, of course, the aws key-pair name keyName: kubernetes # pick a region and availability zone region: us-west-2 availabilityZone: us-west-2a # pick a domain name for your cluster externalDNSName: # pick the size of your cluster workerCount: 3Launch with kube-aws

Once our manifest is ready we can launch our cluster.

kube-aws up --config=~/.kube/clusters/kube/cluster.yaml

This will output the IP address of the master node. Take note of the IP address for the next step.

Set a DNS A record for the Kubernetes IP address

Set an external DNS 'A' record with master node's IP address. IN A <<IPADDRESS>>

If you don't have control of DNS for a domain, you can put an entry into your /etc/hosts file. <<IPADDRESS>>Configure kubectl

Then we'll want to link the kubectl configuration file that kube-aws produced into the right spot (~/.kube/config) so we can talk to our cluster.

ln -sf ~/.kube/clusters/kube/kubeconfig ~/.kube/configInteract with the Kubernetes cluster

Everything should be ready to interact with our new cluster. Let's list the (worker) nodes. You should see 3 based on our manifest file above.

kubectl get nodesHaskell Web ServiceStart a new haskell package

Let's create a new directory for our example haskell web service. (We could have used stack new hello to create an application stub but our needs are as simple as they get. We'll just create a couple of files & our project will be complete.)

mkdir hello cd helloCreate a hello.cabal

As with any Haskell application we need a cabal file describing the project.

name: hello version: 0.1.0 license: BSD3 build-type: Simple cabal-version: >=1.10 executable hello default-language: Haskell2010 ghc-options: -threaded -rtsopts -with-rtsopts=-N main-is: Main.hs build-depends: base , http-types , wai , warpCreate a stack.yaml

We also need a stack.yaml file. This sets the resolver (and the GHC version. In this case it's 7.10.2). The stack.yaml file also describes what packages we are building and it has container image settings.

resolver: lts-3.14 packages: [.] image: container: base: hello:baseCreate a Dockerfile

In order for Stack to build our application & package a docker container, we need to setup a base image with all our application dependencies. Put anything in your base image that your application will need. In our case today, we only need libgmp which is used by most haskell applications.

FROM ubuntu:14.04 RUN apt-get update && apt-get install -y libgmp10

Now we need to build & tag the base image. You most likely only need to do this once. You can optionally tag this base image & share it on DockerHub with your co-workers.

docker build -t hello:base .Create a Main.hs

Every haskell application needs it's Main module. This one simply fires up a Warp web-server on port 8080 and always serves "hello world" as plain text.

{-# LANGUAGE OverloadedStrings #-} import Network.HTTP.Types (status200) import Network.Wai (responseLBS) import Network.Wai.Handler.Warp (run) main :: IO () main = run 8080 (\rq rs -> rs (responseLBS status200 [("Content-Type","text/plain")] "hello world"))Build with Stack

This will compile your haskell executable and layer it on top of your 'base' image.

stack image container

You should now see a hello docker image if you list your images.

Test the image locally

We should now be able to run the docker image locally & see it work. Try it.

docker run -i -t --rm -p 8080:8080 hello hello

In another window use curl or your web-browser to access port 8080.

curl -v http://localhost:8080

Press ctrl-c when you are done with the local docker web-server.

Push the image to dockerhub

Next we'll tag the hello image with our dockerhub user prefix and a version. Then we'll push the image up to

docker tag hello dysinger/hello:0.1.0 docker push dysinger/hello:0.1.0Deploy to Kubernetes

Now that we have our application written & published we can deploy it to our new Kubernetes cluster. In order to deploy any cluster of web-servers to Kubernetes you need two basic yaml files. One is the Replication Controller file & the other is the Service file.

Create a hello-rc.yaml

The Replication Controller file describes our docker container's needs (ports, volumes, number of replicas, etc). Kubernetes will use this information to maintain a number of docker containers on the cluster running your application.

apiVersion: v1 kind: ReplicationController metadata: name: hello spec: replicas: 2 template: metadata: labels: app: hello spec: containers: - name: hello image: dysinger/hello:0.1.0 command: - hello ports: - name: http containerPort: 8080Create a hello-svc.yaml

The Service file describes the external interface for your replicated application. It can optionally create a load-balancer and map ports into your replicated application. If you need your application to be exposed to the internet, then you need a Service file.

apiVersion: v1 kind: Service metadata: name: hello labels: app: hello spec: ports: - name: http port: 80 targetPort: http selector: app: hello type: LoadBalancerDeploy the Controller & Service to Kubernetes

Next we use the kubectl command line tool to tell Kubernetes to deploy this application. We can do it with one command.

kubectl create -f hello-rc.yaml -f hello-svc.yamlDescribe the running Service

We can ask Kubernetes how the deployment went with the get subcommand.

kubectl get pods

You should see your new hello application being deployed (possibly not ready yet.)

You should see 2 of our hello applications deployed on 2 of our 3 worker nodes.

After a few minutes you should be able to get information about the applications external interface.

kubectl describe svc hello

This will show you the Amazon ELB DNS name. You can stick this hostname in your browser & your should see 'hello world'. You can update DNS with a CNAME from your domain to the Amazon ELB DNS name if you would like a shorter URL. CNAME <<ELB-HOSTNAME>>.
Categories: Offsite Blogs

Timing Optimization.

Haskell on Reddit - Thu, 11/19/2015 - 4:01am

I read somewhere that the compiler further optimizes the code and hence the interpreted code on ghci cannot be used to estimate running times. Is this true?

submitted by thezbk
[link] [4 comments]
Categories: Incoming News

ANN: haskell-tor 0.1

haskell-cafe - Thu, 11/19/2015 - 3:22am
Howdy - Galois is pleased to announce an initial release of haskell-tor. Haskell-tor is intended to be a full-featured, drop-in implementation of the Tor onion routing protocol. This release provides full support for resolving names and building connections via anonymized channels, as well as (less tested) support for running relay and exit nodes. There are still many tasks left to do, however, if you're interested in learning about and working on a Tor implementation, including support for hidden services, proper flow control, directory support, etc. So if you're interested, jump in! We welcome your patches. You can find haskell-tor on: GitHub: Hackage: Haskell-tor is HaLVM-ready. - Adam _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Gabriel Gonzalez: Interactive and composable charts

Planet Haskell - Thu, 11/19/2015 - 1:03am

I've added a diagrams backend to my typed-spreadsheet library which you can use to build composable graphical programs that update in response to user input.

Here's an example program that displays a circle that changes in response to various user inputs:

{-# LANGUAGE OverloadedStrings #-}

import Diagrams.Backend.Cairo (Cairo)
import Diagrams.Prelude
import Typed.Spreadsheet

data AColor = Red | Orange | Yellow | Green | Blue | Purple
deriving (Enum, Bounded, Show)

toColor :: AColor -> Colour Double
toColor Red = red
toColor Orange = orange
toColor Yellow = yellow
toColor Green = green
toColor Blue = blue
toColor Purple = purple

main :: IO ()
main = graphicalUI "Example program" logic
logic = combine <$> radioButton "Color" Red [Orange .. Purple]
<*> spinButtonAt 100 "Radius" 1
<*> spinButton "X Coordinate" 1
<*> spinButton "Y Coordinate" 1

combine :: AColor -> Double -> Double -> Double -> Diagram Cairo
combine color r x y =
circle r # fc (toColor color) # translate (r2 (x, -y))

Here is a video showing the example program in action:

<iframe allowfullscreen="allowfullscreen" frameborder="0" height="315" src="" width="420"></iframe> Applicatives

The first half of the main function (named logic) requests four users inputs to parametrize the displayed circle:

  • A radio button for selecting the circle's color
  • A spin button for controlling radius which defaults to 100 (pixels)
  • A spin button for controlling the x coordinate for the center of the circle
  • A spin button for controlling the y coordinate for the center of the circle

Each of these inputs is an Updatable value and we can express that using Haskell's type system:

radioButton "Color" Red [Orange .. Purple] :: Updatable AColor
spinButtonAt 100 "Radius" 1 :: Updatable Double
spinButton "X Coordinate" 1 :: Updatable Double
spinButton "Y Coordinate" 1 :: Updatable Double

The Updatable type implements Haskell's Applicative interface, meaning that you can combine smaller Updatable values into larger Updatable values using Applicative operations.

For example, consider this pure function that consumes four pure inputs and produces a pure diagram:

:: AColor
-> Double
-> Double
-> Double
-> Diagram Cairo

Normally if we compute a pure function we would write something like this:

combine Green 40 10 20
:: Diagram Cairo

However, this result is static and unchanging. I would like to transform this function into one that accepts Updatable arguments and produces an Updatable result.

Fortunately, Haskell's Applicative interface lets us do just that. We can lift any pure function to operate on any type that implements the Applicative interface like the Updatable type. All we have to do is separate the function from the first argument using the (<$>) operator and separate each subsequent argument using the (<*>) operator:

combine <$> radioButton "Color" Red [Orange .. Purple]
<*> spinButtonAt 100 "Radius" 1
<*> spinButton "X Coordinate" 1
<*> spinButton "Y Coordinate" 1
:: Updatable (Diagram Cairo)

Now the combine function accepts four Updatable arguments and produces an Updatable result! I can then pass this result to the graphicalUI function which builds a user interface from any Updatable Diagram:

graphicalUI :: Text -> Updatable Diagram -> IO ()

main = graphicalUI "Example program" logic

The Applicative operations ensure that every time one of our primitive Updatable inputs change, the composite Updatable Diagram immediately reflects that change.


One reason I wanted diagrams integration was to begin building interactive charts for typed spreadsheets. I'll illustrate this using a running example where I building up a successively more complex chart piece-by-piece.

Let's begin with a simple rectangle with an adjustable height (starting at 100 pixels):

{-# LANGUAGE OverloadedStrings #-}

import Diagrams.Backend.Cairo (Cairo)
import Diagrams.Prelude
import Typed.Spreadsheet

import qualified Data.Text as Text

bar :: Int -> Updatable (Diagram Cairo)
bar i = fmap buildRect (spinButtonAt 100 label 1)
buildRect height = alignB (rect 30 height)

label = "Bar #" <> Text.pack (show i)

main :: IO ()
main = graphicalUI "Example program" (bar 1)

When we run this example we get a boring chart with a single bar:

<iframe allowfullscreen="allowfullscreen" frameborder="0" height="315" src="" width="420"></iframe>

However, the beauty of Haskell is composable abstractions like Applicative. We can take smaller pieces and very easily combine them into larger pieces. Each piece does one thing and does it well, and we compose them to build larger functionality from sound building blocks.

For example, if I want to create a bar chart with five individually updatable bars, I only need to add a few lines of code to create five bars and connect them:

{-# LANGUAGE OverloadedStrings #-}

import Diagrams.Backend.Cairo (Cairo)
import Diagrams.Prelude
import Typed.Spreadsheet

import qualified Data.Text as Text

bar :: Int -> Updatable (Diagram Cairo)
bar i = fmap buildRect (spinButtonAt 100 label 1)
buildRect height = alignB (rect 30 height)

label = "Bar #" <> Text.pack (show i)

bars :: Int -> Updatable (Diagram Cairo)
bars n = fmap combine (traverse bar [1..n])
combine bars = alignX 0 (hcat bars)

main :: IO ()
main = graphicalUI "Example program" (bars 5)

This not only creates a bar chart with five bars, but also auto-generates a corresponding input cell for each bar:

<iframe allowfullscreen="allowfullscreen" frameborder="0" height="315" src="" width="420"></iframe>

Even better, all the inputs are strongly typed! The program enforces that all inputs are well-formed and won't let us input non-numeric values.

We also benefit from all the features of Haskell's diagrams library, which is an powerful Haskell library for building diagrams. Let's spruce up the diagram a little bit by adding color, spacing, and other embellishments:

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}

import Diagrams.Backend.Cairo (Cairo)
import Diagrams.Prelude
import Typed.Spreadsheet

import qualified Data.Text as Text

bar :: Int -> Updatable (Diagram Cairo)
bar i = fmap buildBar (spinButtonAt 100 label 0.2)
color = case i `mod` 3 of
0 -> red
1 -> green
2 -> yellow

buildBar height =
( alignB ( vine
<> bubbles
<> alignB ( roundedRect 25 (height - 5) 5 # fc white
<> roundedRect 30 height 5 # fc color
stepSize = 15

vine = strokeP (fromVertices (map toPoint [0..height]))
toPoint n = p2 (5 * cos (pi * n / stepSize), n)

bubble n =
circle radius
# translate (r2 (0, n * stepSize))
# fc lightblue
radius = max 1 (min stepSize (height - n * stepSize)) / 5

bubbles = foldMap bubble [1 .. (height / stepSize) - 1]

label = "Bar #" <> Text.pack (show i)

bars :: Int -> Updatable (Diagram Cairo)
bars n = fmap combine (traverse bar [1..n])
combine bars = alignX 0 (hsep 5 [alignL yAxis, alignL (hsep 5 bars)])

yAxis = arrowV (r2 (0, 150))

main :: IO ()
main = graphicalUI "Example program" (bars 5)

One embellishment is a little animation where bubbles fade in and out near the top of the bar:

<iframe allowfullscreen="allowfullscreen" frameborder="0" height="315" src="" width="420"></iframe>

We can customize the visuals to our heart's content becuse the spreadsheet and diagram logic are both embedded within a fully featured programming language.


The typed-spreadsheet library illustrates how you can use the Haskell language to build high-level APIs that abstract way low-level details such as form building or reactive updates in this case.

In many languages high-level abstractions come at a price: you typically have to learn a domain-specific language in order to take advantage of high-level features. However, Haskell provides reusable interfaces like Applicatives that you learn once and apply over and over and over to each new library that you learn. This makes the Haskell learning curve very much like a "plateau": initially steep as you learn the reusable interfaces but then very flat as you repeatedly apply those interfaces in many diverse domains.

If you would like contribute to the typed-spreadsheet library you can contribute out-of-the-box charting functionality to help the library achieve feature parity with real spreadsheet software.

You can learn more about the library by checking out:

Categories: Offsite Blogs