Re: [Haskell-cafe] Haskellers in Minsk, Belarus?

2013-10-06 Thread Dmitry Vyal

Hi Yuras,

thanks for the link. That's the sad truth. I don't know the actual 
reasons, but suspect there are many. Overtime work, fatigue, greed and 
alienation which are ubiquitous it today's society are among them. I 
admire people who nevertheless manage to work on open source projects in 
spare time.


But let's return to topic. I'm not a Minsk citizen but I visit it 
regularly. I find it to be very interesting to meet like-minded people 
living in distant (or no so) places. So, in case anybody interested, we 
could schedule a meeting on this November holidays or so.


Best regards,
Dmitry.


Hi,

I just read an article (sorry, it is in russian:
http://habrahabr.ru/post/196454/ ). The idea I found interesting: even
in big citied developers complain that nothing happens at their
location, but when you try to make an event -- only few of them want to
participate.

I never participate in events. What is wrong with me/us?

So, any haskellers in Minsk, Belarus? What about beer, coffee or
co-hacking? I don't have solid idea how to organize an event, but I'm
sure haskellers are interesting people, both personally and
professionally.

Thanks,
Yuras


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Tutorial on JS with Haskell: Fay or GHCJS?

2013-09-05 Thread Dmitry Vyal

On 09/04/2013 05:39 PM, Adam Bergmark wrote:
Here are some points I'd like to emphasize in addition to the threads 
above, with the disclaimer that I'm the maintainer of Fay.


Fay tries to be very simple, the code base is small (~4800 LoC). This 
really lowers the entry barrier for contributions which I think is 
very important for open source projects. GHCJS is much more 
complicated since it tries to do so much. For GHCJS I'd be afraid that 
the developers might eventually abandon the project and then it'd be 
pretty hard to take over development. Much easier for someone to take 
over Fay development. I think you can safely expect to find bugs in 
all compilers, and if you do and you are in a hurry you might have to 
fix it yourself. Fay has very simple output that is close to both 
Haskell and JavaScript so it's pretty easy to just add a breakpoint 
and start debugging.


Even if GHCJS can successfully compile most of hackage, would we want 
to have these as dependencies in web projects? An output size of 1MiB 
is nothing when compiling a binary, but for a public website 1MiB can 
still be quite a lot, add some transitive dependencies and output will 
explode. Most people don't optimize their packages to have few 
dependencies, which makes sense since the abstraction usually heavily 
outweighs code size. So either way you would probably want to write 
some specific light-weight versions of libraries you want to use, 
that's one reason both GHCJS and Fay have their own base packages.


One place I think GHCJS can shine is for non standard web applications 
such as WEBGL games. Nothing stopping you from using Fay for this, but 
I expect you can really start to leverage GHCJS's threaded runtime here.




Hi Adam!

We found it to be very convenient to use both Hastec and Fay. Since the 
former (as well, as GHCJS) is based on GHC, you can be quite sure any 
bugs you encounter are yours, not of the compiler. And after your code 
works with Hastec and so your idea is tested, you can try to port it to 
Fay. Usually it's trivial, but prepare to encounter some minor compiler 
deficiencies here and there.


So my proposal to original poster would be to tell a bit about both. 
After all, they represent different approaches to JS generation each 
with it's own pros and cons.


Regards,
Dmitry

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Most used functions in hackage

2013-01-30 Thread Dmitry Vyal

On 01/29/2013 12:23 PM, Casey Basichis wrote:


Why do you think browsing function by function is a bad idea?  It 
seems that knowing exactly what the most used functions are would be 
an extremely effective way of finding both which parts of the Prelude 
and Hackage are most broadly useful (instead of browsing them like a 
phonebook) and also finding support from the community as the most 
commonly used functions would likely be the easiest to find support for.


Well, I think the popularity is best measured on the level of packages, 
not separate functions. And I've seen quite a few ratings of most 
popular packages in the past. For example:


http://corp.galois.com/blog/2009/3/23/one-million-haskell-downloads.html
http://haskellwebnews.wordpress.com/2011/03/16/hackage-stats-the-past-year/

I guess Google can suggest much more. There is one thing you should keep 
in mind while looking at such charts. Authors measure number of 
downloads. Most of them are results of cabal-install fetching 
dependencies, not of the deliberate decision of a programmer to use the 
package.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Most used functions in hackage

2013-01-28 Thread Dmitry Vyal

On 01/29/2013 11:21 AM, Casey Basichis wrote:


Is there any link that counts the use of all functions in all packages 
in Hackage and lists them by frequency or by other stats?


I'm still new to haskell but I've been working my way through tons and 
tons of tutorials and books. It would be very helpful to target in on 
the current reality of the most critical functions.



Hello Casey,

You can use Hoogle http://www.haskell.org/hoogle/ to get information 
about a particular function or to find a function by a part of it's 
signature.


While it's helpful to carefully study some basic modules like Prelude 
function by function, I don't think it's a good approach in general. I 
suggest you to look for reviews of popular modules. Personally, I found 
24 days of hackage http://ocharles.org.uk/blog/ to be quite 
informative. I wouldn't argue it's a best source for a beginner, but at 
least it gives quite a broad perspective.


Best regards,
Dmitry


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Moscow Haskell Users Group (MskHUG) December meeting.

2012-12-03 Thread Dmitry Vyal

On 12/03/2012 07:13 PM, Serguey Zefirov wrote:

I would like to announce MskHUG December meeting and invite everyone interested.

Wow, great idea :) I'd like to participate. May I ask why don't you 
schedule the event on weekend or on Friday at least?


Best wishes,
Dmitry


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [Security] Put haskell.org on https

2012-10-28 Thread Dmitry Vyal

On 10/28/2012 03:20 AM, Niklas Hambüchen wrote:

- abuse your hackage account and override arbitrary packages
   (especially since hackage allows everybody to override everything)
Does hackage at least store the logs of packages uploads? What's the 
reason or such a security model? I guess it was appropriate in the past 
when hackage was an experimental service, but now it's a standard way of 
distributing Haskell code. If anyone can update any package, we are 
waiting for the disaster. I have some haskell code I wrote myself 
running as root and these thoughts make me shiver.


Https is a must-have in current situation, but it's only part of a solution.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A yet another question about subtyping and heterogeneous collections

2012-10-19 Thread Dmitry Vyal

On 10/19/2012 06:14 AM, AntC wrote:

Roman Cheplyaka roma at ro-che.info writes:


* Dmitry Vyal akamaus at gmail.com [2012-10-18 17:31:13+0400]

On 10/18/2012 03:20 PM, MigMit wrote:

Why do you need ALike x, BLike x etc.? Why not just Like u x?


Hmm, looks like a nice idea. I tried it, unfortunately I can't cope
with compiler error messages:

tst.hs:32:15:
 Context reduction stack overflow; size = 201
 Use -fcontext-stack=N to increase stack size to N
   Upcast a b
 In the first argument of `(.)', namely `(upcast :: b - a)'
 In the expression: (upcast :: b - a) . (upcast :: c - b)
 In the expression: (upcast :: b - a) . (upcast :: c - b) $ x
instance (Upcast a b, Upcast b c) = Upcast a c where
   upcast = (upcast :: b - a) . (upcast :: c - b)

This is the offending instance. Remember, GHC only looks at the instance
head (Upcast a c here) when it decides which instance to use.

Roman


Hi Dmitry, looks like you've got the classic (show . read) difficulty. In
your Upcast a c instance, the compiler is trying to figure out the type of b.

You might think there's only one 'chain' to get from (say) type A to type D --
that is via Upcast A B to Upcast B C to Upcast C D; but there's also an
instance Upcast x x -- which means there could be any number of Upcast A A,
Upcast B B, etc links in the chain.

(And this doesn't count all the other possible instances that might be defined
in other modules -- for all the compiler knows at that point.)

The modern way to handle this is using type functions (aka type families aka
associated types), but I'm not sure how that would apply here. (And, for the
record, the old-fashioned way would use functional dependencies, as per the
Heterogenous Collections paper aka 'HList's).

AntC



Hello Antony,
do I understand you correctly, that the error message is the result of 
compiler using depth first search of some kind when calculating 
instances?  Also can you please elaborate a bit more on using functional 
dependencies for this problem? Upcast x y is not a function, it's a 
relation, y can be upcasted to different x'es and different y's can be 
upcasted to single x.


Dmitry

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A yet another question about subtyping and heterogeneous collections

2012-10-19 Thread Dmitry Vyal



Second, all your examples so far used structural subtyping (objects
with the same fields have the same type) rather than nominal
subtyping of C++ (distinct classes have distinct types even if they
have the same fields; the subtyping must be declared in the class
declaration). For the structural subtyping, upcasts and downcasts can
be done mostly automatically. See the OOHaskell paper or the code

Hello Oleg,
I've glanced over both HList and OOHaskell papers when I considered 
taking different approaches. Albeit elegant, OOHaskell looked too heavy 
for my purposes, I don't need mutability, for example. And HList paper 
left me with two questions. The first one is how much such an encoding 
costs both in terms of speed and space. And the second one is can I 
conveniently define a Storable instance for hlists. As I said before, I 
need all this machinery to parse a great number of serialized nested C 
structs from a file.


Best regards
Dmitry

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] A yet another question about subtyping and heterogeneous collections

2012-10-18 Thread Dmitry Vyal

Hello list!

I've been experimenting with emulating subtyping and heterogeneous 
collections in Haskell. I need this to parse a binary representation of 
objects of a class hierarchy in C++ program.


So far I implemented upcasting using a chain of type classes and now I'm 
playing with heterogeneous lists. For future purposes It would be ideal 
to be able to have something like these functions:

upcast_list :: [LikeC] - [LikeA]
downcast_list :: [LikeA] - [LikeC]

First one only replaces the existential wrapper leaving the actual value 
intact, and the second one also filters the list, passing the elements 
with specific enough type.


I can implement this particular functions, but what's about a more 
general one? Something like cast_list :: [a] - [b], where a and b are 
existential types from one hierarchy. Something like LikeA and LikeC in 
my example.


Is my approach feasible? Is there a better one? Am I missing something 
obvious?

Any relevant advices are welcome.

The example code follows:

{-# LANGUAGE FlexibleInstances, UndecidableInstances, 
OverlappingInstances, ExistentialQuantification, DeriveDataTypeable #-}


import Data.Typeable
import Data.Maybe

data A = A {a_x :: Int} deriving (Show, Typeable)
data B = B {b_x :: Int, b_a :: A} deriving (Show, Typeable)
data C = C {c_z :: Int, c_b :: B} deriving (Show, Typeable)
data D = D {d_w :: Int, d_c :: C, d_a :: A} deriving (Show, Typeable)

class ALike x where toA :: x - A
class BLike x where toB :: x - B
class CLike x where toC :: x - C
class DLike x where toD :: x - D

instance ALike A where toA = id
instance BLike B where toB = id
instance CLike C where toC = id
instance DLike D where toD = id

instance ALike B where toA = b_a
instance BLike C where toB = c_b
instance CLike D where toC = d_c

instance (BLike x) = (ALike x) where
  toA = (toA :: B - A) . toB
instance CLike x = BLike x where
  toB = toB . toC

a1 = A 1
b1 = B 2 (A 2)
c1 = C 3 b1
d1 = D 4 c1 (A 10)

print_a :: ALike x = x - String
print_a v = A =  ++ show (a_x $ toA v)

sum_a :: (ALike x, ALike y) = x - y - String
sum_a v1 v2 = A1 =  ++ show (a_x $ toA v1) ++  A2 =  ++ show (a_x $ 
toA v2)



data LikeA = forall a. (ALike a, Typeable a) = LikeA a

instance ALike LikeA where
  toA (LikeA x) = toA x

get_mono :: Typeable b = [LikeA] - [b]
get_mono = catMaybes . map ((\(LikeA x) - cast x))

data LikeC = forall c. (CLike c, Typeable c) = LikeC c

instance CLike LikeC where
  toC (LikeC x) = toC x

lst_a = [LikeA a1, LikeA b1, LikeA c1, LikeA d1]
lst_c = [LikeC c1, LikeC d1]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A yet another question about subtyping and heterogeneous collections

2012-10-18 Thread Dmitry Vyal

On 10/18/2012 03:20 PM, MigMit wrote:

Why do you need ALike x, BLike x etc.? Why not just Like u x?



Hmm, looks like a nice idea. I tried it, unfortunately I can't cope with 
compiler error messages:


tst.hs:32:15:
Context reduction stack overflow; size = 201
Use -fcontext-stack=N to increase stack size to N
  Upcast a b
In the first argument of `(.)', namely `(upcast :: b - a)'
In the expression: (upcast :: b - a) . (upcast :: c - b)
In the expression: (upcast :: b - a) . (upcast :: c - b) $ x


{-# LANGUAGE FlexibleInstances, UndecidableInstances, 
OverlappingInstances, ExistentialQuantification, DeriveDataTypeable, 
MultiParamTypeClasses, FlexibleContexts,

IncoherentInstances #-}

import Data.Typeable
import Data.Maybe

data A = A {a_x :: Int} deriving (Show, Typeable)
data B = B {b_x :: Int, b_a :: A} deriving (Show, Typeable)
data C = C {c_z :: Int, c_b :: B} deriving (Show, Typeable)
data D = D {d_w :: Int, d_c :: C, d_a :: A} deriving (Show, Typeable)

class Upcast c x where
  upcast :: x - c

instance Upcast x x where
  upcast = id

instance Upcast A B where upcast = b_a
instance Upcast B C where upcast = c_b
instance Upcast C D where upcast = d_c

instance (Upcast a b, Upcast b c) = Upcast a c where
  upcast = (upcast :: b - a) . (upcast :: c - b)

a1 = A 1
b1 = B 2 (A 2)
c1 = C 3 b1
d1 = D 4 c1 (A 10)

print_a :: Upcast A x = x - String
print_a v = A =  ++ show (a_x $ upcast v)

sum_a :: (Upcast A x, Upcast A y) = x - y - String
sum_a v1 v2 = A1 =  ++ show (a_x $ upcast v1) ++  A2 =  ++ show (a_x 
$ upcast v2)



data LikeA = forall a. (Upcast A a, Typeable a) = LikeA a

--instance Upcast a LikeA where
--  upcast (LikeA x) = upcast x

lst_a = [LikeA a1, LikeA b1, LikeA c1, LikeA d1]

get_mono :: Typeable b = [LikeA] - [b]
get_mono = catMaybes . map ((\(LikeA x) - cast x))

data LikeC = forall c. (Upcast C c, Typeable c) = LikeC c

--instance Upcast C LikeC where
--  upcast (LikeC x) = upcast x

lst_c = [LikeC c1, LikeC d1]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] poor performance when generating random text

2012-10-17 Thread Dmitry Vyal

Hello anyone

I've written a snippet which generates a file full of random strings. 
When compiled with -O2 on ghc-7.6, the generation speed is about 2Mb per 
second which is on par with interpreted php. That's the fact I find 
rather disappointing. Maybe I've missed something trivial? Any 
suggestions and explanations are welcome. :)


% cat ext_sort.hs
import qualified Data.Text as T
import System.Random
import Control.Exception
import Control.Monad

import System.IO
import qualified Data.Text.IO as TI

gen_string g = let (len, g') = randomR (50, 450) g
   in T.unfoldrN len rand_text (len, g')
 where rand_text (0,_) = Nothing
   rand_text (k,g) = let (c, g') = randomR ('a','z') g
 in Just (c, ((k-1), g'))

write_corpus file = bracket (openFile file WriteMode) hClose $ \h - do
  let size = 10
  sequence $ replicate size $ do
g - newStdGen
let text = gen_string g
TI.hPutStrLn h text

main = do
  putStrLn generating text corpus
  write_corpus test.txt



% cat ext_sort.prof
Wed Oct 17 10:59 2012 Time and Allocation Profiling Report (Final)

   ext_sort +RTS -p -RTS

total time  =   32.56 secs   (32558 ticks @ 1000 us, 1 
processor)

total alloc = 12,742,917,332 bytes  (excludes profiling overheads)

COST CENTREMODULE  %time %alloc

gen_string.rand_text.(...) Main 70.7   69.8
gen_string Main 17.6   15.8
gen_string.rand_text   Main  5.4   13.3
write_corpus.\ Main  4.30.8


individual inherited
COST CENTRE   MODULE no. entries  %time %alloc   
%time %alloc


MAIN MAIN67   00.00.0 
100.0  100.0
 main Main 135   00.00.0   
100.0  100.0
  write_corpusMain 137   00.00.0   
100.0  100.0
   write_corpus.\ Main 138   14.30.8   
100.0  100.0
write_corpus.\.text   Main 140  100.00.0
95.7   99.2
 gen_string   Main 141  10   17.6   15.8
95.7   99.2
  gen_string.g'   Main 147  100.0
0.0 0.00.0
  gen_string.rand_textMain 144251097435.4   13.3
77.5   83.2
   gen_string.rand_text.g'Main 148249097430.6
0.0 0.60.0
   gen_string.rand_text.(...) Main 14625009743   70.7   69.8
70.7   69.8
   gen_string.rand_text.c Main 145250097430.8
0.0 0.80.0
  gen_string.len  Main 143  100.0
0.0 0.00.0
  gen_string.(...)Main 142  100.6
0.3 0.60.3


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] poor performance when generating random text

2012-10-17 Thread Dmitry Vyal

On 10/17/2012 12:45 PM, Alfredo Di Napoli wrote:
What about this? I've tested on my pc and seems pretty fast. The trick 
is to generate the gen only once. Not sure if the inlines helps, though:




 What about this? I've tested on my pc and seems pretty fast. The 
trick is to generate the gen only once. Not sure if the inlines helps, 
though

...

Wow, haskell-cafe is a wonderful place! In just a two hours program run 
time automagically improved 20x ;) Thanks Alfredo, code works wonderful. 
Compared to mine implementation it's 2.5 sec vs 50 sec on my laptop. 
Interesting, how it compares to C now.


Inlining makes about 50x difference when code compiled without 
optimization. A nice example.


Best wishes,
Dmitry


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Using CatchIO on StateT

2012-10-12 Thread Dmitry Vyal

Greetings,

I found The MonadCatchIO-mtl package while looking for a way to catch 
exceptions in my custom StateT s (ReaderT r IO) monad. CatchIO worked 
flawlessly when handlers used the environment r from Reader, but things 
broken when I tried to use state. According to source, the handler is 
run with the same state which was passed to runStateT.


instance MonadCatchIO m = MonadCatchIO (StateT s m) where
m `catch` f = StateT $ \s - (runStateT m s)
   `Control.Monad.CatchIO.catch` (\e - 
runStateT (f e) s)


And my finalizing action depends on the current state. Can you give an 
advice how to get the behavior I need?


Best regards,
Dmitry Vyal


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Can Haskell outperform C++?

2012-05-14 Thread Dmitry Vyal

On 05/11/2012 07:53 AM, Ertugrul Söylemez wrote:
My point is: If you need C-like performance at a certain spot there is 
really no excuse for writing the entire application in C. Haskell has 
a working, powerful enough FFI. Also idiomatic Haskell code nowadays 
performs close to C. If your code doesn't, chances are that it's not 
even idiomatic. Sorting a list is easy and beautiful in code. But it's 
far from idiomatic. Using ST with destructive update is also not 
idiomatic. One of my performance masterpieces is the instinct AI 
library (see Hackage). It uses only immutable vectors and performs 
very heavy Double calculations, yet performs better than the same code 
with mutable arrays did. With a few years of Haskell experience in my 
backpack I know how to utilize laziness to get amazing performance for 
code that most people would feel must be written with destructively 
updating loop. And the resulting code is just beautiful to read and 
watch at work, a great demonstration of the wonders the GHC developers 
have created.

Hello Ertugrul,

Out of the curios, did you compare the performance of Instinct with 
implementations in languages, associated with numerical computations, 
like Octave?


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Any advices on optimizing an array processing in a pitch tuner

2012-02-11 Thread Dmitry Vyal

Hello Haskellers,

Recently I've revived my old toy project. It's a sound spectrum analyzer 
with Gtk2hs interface. I decided to rewrite it into a pitch tuner. 
Basically I'm done, but I've ran into performance difficulties while 
trying to rise a resolution in a frequency space.


The original code used Storable arrays to get data out of fftw, for some 
processing I convert it to lists, which obviously hinders performance. 
I've heard there are some modern efficient array processing libraries 
available. So I hope I can avoid writing all those low-level for loops.


Any hints on how to optimize and prettify the code are welcome.

Repository is located at https://github.com/akamaus/HTuner

Best regards,
Dmitry


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Erlang's module discussion

2011-05-28 Thread Dmitry Vyal

On 28.05.2011 07:10, Tom Murphy wrote:

Hi All,
  I sure love Hackage, but there's a very interesting discussion
going on, on the Erlang mailing list, about completely restructuring
the module-model.
  Before you dismiss it as crazy, know that the topic was brought
up by Joe Armstrong, one of the creators of the language.

Here's the archive:
http://erlang.org/pipermail/erlang-questions/2011-May/058769.html

  Food for thought...

Tom


Hi Tom,
sounds interesting! But seems to me this will raise current package 
dependencies problems on a new level. Now one sees modules and packages 
as some kind of aggregates, black boxes which supply useful 
functionality and hide non relevant complexity and tight dependencies.


Moving to functions all the way down looks to me like a lowering level 
of abstraction. The meta information about the overall program structure 
would inevitably be lost.


Dmitry

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Concurrency question

2005-09-04 Thread Dmitry Vyal

Hi, everyone!

I have a function, which sometimes takes a long time to compute or even
may loop forever. So I want to limit it in time somehow.

I tried to run it in another thread in order to kill it after its time
lapsed. But it seems to lock out other threads so they can't terminate it.

I wonder is there some clever way of dealing with such situation 
(running a computation in background for specific time) ?


Thanks a lot.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Haskell thread preemption

2005-08-31 Thread Dmitry Vyal

Hello, all.

I'm experimenting with concurrent haskell using GHC 6.4.
I wrote these fuctions as described in Tackling the Awkward Squad:

par_io :: IO a - IO a - IO a
par_io t1 t2 = do c - newEmptyMVar :: IO (MVar a)
  id1 - forkIO $ wrapper c t1
  id2 - forkIO $ wrapper c t2
  res - takeMVar c
  killThread id1
  killThread id2
  return res
where wrapper :: MVar a - IO a - IO ()
  wrapper mvar io = do res - io
   putMVar mvar res


timeout :: Int - IO a - IO (Maybe a)
timeout n t = do res - par_io thr timer
 return res
where thr = do res - t
   return $ Just res
  timer = do threadDelay n
 return Nothing

Now, then I try something like
timeout 100 (print $ cycle test)

it behaves correctly: repeatingly prints test for one second and then 
returns Nothing.


But then I try this:
timeout 100 (print $ 2^2^2^2^2^2)

it hangs for about eight seconds.

Currently I'm playing with theorem-proving using resolution. So I need 
some technique to break a computation, if takes too long. It seems that 
using timeout there does nothing. I waited for several minutes for 
invalid theorem and timeout didn't expire.


Control.Concurrent page of Haskell Hierarchical Libraries says, that 
a thread may be pre-empted whenever it allocates some memory, and that 
tight loops which do no allocation tend to lock out other threads.


But resolution function, i wrote, hogs a *lot* of memory and (print $ 
2^2^2^2^2^2) does either. So i'm curious about this behavior of timeout.


So what is the correct way of running such computations in parallel?
Maybe I'm missing something?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] How to speed things up

2005-04-28 Thread Dmitry Vyal
Hello everybody.
I have a long list consisted of a small number (about a dozen) of 
elements repeating in random pattern. I know all the possible elements.
I need to count number of occurences of each particular element and to 
do i quickly.

   For example
quick_func Eq a = [a] - [(a,Int)]
quick_func [1,2,3,1,2,9,1,9] == [(1,3),(2,2),(3,1),(9,2)]
According to profiler this function is the bottle-neck in my sluggish 
program so I really need to speed it up.

Any proposals?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How to speed things up

2005-04-28 Thread Dmitry Vyal
Cale Gibbard wrote:
I'd use a Map in GHC 6.4:
count xs = toList $ fromListWith (+) (zip xs (repeat 1))
or a FiniteMap in earlier versions:
count xs = fmToList $ addListToFM_C (+) emptyFM (zip xs (repeat 1))
both of these seem to be quite fast.
 - Cale
Thanks, this is significaly faster than variant with accumArray i've 
used before.

By the way, how to use Unboxed arrays and unsafeAccumArray Greg Buchholz 
mentioned? I can't find them in GHC 6.2 documentation.

And last question:
When I try to compile my program  with -caf-all I get these messages:
% ghc -prof -caf-all mastermind.hs -o mastermind 

/tmp/ghc3338.hc:25: error: redefinition of `Mainmain_CAF_cc_ccs'
/tmp/ghc3338.hc:24: error: `Mainmain_CAF_cc_ccs' previously defined here
If I use -auto-all it works fine.
I use Slackware Linux 10.1 with gcc 3.3.4 and ghc-6.2.2
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe