Re: [Haskell-cafe] Stack space overflow in HaskellNet
On Mon, 25 Jul 2011, Manfred Lotz wrote: Hi there, If I take example imap.hs import System.IO import Network.HaskellNet.IMAP import Text.Mime import qualified Data.ByteString.Char8 as BS import Control.Monad -- the next lines were changed to fit to my local imap server imapServer = imap.mail.org user = pass = main = do con - connectIMAP imapServer login con user pass mboxes - list con mapM print mboxes This should be mapM_ and 'ghc -Wall' spots this problem since 6.12. select con INBOX msgs - search con [ALLs] mapM_ (\x - print x) (take 4 msgs) forM_ (take 4msgs) (\x - fetch con x = print) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ICFP 2011: Call for participation
= Call for Participation The 16th ACM SIGPLAN International Conference on Functional Programming (ICFP 2011) http://www.icfpconference.org/icfp2011/ Tokyo, Japan September 19-21, 2011 = ICFP provides a forum for researchers and developers to hear about the latest work on the design, implementations, principles, and uses of functional programming. The conference covers the entire spectrum of work, from practice to theory, including its peripheries. * Program: http://www.icfpconference.org/icfp2011/program.html * Registration link: https://regmaster3.com/2011conf/ICFP11/register.php * Local arrangements (including travel and accommodation): http://www.biglab.org/icfp11local/index.html Schedule including related events: September 18 Workshop on Generic Programming (WGP) Workshop on High-Level Parallel Programming and Applications (HLPP) Workshop on ML September 19-21 ICFP September 22 Commercial Users of Functional Programming – Day 1 (CUFP Tutorials) Haskell Symposium September 23 Commercial Users of Functional Programming – Day 2 (CUFP Tutorials) Erlang Workshop Haskell Implementors' Workshop September 24 Commercial Users of Functional Programming – Day 3 (CUFP Talks) Continuation Workshop Conference organizers: * General Co-Chairs: Manuel Chakravarty, University of New South Wales Zhenjiang Hu, National Institute of Informatics * Program Chair: Olivier Danvy, Aarhus University * Local Arrangements Chair: Soichiro Hidaka, National Institute of Informatics * Workshop Co-Chairs: Gabriele Keller, University of New South Wales Derek Dreyer, MPI-SWS * Programming Contest Chair: Eijiro Sumii, Tohoku University * Publicity Chair: Wouter Swierstra, Radboud Universiteit Nijmegen = ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Why the reluctance to introduce the Functor requirement on Monad?
I'll give my two cents about some design I've been thinking about. Instead of trying to derive all instances automatically, the programmer should explicitly tell them (so the problems about conflicting implementations would be minimised). I attach a piece of code of what I think could be done: instance Functor a = Monad a where -- notice the reversed = fmap = ... from Monad MyMonad derive Functor MyMonad With the from_derive_ clause, we are telling exactly from which = declaration to pull the definition from. The part of from should have already been written or derived, so we know exactly which instance the user is speaking about. More refinements to the syntax could be done, for example if we have: instance Functor a = Applicative a where fmap = .. instance Applicative a = Monad a where pure = ... (*) = ... Then, writing from Monad MyMonad derive Functor MyMonad would go through the entire tree of reverse instance declarations and create instances for Applicative, and from that a Functor one (of course, this should fail if we have more than one path, then the user should write the path explicitly as from Monad M derive Applicative M; from Applicative M derive Functor M). But it has the advantage of allowing later addition of classes in the path, that would be derived when recompiling the code that uses it. 2011/7/25 Ryan Ingram ryani.s...@gmail.com My guess is that nobody has put forward a clear enough design that solves all the problems. In particular, orphan instances are tricky. Here's an example: module Prelude where class (Functor m, Applicative m) = Monad m where return :: a - m a (=) :: m a - (a - m b) - m b () :: m a - m b - m b a b = a = const b pure = return (*) = ap fmap = liftM module X where data X a = ... module Y where instance Functor X where fmap = ... module Z where instance Monad X where return = ... (=) = ... -- default implementation of fmap brought in from Monad definition module Main where import X import Z foo :: X Int foo = ... bar :: X Int bar = fmap (+1) foo -- which implementation of fmap is used? The one from Y? -- ryan On Sun, Jul 24, 2011 at 8:55 PM, Ivan Lazar Miljenovic ivan.miljeno...@gmail.com wrote: On 25 July 2011 13:50, Sebastien Zany sebast...@chaoticresearch.com wrote: I was thinking the reverse. We can already give default implementations of class operations that can be overridden by giving them explicitly when we declare instances, so why shouldn't we be able to give default implementations of operations of more general classes, which could be overridden by a separate instance declaration for these? Then I could say something like a monad is also automatically a functor with fmap by default given by... and if I wanted to give a more efficient fmap for a particular monad I would just instantiate it as a functor explicitly. I believe this has been proposed before, but a major problem is that you cannot do such overriding. -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Retaining functions in memory
I have been making programs for mathematical applications (low-dimensional topology) in Haskell, which I find a delight to code in. However, the execution is slow, and this seems to be because recursively defined functions seem to be recomputed. For example f(100) needs f(15) which needs f(7) ... The dependencies are not obvious to the compiler. I was looking for a way to retain the values of a specific function in memory. Is there some way to do this. Thanks, Siddhartha ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Retaining functions in memory
2011/7/26 Siddhartha Gadgil siddhartha.gad...@gmail.com: I have been making programs for mathematical applications (low-dimensional topology) in Haskell, which I find a delight to code in. However, the execution is slow, and this seems to be because recursively defined functions seem to be recomputed. For example f(100) needs f(15) which needs f(7) ... The dependencies are not obvious to the compiler. I was looking for a way to retain the values of a specific function in memory. Is there some way to do this. It seems you are looking for memoization. Have a look at the comparison between slow/memoized_fib at this page http://www.haskell.org/haskellwiki/Memoization#Memoization_with_recursion Cheers, Thu ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Retaining functions in memory
I was looking for a way to retain the values of a specific function in memory. Is there some way to do this. Maybe this helps: http://www.haskell.org/haskellwiki/Memoization I haven't read through it, though.. Cheers, Simon ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Effects of Monads in Parallelisation
Dear List, I am currently trying to understand the effects of monads in the sense of parallelisation in Haskell. Could somebody please explain the difference between 'rpar' and 'par'? I mean, what has been changed after the encapsulation of 'par' function by Eval monad? If you asked to compare the parallelisation via monads with non-monadic manner of it, what could you say? Many thanks in advance. All the best, Burak. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Why the reluctance to introduce the Functor requirement on Monad?
On Jul 25, 2011, at 4:55 PM, Ryan Ingram wrote: My guess is that nobody has put forward a clear enough design that solves all the problems. In particular, orphan instances are tricky. Here's an example: module Prelude where class (Functor m, Applicative m) = Monad m where return :: a - m a (=) :: m a - (a - m b) - m b () :: m a - m b - m b a b = a = const b pure = return (*) = ap fmap = liftM module X where data X a = ... module Y where instance Functor X where fmap = ... module Z where instance Monad X where return = ... (=) = ... -- default implementation of fmap brought in from Monad definition module Main where import X import Z foo :: X Int foo = ... bar :: X Int bar = fmap (+1) foo -- which implementation of fmap is used? The one from Y? I don't believe it would make orphan instances any trickier than they already are. If Functor m = Monad m, you can't have Monad m without Functor m, so module Z must introduce Functor m either implicitly or explicitly or it cannot compile. Viewed from outside a module, the problem is the same either way. I would propose that viewed from outside a module, an implicitly declared instance should be indistinguishable from an explicitly declared one, and within a module the implicit instance would be generated if and only if there is no overlapping instance in scope. An additional warning flag could be added to warn people who are worried about it that they have implicitly created an orphan instance for a superclass. The only real problem I see relating to orphans is in cases where old code declares an orphan Monad instance for a type without a Functor instances, something which I don't think happens very often (except perhaps with Either, but forcing a solution to that hornet's nest would be a Good Thing IMO). But either way, that breakage is more related to the superclass change than to any new means of declaring instances; even without the latter, the former would force those modules to introduce orphan Functor instances explicitly (or to introduce non-orphans somewhere to avoid doing so) -- James -- ryan On Sun, Jul 24, 2011 at 8:55 PM, Ivan Lazar Miljenovic ivan.miljeno...@gmail.com wrote: On 25 July 2011 13:50, Sebastien Zany sebast...@chaoticresearch.com wrote: I was thinking the reverse. We can already give default implementations of class operations that can be overridden by giving them explicitly when we declare instances, so why shouldn't we be able to give default implementations of operations of more general classes, which could be overridden by a separate instance declaration for these? Then I could say something like a monad is also automatically a functor with fmap by default given by... and if I wanted to give a more efficient fmap for a particular monad I would just instantiate it as a functor explicitly. I believe this has been proposed before, but a major problem is that you cannot do such overriding. -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Why the reluctance to introduce the Functor requirement on Monad?
On Tue, Jul 26, 2011 at 1:01 PM, Alejandro Serrano Mena trup...@gmail.com wrote: I'll give my two cents about some design I've been thinking about. Instead of trying to derive all instances automatically, the programmer should explicitly tell them (so the problems about conflicting implementations would be minimised). I attach a piece of code of what I think could be done: instance Functor a = Monad a where -- notice the reversed = fmap = ... from Monad MyMonad derive Functor MyMonad With the from_derive_ clause, we are telling exactly from which = declaration to pull the definition from. The part of from should have already been written or derived, so we know exactly which instance the user is speaking about. More refinements to the syntax could be done, for example if we have: instance Functor a = Applicative a where fmap = .. instance Applicative a = Monad a where pure = ... (*) = ... Then, writing from Monad MyMonad derive Functor MyMonad would go through the entire tree of reverse instance declarations and create instances for Applicative, and from that a Functor one (of course, this should fail if we have more than one path, then the user should write the path explicitly as from Monad M derive Applicative M; from Applicative M derive Functor M). But it has the advantage of allowing later addition of classes in the path, that would be derived when recompiling the code that uses it. I want to support explicit intance derivation. But I'd like to suggest slightly less radical syntax extention: -- class definition: class Fuctor m = Monad m where return :: a - m a (=) :: m a - (a - m b) - m b () :: m a - m b - m b join :: m (m a) - m a -- default implementations: a b = a = (\_ - b) a = f = join . fmap f $ a join a = a = id -- default instances: instance Functor m where fmap f a = a = (return . f) newtype Reader a b = Reader { runReader :: a - b } -- instace declaration: instance Monad (Reader r) where return = Reader . const m = f = Reader $ \r - runReader (f (runReader m r)) r deriving (Functor) So syntax changes are very minor. -- Victor Nazarov ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Fwd: Re: How to select last inserted record from Table Using Database.HSQL.MySQL
Forwarding to list Original Message Subject:Re: [Haskell-cafe] How to select last inserted record from Table Using Database.HSQL.MySQL Date: Tue, 26 Jul 2011 14:27:56 +0300 From: Sergiy Nazarenko nazarenko.ser...@gmail.com To: Steffen Schuldenzucker sschuldenzuc...@uni-bonn.de Thanx a lot! I could solve that problem in this way: cmd = INSERT INTO mytable (bar,foo) VALUES (val1,val2); SELECT LAST_INSERT_ID() as id; lst - query connection cmd = collectRows (\st - getFieldValue st id) lst has required value Cheers! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Stack space overflow in HaskellNet
On Tue, 26 Jul 2011 10:17:22 +0200 (CEST) Henning Thielemann lemm...@henning-thielemann.de wrote: On Mon, 25 Jul 2011, Manfred Lotz wrote: Hi there, If I take example imap.hs import System.IO import Network.HaskellNet.IMAP import Text.Mime import qualified Data.ByteString.Char8 as BS import Control.Monad -- the next lines were changed to fit to my local imap server imapServer = imap.mail.org user = pass = main = do s con - connectIMAP imapServer login con user pass mboxes - list con mapM print mboxes This should be mapM_ and 'ghc -Wall' spots this problem since 6.12. The compiler (7.04) doesn't tell me anything about it. select con INBOX msgs - search con [ALLs] mapM_ (\x - print x) (take 4 msgs) forM_ (take 4msgs) (\x - fetch con x = print) I'm not quite sure I understand what you mean. Stack overflow comes from this: forM_ msgs (\x - fetch con x = print) If I change it to: mapM_ (\x - fetch con x = print) msgs there is the same stack overflow. -- Thanks, Manfred ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Effects of Monads in Parallelisation
2011/7/26 Burak Ekici ekcbu...@hotmail.com what has been changed after the encapsulation of 'par' function by Eval monad? If you asked to compare the parallelisation via monads with non-monadic manner of it, what could you say? 'Eval' provides some useful discipline and structure. It allows you to more clearly start sparks or sequence certain evaluations before you progress past the 'using' or 'withStrategy' directive, even if you don't need the output right away. rpar x = x `par` return x rseq x = x `pseq` return x data Eval a = Done a runEval :: Eval a - a runEval (Done x) = x instance Monad Eval where return x = Done x Done x = k = k x You will eventually 'runEval' to extract the evaluated result. This is strict on the monad itself - i.e. you pattern-match against 'Done' in both 'runEval' and in the binding operator (=). Therefore, you know that ALL your 'rseq' and 'rpar' statements have finished executing before you get past 'runEval'. Without use of a monad, this is a bit more difficult - i.e. a use of 'par' might be buried deep in some lazy evaluation, unless you're very careful. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?
Hi all, I'm currently embarking on my first major project in Haskell, after dabbling with it for several years, and seem to keep finding myself in situations where I create a typeclass that seems to be some sort of specialisation of another, more general typeclass. Given that this is the case, I've then decided that all instances of the specific class should therefore also be instances of the general class, and arrived at the following method of doing so, using the FlexibleInstances and UndecidableInstances extensions to GHC: {-# LANGUAGE FlexibleInstances, UndecidableInstances #-} class Max a where maximum :: a - a - a instance (Ord a) = Max a where maximum = max (Obviously, this is a very trivial, and rather silly example - I'm not really trying to implement a class called 'Max'). However, I'd be curious to know if (a) There are better or more idiomatic ways of achieving the same effect, and (b) Whether or not I should be doing this at all; It did occur to me that this seems rather trying to re-implement OOP-style inheritance with typeclasses, and therefore perhaps not a very Haskellish approach to designing software. Therefore - are there better ways to achieve this, or should I not be doing this at all, and, if the latter, what would be the best means of achieving a similar result (i.e. a typeclass that implements all the functionality of one or more others, optionally with some additional specialism)? Many thanks, Tim ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?
For: instance (Ord a) = Max a where maximum = max The same could more simply be achieved with a function: maximum :: Ord a = a maximum = max Now, you probably wanted both a base-case using max and type specific, special cases: instance Max Int where maximum = 2^16 If you have both instances defined in the same module, GHC should always pick the special case for Int if overlapping instances is turned on. However, I've never found a description of how it resolves instance selection if you have the specialized cases in different modules. Unspecified [*] behaviour is not something I'd want to rely on, so I always avoid Overlapping Instances. [*] Of course, the multiple module behaviour might be specified somewhere... ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?
However, I'd be curious to know if (a) There are better or more idiomatic ways of achieving the same effect, and (b) Whether or not I should be doing this at all; It did occur to me that this seems rather trying to re-implement OOP-style inheritance with typeclasses, and therefore perhaps not a very Haskellish approach to designing Could you give a specific example of the problem you're trying to solve? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Library for Sparse Vectors?
Hi, Can't find on hackage any sparse vector library. Does such thing exist? I need efficient storage and dot product calculation for very sparse vectors with about 10 out of 40 000 non-zero components. One solution would be to represent Sparse Vector as Data.Map with (component_index, component_value) pairs to store non-zero components of the vector. In this case, for example, calculating cosine similarity ( http://en.wikipedia.org/wiki/Cosine_similarity) for for every pair of 10 000 vectors, would not be very nice and efficient, I am afraid. Thanks! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?
On Tue, Jul 26, 2011 at 7:46 PM, Evan Laforge qdun...@gmail.com wrote: Could you give a specific example of the problem you're trying to solve? Sorry, yes, that'd be useful :-) So, the project I'm working on involves developing a simulation of a securities market. I have a type which models an order book, on which orders can be placed or removed (and later filled): eg. placeOrder :: (Order e) - e - OrderBook - OrderBook deleteOrder :: (Order e) - e - OrderBook - OrderBook Now, i've chosen to model orders as a typeclass, as there are various semantic differences between different types of order that I can model as different types implementing this typeclass (limit orders vs market orders, buy side vs sell side), and these differences can be specified in the type's implementation of the class. However, there are a number of other typeclasses that all orders should also be instances of (and in terms of which their semantics don't differ, eg Eq or Ord. For instance, for a typeclass representing the interface that any Order type should implement: class Order o where price :: o - Int size :: o - Int I'd like to be able to specify an Eq instance for all types of class Order in a manner similar to this: instance (Order o) = Eq o where o1 == o2 = (price o1 == price o2) (size o1 == size o2) I hope this clarifies my query - I'd be interested to know if this is possible, and whether or not it's a recommended approach, and if not, how else I could achieve something similar. Many thanks, Tim ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Stack space overflow in HaskellNet
Quoth Manfred Lotz manfred.l...@arcor.de, ... I'm not quite sure I understand what you mean. Stack overflow comes from this: forM_ msgs (\x - fetch con x = print) If I change it to: mapM_ (\x - fetch con x = print) msgs there is the same stack overflow. I didn't understand that myself, but neither do I know what might be wrong. One thing to consider is that email messages can be very large. Looking at messages received in the last 10 days I see I have one that exceeds your reported stack size, and that isn't counting the extra space required for text representation of non printing characters etc. There may be messages that you simply can't print. The HaskellNet IMAP fetch is actually FETCH ... BODY[], i.e., the whole contents of the message. Normal practice for giant data files is to send them as part of a MIME multipart/mixed message, and something like the above can proceed with a reasonable chance of success if it avoids these attachments by fetching BODY[1] (or BODY[1.1], etc. depending on actual structure.) I just fetched the 10Mb message I mentioned above to check the structure, and it happened in the blink of an eye - BODY[1] is smaller than the header. I don't see any support for fetch by part, you might have to hack that up yourself. You may ideally also want to fetch BODYSTRUCTURE, but practically I might go out on a limb and predict that you won't run into messages where the first part is a multipart/mixed with a large attachment - so if the object is just a survivable first part, you could live without BODYSTRUCTURE analysis and optimistically ask for BODY[1]. Moving on to practical use of email via IMAP, you'd also want to be able to fetch and decode the attachments. At this point, it's interesting to return to the question of space requirements. Donn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Library for Sparse Vectors?
On Tue, Jul 26, 2011 at 1:30 PM, dokondr doko...@gmail.com wrote: Hi, Can't find on hackage any sparse vector library. Does such thing exist? I need efficient storage and dot product calculation for very sparse vectors with about 10 out of 40 000 non-zero components. One solution would be to represent Sparse Vector as Data.Map with (component_index, component_value) pairs to store non-zero components of the vector. I would make a different suggestion: Store a Set (or maybe an IntMap) of (IntMap scalar)s. In other words, let each vector be represented by an IntMap whose key represents the n'th component of the vector, and whose value is the proper scalar. In this case, for example, calculating cosine similarity ( http://en.wikipedia.org/wiki/Cosine_similarity) for for every pair of 10 000 vectors, would not be very nice and efficient, I am afraid. Given two (IntMap Double)s a and b, I would compute the projection of a along b as cosineSimilarity :: IntMap Double - IntMap Double - Double cosineSimilarity a b = (dot a b) / ((norm a) * (norm b)) where dot = sum . elems . intersectionWith (*) norm = (**0.5) . sum . fmap (**2) . elems The only part I find tricky is enumerating all 1^2 pairs pairs :: Int - [Int] pairs dim = do x - [1..dim] y - [1..dim] return (x,y) and computing the projection for each pair: projections :: Floating scalar = IntMap (IntMap scalar) - Map (Int, Int) scalar projections space = let dimensions = undefined -- find max key in elements of space? m_projection space x y = cosineSimilarity $ lookup x space * lookup y space in fromList . filter (isMaybe . snd) . fmap (\(x,y) - ((x,y), m_projection space x y) . pairs $ dimensions ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?
On Tue, Jul 26, 2011 at 1:52 PM, Tim Cowlishaw t...@timcowlishaw.co.ukwrote: On Tue, Jul 26, 2011 at 7:46 PM, Evan Laforge qdun...@gmail.com wrote: Could you give a specific example of the problem you're trying to solve? Sorry, yes, that'd be useful :-) So, the project I'm working on involves developing a simulation of a securities market. I have a type which models an order book, on which orders can be placed or removed (and later filled): eg. placeOrder :: (Order e) - e - OrderBook - OrderBook deleteOrder :: (Order e) - e - OrderBook - OrderBook Now, i've chosen to model orders as a typeclass, as there are various semantic differences between different types of order that I can model as different types implementing this typeclass (limit orders vs market orders, buy side vs sell side), and these differences can be specified in the type's implementation of the class. Use Maybe to demarcate nonsense semantics/undefinedness. However, there are a number of other typeclasses that all orders should also be instances of (and in terms of which their semantics don't differ, eg Eq or Ord. data OrderType = Market Size | Limit LimitPrice Expiration Size | Stop (Either Percent Price) newtype Sell = Sell OrderType newtype Buy = Buy OrderType newtype Order = Order (Either Buy Sell) class Order o where price :: o - Int size :: o - Int size :: Order - Int size (Order (Left (Buy (Market s))) = s size (Order (Left (Buy (Limit _ _ s))) = s etc. I'd like to be able to specify an Eq instance for all types of class Order in a manner similar to this: instance (Order o) = Eq o where o1 == o2 = (price o1 == price o2) (size o1 == size o2) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Library for Sparse Vectors?
Thanks for the detailed reply and example! Using IntMap as a vector seems to be a good idea. In your example: 1) I would use: dot = dot' * dot' dot' = sum . elems . intersectionWith (*) norm = sum . fmap (**2) . elems instead of: dot = sum . elems . intersectionWith (*) norm = (**0.5) . sum . fmap (**2) . elems 2) I don't understand the syntax: cosineSimilarity $ lookup x space * lookup y space What are $ and *? Thanks, Dmitri On Wed, Jul 27, 2011 at 1:46 AM, Alexander Solla alex.so...@gmail.comwrote: On Tue, Jul 26, 2011 at 1:30 PM, dokondr doko...@gmail.com wrote: Hi, Can't find on hackage any sparse vector library. Does such thing exist? I need efficient storage and dot product calculation for very sparse vectors with about 10 out of 40 000 non-zero components. One solution would be to represent Sparse Vector as Data.Map with (component_index, component_value) pairs to store non-zero components of the vector. I would make a different suggestion: Store a Set (or maybe an IntMap) of (IntMap scalar)s. In other words, let each vector be represented by an IntMap whose key represents the n'th component of the vector, and whose value is the proper scalar. In this case, for example, calculating cosine similarity ( http://en.wikipedia.org/wiki/Cosine_similarity) for for every pair of 10 000 vectors, would not be very nice and efficient, I am afraid. Given two (IntMap Double)s a and b, I would compute the projection of a along b as cosineSimilarity :: IntMap Double - IntMap Double - Double cosineSimilarity a b = (dot a b) / ((norm a) * (norm b)) where dot = sum . elems . intersectionWith (*) norm = (**0.5) . sum . fmap (**2) . elems The only part I find tricky is enumerating all 1^2 pairs pairs :: Int - [Int] pairs dim = do x - [1..dim] y - [1..dim] return (x,y) and computing the projection for each pair: projections :: Floating scalar = IntMap (IntMap scalar) - Map (Int, Int) scalar projections space = let dimensions = undefined -- find max key in elements of space? m_projection space x y = cosineSimilarity $ lookup x space * lookup y space in fromList . filter (isMaybe . snd) . fmap (\(x,y) - ((x,y), m_projection space x y) . pairs $ dimensions ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Idiomatic ways to make all instances of a certain class also instances of another?
On Tue, 26 Jul 2011, Tim Cowlishaw wrote: For instance, for a typeclass representing the interface that any Order type should implement: class Order o where price :: o - Int size :: o - Int I'd like to be able to specify an Eq instance for all types of class Order in a manner similar to this: instance (Order o) = Eq o where o1 == o2 = (price o1 == price o2) (size o1 == size o2) You may define once: orderEq :: Order o = o - o - Bool orderEq o1 o2 = (price o1 == price o2) (size o1 == size o2) and then define instances like instance Order A where ... instance Eq A where (==) = orderEq instance Order B where ... instance Eq B where (==) = orderEq I don't think there is an easier and still predictable way of defining the Eq instances. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Library for Sparse Vectors?
On Tue, 26 Jul 2011, Alexander Solla wrote: Given two (IntMap Double)s a and b, I would compute the projection of a along b as cosineSimilarity :: IntMap Double - IntMap Double - Double cosineSimilarity a b = (dot a b) / ((norm a) * (norm b)) where dot = sum . elems . intersectionWith (*) norm = (**0.5) . sum . fmap (**2) . elems Never write (**2) and (**0.5)! Use (^2) and sqrt! http://www.haskell.org/haskellwiki/Power_function ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Library for Sparse Vectors?
On Wed, 27 Jul 2011, dokondr wrote: In your example: 1) I would use: dot = dot' * dot' dot' = sum . elems . intersectionWith (*) norm = sum . fmap (**2) . elems instead of: dot = sum . elems . intersectionWith (*) norm = (**0.5) . sum . fmap (**2) . elems 2) I don't understand the syntax: cosineSimilarity $ lookup x space * lookup y space What are $ and *? They are from Control.Applicative and are applied to a Maybe type here. It means that cosineSimilarity is applied to the result of looking up 'x' and 'y' in space. If 'x' or 'y' is not in space, then cosineSimilarity $ lookup x space * lookup y space is Nothing. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Library for Sparse Vectors?
On Tue, Jul 26, 2011 at 3:27 PM, dokondr doko...@gmail.com wrote: Thanks for the detailed reply and example! Using IntMap as a vector seems to be a good idea. In your example: 1) I would use: dot = dot' * dot' dot' = sum . elems . intersectionWith (*) norm = sum . fmap (**2) . elems instead of: dot = sum . elems . intersectionWith (*) norm = (**0.5) . sum . fmap (**2) . elems Your dot' is a function, so (dot' * dot') wouldn't type check. 2) I don't understand the syntax: cosineSimilarity $ lookup x space * lookup y space What are $ and *? (lookup x space) has type (Maybe something). So does (lookup y space). We are using applicative functors to pull out values from (lookup x space) and (lookup y space), apply cosineSimilarity to the values we pulled out, and wrapping it all back up in a Maybe, depending on whether the lookups found something or not. $ is exactly the same thing as fmap. * is harder to explain. But it is just plumbing. http://learnyouahaskell.com/functors-applicative-functors-and-monoids ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Stack space overflow in HaskellNet
On Tue, 26 Jul 2011, Manfred Lotz wrote: main = do s con - connectIMAP imapServer login con user pass mboxes - list con mapM print mboxes This should be mapM_ and 'ghc -Wall' spots this problem since 6.12. The compiler (7.04) doesn't tell me anything about it. It seems that it is no longer part of -Wall. But since this mistake is very common, I think it would be better. Problem is, that several libraries like parser libraries are designed for silently throwing away results. You have to switch on -fwarn-unused-do-bind, according to http://www.haskell.org/ghc/docs/7.0-latest/html/users_guide/options-sanity.html Also in case this does not fix your stack space overflow, (mapM_ print) is the correct (space-efficient) way. I'm not quite sure I understand what you mean. Stack overflow comes from this: forM_ msgs (\x - fetch con x = print) If I change it to: mapM_ (\x - fetch con x = print) msgs there is the same stack overflow. forM_ and mapM_ are equal in this respect, the underscore is important. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] file splitter with enumerator package
On Tue, Jul 26, 2011 at 12:19 PM, yi huang yi.codepla...@gmail.com wrote: Actually, i'm wondering how to do exception handling and resource cleanup in iteratee, e.g. your `writer` iteratee, i found it difficult, because iteratee is designed to let enumerator manage resources. I've found the answer for myself, `catchError` and `tryIO` is for this. here is an example code: http://hpaste.org/49530#a49565 On Sat, Jul 23, 2011 at 2:41 AM, Eric Rasmussen ericrasmus...@gmail.comwrote: Hi everyone, A friend of mine recently asked if I knew of a utility to split a large file (4gb in his case) into arbitrarily-sized files on Windows. Although there are a number of file-splitting utilities, the catch was it couldn't break in the middle of a line. When the standard why don't you use Linux? response proved unhelpful, I took this as an opportunity to write my first program using the enumerator package. If anyone has time, I'm really interested in knowing if there's a better way to take the incoming stream and output it directly to a file. The basic steps I'm taking are: 1) Data.Enumerator.Binary.take -- grabs the user-specified number of bytes, then (because it returns a lazy ByteString) I use Data.ByteString.Lazy.hPut to output the chunk 2) Data.Enumerator.Binary.head -- after using take for the big chunk, it inspects and outputs individual characters and stops after it outputs the next newline character 3) I close the handle that steps 12 used to output the data and then repeat 12 with the next handle (an infinite lazy list of filepaths like part1.csv, part2.csv, and so on) The full code is pasted here: http://hpaste.org/49366, and while I'd like to get any other feedback on how to make it better, I want to note that I'm not planning to release this as a utility so I wouldn't want anyone to spend extra time performing a full code review. Thanks! Eric ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- http://www.yi-programmer.com/blog/ -- http://www.yi-programmer.com/blog/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] XCode Dependency for HP on Mac
This may sound ignorant because, well, it is ignorant: I know very little about the underlying mechanics here. Installing the Haskell Platform currently requires XCode developer tools. To get XCode on my 10.6 machine, I... [*** begin ranty details (skippable) ... was told I could get a free version by registering as an Apple Developer. So I lie on the forms (phone number and address, for example, are _required_ fields!), and lie on the _required_ 2-3 page survey. I put in a code that they sent to my email (couldn't lie on that!), and log in. The page tells me, in the exact box that told me if I registered I could get XCode for free, that I... *** end ranty details] ...have to either pay to upgrade to their newer OS (10.7: Lion), or pay $99/year for a Mac OS Developer Membership. Is there a way to install HP without XCode? Could there be in the future? I'm tired of dealing with Apple's constant upgrade requirements, registration requirements, etc., and it seems like a small function that XCode actually performs in the Haskell development toolchain. Again, I'm ignorant of the details and I'm sorry if this is ranty, but I'd love to hear your reactions. Thanks! Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] XCode Dependency for HP on Mac
On 27 July 2011 13:55, Tom Murphy amin...@gmail.com wrote: This may sound ignorant because, well, it is ignorant: I know very little about the underlying mechanics here. Installing the Haskell Platform currently requires XCode developer tools. To get XCode on my 10.6 machine, I... My understanding is that it's about $5 (though I seem to recall hearing that they recently made it free), but I don't use OSX so I can't really help you. Is there a way to install HP without XCode? Could there be in the future? I'm tired of dealing with Apple's constant upgrade requirements, registration requirements, etc., and it seems like a small function that XCode actually performs in the Haskell development toolchain. A C compiler (specifically gcc; not sure if anyone has tried GHC with clang yet). Whilst GHC doesn't need to go via C any more, the Haskell Platform does come with some libraries that have a C component; GHC is also partly written in C (for the RTS if memory serves) though that shouldn't be a factor here as you're getting a binary. -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] XCode Dependency for HP on Mac
On 7/27/11, Ivan Lazar Miljenovic ivan.miljeno...@gmail.com wrote: Installing the Haskell Platform currently requires XCode developer tools. To get XCode on my 10.6 machine, I... My understanding is that it's about $5 (though I seem to recall hearing that they recently made it free), but I don't use OSX so I can't really help you. The choices Apple's given me are OS X 10.7 ($30), or Developer Account ($99/1 year). I don't see XCode available for sale without one of these two. Is there a way to install HP without XCode? Could there be in the future? [...] it seems like a small function that XCode actually performs in the Haskell development toolchain. A C compiler (specifically gcc; not sure if anyone has tried GHC with clang yet). Whilst GHC doesn't need to go via C any more, the Haskell Platform does come with some libraries that have a C component; GHC is also partly written in C (for the RTS if memory serves) though that shouldn't be a factor here as you're getting a binary. If this is the case, couldn't the HP use gcc instead? I'd personally advocate gcc as standard, not as a workaround, because a) gcc is FOSS. b) XCode is 4GB and its functionality is basically orthogonal to the needs of Haskell developers. Thanks for your time, Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] XCode Dependency for HP on Mac
On 27 July 2011 14:18, Tom Murphy amin...@gmail.com wrote: On 7/27/11, Ivan Lazar Miljenovic ivan.miljeno...@gmail.com wrote: Installing the Haskell Platform currently requires XCode developer tools. To get XCode on my 10.6 machine, I... My understanding is that it's about $5 (though I seem to recall hearing that they recently made it free), but I don't use OSX so I can't really help you. The choices Apple's given me are OS X 10.7 ($30), or Developer Account ($99/1 year). I don't see XCode available for sale without one of these two. Apparently it's now in the Mac App store: http://developer.apple.com/xcode/ Is there a way to install HP without XCode? Could there be in the future? [...] it seems like a small function that XCode actually performs in the Haskell development toolchain. A C compiler (specifically gcc; not sure if anyone has tried GHC with clang yet). Whilst GHC doesn't need to go via C any more, the Haskell Platform does come with some libraries that have a C component; GHC is also partly written in C (for the RTS if memory serves) though that shouldn't be a factor here as you're getting a binary. If this is the case, couldn't the HP use gcc instead? I'd personally advocate gcc as standard, not as a workaround, because a) gcc is FOSS. b) XCode is 4GB and its functionality is basically orthogonal to the needs of Haskell developers. Yes, but AFAIK to get gcc for OSX you get it from XCode :/ -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] XCode Dependency for HP on Mac
On 27/07/2011, at 4:24 PM, Ivan Lazar Miljenovic wrote: If this is the case, couldn't the HP use gcc instead? I'd personally advocate gcc as standard, not as a workaround, because a) gcc is FOSS. b) XCode is 4GB and its functionality is basically orthogonal to the needs of Haskell developers. Yes, but AFAIK to get gcc for OSX you get it from XCode :/ A quick web search for Mac OS X gcc binary turned up http://hpc.sourceforge.net/index.php with binary releases of GCC 4.6 for Lion and Snow Leopard. This requires Developer Tools, but that isn't XCode, and it's on the OS X DVD. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] XCode Dependency for HP on Mac
If you're using Lion you can get Xcode from the App Store (Apple used to charge something for it, but now it is free). If you're using Snow Leopard you can download Xcode from developer.apple.com/xcode. See Looking for Xcode 3? Download Now in the bottom right corner of the page. You need to register with Apple but you don't need the paid developer account. -- Maciej On Wed, Jul 27, 2011 at 12:55 PM, Tom Murphy amin...@gmail.com wrote: This may sound ignorant because, well, it is ignorant: I know very little about the underlying mechanics here. Installing the Haskell Platform currently requires XCode developer tools. To get XCode on my 10.6 machine, I... [*** begin ranty details (skippable) ... was told I could get a free version by registering as an Apple Developer. So I lie on the forms (phone number and address, for example, are _required_ fields!), and lie on the _required_ 2-3 page survey. I put in a code that they sent to my email (couldn't lie on that!), and log in. The page tells me, in the exact box that told me if I registered I could get XCode for free, that I... *** end ranty details] ...have to either pay to upgrade to their newer OS (10.7: Lion), or pay $99/year for a Mac OS Developer Membership. Is there a way to install HP without XCode? Could there be in the future? I'm tired of dealing with Apple's constant upgrade requirements, registration requirements, etc., and it seems like a small function that XCode actually performs in the Haskell development toolchain. Again, I'm ignorant of the details and I'm sorry if this is ranty, but I'd love to hear your reactions. Thanks! Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] XCode Dependency for HP on Mac
27/07/2011 3:27 PM, Maciej Wos kirjutas: If you're using Lion you can get Xcode from the App Store (Apple used to charge something for it, but now it is free). If you're using Snow Leopard you can download Xcode from developer.apple.com/xcode. See Looking for Xcode 3? Download Now in the bottom right corner of the page. You need to register with Apple but you don't need the paid developer account. It's also included on the OS X CDs IIRC. A ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe