1
{-# LANGUAGE DeriveDataTypeable #-}
2
module Agda.Utils.Trace where
6
import Data.Generics (Typeable, Data)
8
type Trace = CurrentCall
9
type SiblingCall = ChildCall
12
= Current a (ParentCall a) [SiblingCall a] [ChildCall a]
13
| TopLevel [ChildCall a]
14
deriving (Typeable, Data)
16
= Parent a (ParentCall a) [SiblingCall a]
18
deriving (Typeable, Data)
19
data ChildCall a = Child a [ChildCall a]
20
deriving (Typeable, Data)
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 []
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
32
matchCall :: (call -> Maybe a) -> Trace call -> Maybe a
33
matchCall f tr = case matchTrace f' tr of
37
f' (Child c _) = maybe [] (:[]) $ f c
39
matchCalls :: (call -> Maybe a) -> Trace call -> [a]
40
matchCalls f = matchTrace f'
42
f' (Child c _) = maybe [] (:[]) $ f c
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)