on master help sign in
Put Log.hs in order
commitb2f67d3482f23c0072e526fe1ea70d8712f7e06d authorHarendra Kumar <harendra.kumar@gmail.com> dateMon 26 Jun 2017, 18:03 (9 years ago) parent6afc667
1 file changed+33−24
src/Strands/Log.hs+33−24
src/Strands/Log.hs+33−24
@@ -3,21 +3,28 @@
33{-# LANGUAGE ScopedTypeVariables #-}
44
55module Strands.Log
6− ( Log(..)
7− , LogElem(..)
6+ ( Loggable
7+ , Replaying
8+ , LogEntry (..)
9+ , Log (..)
10+ , IDynamic (..)
11+ , fromIDyn
12+ , toIDyn
813 )
914where
1015
11−import Control.Exception (SomeException, catch)
12−import Data.Dynamic (Typeable, typeOf)
13−import System.IO.Unsafe (unsafePerformIO)
16+import Unsafe.Coerce (unsafeCoerce)
1417
1518------------------------------------------------------------------------------
1619-- Log types
1720------------------------------------------------------------------------------
1821
1922-- | Constraint type synonym for a value that can be logged.
20−type Loggable a = (Show a, Read a, Typeable a)
23+type Loggable a = (Show a, Read a)
24+
25+------------------------------------------------------------------------------
26+-- Serialization
27+------------------------------------------------------------------------------
2128
2229-- | Dynamic serializable data for logging.
2330data IDynamic =
@@ -28,27 +35,29 @@ instance Show IDynamic where
2835 show (IDynamic x) = show (show x)
2936 show (IDyns s) = show s
3037
31−readWithErr :: (Typeable a, Read a) => String -> IO [(a, String)]
32−readWithErr line =
33− (v `seq` return [(v, left)])
34− `catch` (\(_ :: SomeException) ->
35− error $ "read error trying to read type: \"" ++ show (typeOf v)
36− ++ "\" in: " ++ " <" ++ show line ++ "> ")
37− where [(v, left)] = readsPrec 0 line
38+instance Read IDynamic where
39+ readsPrec _ str = map (\(x,s) -> (IDyns x,s)) $ readsPrec 0 str
3840
39−readsPrec' :: (Typeable a, Read a) => t -> String -> [(a, String)]
40−readsPrec' _ = unsafePerformIO . readWithErr
41+fromIDyn :: Loggable a => IDynamic -> a
42+fromIDyn (IDynamic x) = r
43+ where r = unsafeCoerce x
4144
42−instance Read IDynamic where
43− readsPrec n str = map (\(x,s) -> (IDyns x,s)) $ readsPrec' n str
45+fromIDyn (IDyns s) = r `seq` r
46+ where r = read s
4447
45−data LogElem = Wait | Exec | Var IDynamic
46− deriving (Read, Show)
48+toIDyn :: Loggable a => a -> IDynamic
49+toIDyn x = IDynamic x
4750
48−type Recover = Bool
49−type CurrentPointer = [LogElem]
50−type LogEntries = [LogElem]
51+------------------------------------------------------------------------------
52+-- Log
53+------------------------------------------------------------------------------
54+
55+data LogEntry =
56+ Executing -- we are inside this computation, not yet done
57+ | Waiting -- the computation is done and it returned mzero
58+ | Result IDynamic -- computation is done and we have the result to replay
59+ deriving (Read, Show)
5160
52−data Log = Log Recover CurrentPointer LogEntries
53− deriving (Typeable, Show)
61+type Replaying = Bool
5462
63+data Log = Log Replaying [LogEntry] deriving (Show)