module Hydra.Util.Context where

import Hydra.All
import qualified Hydra.Impl.Haskell.Dsl.Terms as Terms

import qualified Data.Map as M
import qualified Data.Maybe as Y


getAttr :: String -> Flow s (Maybe (Term Meta))
getAttr :: forall s. String -> Flow s (Maybe (Term Meta))
getAttr String
key = forall s a. (s -> Trace -> FlowState s a) -> Flow s a
Flow forall {s}. s -> Trace -> FlowState s (Maybe (Term Meta))
q
  where
    q :: s -> Trace -> FlowState s (Maybe (Term Meta))
q s
s0 Trace
t0 = forall s a. Maybe a -> s -> Trace -> FlowState s a
FlowState (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
key forall a b. (a -> b) -> a -> b
$ Trace -> Map String (Term Meta)
traceOther Trace
t0) s
s0 Trace
t0

getAttrWithDefault :: String -> Term Meta -> Flow s (Term Meta)
getAttrWithDefault :: forall s. String -> Term Meta -> Flow s (Term Meta)
getAttrWithDefault String
key Term Meta
def = forall a. a -> Maybe a -> a
Y.fromMaybe Term Meta
def forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s. String -> Flow s (Maybe (Term Meta))
getAttr String
key

nextCount :: String -> Flow s Int
nextCount :: forall s. String -> Flow s Int
nextCount String
attrName = do
  Int
count <- forall s. String -> Term Meta -> Flow s (Term Meta)
getAttrWithDefault String
attrName (forall m. Int -> Term m
Terms.int32 Int
0) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall m s. Show m => Term m -> Flow s Int
Terms.expectInt32
  forall s. String -> Term Meta -> Flow s ()
putAttr String
attrName (forall m. Int -> Term m
Terms.int32 forall a b. (a -> b) -> a -> b
$ Int
count forall a. Num a => a -> a -> a
+ Int
1)
  forall (m :: * -> *) a. Monad m => a -> m a
return Int
count

putAttr :: String -> Term Meta -> Flow s ()
putAttr :: forall s. String -> Term Meta -> Flow s ()
putAttr String
key Term Meta
val = forall s a. (s -> Trace -> FlowState s a) -> Flow s a
Flow forall {s}. s -> Trace -> FlowState s ()
q
  where
    q :: s -> Trace -> FlowState s ()
q s
s0 Trace
t0 = forall s a. Maybe a -> s -> Trace -> FlowState s a
FlowState (forall a. a -> Maybe a
Just ()) s
s0 (Trace
t0 {traceOther :: Map String (Term Meta)
traceOther = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert String
key Term Meta
val forall a b. (a -> b) -> a -> b
$ Trace -> Map String (Term Meta)
traceOther Trace
t0})