Re: H98 Text IO

2008-02-26 Thread John Vogel
Why not leave the defaults as they ARE OR USE utf-8 and give the
programmer the capability to specify what encoding they want when
they want to use a different one?

John
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: Some problems writing a threaded program

2008-02-11 Thread John Vogel

 Thankyou both Don Stewart and Simon Marlow for your responses.



By adding yield and threadDelay in certain spots I have at least prevented
some of the threads from being starved of CPU time.

The only issue now is that terminateProcess doesn't always terminate
netstat.exe in the cmd.exe so I don't get an exit condition.
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Some problems writing a threaded program

2008-02-10 Thread John Vogel
I am running my program in WinXP with ghc 2.6.8

If you install netstat and change the parameters it should still work in
linux.


Why does thread # 3 dominate over the over threads int the output?
Why does thread # 4 never seem to run?

I can't use the sleep function in System.Process.Win32 since it puts all the

threads asleep at the same time.  Is there a way to put only one thread
asleep?

That would allow more of a chance for thread #4 to run.



The simplified program:
---


module Main where

import Data.IORef
import Data.List
import System.IO
import System.Process

import Control.Concurrent
import Control.Concurrent.Chan


data Connection = Null | Last | Active deriving (Eq)

instance Show Connection where
show Null = Null
show Last = Last
show Active = Active

instance Read Connection where
readsPrec _ s = case take 5 s of
 UDP - [(Active, )]
 TCP - [(Active, )]
Last - [(Last,)]
_ - [(Null,)]


-- ptrints one 0 and 1
main = do
stop - newIORef False
cbuffer - newChan :: IO (Chan Connection)
putStr 0
(_,output,_,ph) - runInteractiveCommand netstat -noa 5
sequence $ map forkIO $ [(processConnections ph output cbuffer),
(stopNetstat ph stop False), (printChan cbuffer),(checkStop stop )]
putStr 1
_ - waitForProcess ph
--mapM killThread ts
putStrLn \nDone

-- thread # 2
processConnections :: ProcessHandle - Handle - (Chan Connection) - IO ()
processConnections ph hout chan = do
h - hReady hout
e - getProcessExitCode ph
putStr 2
if (not h  e /= Nothing) then do writeChan chan Last  return () else do
if h then do readConnection hout = writeChan chan else do
processConnections ph hout chan


readConnection :: Handle - IO Connection
readConnection hout = do
l - hGetLine hout
let c = (read l :: Connection)
if (c == Null)
then do (readConnection hout)
else do (return c)

-- thread number 3
stopNetstat :: ProcessHandle - (IORef Bool) - Bool - IO ()
stopNetstat netstat _ True = terminateProcess netstat
stopNetstat netstat gref False = putStr 3  yield  readIORef gref =
stopNetstat netstat gref


--thread 4
printChan :: (Chan Connection) - IO ()
printChan chan = do
putStr 4
c - readChan chan
printConnection c
printChan chan


checkStop :: (IORef Bool) - String - IO ()
checkStop ref s = do
if (take 4 s == stop)
then do (writeIORef ref True)
else do (getChar = (\x - checkStop ref ((tail s) ++ [x])))

printConnection :: Connection - IO ()
printConnection c = case c of
Null - putStr N
Last - putStr L
_ - putStr A
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: [Haskell] Simulating client server communication with recursive monads

2008-01-14 Thread John Vogel
If you redefine as follows:

server :: [Integer] - Writer [String] [Integer]
server [] = return []
server (a:as) = do
tell [Server  ++ show a]
rs - server as
return (a:rs)

You get this for the output:


[Server 0,Server 1,Server 2,Server 3,Server 4,Server 5,Server
6,Server 7,Server 8,Server 9,Server 1
0,Client 0,Client 1,Client 2,Client 3,Client 4,Client 5,Client
6,Client 7,Client 8,Client 9]

Then you just need to alternate the pattern.  Though a real simulation
of sever traffic would account for the packets
not being recieved, rerequested, or recieved out of order.  It also depends
whether you are simulating TCP or UDP.

On 1/14/08, Jan Stranik [EMAIL PROTECTED] wrote:

 Hello,

 I am trying to simulate a client server traffic using recursive lazy
 evaluation. I am trying to do that in a recursive writer monad.

 Following code is my attempt to simulate client server interaction and
 collect its transcript:

 {-# OPTIONS -fglasgow-exts #-}

 module Main where



 import Control.Monad.Writer.Lazy

 simulation:: Writer [String] ()

 simulation = mdo

 a - server cr

 cr - client $ take 10 a

 return ()



 server:: [Integer] - Writer [String] [Integer]

 server (a:as) = do

   tell [server  ++ show a]

   rs - server as

   return ((a*2):rs)



 server [] = return []



 client:: [Integer] - Writer [String] [Integer]

 client as = do

   dc - doClient as

   return (0:dc)

 where

   doClient (a:as) = do

   tell [Client  ++ show a]

   as' - doClient as

   return ((a+1):as')

   doClient [] = return []



 main = return $ snd $ runWriter simulation



 The problem that I see is that the transcript collected contains first all
 output from the server, and then output from the client.

 Here is an example of output that I see:

 :[server 0,server 1,server 3,server 7,server 15,server
 31,server 63,server 127,server 255,server 511,server 1023,Client
 0,Client 2,Client 6,Client 14,Client 30,Client 62,Client
 126,Client 254,Client 510,Client 1022]



 I would like to collect the output like:

 :[client 0,server 0, client 1,…]



 This would allow me to remove the ending condition in simulation (take
 10), and instead rely fully on lazy evaluation to collect as many simulation
 steps as needed by my computation.

 I am still relatively new to the concepts of recursive monadic
 computations, so I would appreciate any suggestions from experts on this
 mailing list.



 Thank you



 Jan



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


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


How do you get the process id out of the ProcessHandle in Win XP?

2007-12-19 Thread John Vogel
Here is what I have tried:

module Main where

import System.Process   -- using   runInteractiveProcess
import System.Posix.Types
import System.Process.Internals
import Control.Concurrent.MVar


main = do
(input,output,err,ph) - runInteractiveProcess calc.exe ([]) Nothing
Nothing
p - Main.getPID ph
putStrLn $ show p --- The output

getPID :: ProcessHandle - IO CPid
getPID (ProcessHandle p) = do
(OpenHandle pp) - takeMVar p
return (toPID pp)

toPID :: PHANDLE - CPid
toPID ph = toEnum $ fromEnum ph

---
 But the CPid always returns some const value (like 1904 in cygwin; 1916 in
cmd.exe)

I think, I saw that CPid was a newtype of Ptr ()

So, currently I need to get the process id from CPid or something else.

All help is appreciated.

John
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: Foreign C with pointers

2007-12-18 Thread John Vogel
You do realize that the example you gave is just as general as all the
tutorials.

Here is an example I was working, but it gives a *segmentation fault* for
some reason:

example.h

typedef struct
{
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
} IP;

IP* shiftIP(IP* addr);


example.c

#include example.h

IP* shiftIP(IP* addr){
unsigned char t;
t = addr-a;
addr-a = addr-b;
addr-b = addr-c;
addr-c = addr-d;
addr-d = t;
}

Example.hsc

{-# OPTIONS -ffi -fglasgow-exts #-}
module Example where

import Foreign
import Foreign.C.Types
import Control.Monad

#include buzz.h

data MyIP = MyIP
{ a :: CUChar
, b :: CUChar
, c :: CUChar
, d :: CUChar
} deriving (Show)

instance Storable MyIP where
sizeOf _ = #{size IP} -- 4
alignment _ = alignment (undefined :: CUChar) -- 1
peek p = return MyIP
  `ap` (#{peek IP, a} p)
  `ap` (#{peek IP, b} p)
  `ap` (#{peek IP, c} p)
  `ap` (#{peek IP, d} p)
poke p ip = do
  #{poke IP, a} p $ a ip
  #{poke IP, b} p $ b ip
  #{poke IP, c} p $ c ip
  #{poke IP, d} p $ d ip

foreign import ccall safe static buzzlib.h shiftIP
shiftIP :: Ptr MyIP - Ptr MyIP

shiftMyIP :: MyIP - MyIP
shiftMyIP ip = unsafePerformIO . alloca $ \ptr - poke ptr ip  peek
(shiftIP ptr)
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: Can't seem to figure how to compile

2007-10-23 Thread John Vogel
Yup, that fixed the problem.  Thanks for your help. :)






On 10/22/07, Simon Marlow [EMAIL PROTECTED] wrote:

 John Vogel wrote:
  Running ghc 6.6.1
  On Windows Vista
 
  I get this error when I run: ghc Main.hs
 
  gcc: installation problem, cannot exec `as': No such file or directory

 This is a known problem on Vista.  The GHC 6.8.1 snapshot should work for
 you, e.g.


 http://www.haskell.org/ghc/dist/stable/dist/ghc-6.8.0.20071020-i386-unknown-mingw32.exe

 Please try it out and let us know if that helps.

 FYI, the bug we fixed is this:

 http://hackage.haskell.org/trac/ghc/ticket/1110

 Cheers,
 Simon


___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Can't seem to figure how to compile

2007-10-21 Thread John Vogel
Running ghc 6.6.1
On Windows Vista

I get this error when I run: ghc Main.hs

gcc: installation problem, cannot exec `as': No such file or directory

On this code:

module Main where

main = do
  putStrLn Please enter your name: 
  name - getLine
  putStrLn (Hello,  ++ name ++ , how are you?)


What is my problem?
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: Can't seem to figure how to compile

2007-10-21 Thread John Vogel
It is the default gcc with ghc.
ghci works just fine which invokes ghc --interactive.

But this command does work:

ghc -e Main.main Main.hs

But there is no executable.



On 10/21/07, Matthew Danish [EMAIL PROTECTED] wrote:

 On Sun, Oct 21, 2007 at 01:46:16PM -0500, John Vogel wrote:
  gcc: installation problem, cannot exec `as': No such file or directory

 `as' is the assembler that gcc uses.  Check your gcc.  Can you even
 compile C programs successfully?

 --
 -- Matthew Danish -- user: mrd domain: cmu.edu
 -- OpenPGP public key: C24B6010 on keyring.debian.org

___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users