{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE OverloadedStrings #-}
module Cryptol.Eval.Monad
(
Eval(..)
, runEval
, EvalOpts(..)
, getEvalOpts
, PPOpts(..)
, PPFloatFormat(..)
, PPFloatExp(..)
, defaultPPOpts
, io
, delayFill
, ready
, blackhole
, evalSpark
, Unsupported(..)
, EvalError(..)
, evalPanic
, wordTooWide
, typeCannotBeDemoted
) where
import Control.Concurrent.Async
import Control.DeepSeq
import Control.Monad
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Data.IORef
import Data.Typeable (Typeable)
import qualified Control.Exception as X
import Cryptol.Utils.Panic
import Cryptol.Utils.PP
import Cryptol.Utils.Logger(Logger)
import Cryptol.TypeCheck.AST(Type,Name)
ready :: a -> Eval a
ready a = Ready a
data PPOpts = PPOpts
{ useAscii :: Bool
, useBase :: Int
, useInfLength :: Int
, useFPBase :: Int
, useFPFormat :: PPFloatFormat
}
data PPFloatFormat =
FloatFixed Int PPFloatExp
| FloatFrac Int
| FloatFree PPFloatExp
data PPFloatExp = ForceExponent
| AutoExponent
defaultPPOpts :: PPOpts
defaultPPOpts = PPOpts { useAscii = False, useBase = 10, useInfLength = 5
, useFPBase = 16
, useFPFormat = FloatFree AutoExponent
}
data EvalOpts = EvalOpts
{ evalLogger :: Logger
, evalPPOpts :: PPOpts
}
data Eval a
= Ready !a
| Thunk !(EvalOpts -> IO a)
data ThunkState a
= Unforced
| BlackHole
| Forced !(Either EvalError a)
getEvalOpts :: Eval EvalOpts
getEvalOpts = Thunk pure
{-# INLINE delayFill #-}
delayFill ::
Eval a ->
Eval a ->
Eval (Eval a)
delayFill (Ready x) _ = Ready (Ready x)
delayFill (Thunk x) retry = Thunk $ \opts -> do
r <- newIORef Unforced
return $ unDelay retry r (x opts)
evalSpark ::
Eval a ->
Eval (Eval a)
evalSpark (Ready x) = Ready (Ready x)
evalSpark (Thunk x) = Thunk $ \opts ->
do a <- async (x opts)
return (Thunk $ \_ -> wait a)
blackhole ::
String ->
Eval (Eval a, Eval a -> Eval ())
blackhole msg = do
r <- io $ newIORef (fail msg)
let get = join (io $ readIORef r)
let set = io . writeIORef r
return (get, set)
unDelay :: Eval a -> IORef (ThunkState a) -> IO a -> Eval a
unDelay retry r x = do
rval <- io $ readIORef r
case rval of
Forced val -> io (toVal val)
BlackHole ->
retry
Unforced -> io $ do
writeIORef r BlackHole
val <- X.try x
writeIORef r (Forced val)
toVal val
where
toVal mbV = case mbV of
Right a -> pure a
Left e -> X.throwIO e
runEval :: EvalOpts -> Eval a -> IO a
runEval _ (Ready a) = return a
runEval opts (Thunk x) = x opts
{-# INLINE evalBind #-}
evalBind :: Eval a -> (a -> Eval b) -> Eval b
evalBind (Ready a) f = f a
evalBind (Thunk x) f = Thunk $ \opts -> x opts >>= runEval opts . f
instance Functor Eval where
fmap f (Ready x) = Ready (f x)
fmap f (Thunk m) = Thunk $ \opts -> f <$> m opts
{-# INLINE fmap #-}
instance Applicative Eval where
pure = return
(<*>) = ap
{-# INLINE pure #-}
{-# INLINE (<*>) #-}
instance Monad Eval where
return = Ready
(>>=) = evalBind
{-# INLINE return #-}
{-# INLINE (>>=) #-}
instance Fail.MonadFail Eval where
fail x = Thunk (\_ -> fail x)
instance MonadIO Eval where
liftIO = io
instance NFData a => NFData (Eval a) where
rnf (Ready a) = rnf a
rnf (Thunk _) = ()
instance MonadFix Eval where
mfix f = Thunk $ \opts -> mfix (\x -> runEval opts (f x))
io :: IO a -> Eval a
io m = Thunk (\_ -> m)
{-# INLINE io #-}
evalPanic :: HasCallStack => String -> [String] -> a
evalPanic cxt = panic ("[Eval] " ++ cxt)
data EvalError
= InvalidIndex (Maybe Integer)
| TypeCannotBeDemoted Type
| DivideByZero
| NegativeExponent
| LogNegative
| WordTooWide Integer
| UserError String
| LoopError String
| NoPrim Name
| BadRoundingMode Integer
| BadValue String
deriving (Typeable,Show)
instance PP EvalError where
ppPrec _ e = case e of
InvalidIndex (Just i) -> text "invalid sequence index:" <+> integer i
InvalidIndex Nothing -> text "invalid sequence index"
TypeCannotBeDemoted t -> text "type cannot be demoted:" <+> pp t
DivideByZero -> text "division by 0"
NegativeExponent -> text "negative exponent"
LogNegative -> text "logarithm of negative"
WordTooWide w ->
text "word too wide for memory:" <+> integer w <+> text "bits"
UserError x -> text "Run-time error:" <+> text x
LoopError x -> text "<<loop>>" <+> text x
BadRoundingMode r -> "invalid rounding mode" <+> integer r
BadValue x -> "invalid input for" <+> backticks (text x)
NoPrim x -> text "unimplemented primitive:" <+> pp x
instance X.Exception EvalError
data Unsupported
= UnsupportedSymbolicOp String
deriving (Typeable,Show)
instance PP Unsupported where
ppPrec _ e = case e of
UnsupportedSymbolicOp nm -> text "operation can not be supported on symbolic values:" <+> text nm
instance X.Exception Unsupported
typeCannotBeDemoted :: Type -> a
typeCannotBeDemoted t = X.throw (TypeCannotBeDemoted t)
wordTooWide :: Integer -> a
wordTooWide w = X.throw (WordTooWide w)