Re: runST and LazyST don't mix -- ghc-2.09

1997-12-01 Thread Simon Marlow

Byron Cook [EMAIL PROTECTED] writes:

 in ghc-2.09 (solaris) i cannot use runST with LazyST
 observe:

 $ cat lazy.hs
 import LazyST
 main = print $ f True
 
 f x = runST(
   do n - newSTRef x
  readSTRef n
   )

Yes: GHC's LazyST doesn't define runST at the moment (I should have
documented this deficiency, sorry about that).  For now, you can do it
like this:

import LazyST
import ST (runST)
main = print $ f True

f x = runST (lazyToStrictST (
  do n - newSTRef x
 readSTRef n
  ))

I make no claims about the LazyST stuff: it hasn't been thoroughly
tested by any means.

Cheers,
Simon

-- 
Simon Marlow [EMAIL PROTECTED]
University of Glasgow   http://www.dcs.gla.ac.uk/~simonm/
finger for PGP public key



Re: runST and LazyST don't mix -- ghc-2.09

1997-12-01 Thread Simon Marlow

Byron Cook [EMAIL PROTECTED] writes:

 that didn't quite work --- it gave a type error.  

Bizarre... it worked for me.  Did you get the indentation right when
you cut 'n' pasted it? :-)

-- 
Simon Marlow [EMAIL PROTECTED]
University of Glasgow   http://www.dcs.gla.ac.uk/~simonm/
finger for PGP public key



Re: runST and LazyST don't mix -- ghc-2.09

1997-12-01 Thread Simon Marlow

Byron Cook [EMAIL PROTECTED] writes:

 i tried your solution on the function "f" used in earlier mesgs and got
 the same problem:
 paratha% ghc lazy.hs
  
 lazy.hs:6: Couldn't match the type `ST' against `ST'
 Expected: `ST taMI taML'
 Inferred: `ST taN9 taMu'

This really means: 

lazy.hs:6: Couldn't match the type `LazyST.ST' against `ST.ST'
Expected: `LazyST.ST taMI taML'
Inferred: `ST.ST taN9 taMu'

(i.e. one example of why it's not always a good idea to print
unqualified Id's  in the type error :-)

Anyway, the type of LazyST differs from ST, so you can't mix them
without using lazyToStrictST and strictToLazyST.

Cheers,
Simon

-- 
Simon Marlow [EMAIL PROTECTED]
University of Glasgow   http://www.dcs.gla.ac.uk/~simonm/
finger for PGP public key



Re: runST and LazyST don't mix -- ghc-2.09

1997-12-01 Thread Byron Cook

sorry, i should clarify.  i'm not using the little "f x" function
refered to in earlier mesgs.

i got this error mesg:
SignalArray.hs:78: Couldn't match the type `ST' against `ST'
Expected: `ST ta1tm [[ArrResp ta1m9 ta1ma]]'
Inferred: `ST ta1yp [[ArrResp ta1m9 ta1ma]]'
In the first argument of `runST', namely
`(do
arr - newSTArray bounds initVal
initArray arr
performRequests arr)'
In the second argument of `$', namely
`runST (do
  arr - newSTArray bounds initVal
  initArray arr
  performRequests arr)'
In an equation for function `stateArray':
`stateArray ((bounds@(loBound, hiBound)), initWrites) (Sig reqss)
= Sig
  $ (runST (do...


when compiling this rather long and ugly attached below.

however, lazyToStrictST, did the trick.  
--
module SignalArray
  (
 stateArray
,updateArray
,ArrayDesc
,ArrReq(..)
,ArrResp(..)
  ) where

import Array
import Signal
import LazyST 
import ST(runST)
import BasicTypes

-- Begin Signature 
stateArray  :: (Enum a, Ix a) = ArrayDesc a b - Signal [ArrReq a b] - 
Signal [ArrResp a b]

updateArray :: Ix a =
  Signal (Array a b) -
  [(Signal Bool,(Signal a,Signal b))] -
  Signal (Array a b)

-- End Signature 



-- Updates an array Signal, given a static list of updaters. Each
--  updater consists of a Boolean enable signal, and a signal pair
--  of the update address and update value.
updateArray arr updaters
  = foldr (\(updateEnable,updater) prevArray -
if' updateEnable 
   then' (lift2 (//) prevArray (singleton (bundle updater)))
   else' prevArray)
  arr
  updaters
where singleton = lift1 $ \x - [x]


 Array implemented with lazy state -

-- Info needed to initialize a stateful array.
--  the list of tuples denotes what the various array subranges
--  should be initialized to.
--type ArrayDesc index val = ((index,index),[(index,index,val)])

-- Array request
data ArrReq i a  = ReadArr i |
   WriteArr i i a |
   WriteFn i (a - a) | -- modify contents at location i
   FreezeArr
   deriving Show

-- Array response
data ArrResp i a = ReadVal a |
   Written |
   WrittenFn a |
   ArrayVal (Array i a)
   deriving Show

{-
stateArray :: (Ix i, Enum i) =
ArrayDesc i a   -- array initialization info
-
Signal [ArrReq i a] -- array requests to read
--  and write values from/to
--  the array.
-
Signal [ArrResp i a]-- array responses corresponding
--  to ReadArr and FreezeArr
--  requests.
-}
stateArray (bounds@(loBound,hiBound),initWrites) (Sig reqss)
  = Sig $ runST (
do arr - newSTArray bounds initVal
   initArray arr
   performRequests arr)
where
  -- Determine what the array should be initialized to; remove
  --  some of the writes that would initialize the array to the
  --  same value to speed up the initialization process.
  contigWrites = contigWriteRanges 
  (loBound,hiBound,
   error "uninitialized value read from stateArray") 
  initWrites
  maxRange@(_,_,initVal) = maxWriteRange contigWrites
  reducedInitWrites = removeWriteRange maxRange contigWrites


  -- Initialize the array according to 'initWrites'
  initArray arr
= strictSequence [ writeSTArray arr index val |
(lowIdx,hiIdx,val) - reducedInitWrites,
index - range (lowIdx,hiIdx) ]

  --accumulate   :: Monad m = [m a] - m [a]
  accumulate [] = return []
  accumulate (c:cs) = do x - c
 xs - (accumulate cs) 
 return (x:xs)

  -- Perform the requested writes, reads, and freezes for each clock cycle
  performRequests arr
= accumulate $ map performReqs reqss
  where
performReqs reqs
  = mapM performReq reqs

performReq (ReadArr i)
  = do val - readSTArray arr i
   return (ReadVal val)

performReq (WriteArr loAddr hiAddr val)
  = do sequence [ writeSTArray arr loc val |
 

runST and LazyST don't mix -- ghc-2.09

1997-12-01 Thread Byron Cook

hi, 

in ghc-2.09 (solaris) i cannot use runST with LazyST
observe:

-
STRICT
$ ghc strict.hs
ghc: module version changed to 1; reason: no old .hi file
paratha$ cat strict.hs
import ST
main = print $ f True

f x = runST(
  do n - newSTRef x
 readSTRef n
  )
-
LAZY
$ ghc lazy.hs
 
lazy.hs:7: Value not in scope: `runST'

Compilation had errors


$ cat lazy.hs
import LazyST
main = print $ f True

f x = runST(
  do n - newSTRef x
 readSTRef n
  )



byron