{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}

{-
--------------------------------------------------------------------------------
--
-- Copyright (C) 2008 Martin Sulzmann, Edmund Lam. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-}


module MultiSetRewrite.StorePrettyPrinter where

import Data.IORef
import Control.Concurrent
import Control.Concurrent.STM

import MultiSetRewrite.ConcurrentBag as B
import MultiSetRewrite.ConcurrentList as L
import MultiSetRewrite.Base
import MultiSetRewrite.StoreRepresentation

----------------------------------------
-- Additional List/Bag helper functions

retrieveList :: ListHandle a -> IO [a]
retrieveList (ListHandle {headList = ptrPtr}) =
  do { startptr <- 
          do { ptr <- readIORef ptrPtr
             ; Head {next = startptr} <- readIORef ptr
             ; return startptr }
     ; retrieveListHelp startptr }
     where
       retrieveListHelp :: IORef (List a) -> IO [a]
       retrieveListHelp curNodePtr =
          do { curNode <- readIORef curNodePtr
             ; case curNode of
                Null -> return []
                Node {val = curval, next = curnext} ->
                    do { as <- retrieveListHelp curnext
                       ; return $ curval:as } 
                DelNode {next = curnext} -> retrieveListHelp curnext 
             } 
             
retrieveBag :: Bag a -> IO [a]
retrieveBag (Bag _ ls) =
  do { ass <- mapM retrieveList ls
     ; return $ foldl (++) [] ass }

-------------------------------------

class PrettyPrint a where
  prettyIt :: a -> IO String

prettyItList :: PrettyPrint a => [a] -> String -> IO String
prettyItList [] _ = return ""
prettyItList [a] _ = prettyIt a
prettyItList (a:as) d = do 
    { s  <- prettyIt a
    ; s' <- prettyItList as d
    ; return $ s ++ d ++ s' }

-------------------------------------

instance PrettyPrint a => PrettyPrint (VAR a) where
  prettyIt (VAR iref _) = do
    { v <- readIORef iref
    ; prettyIt v }
  prettyIt DontCare = return "_"

instance PrettyPrint a => PrettyPrint (L a) where
  prettyIt (Val a) = prettyIt a
  prettyIt (Var var) = prettyIt var

instance PrettyPrint a => PrettyPrint [a] where
  prettyIt [a] = prettyIt a
  prettyIt (a:as) = do
    { s  <- prettyIt a
    ; s' <- prettyIt as
    ; return $ s ++ "," ++ s' }
  
-------------------------------------

instance PrettyPrint Int where
  prettyIt i = return $ show i
  
instance PrettyPrint Char where
  prettyIt c = return $ show c
  
instance PrettyPrint Bool where
  prettyIt b = return $ show b
  
instance PrettyPrint a => PrettyPrint (MVar a) where
  prettyIt mv = do
    { mb <- tryTakeMVar mv
    ; case mb of
       Just v  -> do { s <- prettyIt v
                     ; tryPutMVar mv v
                     ; return s }
       Nothing -> return "?" }
    
instance PrettyPrint a => PrettyPrint (TVar a) where
  prettyIt tv = do
    { v <- atomically $ readTVar tv
    ; prettyIt v }
       
-------------------------------------

instance PrettyPrint a => PrettyPrint (InternalMsg a) where
  prettyIt (InternalMsg a _) = prettyIt a
  
instance PrettyPrint a => PrettyPrint (B.Bag a) where
  prettyIt bag = do
    { as <- retrieveBag bag
    ; prettyItList as "," }
  
instance PrettyPrint a => PrettyPrint (Store a) where
  prettyIt (Store as _) = prettyItList as "\n"