blob: 478f25b2ebc576a3894cb21772ca91cf73f640ae (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
{- simple.hs -- basic ppamltracer example
This file is in the public domain.
Compile this with
ghc --make simple.hs
-}
{-# LANGUAGE LambdaCase #-}
module Main where
import Control.Applicative ((<$>), (<*>))
import Control.Monad (liftM)
import PPAML.Tracer
main :: IO ()
main = do
-- Start ppamltracer.
withTracer "/tmp/simple_report" $ \tracer -> do
-- Register the factorial phase.
withPhase tracer "fact" $ \phase -> do
-- Print factorials.
putStr "Factorials:"
mapM_ (putStr . (' ':) . show) =<< mapM (fact phase) [0 .. 40]
putStrLn ""
-- Register the Fibonacci phase.
withPhase tracer "fib" $ \phase -> do
-- Print Fibonacci numbers.
putStr "Fibonacci numbers:"
mapM_ (putStr . (' ':) . show) =<< mapM (fib phase) [0 .. 25]
putStrLn ""
{- Records that we're running inside the provided phase and computes a
factorial. -}
fact :: PhaseHandle -> Integer -> IO Integer
fact phase = withPhaseRunning phase . \case
0 -> return 1
n -> liftM (n*) $ fact phase (n - 1)
{- Records that we're running inside the provided phase and computes a Fibonacci
number. -}
fib :: PhaseHandle -> Integer -> IO Integer
fib phase = withPhaseRunning phase . \case
0 -> return 0
1 -> return 1
n -> (+) <$> fib phase (n - 1) <*> fib phase (n - 2)
|