{-# LANGUAGE TypeSynonymInstances, MultiParamTypeClasses, FunctionalDependencies, 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.StoreRepresentation where


-- An actual representation of the store

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

import GHC.Conc
import System.IO.Unsafe

import Data.List

import qualified MultiSetRewrite.ConcurrentList as L
import qualified MultiSetRewrite.ConcurrentBag as B

import MultiSetRewrite.Base
import MultiSetRewrite.RuleSyntax
import MultiSetRewrite.RuleCompiler


----------------------------------------
-- primitives for hashing messages

type HashIdx = Int

data HashOp msg = 
   HashOp { numberOfTables :: HashIdx
          , hashMsg :: msg -> HashIdx }

-- store representation

data Store msg = Store { msgTable :: [B.Bag (InternalMsg msg)],
                       hashOp :: HashOp msg
                     }

type Location msg = IORef (L.List (InternalMsg msg))
-- we need the location (pointer ref) of the message in internal form
-- for quick access (eg logical delete)


-- internal details of the store

distribution = 7 -- we assume that store is 
                 -- a bag of linked lists
                 
-- NOTE: we could assume a different distribution for
-- the individual messages

-- The run-time distribution is currently monolithic
-- We simply take the threadId 'mod' distribution


instance (EMatch msg, Eq msg, Show msg) => RuleCompiler (Store msg) 
                                       msg (Location msg)
                                       HashIdx (B.Iterator (InternalMsg msg)) where
--atomicVerifyAndDeleteCnt :: Store msg -> Code_RHS a 
--                         -> [(Check (Location msg),IO (Maybe (Code_RHS a)))] -> IO (Maybe (Code_RHS a))
  -- we don't need the store here, but otherwise the method's type is ambiguous
  atomicVerifyAndDeleteCnt _ body checks =
   let 
       -- reverify that all verify fields are True
       -- starting with the first field, 
       -- if unsuccessful, return the continuation of the first failed field
       reVerify [] = return Nothing
       reVerify ((n,cnt):rest) = do
            let n' = case n of
                       Verify n -> n
                       VerifyAndDelete n -> n
            b <- readTVar n'
            if b then reVerify rest
             else return (Just cnt)

       -- we check and set to False simplified heads
       -- we only check propagated heads
       reVerifyAndSetFalse [] = return Nothing
       reVerifyAndSetFalse ((n,cnt):rest) = do
          case n of
            Verify n -> do b <- readTVar n
                           if b then reVerifyAndSetFalse rest
                            else return (Just cnt)

            VerifyAndDelete n -> do 
                           b <- readTVar n
                           if b then do writeTVar n False
                                        reVerifyAndSetFalse rest
                            else return (Just cnt)

            


       -- set verify fields to False
       -- only those which actually need to be deleted (the simp heads)
       setFalse [] = return ()
       setFalse ((Verify _,_):ns) = setFalse ns
       setFalse ((VerifyAndDelete n,_):ns) = do
            writeTVar n False
            setFalse ns

       -- accumulates all deletes (only simp heads)
       -- must be performed in IO
       accDeletes [] = return ()
       accDeletes ((Verify _,_):ns) = accDeletes ns
       accDeletes ((VerifyAndDelete msg,_):ns) = do
           node <- readIORef msg
           writeIORef msg (L.DelNode {L.verify = L.verify node,
                                         L.next = L.next node})
           accDeletes ns


       -- verify all atomic guards, return continuation if unsuccessful
       verifyAtomicGuard [] = return Nothing
       verifyAtomicGuard ((g,cnt):rest) =
                case g of
                   AtomicGuardVerify g -> do
                            b <- g
                            if b then verifyAtomicGuard rest
                             else return (Just cnt)
                            
       
       selectNode = \n -> case n of
                      (Verify _,_) -> True
                      (VerifyAndDelete _,_) -> True
                      _ -> False

       checkNodes = filter selectNode checks
         -- select the to be verified nodes

       checkAtomicGuards = filter (not.selectNode) checks
         -- select the atomic guards (those with side-effects)
         -- side-effect guards have already been check earlier
                                  

   in do ns <- -- extract the to be verified fields
               -- from the IORef nodes
               mapM (\ (m,cnt) ->
                     case m of
                       Verify msg -> do node <- readIORef msg
                                        return (Verify (L.verify node),cnt)
                       VerifyAndDelete msg -> do node <- readIORef msg
                                                 return (VerifyAndDelete (L.verify node),cnt))
                    checkNodes

               -- perform atomic re-verification
               --   check that nodes are still there and
               --   check that atomic guards are true, if successful
               --     set node flags to False
               --     we continue by actually deleting the nodes followed by
               --     executing the body
               --   otherwise, we abort and 
                --    (1) either pick the continuation of the 
               --         first node which couldn't be reverified   (back-jumping!)
               --     (2) if all nodes could be verified but the atomic guard caused failure
               --         pick the continuation of the last node
               --         (NOTE: atomic guards must be the last match tasks, simply because
               --          they contain side-effects and therefore they must be atomically
               --          checked as part of the re-verification phase)


         -- Version1 (uses side-effect we communicate which retry failed):
         --   in the first transaction
         --    (1) we reverify and set to false the fields
         --        (a) in case of failure, we collect the continuation
         --        we retry (to cancel all the set to false effects)
         --        and pass the continuation (as a side-effect) to
         --        the 'else' transaction
         --        (b) if successful continue with (2)
         --    (2) verify the atomic guards (with side-effects)
         --        (a) in case of failure, we collect the continuation
         --        we retry (to cancel all the set to false effects)
         --        and pass the continuation (as a side-effect) to
         --        the 'else' transaction (see 1a).
         --        (b) if successful continue with (3)
         --    (3) on commit we perform the actual (logical) delete of the nodes
         
         retryRes <- newIORef Nothing
           -- introduce IORef to communicate which retry led to abort

         next <- atomically $ (do
                    r <- reVerifyAndSetFalse ns              -- (1)
                    case r of
                      Just cnt -> do
                        unsafeIOToSTM $ writeIORef retryRes (Just cnt)
                         -- pass info to else branch, the continuation
                         -- of the first failed verify field
                        retry
                      Nothing -> do
                        r2 <- verifyAtomicGuard checkAtomicGuards    -- (2)
                        case r2 of
                          Just cnt -> do
                            unsafeIOToSTM $ writeIORef retryRes (Just cnt)
                            -- pass info to else branch, continuation of 
                            -- the last field (need to continue search)
                            retry
                          Nothing -> do
                            return (return (Just (do {accDeletes checkNodes; body}))) -- (3)
                    )
                     `orElse` (do
                        x <- unsafeIOToSTM $ readIORef retryRes
                        case x of
                          Nothing -> error "impossible"
                          Just cnt -> return cnt)
         next

{-
         -- Version2 (pure version):
         --   in the first transaction
         --   (i) we reverify the fields (setting to false comes later!)
         --       (a) in case of failure, collect the continuation
         --           and return (commit) 
         --           (no retry necessary cause no setting to false yet)
         --       (b) if ok go to (ii)
         --   (ii) (a) verify atomic guards (with side-effects)
         --        (a) in case of failure, we retry
         --            then second transaction will select the continuation
         --            of the last atomic guard
         --        (b) if successful continue with (3)
         --    (3) set fields to false
         --        on commit we perform the actual (logical) delete of the nodes


         next <- atomically $ (do
                    r <- reVerify ns              -- (i)
                    case r of
                      Just cnt -> return cnt
                      Nothing -> do
                        r2 <- verifyAtomicGuard checkAtomicGuards    -- (ii)
                        case r2 of
                          Just cnt -> retry
                          Nothing -> do
                            setFalse ns
                            return (return (Just (do {accDeletes checkNodes; body}))) -- (iii)
                    )
                     `orElse` (do
                        let (AtomicGuardVerify _, cnt) = last checkAtomicGuards
                        return cnt)
         next
-}

--getIndex :: Store msg -> msg  -> IO HashIdx
  getIndex act m = return ((hashMsg (hashOp act)) m)

--initSearch :: Store msg -> HashIdx -> IO (B.Iterator (InternalMsg msg))
  initSearch  act idx =
     do let bag = (msgTable act) !! (idx -1) 
        t_id <- myThreadId
        let no =  threadIdToInt t_id
        let bagEntryIdx = no `mod` distribution
        it <- B.newIterator bag bagEntryIdx
        return it

-- we use the iterator to scan through the queue 
--nextMsg :: Store msg -> (B.Iterator (InternalMsg msg)) -> IO (Maybe (Location msg))
  nextMsg act curIterator =
    do res <- B.iterateBag curIterator
       return res

--extractMsg :: Location msg -> IO (Maybe (InternalMsg msg))
  extractMsg ptr =
    do node <- readIORef ptr
       case node of
          L.Node {} -> return (Just (L.val node))
          _         -> return Nothing

  printMsg _ ptr = do
    do m <- readIORef ptr
       return (show (L.val m))


  printReachMsg _ it = error "TODO"


-- HACK!!!!!
-- FIX
-- length "ThreadId " = 9
threadIdToInt x = read (drop 9 (show x)) :: Int

newStore :: (EMatch msg, Eq msg, Show msg) => 
           HashOp msg -> IO (Store msg)
newStore hash  = 
      do let n = numberOfTables hash
         mt <- mapM (\ _ -> B.newBag (distribution-1)) [1..n]
                -- distribution starts with 0 !!!
         return (Store {msgTable = mt,                       
                       hashOp = hash})


compileRulePattern :: (EMatch msg, Eq msg, Show msg) => 
                      [([MatchTask msg], Code_RHS ())] -> [CompClause (Store msg) (Location msg) ()]
compileRulePattern prog =
   let build [] = []  
       build ((tasks,body):rest) = 
               (compileCnt body tasks) 
               ++ (build rest)
   in build prog

-- adds the message in external format and
-- returns (added) message in internal format
-- this is the format we need to perform the search      
addMsg :: (EMatch msg, Eq msg, Show msg) => 
               (Store msg) -> msg -> IO (Location msg)
addMsg (Store {msgTable = mt,
              hashOp = hash}) 
       msg =
  do new_tag <- newTag
     let new_msg = InternalMsg {message = msg, 
                                msg_tag = new_tag}
     let hashIdx = (hashMsg hash) msg
     let bag = mt !! (hashIdx - 1)
     t_id <- myThreadId
     let no =  threadIdToInt t_id
     let bagEntryIdx = no `mod` distribution
     loc <- B.addToBag bag bagEntryIdx new_msg 
     return loc


   
executeRules :: (EMatch msg, Eq msg, Show msg) => 
               Store msg -> Location msg -> [CompClause (Store msg) (Location msg) ()] -> IO (Maybe (Code_RHS ()))
executeRules store active_msg prog =
   select store active_msg prog


{-
-----------------------------------
-- some old stuff
-----------------------------------

--atomicVerifyAndDeleteCnt :: Store msg -> Code_RHS a 
--                         -> [(Check (Location msg),IO (Maybe (Code_RHS a)))] -> IO (Maybe (Code_RHS a))
  -- we don't need the store here, but otherwise the method's type is ambiguous
  atomicVerifyAndDeleteCnt _ body checks =
   let 
       -- reverify that all verify fields are True
       reVerify [] = return True
       reVerify (n:ns) = do
            let n' = case n of
                       Verify n -> n
                       VerifyAndDelete n -> n
            b <- readTVar n'
            if b then reVerify ns
             else return False

       -- build Boolean conjunction of all atomic guards
       verifyAtomicGuard gs = do
           bs <- mapM (\g -> case g of
                          AtomicGuardVerify g -> do
                              b <- g
                              return b
                          _ -> error "impossible: verifyAtomicGuard")
                      gs
           return (and bs)

       -- set verify fields to False
       -- only those which actually need to be deleted (the simp heads)
       setFalse [] = return ()
       setFalse ((Verify _):ns) = setFalse ns
       setFalse ((VerifyAndDelete n):ns) = do
            writeTVar n False
            setFalse ns

       -- accumulates all deletes (only simp heads)
       accDeletes [] = return ()
       accDeletes ((Verify _,_):ns) = accDeletes ns
       accDeletes ((VerifyAndDelete msg,_):ns) = do
           node <- readIORef msg
           writeIORef msg (L.DelNode {L.verify = L.verify node,
                                         L.next = L.next node})
           accDeletes ns
                            
       
       -- in case of failure, pick the continuation of the node
       -- which failed (the earliest from left to right)
       checkNode (msg,cnt) = do
          node <- readIORef msg
          b <- atomically $ readTVar (L.verify node)
          if b then return Nothing
           else return (Just cnt)         
                 
       pickCont [] = error "pickCont: impossible, there must be a failed node"
       pickCont (c:rest) = do
          let (msg,cnt) = case c of
                            (Verify msg, cnt) -> (msg, cnt)
                            (VerifyAndDelete msg, cnt) -> (msg, cnt)
          res <- checkNode (msg,cnt)
          case res of
            Nothing -> pickCont rest
            Just cnt -> cnt

      selectNode = \n -> case n of
                      Verify _ -> True
                      VerifyAndDelete -> True
                      _ -> False

      checkNodes = filter selectNode checks
         -- select to be verified nodes
      checkAtomicGuards = filter (not.nodeCheck) checks
         -- select the atomic guards (those with side-effects)
         -- side-effect guards have already been check earlier
                                  

   in do ns <- -- extract the to be verified fields
               mapM (\ (m,_) ->
                     case m of
                       Verify msg -> do node <- readIORef msg
                                        return (Verify (L.verify node))
                       VerifyAndDelete msg -> do node <- readIORef msg
                                                 return (VerifyAndDelete (L.verify node)))
                    checkNodes

               -- perform atomic re-verification
               --   check that nodes are still there and
               --   check that atomic guards are true, if successful
               --     set node flags to False
               --     we continue by actually deleting the nodes followed by
               --     executing the body
               --   otherwise, we abort and 
                --    (1) either pick the continuation of the 
               --         first node which couldn't be reverified   (back-jumping!)
               --     (2) if all nodes could be verified but the atomic guard caused failure
               --         pick the continuation of the last node
         next <- atomically $
                   (do r <- reVerify ns
                       r2 <- verifyAtomicGuarad checkAtomicGuards
                       if (r && r2) then do   
                          setFalse ns 
                          return (return (Just (do {accDeletes checks; body})))
                        else retry
                    )
                     `orElse`
                    return (pickCont checks)
         next

-}