aboutsummaryrefslogtreecommitdiff
path: root/bindings/haskell/examples/simple.hs
diff options
context:
space:
mode:
Diffstat (limited to 'bindings/haskell/examples/simple.hs')
-rw-r--r--bindings/haskell/examples/simple.hs46
1 files changed, 46 insertions, 0 deletions
diff --git a/bindings/haskell/examples/simple.hs b/bindings/haskell/examples/simple.hs
new file mode 100644
index 0000000..478f25b
--- /dev/null
+++ b/bindings/haskell/examples/simple.hs
@@ -0,0 +1,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)