summaryrefslogtreecommitdiff
path: root/events/src/Events/Types.hs
diff options
context:
space:
mode:
authorGregor Kleen <pngwjpgh@users.noreply.github.com>2016-07-28 23:14:50 +0200
committerGregor Kleen <pngwjpgh@users.noreply.github.com>2016-07-28 23:14:50 +0200
commitb2e4264e7849f322cbb2bb592b15d2ea7aec9149 (patch)
treeaea74b3cf9311932e243f7088b0e3377616aa329 /events/src/Events/Types.hs
parent69081d160dbf6f7d06b9cafd876e0fea423b8066 (diff)
downloadevents-b2e4264e7849f322cbb2bb592b15d2ea7aec9149.tar
events-b2e4264e7849f322cbb2bb592b15d2ea7aec9149.tar.gz
events-b2e4264e7849f322cbb2bb592b15d2ea7aec9149.tar.bz2
events-b2e4264e7849f322cbb2bb592b15d2ea7aec9149.tar.xz
events-b2e4264e7849f322cbb2bb592b15d2ea7aec9149.zip
Switch from monoid to foldable container
Diffstat (limited to 'events/src/Events/Types.hs')
-rw-r--r--events/src/Events/Types.hs63
1 files changed, 32 insertions, 31 deletions
diff --git a/events/src/Events/Types.hs b/events/src/Events/Types.hs
index 22faf94..0eff7aa 100644
--- a/events/src/Events/Types.hs
+++ b/events/src/Events/Types.hs
@@ -1,37 +1,34 @@
1{-# LANGUAGE TemplateHaskell #-} 1{-# LANGUAGE TemplateHaskell #-}
2{-# LANGUAGE MultiParamTypeClasses #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE UndecidableInstances #-}
5 2
6module Events.Types 3module Events.Types
7 ( TimeRange(..), rangeStart, rangeDuration 4 ( TimeRange(..), rangeStart, rangeDuration
8 , Event(..), payload, occursWithin 5 , Event(..), payload, occursWithin
9 , EvalCtx(..), ctxVars, ctxEvents 6 , EvalCtx(..), ctxEvents
10 , ObjCtx(..), objOccurs, objPayload 7 , ObjCtx(..), objVars, objOccurs, objPayload
11 , Eval, evaluate 8 , Eval, evaluate
12 , module Data.Aeson 9 , module Data.Aeson
13 , module Data.Time.Clock 10 , module Data.Time.Clock
14 , module Data.Default.Class 11 , module Data.Default.Class
15 ) where 12 ) where
16 13
17import Control.Lens.TH 14import Control.Lens.TH (makeLenses)
18 15
19import Data.Aeson (Object) 16import Data.Aeson (Object)
20 17
21import Data.Time.Clock 18import Data.Time.Clock (UTCTime, NominalDiffTime)
22 19
23import Control.Monad.State.Lazy 20import Control.Monad.State.Lazy (StateT, evalStateT, execStateT)
24import ListT (ListT)
25import qualified ListT
26 21
27import Data.Default.Class 22import Data.Default.Class (Default(def))
28 23
29import Data.Monoid 24-- import Data.Monoid
30import Control.Monad.Fix 25import Control.Monad.Fix (MonadFix(mfix))
31import Control.Lens 26import Control.Lens ((^.), set)
32import Data.Maybe 27import Data.Maybe (catMaybes)
33 28
34import Debug.Trace 29-- import Debug.Trace
30
31import Events.Types.NDT (NDT, foldNDT)
35 32
36data TimeRange = TimeRange 33data TimeRange = TimeRange
37 { _rangeStart :: UTCTime 34 { _rangeStart :: UTCTime
@@ -46,40 +43,44 @@ data Event = Event
46makeLenses ''Event 43makeLenses ''Event
47 44
48data EvalCtx = EvalCtx 45data EvalCtx = EvalCtx
49 { _ctxVars :: Object 46 { _ctxEvents :: [Object]
50 , _ctxEvents :: [Object]
51 } deriving (Show) 47 } deriving (Show)
52makeLenses ''EvalCtx 48makeLenses ''EvalCtx
53 49
54instance Default EvalCtx where 50instance Default EvalCtx where
55 def = EvalCtx 51 def = EvalCtx
56 { _ctxVars = mempty 52 { _ctxEvents = mempty
57 , _ctxEvents = mempty
58 } 53 }
59 54
60data ObjCtx = ObjCtx 55data ObjCtx = ObjCtx
61 { _objOccurs :: Bool 56 { _objOccurs :: Bool
62 , _objPayload :: Maybe Object 57 , _objPayload :: Maybe Object
58 , _objVars :: Object
63 } 59 }
64makeLenses ''ObjCtx 60makeLenses ''ObjCtx
65 61
66instance Default ObjCtx where 62instance Default ObjCtx where
67 def = ObjCtx 63 def = ObjCtx
68 { _objOccurs = False 64 { _objOccurs = True
69 , _objPayload = Nothing 65 , _objPayload = Nothing
66 , _objVars = mempty
70 } 67 }
71 68
72objCtx :: ObjCtx -> Maybe Object 69objCtx :: ObjCtx -> Maybe Object
73objCtx (ObjCtx False _) = Nothing 70objCtx ctx
74objCtx (ObjCtx True o) = o 71 | ctx ^. objOccurs = ctx ^. objPayload
72 | otherwise = Nothing
75 73
76type Eval m a = StateT ObjCtx (ListT (StateT EvalCtx m)) a 74-- type Eval m a = StateT ObjCtx (ListT (StateT EvalCtx m)) a
77 75
78evaluate :: MonadFix m => Eval m () -> m [Object] 76-- evaluate :: MonadFix m => Eval m () -> m [Object] {- TODO: Switch to `ListT m Object` – `m [Object]` will turn out to be to strict. There is no instance for `MonadFix (ListT m)` – writing one seems to be possible. -}
79evaluate x = catMaybes <$> mfix x' 77-- evaluate x = catMaybes <$> mfix x'
80 where 78-- where
81 x' = evalStateT (ListT.toReverseList (objCtx <$> execStateT x def)) . flip (set ctxEvents) def . catMaybes 79-- x' = evalStateT (ListT.toList (objCtx <$> execStateT x def)) . flip (set ctxEvents) def . catMaybes
82 80
83instance MonadState s m => MonadState s (ListT m) where 81type Eval m a = StateT ObjCtx (NDT (StateT EvalCtx m)) a
84 get = lift get 82
85 put = lift . put 83evaluate :: MonadFix m => (Maybe Object -> StateT EvalCtx m Bool) -> Eval m () -> m [Object] {- TODO: Switch to `ListT m Object` – `m [Object]` will turn out to be to strict. There is no instance for `MonadFix (ListT m)` – writing one seems to be possible. -}
84evaluate predicate x = catMaybes <$> mfix x'
85 where
86 x' = evalStateT (foldNDT predicate (objCtx <$> execStateT x def)) . flip (set ctxEvents) def . catMaybes