pykinos-penelope-0.1.1.0: src/Pykinos/Penelope/ConditionalTrail.hs
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DerivingStrategies #-}
-- | Module: Pykinos.Penelope.ConditionalTrail
-- Description: WAM-style epoch-guarded conditional undo trail
--
-- Implements Warren Abstract Machine (WAM) conditional trailing: modifications
-- are only recorded in the undo log when a slot was created or last modified in
-- an earlier epoch (@slotEpoch < currentChoiceEpoch@). Repeated mutations in the
-- same epoch require zero trailing overhead and roll back in O(mutations).
module Pykinos.Penelope.ConditionalTrail
( Epoch(..)
, Checkpoint(..)
, TrailEntry(..)
, ConditionalTrail
, newConditionalTrail
, readSlot
, writeSlot
, checkpointTrail
, rollbackTrail
, trailLogLength
) where
import Control.Monad (forM_)
import Control.Monad.ST (ST)
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.STRef (STRef, newSTRef, readSTRef, writeSTRef)
-- | Monotonically increasing choice point epoch identifier.
newtype Epoch = Epoch { unEpoch :: Int }
deriving stock (Eq, Ord, Show)
-- | O(1) restoration marker capturing epoch and undo-log watermark.
data Checkpoint = Checkpoint
{ cpEpoch :: !Epoch
, cpTrailMark :: !Int
} deriving stock (Eq, Ord, Show)
-- | Value paired with its last-modified epoch.
data SlotState v = SlotState
{ slotVal :: !v
, slotEpoch :: !Epoch
} deriving stock (Eq, Show)
-- | Undo log entry recording prior slot state for restoration.
data TrailEntry v = TrailEntry
{ teKey :: !Int
, tePrevVal :: !(Maybe (SlotState v))
} deriving stock (Eq, Show)
data TrailState v = TrailState
{ tsSlots :: !(IntMap (SlotState v))
, tsEpoch :: !Epoch
, tsLogRev :: ![TrailEntry v]
, tsLogLen :: !Int
}
-- | Mutable conditional trail in @ST s@.
newtype ConditionalTrail s v = ConditionalTrail (STRef s (TrailState v))
-- | Allocates a fresh empty 'ConditionalTrail' at epoch 0.
newConditionalTrail :: ST s (ConditionalTrail s v)
newConditionalTrail = do
ref <- newSTRef (TrailState IntMap.empty (Epoch 0) [] 0)
pure (ConditionalTrail ref)
-- | Reads the current value of slot @k@.
readSlot :: ConditionalTrail s v -> Int -> ST s (Maybe v)
readSlot (ConditionalTrail ref) !k = do
st <- readSTRef ref
pure (slotVal <$> IntMap.lookup k (tsSlots st))
-- | Writes a value into slot @k@, applying WAM conditional trailing rules.
writeSlot :: ConditionalTrail s v -> Int -> v -> ST s ()
writeSlot (ConditionalTrail ref) !k !val = do
st <- readSTRef ref
let curEpoch = tsEpoch st
existing = IntMap.lookup k (tsSlots st)
case existing of
Nothing -> do
-- Fresh slot: record that it previously did not exist
let entry = TrailEntry k Nothing
writeSTRef ref st
{ tsSlots = IntMap.insert k (SlotState val curEpoch) (tsSlots st)
, tsLogRev = entry : tsLogRev st
, tsLogLen = tsLogLen st + 1
}
Just oldSlot
| slotEpoch oldSlot < curEpoch -> do
-- Modified across epoch boundary: trail prior state
let entry = TrailEntry k (Just oldSlot)
writeSTRef ref st
{ tsSlots = IntMap.insert k (SlotState val curEpoch) (tsSlots st)
, tsLogRev = entry : tsLogRev st
, tsLogLen = tsLogLen st + 1
}
| otherwise -> do
-- Modified within the same epoch: NO trailing needed!
writeSTRef ref st
{ tsSlots = IntMap.insert k (SlotState val curEpoch) (tsSlots st)
}
-- | Creates a new choice point checkpoint, advancing the active epoch.
checkpointTrail :: ConditionalTrail s v -> ST s Checkpoint
checkpointTrail (ConditionalTrail ref) = do
st <- readSTRef ref
let curCp = Checkpoint (tsEpoch st) (tsLogLen st)
nextEpoch = Epoch (unEpoch (tsEpoch st) + 1)
writeSTRef ref st { tsEpoch = nextEpoch }
pure curCp
-- | Rolls back the trail state to the specified checkpoint.
rollbackTrail :: ConditionalTrail s v -> Checkpoint -> ST s ()
rollbackTrail (ConditionalTrail ref) cp = do
st <- readSTRef ref
let targetLen = cpTrailMark cp
toUndoCount = tsLogLen st - targetLen
if toUndoCount <= 0
then writeSTRef ref st { tsEpoch = cpEpoch cp }
else do
let (toRevert, remainingLog) = splitAt toUndoCount (tsLogRev st)
restoredSlots = foldl revertSlot (tsSlots st) toRevert
writeSTRef ref st
{ tsSlots = restoredSlots
, tsEpoch = cpEpoch cp
, tsLogRev = remainingLog
, tsLogLen = targetLen
}
where
revertSlot slots (TrailEntry k Nothing) = IntMap.delete k slots
revertSlot slots (TrailEntry k (Just prevSlot)) = IntMap.insert k prevSlot slots
-- | Returns the total number of entries currently in the undo log.
trailLogLength :: ConditionalTrail s v -> ST s Int
trailLogLength (ConditionalTrail ref) = tsLogLen <$> readSTRef ref