News aggregator
Question about `MonadIO m` and `MonadBaseControl IO m`
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) = xnote 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]
-XPolyKinds and undefined arguments
FP activities and researchers in Warwickshire
Domain Specific Languages and better Error messages
[ANNOUNCE] JuicyPixels 1.1 - image loading library(maintenance release)
Twan van Laarhoven: Search trees without sorting
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.
BoundsUsually, 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 = aThe 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 == abecause
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.
ImplementationGiven 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 _ _ = NothingCompletely 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 _ _ = NothingOr 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.
ConstructingThe 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 yA 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) xsAnd 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 usesHere 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) . getMaxFirst, 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]) NothingSemilattices 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 dNow 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 bThat 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) xs3Now suppose that we are searching a tree of the form st = mkBranch a b with findFirst q. Then there are three cases:
- not (satisfy q (bound st)).
- not (satisfy q (bound a)).
- 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 awhere satisfy1 has exact bounds, and the tree is sorted for satisfy2, then queries still take O(log n) time.
Closing exampleFinally, 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") -- etcThe 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")Haskell mode for Emacs - HaskellWiki
This FTP site
What is the canonical way to check that all items are the same?
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 _ = TrueI'm gonna kick myself, I know it.
submitted by ithika[link] [14 comments]