News aggregator

Question about `MonadIO m` and `MonadBaseControl IO m`

Haskell on Reddit - Tue, 02/07/2012 - 10:45pm

I'm trying to get Snap and MongoDB to work together and I need an instance for MonadBaseControl IO Snap and I'm having trouble understanding why the following code typechecks but won't run correctly (it times out, but if I run the database query inside the snap monad, rather than wrapping the Snap monad in MongoDB's Action transformer, it works fine, so it's not the db connection):

instance MonadBaseControl IO Snap where newtype StM Snap a = STio (Snap a) liftBaseWith f = liftIO (f $ return . STio) restoreM (STio x) = x

note that you need FlexibleInstances. As a bonus, if someone could explain why replacing liftBaseWith above with liftIO . f $ return . STio doesn't typecheck, I would appreciate it.

submitted by kcuf
[link] [comment]
Categories: Incoming News

-XPolyKinds and undefined arguments

haskell-cafe - Tue, 02/07/2012 - 10:19pm
There are all sorts of useful functions that would otherwise require explicit type applications which we work around by passing undefined and ignoring its value. (See http://www.haskell.org/pipermail/libraries/2010-October/014742.html for a prior discussion of why this is, how the use of undefined for something that is perfectly well defined smells, and various suggestions about one day changing it.) Examples: Data.Typeable.typeOf, Foreign.Storable.sizeOf, etc. It seems to me that the new -XPolyKinds extension might provide a good long-term solution to statically insuring that arguments like this are always undefined and are never inspected. If we had a library definition: -- a kind-indexed family of empty types, Proxy :: AnyK -> * data Proxy a then we could write things that resemble type applications such as "sizeOf < at >Int64". Unlike the current approach, we'd be assured that nobody tries to use the value of such a proxy in any meaningful way, since you can't pattern match it or pass a "Proxy t" in
Categories: Offsite Discussion

FP activities and researchers in Warwickshire

haskell-cafe - Tue, 02/07/2012 - 8:32pm
Hello, I recently moved to Kenilworth, Warwickshire, UK, and I'd like to know if there are meetings, talks, or any FP-related activities going on around here. I contacted somebody at Warwick University but, from what I understood, their Formal Methods group doesn't exist as such any longer and they don't carry out any coordinated, regular events regarding FP anymore. Cheers, Ivan
Categories: Offsite Discussion

Domain Specific Languages and better Error messages

haskell-cafe - Tue, 02/07/2012 - 8:09pm
This week, at work, I've been attending a workshop which is concerned with writing (actually rewriting) a domain specific language that we've been using. Although I've only known Haskell for four months, I can see that it's infected my brain, since I kept seeing Haskell solutions to all sorts of problems that were coming up. This DSL was written by two demon programmers in C. They've made many ad-hoc choices which look rather ugly to me. Even worse, they've invented a very primitive type system which would only be checked at run time. This looks particularly bad since the applications that we'd be running are very large parallel computations. I would like to make a try at writing an alternative based on Haskell, even though there's probably little chance that I could get it adopted (since no-one else here is familiar with functional programs, etc.). However, there is one issue that I'd like to address. I was looking at http://donsbot.wordpress.com/2007/03/10/practical-haskell-shell-scripting-with-e
Categories: Offsite Discussion

[ANNOUNCE] JuicyPixels 1.1 - image loading library(maintenance release)

General haskell list - Tue, 02/07/2012 - 8:06pm
Hello list, This is a maintenance release without new functionality, the central change is the switch from Haskell Array to Data.Vector (to ease it's use for OpenGL texture loading). Juicy.Pixels 1.1 change log : - Fixing automatic bitmap decoding, the decodeImage function wasn't trying to decode bitmap - Converting Haskell array types to Data.Vector (thanks to Jason Dagit) - Some tiny documentation enhancement Hackage : http://hackage.haskell.org/package/JuicyPixels GitHub : https://github.com/Twinside/Juicy.Pixels Thanks Vincent Berthoux
Categories: Incoming News

Twan van Laarhoven: Search trees without sorting

Planet Haskell - Tue, 02/07/2012 - 4:56pm

Binary search trees are used quite often for storing or finding values. Like a binary search, they essentially work by sorting the items.

In this post I will describe a search tree that does not require that the items be sorted. Hence, the tree can support some interesting queries. The queries will always be correct, but they will only be fast in some cases.

Bounds

Usually, to make searching fast, each branch in a search tree stores information that helps to decide whether to go left or right. But if we want to be able to construct a tree for any possible type of query, then that is not always possible. Instead, we can still aim to eliminate large parts of the search space, by storing bounds.

Suppose we have a tree that stores integers, and we want to find the first item in the tree that is greater or equal to some query integer. In each branch of the tree, we could store the maximum of all values in that subtree. Call it the upper bound of the subtree. If this upper bound is less than the query, then we can eliminate the entire subtree from consideration.

Now let's generalize that. The maximum value is an example of a semilattice. That is just a fancy way of saying that for a pair of values we can get some kind of bound. As a typeclass it looks like

class Semilattice a where meet :: a -> a -> a -- Laws: meet is associative, commutative and idempotent: -- meet a (meet b c) = meet (meet a b) c -- meet a b = meet b a -- meet a a = a

The queries we perform on the tree should of course work together with the bounds. That means that if a bound for a branch in the tree doesn't satisfy the query, then none of the values in the subtree do. In haskell terms:

class Semilattice a => Satisfy q a | q -> a where satisfy :: q -> a -> Bool -- Law: satisfy q a || satisfy q b ==> satisfy q (meet a b)

Note that a semilattice always gives a partial order, and hence a satisfy function by

satisfy q a = meet q a == a

because

satisfy q a || satisfy q b <=> meet q a == a || meet q b == b ==> meet (meet q a) b == meet a b || meet a (meet q b) == meet a b <=> meet q (meet a b) == meet a b || meet q (meet a b) == meet a b <=> meet q (meet a b) == meet a b <=> satisfy q (meet a b)

However, I keep the distinction between the query and value type for more flexibility and for more descriptive types.

Implementation

Given the Satisfy and Semilattice typeclasses, the search tree datastructure is straight forward. A search tree can be empty, a single value, or a branch. In each branch we store the bound of that subtree.

data SearchTree a = Empty | Leaf !a | Branch !a (SearchTree a) (SearchTree a) deriving (Show) bound :: SearchTree a -> a bound (Leaf a) = a bound (Branch a _ _) = a bound Empty = error "bound Empty"

If we have a SearchTree, then we can find the first element that satisfies a query, simply by searching both sides of each branch. The trick to making the search faster is to only continue as long as the bound satisfies the query:

-- Find the first element in the tree that satisfies the query findFirst :: Satisfy q a => q -> SearchTree a -> Maybe a findFirst q (Leaf a) | satisfy q a = Just a findFirst q (Branch a x y) | satisfy q a = findFirst q x `mplus` findFirst q y findFirst _ _ = Nothing

Completely analogously, we can find the last satisfied item instead:

-- Find the last element in the tree that satisfies the query findLast :: Satisfy q a => q -> SearchTree a -> Maybe a findLast q (Leaf a) | satisfy q a = Just a findLast q (Branch a x y) | satisfy q a = findLast q y `mplus` findLast q x findLast _ _ = Nothing

Or we can even generalize this search to any Monoid, where the above are for the First and Last monoids respectively. I will this leave as an exercise for the reader.

Constructing

The basis of each tree are branches. We will always construct branches with a smart constructor that calculates the bound as the meet of the bounds of its two arguments. That way, the stored bound is always correct.

mkBranch :: Semilattice a => SearchTree a -> SearchTree a -> SearchTree a mkBranch Empty y = y mkBranch x Empty = x mkBranch x y = Branch (bound x `meet` bound y) x y

A search will always take time at least linear in the depth of the tree. So, for fast searches we need a balanced tree, where each subtree has roughly the same size. Here is arguably the most tricky part of the code, which converts a list to a balanced search tree.

-- /O(n*log n)/ -- Convert a list to a balanced search tree fromList :: Semilattice a => [a] -> SearchTree a fromList [] = Empty fromList [x] = Leaf x fromList xs = mkBranch (fromList ys) (fromList zs) where (ys,zs) = splitAt (length xs `div` 2) xs

And that's it. I use this data structure for finding rectangles (more about that in a future post), and there I only needed to build the search structure once, and use it multiple times. So, in this post I am not going to talk about updates at all. If you wanted to do updates efficiently, then you would need to worry about updating bounds, rebalancing etc.

Example uses

Here is an example of the search tree in action. The query will be to find a value (>= q) for a given q. The bounds will be maximum values.

newtype Max a = Max { getMax :: a } deriving (Show) instance Ord a => Semilattice (Max a) where meet (Max a) (Max b) = Max (max a b) newtype Ge a = Ge a deriving (Show) instance Ord a => Satisfy (Ge a) (Max a) where satisfy (Ge q) = (>= q) . getMax

First, check the satisfy law:

satisfy (Ge q) (Max a) || satisfy (Ge q) (Max b) <=> a >= q || b >= q <=> if a >= b then a >= q else b >= q <=> (if a >= b then a else b) >= q <=> max a b >= q <=> satisfy (Ge q) (Max (max a b)) <=> satisfy (Ge q) (meet (Max a) (Max b))
The search tree corresponding to fromList (map Max [1..5]). Circles are Leafs and squares are Branches.

So indeed, satisfy q a || satisfy q b ==> satisfy q (meet a b). And this bound is in fact tight, so also the other way around satisfy q (meet a b) ==> satisfy q a || satisfy q b. This will become important later.

Now here are some example queries:

λ> findFirst (Ge 3) (fromList $ map Max [1,2,3,4,5]) Just (Max 3) λ> findFirst (Ge 3) (fromList $ map Max [2,4,6]) Just (Max 4) λ> findFirst (Ge 3) (fromList $ map Max [6,4,2]) Just (Max 6) λ> findFirst (Ge 7) (fromList $ map Max [2,4,6]) Nothing

Semilattices and queries can easily be combined into tuples. For a tree of pairs, and queries of pairs, you could use.

instance (Semilattice a, Semilattice b) => Semilattice (a,b) where meet (a,b) (c,d) = (meet a c, meet b d) instance (Satisfy a b, Satisfy c d) => Satisfy (a,c) (b,d) where satisfy (a,c) (b,d) = satisfy a b && satisfy c d

Now we can not only questions like "What is the first/last/smallest element that is greater than some given query?". But also "What is the first/last/smallest element greater than a given query that also satisfies some other property?".

When is it efficient?

It's nice that we now have a search tree that always gives correct answers. But is it also efficient?

As hinted in the introduction, that is not always the case. First of all, meet could give a really bad bound. For example, if meet a b = Bottom for all a /= b, and Bottom satisfies everything, then we really can do no better than a brute force search.

On the other hand, suppose that meet gives 'perfect' information, like the Ge example above,

satisfy q (meet a b) ==> satisfy q a || satisfy q b

That is equivalent to saying that

not (satisfy q a) && not (satisfy q b) ==> not (satisfy q (meet a b))

Then for any Branch, we only have to search either the left or the right subtree. Because, if a subtree doesn't contain the value, we know can see so from the bound. For a balanced tree, that means the search takes O(log n) time.

Another efficient case is when the items are sorted. By that I mean that, if an item satisfies the query, then all items after it also satisfy that query. We actually need something slightly more restrictive: namely that if a query is satisfied for the meet of some items, then all items after them also satisfy the query. In terms of code:

let st = fromList (xs1 ++ xs2 ++ xs3) satisfy q (meet xs2) ==> all (satisfy q) xs3

Now suppose that we are searching a tree of the form st = mkBranch a b with findFirst q. Then there are three cases:

  1. not (satisfy q (bound st)).
  2. not (satisfy q (bound a)).
  3. satisfy q (bound a).

In the first case the search fails, and we are done. In the second case, we only have to search b, which by induction can be done efficiently. The third case is not so clear. In fact, there are two sub cases:

  • 3a. findFirst q a = Just someResult
  • 3b. findFirst q b = Nothing

In case 3a we found something in the left branch. Since we are only interested in the first result, that means we are done. In case 3b, we get to use the fact that the items are sorted. Since we have satisfy q (bound a), that means that all items in b will satisfy the query. So when searching b, in all cases we take the left branch.

Overall, the search time will be at most twice the depth of the tree, which is O(log n).

The really cool thing is that we can combine the two conditions. If satisfy can be written as

satisfy q a == satisfy1 q a && satisfy2 q a

where satisfy1 has exact bounds, and the tree is sorted for satisfy2, then queries still take O(log n) time.

Closing example

Finally, here is an example that makes use of efficient searching with the two conditions. I make use of the Semilattice and Satisfy instances for pairs which I defined above.

treeOfPresidents :: SearchTree (Max Int, Max String) treeOfPresidents = fromList [ (Max year, Max name) | (year,name) <- usPresidents ] where usPresidents = [(1789,"George Washington") ,(1797,"John Adams") ,(1801,"Thomas Jefferson") -- etc

The tree is ordered by year of election, and the Max semilattice gives tight bounds for names. So we can efficiently search for the first US presidents elected after 1850 who's name comes starts with a letter after "P":

λ> findFirst (Ge 1850,Ge "P") treeOfPresidents Just (Max 1869,Max "Ulysses S. Grant")

And with the following query type we can search on just one of the elements of the tuple. Note that we need the type parameter in Any because of the functional dependency in the Satisfy class.

data Any s = Any instance Semilattice s => Satisfy (Any s) s where satisfy _ _ = True λ> findFirst (Ge 1911,Any) treeOfPresidents Just (Max 1913,Max "Woodrow Wilson")
Categories: Offsite Blogs

This FTP site

del.icio.us/haskell - Tue, 02/07/2012 - 2:55pm
Categories: Offsite Blogs

What is the canonical way to check that all items are the same?

Haskell on Reddit - Tue, 02/07/2012 - 2:46pm

I don't know of a simple way to recreate the following function without doing it manually and I feel a bit daft.

same :: Eq a => [a] -> Bool same (x:y:zs) = x==y && same (y:zs) same _ = True

I'm gonna kick myself, I know it.

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

auto-orphans?

glasgow-user - Tue, 02/07/2012 - 1:32pm
Hi, in http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/options-sanity.html I've found no description for -fwarn-auto-orphans Cheers Christian
Categories: Offsite Discussion

XMonad.Doc.Extending

del.icio.us/haskell - Tue, 02/07/2012 - 1:08pm
Categories: Offsite Blogs

Effective Scala

Lambda the Ultimate - Tue, 02/07/2012 - 12:41pm

It's from Twitter, It's open source, It's about Scala. What's not to like?

Categories: Offsite Discussion

Haskell - HaskellWiki

del.icio.us/haskell - Tue, 02/07/2012 - 12:25pm
Categories: Offsite Blogs