{-# LANGUAGE TypeSynonymInstances, MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, FlexibleContexts, Safe #-}

{-
  This module is part of Chatty.
  Copyleft (c) 2014 Marvin Cohrs

  All wrongs reversed. Sharing is an act of love, not crime.
  Please share Chatty with everyone you like.

  Chatty is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Chatty is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Chatty. If not, see <http://www.gnu.org/licenses/>.
-}

module Data.Chatty.ListBuilder where

import Control.Applicative
import Control.Arrow
import Control.Monad.Trans.Class
import Control.Monad.Identity

class Monad l => ListBuilder l i | l -> i where
  li :: i -> l ()
  buildList :: l () -> [i]

newtype StrictBuilderT i m a = StrictBuilder { runStrictBuilderT :: [i] -> m (a,[i]) }
type StrictBuilder i = StrictBuilderT i Identity

instance Functor m => Functor (StrictBuilderT i m) where
  fmap f a = StrictBuilder $ \s -> fmap (first f) $ runStrictBuilderT a s

instance (Functor m, Monad m) => Applicative (StrictBuilderT i m) where
  (<*>) = ap
  pure = return

instance Monad m => Monad (StrictBuilderT i m) where
  return a = StrictBuilder $ \s -> return (a,s)
  m >>= f = StrictBuilder $ \s -> do (a,s') <- runStrictBuilderT m s; runStrictBuilderT (f a) s'

instance MonadTrans (StrictBuilderT i) where
  lift m = StrictBuilder $ \s -> do a <- m; return (a,s)

instance ListBuilder (StrictBuilder i) i where
  li a = seq a $ StrictBuilder $ \s -> return ((),s++[a])
  buildList m = snd $ runIdentity $ runStrictBuilderT m []

strictBuild :: StrictBuilderT i Identity () -> [i]
strictBuild = buildList

newtype LazyBuilderT i m a = LazyBuilder { runLazyBuilderT :: ([i] -> [i]) -> m (a,[i] -> [i]) }
type LazyBuilder i = LazyBuilderT i Identity

instance Functor m => Functor (LazyBuilderT i m) where
  fmap f a = LazyBuilder $ \s -> fmap (first f) $ runLazyBuilderT a s

instance (Functor m,Monad m) => Applicative (LazyBuilderT i m) where
  pure = return
  (<*>) = ap

instance Monad m => Monad (LazyBuilderT i m) where
  return a = LazyBuilder $ \s -> return (a,s)
  m >>= f = LazyBuilder $ \s -> do (a,s') <- runLazyBuilderT m s; runLazyBuilderT (f a) s'

instance MonadTrans (LazyBuilderT i) where
  lift m = LazyBuilder $ \s -> do a <- m; return (a,s)

instance ListBuilder (LazyBuilder i) i where
  li a = LazyBuilder $ \s -> return ((),s.([a]++))
  buildList m = ($[]) $ snd $ runIdentity $ runLazyBuilderT m ([]++)

lazyBuild :: LazyBuilderT i Identity () -> [i]
lazyBuild = buildList

lis :: ListBuilder l i => [i] -> l ()
lis = mapM_ li

lit :: ListBuilder l (a,b) => a -> b -> l ()
lit a b = li (a,b)

(>-<) :: ListBuilder l (a,b) => a -> b -> l ()
(>-<) = lit