Both parallel and sequential computation must be carefully controlled to produce good parallel and distributed Haskell programs. Several languages including Glasgow parallel Haskell and Eden use *evaluation strategies*: overloaded polymorphic functions to describe the amount of evaluation.
> type Done = ()
> type Strategy a = a -> Done
There are three basic strategies:
- r0 performs no evaluation on its argument,
- rwhnf reduces its argument to weak head normal form,
- rnf reduces its argument to (head) normal form: i.e. deepseq. System support
for rnf/deepseq would be A Good Thing.
> r0, rwhnf :: Strategy a
> r0 x = ()
> rwhnf x = x `seq` ()
> class NFData a where
> class Eval a => NFData a where
> rnf :: Strategy a
Example instances of NFData are given below, a useful set are predefined in the
module below:
> instance (NFData a, NFData b) => NFData (a,b) where
> rnf (x,y) = rnf x `seq` rnf y
> instance NFData a => NFData [a] where
> rnf [] = ()
> rnf (x:xs) = rnf x `seq` rnf xs
Strategies are simply functions that can be abstracted and composed lilke
any others, e.g. SeqList applies a strategy to each element of a list
sequentially.
> seqList :: Strategy a -> Strategy [a]
> seqList strat [] = ()
> seqList strat (x:xs) = strat x `seq` (seqList strat xs)
So 'SeqList r0' evaluates just the spine of the list, and 'seqList (seqList
rwhnf)' evaluates the elements of a list of lists to weak head normal form.
You can apply a strategy to an expression
> using :: a -> Strategy a -> a
> using x s = s x `seq` x
So to force newcounts in your program you want to change the last line to be
something like:
countAFile oldcounts filename =
do
compiledFile <- readAndCompile filename
let newcounts = countAssociations compiledFile oldcounts
return newcounts 'using' rnf
There's a copy of the Strategies module attached. More info in "Algorithm +
Strategy = Parallelism",
P.W. Trinder, K. Hammond, H-W. Loidl, S.L. Peyton Jones
In Journal of Functional Programming 8(1):23--60, January 1998.
URL: http://www.cee.hw.ac.uk/~dsg/gph/papers/ps/strategies.ps.gz
Phil
--------------------------------------------------
Phil Trinder
Department of Computing and Electrical Engineering
Heriot Watt University
Riccarton
Edinburgh, EH14 4AS
E-mail: [EMAIL PROTECTED]
Teleph: +44 (0)131 451 3435
Depart: +44 (0)131 451 3328
Fasmly: +44 (0)131 451 3327
Intrnt: http://www.cee.hw.ac.uk/~trinder
Strategies.lhs
Description: Binary data
