module Debug.Hoed
(
observe
, runO
, printO
, testO
, runOwith
, HoedOptions(..)
, defaultHoedOptions
, runOwp
, printOwp
, testOwp
, Propositions(..)
, PropType(..)
, Proposition(..)
, mkProposition
, ofType
, withSignature
, sizeHint
, withTestGen
, TestGen(..)
, PropositionType(..)
, Module(..)
, Signature(..)
, ParEq(..)
, (===)
, runOstore
, conAp
, HoedAnalysis(..)
, runO'
, judge
, unjudgedCharacterCount
, CompTree
, Vertex(..)
, CompStmt(..)
, Judge(..)
, Verbosity(..)
, logO
, logOwp
, traceOnly
, UnevalHandler(..)
, Observer(..)
, Observable(..)
, (<<)
, thunk
, send
, observeOpaque
, observeBase
, constrainBase
, debugO
, CDS
, Generic
) where
import Control.DeepSeq
import Debug.Hoed.CompTree
import Debug.Hoed.Console
import Debug.Hoed.Observe
import Debug.Hoed.Prop
import Debug.Hoed.Render
import Debug.Hoed.Serialize
import Data.IORef
import Prelude hiding (Right)
import System.Clock
import System.Console.Terminal.Size
import System.Directory (createDirectoryIfMissing)
import System.IO
import System.IO.Unsafe
import GHC.Generics
import Data.Graph.Libgraph
runOnce :: IO ()
runOnce = do
f <- readIORef firstRun
if f
then writeIORef firstRun False
else error "It is best not to run Hoed more that once (maybe you want to restart GHCI?)"
firstRun :: IORef Bool
firstRun = unsafePerformIO $ newIORef True
debugO :: IO a -> IO Trace
debugO program =
do { runOnce
; initUniq
; startEventStream
; let errorMsg e = "[Escaping Exception in Code : " ++ show e ++ "]"
; ourCatchAllIO (do { _ <- program ; return () })
(hPutStrLn stderr . errorMsg)
; endEventStream
}
runO :: IO a -> IO ()
runO program = do
window <- size
let w = maybe (prettyWidth defaultHoedOptions) width window
runOwith defaultHoedOptions{prettyWidth=w, verbose=Verbose} program
runOwith :: HoedOptions -> IO a -> IO ()
runOwith options program = do
HoedAnalysis{..} <- runO' options program
debugSession hoedTrace hoedCompTree []
return ()
runOstore :: String -> IO a -> IO ()
runOstore tag program = do
HoedAnalysis{..} <- runO' defaultHoedOptions{verbose=Silent} program
storeTree (treeFilePath ++ tag) hoedCompTree
storeTrace (traceFilePath ++ tag) hoedTrace
testO :: Show a => (a->Bool) -> a -> IO ()
testO p x = runO $ putStrLn $ if p x then "Passed 1 test."
else " *** Failed! Falsifiable: " ++ show x
runOwp :: [Propositions] -> IO a -> IO ()
runOwp ps program = do
HoedAnalysis{..} <- runO' defaultHoedOptions{verbose=Verbose} program
let compTree' = hoedCompTree
debugSession hoedTrace compTree' ps
return ()
testOwp :: Show a => [Propositions] -> (a->Bool) -> a -> IO ()
testOwp ps p x = runOwp ps $ putStrLn $
if p x then "Passed 1 test."
else " *** Failed! Falsifiable: " ++ show x
printO :: (Show a) => a -> IO ()
printO expr = runO (print expr)
printOwp :: (Show a) => [Propositions] -> a -> IO ()
printOwp ps expr = runOwp ps (print expr)
traceOnly :: IO a -> IO ()
traceOnly program = do
_ <- debugO program
return ()
data Verbosity = Verbose | Silent
condPutStrLn :: Verbosity -> String -> IO ()
condPutStrLn Silent _ = return ()
condPutStrLn Verbose msg = hPutStrLn stderr msg
data HoedAnalysis = HoedAnalysis
{ hoedTrace :: [Event]
, hoedCompTree :: CompTree
}
data HoedOptions = HoedOptions
{ verbose :: Verbosity
, prettyWidth :: Int
}
defaultHoedOptions :: HoedOptions
defaultHoedOptions = HoedOptions Silent 110
runO' :: HoedOptions -> IO a -> IO HoedAnalysis
runO' HoedOptions{..} program = let ?statementWidth = prettyWidth in do
createDirectoryIfMissing True ".Hoed/"
t1 <- getTime Monotonic
condPutStrLn verbose "=== program output ===\n"
events <- debugO program
t2 <- getTime Monotonic
let programTime = toSecs(diffTimeSpec t1 t2)
condPutStrLn verbose $ "\n=== program terminated (" ++ show programTime ++ " seconds) ==="
condPutStrLn verbose"Please wait while the computation tree is constructed..."
let e = length events
condPutStrLn verbose "\n=== Statistics ===\n"
condPutStrLn verbose $ show e ++ " events"
let !cdss = eventsToCDS events
!eqs = force $ renderCompStmts cdss
ti = traceInfo e (reverse events)
!ds = force $ dependencies ti
ct = mkCompTree eqs ds
#if defined(DEBUG)
writeFile ".Hoed/Events" (unlines . map show . reverse $ events)
writeFile ".Hoed/Cdss" (unlines . map show $ cdss2)
writeFile ".Hoed/Eqs" (unlines . map show $ eqs)
#endif
#if defined(TRANSCRIPT)
writeFile ".Hoed/Transcript" (getTranscript events ti)
#endif
let n = length eqs
b = fromIntegral (length . arcs $ ct ) / fromIntegral ((length . vertices $ ct) (length . leafs $ ct))
condPutStrLn verbose $ show n ++ " computation statements"
condPutStrLn verbose $ show ((length . vertices $ ct) 1) ++ " nodes + 1 virtual root node in the computation tree"
condPutStrLn verbose $ show (length . arcs $ ct) ++ " edges in computation tree"
condPutStrLn verbose $ "computation tree has a branch factor of " ++ show b ++ " (i.e the average number of children of non-leaf nodes)"
t3 <- getTime Monotonic
let compTime = toSecs(diffTimeSpec t2 t3)
condPutStrLn verbose $ "\n=== Debug Session (" ++ show compTime ++ " seconds) ===\n"
return $ HoedAnalysis events ct
where
toSecs :: TimeSpec -> Double
toSecs spec = fromIntegral(sec spec) + fromIntegral(nsec spec) * 1e-9
logO :: FilePath -> IO a -> IO ()
logO filePath program = do
HoedAnalysis{..} <- runO' defaultHoedOptions{verbose=Verbose} program
writeFile filePath (showGraph hoedCompTree)
return ()
where showGraph g = showWith g showVertex showArc
showVertex RootVertex = ("\".\"","shape=none")
showVertex v = ("\"" ++ (escape . showCompStmt) v ++ "\"", "")
showArc _ = ""
showCompStmt = show . vertexStmt
logOwp :: UnevalHandler -> FilePath -> [Propositions] -> IO a -> IO ()
logOwp handler filePath properties program = do
HoedAnalysis{..} <- runO' defaultHoedOptions{verbose=Verbose} program
hPutStrLn stderr "\n=== Evaluating assigned properties ===\n"
compTree' <- judgeAll handler unjudgedCharacterCount hoedTrace properties hoedCompTree
writeFile filePath (showGraph compTree')
return ()
where showGraph g = showWith g showVertex showArc
showVertex RootVertex = ("root","")
showVertex v = ("\"" ++ (escape . showCompStmt) v ++ "\"", "")
showArc _ = ""
showCompStmt s = (show . vertexJmt) s ++ ": " ++ (show . vertexStmt) s
#if __GLASGOW_HASKELL__ >= 710
instance Observable a where
observer = observeOpaque "<?>"
constrain _ _ = error "constrained by untraced value"
#endif