~ubuntu-branches/ubuntu/raring/agda/raring-proposed

« back to all changes in this revision

Viewing changes to src/full/Agda/Utils/Trace.hs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2009-07-20 19:49:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090720194941-hcjy91vrn16csh7d
Tags: upstream-2.2.4+dfsg
ImportĀ upstreamĀ versionĀ 2.2.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-# LANGUAGE DeriveDataTypeable #-}
 
2
module Agda.Utils.Trace where
 
3
 
 
4
import Control.Monad
 
5
import Data.Monoid
 
6
import Data.Generics (Typeable, Data)
 
7
 
 
8
type Trace = CurrentCall
 
9
type SiblingCall = ChildCall
 
10
 
 
11
data CurrentCall a
 
12
    = Current a (ParentCall a) [SiblingCall a] [ChildCall a]
 
13
    | TopLevel [ChildCall a]
 
14
  deriving (Typeable, Data)
 
15
data ParentCall a
 
16
    = Parent  a (ParentCall a) [SiblingCall a]
 
17
    | NoParent
 
18
  deriving (Typeable, Data)
 
19
data ChildCall a = Child a [ChildCall a]
 
20
  deriving (Typeable, Data)
 
21
 
 
22
newCall :: a -> Trace a -> Trace a
 
23
newCall c (TopLevel cs)        = Current c NoParent cs []
 
24
newCall c (Current c' p ss cs) = Current c (Parent c' p ss) cs []
 
25
 
 
26
updateCall :: a -> Trace a -> Trace a
 
27
updateCall c (TopLevel _)        = error $ "updateCall: no a in progress"
 
28
updateCall c (Current _ p ss cs) = case p of
 
29
    NoParent         -> TopLevel $ Child c cs : ss
 
30
    Parent c' p' ss' -> Current c' p' ss' $ Child c cs : ss
 
31
 
 
32
matchCall :: (call -> Maybe a) -> Trace call -> Maybe a
 
33
matchCall f tr = case matchTrace f' tr of
 
34
    []    -> Nothing
 
35
    x : _ -> Just x
 
36
    where
 
37
        f' (Child c _) = maybe [] (:[]) $ f c
 
38
 
 
39
matchCalls :: (call -> Maybe a) -> Trace call -> [a]
 
40
matchCalls f = matchTrace f'
 
41
  where
 
42
    f' (Child c _) = maybe [] (:[]) $ f c
 
43
 
 
44
matchTrace :: Monoid m => (ChildCall call -> m) -> Trace call -> m
 
45
matchTrace f (TopLevel _) = mempty
 
46
matchTrace f t@(Current c _ _ cs) =
 
47
    f (Child c cs) `mappend` matchTrace f (updateCall c t)
 
48