Re: [Haskell-cafe] Re: Space Leak Help

2007-02-04 Thread Anatoly Zaretsky

On 2/4/07, Dominic Steinitz [EMAIL PROTECTED] wrote:

pad causes a stack overflow and pad1 uses up about 6m of heap.

pad = pad' 0
  where pad' l [] = [0x80] ++ ps
  where pl = (64-(l+9)) `mod` 64
ps = replicate pl 0x00
pad' l (x:xs) = x : pad' (l+1) xs


pad = pad' 0
 where pad' l [] = [0x80] ++ ps
 where pl = (64-(l+9)) `mod` 64
   ps = replicate pl 0x00
   pad' l (x:xs) = x : (pad' $! l+1) xs -- otherwise (l+1) it
will be deferred until replicate

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


Re: [Haskell-cafe] Another Space Leak

2007-02-04 Thread Anatoly Zaretsky

On 2/4/07, Dominic Steinitz [EMAIL PROTECTED] wrote:

test1 :: Int - [Word8]
test1 n = foldl' (zipWith xor) [0x01..0x40] (blockWord8sIn512 (pad (replicate
n 0x55)))

test2 :: Int - [Word8]
test2 n = foldl' (zipWith xor) [0x01..0x40] (bws (pad (replicate n 0x55)))


The problem really is here: foldl' demands the value of 'zipWith xor
xs ys' (which is a list) but not its elemants. So we need a modified
zipWith:

zipWith' f xs ys = forceList (zipWith f xs ys)
 where forceList zs = foldr seq zs zs

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


Re: [Haskell-cafe] FFI and LD_PRELOAD - segfault

2006-09-24 Thread Anatoly Zaretsky

On 9/23/06, Stephan Walter [EMAIL PROTECTED] wrote:

I'm trying to use a shared lib written in Haskell to overload C functions
via LD_PRELOAD.

[snip]

This aborts with a segfault in scheduleWaitThread() from ./libtestffi.so

The test program doesn't use threads, so I'm wondering what I did wrong?



From 
http://www.haskell.org/ghc/docs/latest/html/users_guide/sec-ffi-ghc.html#using-own-main:

 The call to hs_init()  initializes GHC's runtime system. Do NOT try to invoke
 any Haskell functions before calling hs_init(): strange things will
undoubtedly happen.

If you are using gcc you can add something like this when linking libtestffi.so:

#include HsFFI.h

extern void __stginit_Socks(void);

static void __attribute__ ((constructor)) my_init(void) {
 int argc = 1;
 char *argv[] = {Haskell shared object};
 char **argvp = argv;
 hs_init(argc, argvp);
 hs_add_root(__stginit_Socks);
}

static void __attribute__ ((destructor)) my_fini(void) {
 hs_exit();
}

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


Re: [Haskell-cafe] Re: FFI and LD_PRELOAD - segfault

2006-09-24 Thread Anatoly Zaretsky

On 9/24/06, Stephan Walter [EMAIL PROTECTED] wrote:

  gcc -g -Wall -I/usr/lib/ghc-6.4.2/include -c -o hsinit.o hsinit.c


or
  ghc -c hsinit.c

or even
  ghc -Wall -optl -shared -o libtestffi.so \
hsinit.c testffi.o testffi_stub.o

Could anybody familiar with ghc linking details comment on this
constructor/destructor thing so that we can add a wiki about building
shared objects?

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


Re: [Haskell-cafe] Why is type 'b' forced to be type 'm a' and not possibly 'm a - m a'

2006-09-15 Thread Anatoly Zaretsky

On 9/15/06, Vivian McPhail [EMAIL PROTECTED] wrote:


class Forkable a where
fork :: String - a - a - a

...
{-
instance (Monad m, Forkable (m a), Forkable b) = Forkable (m a - b) where
fork n a1 a2 a = do
 a' - a
 fork n (a1 $ return a') (a2 $ return a')
-}



Let's do manual type checking.
First, fork :: Forkable a = String - a - a - a
So for Forkable (m a - b)
 fork :: String - (m a - b) - (m a - b) - m a - b
Then
 fork n a1 a2 a :: b
But you define it as
 fork n a1 a2 a = do {...}
So it should be of type Monad t = t a, not just any `b'.

Instead, you can define
 instance (Monad m, Forkable (m b)) = Forkable (m a - m b) where
   ...

Note that to compile it you also need -fallow-undecidable-instances
and -fallow-overlapping-instances.

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


Re: [Haskell] How to generate a dll?

2006-09-07 Thread Anatoly Zaretsky

On 9/7/06, Andreas Marth [EMAIL PROTECTED] wrote:

I meant that the dll crashed Excel wenn I stopped the debugging mode and
also if I used it in a VB-projekt it crahed that when stopped.


Hi, Andreas!
Try adding these lines to your DllMain:

 if (reason == DLL_PROCESS_DETACH) {
 shutdownHaskell();
 return TRUE;
 }


If I tried to return a String (marshalled to CString) the dll just plain
crashed when called.

How exactly is it marshalled?

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


Re: [Haskell] using the ffi with callbacks

2006-07-20 Thread Anatoly Zaretsky

On 7/20/06, Evan Martin [EMAIL PROTECTED] wrote:

To elaborate, the code setting this up looks something like this:
  callback_fcn - ... -- get a FunPtr using wrapper from the ffi
  free_fcn - ... -- as above
  -- the callback data is just stuff that needs freeing
  callback_data - newStablePtr (callback_fcn, free_fcn)
  register_callback callback_fcn callback_data free_fcn
And my plan was: within the function free_fcn wraps, free
callback_fcn, free the StablePtr, and then finally free free_fcn
itself.


As Taral mentioned there's no need in creating different free_fcn's
for each new callback.
The following may be considered as a linker hack but I see no reason
why it could not work:


freeCallback :: StablePtr (FunPtr a) - IO ()
freeCallback sPtr = do
  fPtr - deRefStablePtr sPtr
  freeStablePtr sPtr
  freeHaskellFunPtr fPtr

foreign export ccall _hs_some_really_private_symbol
  freeCallback :: StablePtr (FunPtr a) - IO ()
foreign import ccall _hs_some_really_private_symbol
  free_fcn :: FunPtr (StablePtr (FunPtr a) - IO ())


And then you can use it like this:
 ...
 callback_data - newStablePtr callback_fcn
 register_callback callback_fcn callback_data free_fcn

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


Re: [Haskell-cafe] Arrows and 'do' syntax

2006-07-12 Thread Anatoly Zaretsky

On 7/12/06, Greg Fitzgerald [EMAIL PROTECTED] wrote:

I'm trying to translate this HXT code to use the Arrow 'do' syntax:
readWriteDoc :: String - IOSLA (XIOState s) b Int
readWriteDoc path = readDocument [(a_validate, 0)] path
   writeDocument [(a_output_encoding, isoLatin1)] -
   getErrStatus

This attempt fails to compile:
readWriteDoc :: String - IOSLA (XIOState s) b Int
 readWriteDoc = proc path - do
   doc   - readDocument [(a_validate, 0)]   - path
   result- writeDocument [(a_output_encoding, isoLatin1)] - - doc
   getErrStatus - result


Hi, Greg.

Looks like readWriteDoc is not an arrow but a function from strings to
arrows. So 'path' is just an argument, not arrow input. Maybe this
should work:

readWriteDoc :: String - IOSLA (XIOState s) b Int
readWriteDoc path = proc input - do
  doc   - readDocument [(a_validate, 0)] path - input
  result- writeDocument [(a_output_encoding, isoLatin1)] - - doc
  getErrStatus - result

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


Re: [Haskell-cafe] Re: socket TChan problem

2006-02-28 Thread Anatoly Zaretsky
Why do you need to duplicate channels?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe