1{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-} 2 3module DeepCopy where 4 5import qualified Data.Vector as V 6import Control.Monad.State.Strict 7import Control.Applicative 8import Prelude 9 10import Types 11 12class DeepCopy a where 13 deepCopy :: a -> M a 14 15instance DeepCopy S where 16 deepCopy s = S 17 <$> deepCopy (world s) 18 <*> deepCopy (flipSide s) 19 <*> pure (player s) 20 <*> deepCopy (bottomBuffer s) 21 <*> pure (topBuffer s) 22 <*> pure (peruser s) 23 <*> pure (randSource s) 24 <*> pure (helpShown s) 25 <*> pure (messages s) 26 <*> pure (spells s) 27 <*> pure (poisons s) 28 <*> pure (windows s) 29 30instance DeepCopy (World, World) where 31 deepCopy (w1, w2) = (,) 32 <$> deepCopy w1 33 <*> deepCopy w2 34 35instance DeepCopy World where 36 deepCopy w = lift $ V.unsafeThaw =<< V.mapM copyline =<< V.freeze w 37 where 38 copyline v = V.freeze v >>= V.unsafeThaw 39