News aggregator

Ian Ross: Constraint kinds and associated types

Planet Haskell - Sun, 03/08/2015 - 12:32pm
Constraint kinds and associated types March 8, 2015

This is going to be the oldest of old hat for the cool Haskell kids who invent existential higher-kinded polymorphic whatsits before breakfast, but it amused me, and it’s the first time I’ve used some of these more interesting language extensions for something “real”.


I have a Haskell library called hnetcdf for reading and writing NetCDF files. NetCDF is a format for gridded data that’s very widely used in climate science, meteorology and oceanography. A NetCDF file contains a number of gridded data sets, along with associated information describing the coordinate axes for the data. For example, in a climate application, you might have air temperature or humidity on a latitude/longitude/height grid.

So far, so simple. There are C and Fortran libraries for reading and writing NetCDF files and the interfaces are pretty straightforward. Writing a basic Haskell binding for this stuff isn’t very complicated, but one thing is a little tricky, which is the choice of Haskell type to represent the gridded data.

In Haskell, we have a number of different array abstractions that are in common use – you can think of flattening your array data into a vector, using a Repa array, using a hmatrix matrix, or a number of other possibilities. I wanted to support a sort of “store polymorphism” over these different options, so you’d be able to use the same approach to read data directly into a Repa array or a hmatrix matrix.

NcStore: first try

To do this, I wrote an NcStore class, whose first version looked something like this:

class NcStore s where toForeignPtr :: Storable e => s e -> ForeignPtr e fromForeignPtr :: Storable e => ForeignPtr e -> [Int] -> s e smap :: (Storable a, Storable b) => (a -> b) -> s a -> s b

It’s basically just a way of getting data in and out of a “store”, in the form of a foreign pointer that can be used to pass data to the NetCDF C functions, plus a mapping method. This thing can’t be a functor because of the Storable constraints on the types to be stored (which we need so that we can pass these things to C functions).

That works fine for vectors from Data.Vector.Storable:

instance NcStore Vector where toForeignPtr = fst . unsafeToForeignPtr0 fromForeignPtr p s = unsafeFromForeignPtr0 p (Prelude.product s) smap = map

and for Repa foreign arrays:

import Data.Array.Repa import qualified Data.Array.Repa as R import qualified Data.Array.Repa.Repr.ForeignPtr as RF import Data.Array.Repa.Repr.ForeignPtr (F) instance Shape sh => NcStore (Array F sh) where toForeignPtr = RF.toForeignPtr fromForeignPtr p s = RF.fromForeignPtr (shapeOfList $ reverse s) p smap f s = computeS $ f s Additional element type constraints

However, there’s a problem if we try to write an instance of NcStore for hmatrix matrices. Most hmatrix functions require that the values stored in a hmatrix matrix are instances of the hmatrix Element class. While it’s completely trivial to make types instances of this class (you just write instance Element Blah and you’re good), you still need to propagate the Element constraint through your code. In particular, I needed to use the hmatrix flatten function to turn a matrix into a vector of values in row-major order for passing to the NetCDF C API. The flatten function has type signature

flatten :: Element t => Matrix t -> Vector t

so that Element constraint somehow has to get into NcStore, but only for cases when the “store” is a hmatrix matrix.

Constraint kinds to the rescue

At this point, all the real Haskell programmers are asking what the big deal is. You just switch on the ConstraintKinds and TypeFamilies extensions and rewrite NcStore like this:

class NcStore s where type NcStoreExtraCon s a :: Constraint type NcStoreExtraCon s a = () toForeignPtr :: (Storable e, NcStoreExtraCon s e) => s e -> ForeignPtr e fromForeignPtr :: (Storable e, NcStoreExtraCon s e) => ForeignPtr e -> [Int] -> s e smap :: (Storable a, Storable b, NcStoreExtraCon s a, NcStoreExtraCon s b) => (a -> b) -> s a -> s b

Here, I’ve added an associated type called NcStoreExtraCon s a, which is a constraint, I’ve given a default for this (of (), which is a “do nothing” empty constraint), and I’ve added the relevant constraint to each of the methods of NcStore. The NcStore instances for storable Vectors and Repa arrays look the same as before, but the instance for hmatrix matrices now looks like this:

instance NcStore HMatrix where type NcStoreExtraCon HMatrix a = C.Element a toForeignPtr (HMatrix m) = fst3 $ unsafeToForeignPtr $ C.flatten m fromForeignPtr p s = let c = last s d = product s in HMatrix $ matrixFromVector RowMajor (d `div` c) (last s) $ unsafeFromForeignPtr p 0 (Prelude.product s) smap f (HMatrix m) = HMatrix $ C.mapMatrix f m

I’ve just added the Element constraint on the type of values contained in the “store” to the instance, and I can then use any hmatrix function that requires this constraint without any trouble: you can see the use of flatten in the toForeignPtr method definition.


The problem I had here is really just an instance of what’s come to be called the “restricted monad” problem. This is where you have a type class, possibly with constraints, and you want to write instances of the class where you impose additional constraints. The classic case is making Set a monad: Set requires its elements to be members of Ord, but Monad is fully polymorphic, and so there’s no way to make an instance of something like Ord a => Monad (Set a).

There’s even a package on Hackage called rmonad that uses just this “constraint kinds + associated types” approach to allow you to write “restricted monads” of this kind. So this appears to be a well-known method, but it was fun to rediscover it. The ability to combine these two language extensions in this (to me) quite unexpected way is really rather satisfying!

haskell <script src="" type="text/javascript"></script> <script type="text/javascript"> (function () { var articleId = fyre.conv.load.makeArticleId(null); fyre.conv.load({}, [{ el: 'livefyre-comments', network: "", siteId: "290329", articleId: articleId, signed: false, collectionMeta: { articleId: articleId, url: fyre.conv.load.makeCollectionUrl(), } }], function() {}); }()); </script>
Categories: Offsite Blogs

Philip Wadler: Multiple inheritance

Planet Haskell - Sun, 03/08/2015 - 10:42am
Retweeted by Don Syme.
Categories: Offsite Blogs

raspberrypi and ghci

haskell-cafe - Sun, 03/08/2015 - 8:24am
Hello, I have a B++ pi. I did a "apt-get install haskell-platform. No " ghci"!. So I did a google on this issue. Circa 2013 ghci seemed to be an issue with raspberrypi/arm. What is the current status of "ghci" on the pi/arm??. Vasili _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Missing instances for Eq1 (Const a), Ord1 (Const a), Show1 (Const a), Ord1 (Const a)

libraries list - Sat, 03/07/2015 - 9:39pm
I was looking at switching a chunk of my code over from prelude-extras to transformers 0.5, but the classes in are currently missing instances for Control.Applicative.Const. =/ -Edward _______________________________________________ Libraries mailing list Libraries< at >
Categories: Offsite Discussion

[Announce] New release of SBV is out

haskell-cafe - Sat, 03/07/2015 - 8:18pm
A new release of SBV is out (v4.1): The main change in this release is the connection to Berkeley's ABC <> system, which is an industrial strength synthesis and verification system. SBV can now generate proof goals that are discharged by ABC, using state-of-the-art SAT based techniques. ABC is especially extremely well-tuned for dealing with problems in the bit-vector theory, and is heavily used in the hardware industry as a backend verification tool. Also included in this release is better support for IEEE-floats, and a reworked implementation of the connection to the Boolector <> solver. Thanks to Adam Foltzer of Galois for contributing the connection to ABC, and Alan Mischenko from Berkeley to add the necessary infrastructure to ABC to allow for this bridge to be built. Full release notes: SBV web page:
Categories: Offsite Discussion

Any Haskell-OpenGL tutorial including shader compilation, buffer usage and so on?

Haskell on Reddit - Sat, 03/07/2015 - 7:23pm

Does anyone who has already worked with Haskell's OpenGL library know if there is a tutorial including shader compilation, uploading buffers and so on? If not, what is your approach to using OpenGL's lib? What documentation are you using, and how do you link its information to the lib (the high-level wrapper, not the raw bindings).


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

ghc -O2 out of memory

haskell-cafe - Sat, 03/07/2015 - 5:56pm
I've written a program that reads a Excel file, parses the contents and generates a haskell module. Every cell in the xls has it's own function in the module. This worked fine. Now I have a bigger xls. The resulting module Xls.hs contains 30000 lines haskell code (without comment). I created a testprogram test.hs that calculates and prints one cell. I did ghc --make test.hs and everything works fine. The generated execuatable is very slow. I did ghc --make -O2 test.hs. The compilations takes 15 minutes and aborts with 'ghc: out of memory'. I'am working on Win 7 64 bits , ghc version = 7.8.3. What can I do to fix this? I do need -O2. I found with smaller xls's that optimization speeds up the executable dramatically. Before I start experimenting, does it help to split up the xls .hs in seperate files? I.e. the cells that refer to other cells and cells that don't refer to other cells. Or will I still get 'out of memory' because the optimization is global? Kees ______________________________
Categories: Offsite Discussion

-staticlib flag for building standalone static libraries producing very large libraries

glasgow-user - Sat, 03/07/2015 - 1:18pm
Hi all, Can anyone explain the following problem I'm having? I'm currently writing a game in Haskell. When I produce a plain old executable (for local testing) it's about 23M. However, when I create a static lib using the -staticlib flag it is 54M. Why the discrepancy? Sean _______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users< at >
Categories: Offsite Discussion

New release of SBV (v4.1)

Haskell on Reddit - Sat, 03/07/2015 - 12:25pm
Categories: Incoming News

Philip Wadler: A paean to the Western General

Planet Haskell - Sat, 03/07/2015 - 11:49am

I resided in Ward 42 of the Regional Infectious Disease Unit of Edinburgh's Western General Hospital from 17 Dec—2 Jan. For most of the time I received antibiotic by drip or injection every four hours, day and night, and I thought my stay would be six weeks. Fortunately, my infection reacted well to antibiotic and I could be released early for outpatient treatment.

The building is ugly, but the people inside it are beautiful. I cannot recall another time or place where everyone was so unfailingly helpful and friendly. On the first day, a nurse found a staff member willing to lend me a charger when my phone ran down. The doctors took as much time as needed to answer my questions. The nurses were cheerful despite constant interruptions. The men who brought the coffee remembered that I liked it with milk, and one often asked after my twins (he had twins as well). No one objected when my daughter brought me a poster of the Justice League and I blue-tacked to my wall; several admired it.

Most often, I interact with institutions where the people who help you are, clearly, in it for the pay. They are nice only to the extent required by their job, and often less than that. Part of the difference in attitude here must be that the people know they are actively helping patients in need. I take my hat off to an institution that inculcates a caring attitude in everyone.

(Picture above courtesy of The Edinburgh Sketcher, whose pictures adorn a couple of corridors in the Western General. The RIDU is a different building, though somehow every building in the complex contrives to be equally unattractive.)

Related: Status report, Status report 2.
Categories: Offsite Blogs

A step by step tutorial for a Web Framework

Haskell on Reddit - Sat, 03/07/2015 - 10:32am

I've been learning Haskell and would like to have a go at actually writing something I can use. I've worked through LYAH and read several other tutorials and books. Unfortunately I can't find a step by step, explanative tutorial for any of the web frameworks. There is the Yesod book but it didn't give me a good foundation for building something of my own.

I'm looking for something akin to this (Python, Pyramid).

Is there anything like this written already?


submitted by Teifion
[link] [26 comments]
Categories: Incoming News

SPDX and hackage

haskell-cafe - Fri, 03/06/2015 - 11:41pm
I stumbled across the SPDX ( Possibly the hackage license list should be expanded to incorporate this in some way? Or at least the OSI-approved entries? _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Data.Graph (and Data.Tree) improvements

libraries list - Fri, 03/06/2015 - 11:38pm
Data.Graph has always been a bit of an unloved module in containers. However, at GHC HQ the graph has always been a bit of an important data type, and we've been maintaining basically another version of the module under the name Digraph. I was refactoring this module to use Data.Graph and I noticed that there were a few functions which were conspicuously missing from the module proper. So here they are: For Data.Graph: - Generalized reachable :: Graph -> [Vertex] -> [Vertex] (compare to reachable :: Graph -> Vertex -> [Vertex] - Returns an undirected version of a graph which, for any edge a -> b, an edge b -> a also exists. undirected :: Graph -> Graph - Extract a list of groups of elements of a graph such that each group has no dependence except on nodes in previous groups (i.e. they may not depend on nodes in their own group) such that the groups are maximal. No solution exists for cyclic graphs vertexGroups :: Graph -> [[Vertex]]
Categories: Offsite Discussion