+-- Note [freevars]
+-- For breakpoints we want to collect the free variables of an
+-- expression for pinning on the HsTick. We don't want to collect
+-- *all* free variables though: in particular there's no point pinning
+-- on free variables that are will otherwise be in scope at the GHCi
+-- prompt, which means all top-level bindings. Unfortunately detecting
+-- top-level bindings isn't easy (collectHsBindsBinders on the top-level
+-- bindings doesn't do it), so we keep track of a set of "in-scope"
+-- variables in addition to the free variables, and the former is used
+-- to filter additions to the latter. This gives us complete control
+-- over what free variables we track.
+
+data TM a = TM { unTM :: TickTransEnv -> TickTransState -> (a,FreeVars,TickTransState) }
+ -- a combination of a state monad (TickTransState) and a writer
+ -- monad (FreeVars).
+
+instance Monad TM where
+ return a = TM $ \ env st -> (a,noFVs,st)
+ (TM m) >>= k = TM $ \ env st ->
+ case m env st of
+ (r1,fv1,st1) ->
+ case unTM (k r1) env st1 of
+ (r2,fv2,st2) ->
+ (r2, fv1 `plusOccEnv` fv2, st2)
+
+-- getState :: TM TickTransState
+-- getState = TM $ \ env st -> (st, noFVs, st)
+
+setState :: (TickTransState -> TickTransState) -> TM ()
+setState f = TM $ \ env st -> ((), noFVs, f st)
+
+getEnv :: TM TickTransEnv
+getEnv = TM $ \ env st -> (env, noFVs, st)
+
+withEnv :: (TickTransEnv -> TickTransEnv) -> TM a -> TM a
+withEnv f (TM m) = TM $ \ env st ->
+ case m (f env) st of
+ (a, fvs, st') -> (a, fvs, st')
+
+getFreeVars :: TM a -> TM (FreeVars, a)
+getFreeVars (TM m)
+ = TM $ \ env st -> case m env st of (a, fv, st') -> ((fv,a), fv, st')
+
+freeVar :: Id -> TM ()
+freeVar id = TM $ \ env st ->
+ if id `elemVarSet` inScope env
+ then ((), unitOccEnv (nameOccName (idName id)) id, st)
+ else ((), noFVs, st)