module Language.Haskell.FreeTheorems.Variations.PolySeq.M (M,runM,newLab,abort,track) where type State = (Int,[String]) data M a = M (State -> [(a,State)]) instance Monad M where return a = M (\s -> [(a,s)]) M m >>= k = M (\s -> concatMap (\(a,s') -> case k a of M l -> l s') (m s)) runM :: M a -> Maybe (a, [String]) runM (M m) = case m (1,[]) of [] -> Nothing ((a,(_,ls)):_) -> Just (a,reverse ls) newLab :: M Int newLab = M(\(i,ls) -> [(i,(i+1,ls))]) abort :: M a abort = M (\s -> []) track :: String -> M () track l = M (\(i,ls) -> [((),(i,l:ls))])