Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Prolog/Programming/CodeAnalysis/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Prolog.Programming.CodeAnalysis.Types (
SymbolOverloadConfig (..),
RedundantCodeConfig (..),
CheckStage (..),
TimeoutDivisor (..),
)
where

Expand Down Expand Up @@ -94,7 +95,10 @@ newtype EarlyRecursionConfig = EarlyRecursionConfig (CodeAnalysisRuleConfig Addi
newtype SymbolOverloadConfig = SymbolOverloadConfig (CodeAnalysisRuleConfig ())
deriving (Data, Generic, Reader, Show, ToDoc)

newtype RedundantCodeConfig = RedundantCodeConfig (CodeAnalysisRuleConfig ())
newtype TimeoutDivisor = TimeoutDivisor {timeoutDivisor :: Int}
deriving (Data, Generic, Reader, Show, ToDoc)

newtype RedundantCodeConfig = RedundantCodeConfig (CodeAnalysisRuleConfig TimeoutDivisor)
deriving (Data, Generic, Reader, Show, ToDoc)

{- FOURMOLU_DISABLE -} -- Remove when https://github.com/fourmolu/fourmolu/issues/552 is fixed
Expand Down
3 changes: 3 additions & 0 deletions src/Prolog/Programming/ExampleConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ codeAnalysis:
redundantCode:
# what to do concerning redundant clauses or goals being present
status: hint
# divisor for timeouts during redundancy check (not used during rigorous validation)
# timeoutDivisor: 2
# This field is only allowed to appear when status is not set to 'ignore', and even otherwise it is optional (defaults to 1).

# which predicates are not allowed to be used
forbiddenPredicates:
Expand Down
9 changes: 9 additions & 0 deletions src/Prolog/Programming/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Prolog.Programming.CodeAnalysis.Types (
RedundantCodeConfig (..),
SingletonVariablesConfig (..),
SymbolOverloadConfig (..),
TimeoutDivisor (..),
UngroupedDefinitionsConfig (..),
WithSeverity (..),
)
Expand Down Expand Up @@ -188,6 +189,14 @@ instance FromJSON EarlyRecursionConfig where
instance FromJSON SymbolOverloadConfig where
parseJSON = withRuleParser SymbolOverloadConfig Nothing

instance FromJSON TimeoutDivisor where
parseJSON = withObject "TimeoutDivisor" $ \v -> do
divisor <- v .:? "timeoutDivisor" .!= 1

if divisor <= 0
then fail "timeoutDivisor must be greater than zero"
else pure $ TimeoutDivisor divisor

instance FromJSON RedundantCodeConfig where
parseJSON = withRuleParser RedundantCodeConfig (Just AfterTests)

Expand Down
18 changes: 14 additions & 4 deletions src/Prolog/Programming/Task.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import Prolog.Programming.CodeAnalysis.Types (
Severity (..),
SingletonVariablesConfig (SingletonVariablesConfig),
SymbolOverloadConfig (SymbolOverloadConfig),
TimeoutDivisor (..),
UngroupedDefinitionsConfig (UngroupedDefinitionsConfig),
WithSeverity (..),
)
Expand Down Expand Up @@ -150,7 +151,7 @@ verifyConfig cfg = case parseInstance cfg of
, earlyRecursion = escalate earlyRecursion
, inlining = escalate inlining
, symbolOverload = SymbolOverloadConfig $ withError ()
, redundantCode = RedundantCodeConfig $ Just (WithSeverity Error (), AfterTests)
, redundantCode = RedundantCodeConfig $ Just (WithSeverity Error (TimeoutDivisor 1), AfterTests)
}

toInstance :: Config -> TaskInstance
Expand Down Expand Up @@ -250,7 +251,16 @@ checkTask reject inform drawPicture TaskInstance {taskConfig = TaskConfig {..},
[] -> pure ()
pbs -> either reject undefined $ displayProblems pbs

testResult <- liftIO $ testRunner globalTimeout errorOnFirstTimeout factProg inProg specifications newDefs
testResult <-
liftIO $
testRunner
globalTimeout
(TimeoutDivisor 1)
errorOnFirstTimeout
factProg
inProg
specifications
newDefs
case testResult of
(Finished AllOk, (passed, _)) ->
inform $
Expand Down Expand Up @@ -306,10 +316,10 @@ checkTask reject inform drawPicture TaskInstance {taskConfig = TaskConfig {..},

case redundantCode codeAnalysis of
RedundantCodeConfig Nothing -> pure ()
RedundantCodeConfig (Just (WithSeverity severity (), AfterTests)) -> do
RedundantCodeConfig (Just (WithSeverity severity timeoutDivisor, AfterTests)) -> do
let checker p =
allTestResultsAreOkay . fst
<$> testRunner globalTimeout True factProg p specifications newDefs
<$> testRunner globalTimeout timeoutDivisor True factProg p specifications newDefs
redundantCodeProblem <- liftIO $ checkForRedundantCode checker inProg

case redundantCodeProblem of
Expand Down
8 changes: 5 additions & 3 deletions src/Prolog/Programming/TestRunner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Language.Prolog.GraphViz (Graph, resolveFirstTree, resolveTree)

import Data.List.Extra (notNull)
import Data.Tuple.Extra (uncurry3)
import Prolog.Programming.CodeAnalysis.Types (TimeoutDivisor (..))
import Prolog.Programming.Types (
Expection (..),
Requirement (..),
Expand All @@ -34,13 +35,14 @@ import System.Timeout (timeout)

testRunner
:: TimeoutDuration
-> TimeoutDivisor
-> Bool
-> [ProgramClause]
-> [ProgramClause]
-> [Spec]
-> [(Atom, Atom)]
-> IO (TestSuiteResult Spec (Maybe [Unifier]), (Int, Int))
testRunner globalTO stopOnTimeout factProg inputProg specs newDefs = do
testRunner globalTO (TimeoutDivisor divisor) stopOnTimeout factProg inputProg specs newDefs = do
let
newProg = useFoundDefsInProgram newDefs factProg
newSpecs = useFoundDefsInSpecs newDefs specs
Expand All @@ -52,9 +54,9 @@ testRunner globalTO stopOnTimeout factProg inputProg specs newDefs = do
checkWithTimeout p s@(Spec _ _ _ to _) = do
res <- case to of
GlobalTimeout -> do
timeout (millisecond2microSec globalTO) $ evaluate (checkProgram p s)
timeout (millisecond2microSec globalTO `div` divisor) $ evaluate (checkProgram p s)
LocalTimeout d -> do
timeout (millisecond2microSec d) $ evaluate (checkProgram p s)
timeout (millisecond2microSec d `div` divisor) $ evaluate (checkProgram p s)
pure $ fromMaybe Timeout res
millisecond2microSec = (* 1000)

Expand Down
3 changes: 2 additions & 1 deletion test/ConfigSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Prolog.Programming.CodeAnalysis.Types (
RedundantCodeConfig (..),
Severity (..),
SymbolOverloadConfig (..),
TimeoutDivisor (TimeoutDivisor),
WithSeverity (WithSeverity),
)
import Prolog.Programming.Data (Config)
Expand Down Expand Up @@ -91,7 +92,7 @@ codeAnalysis:
Left _ -> False
Right () -> case codeAnalysis $ taskConfig $ toInstance config of
CodeAnalysisConfig {
redundantCode = RedundantCodeConfig (Just (WithSeverity Warn (), AfterTests))
redundantCode = RedundantCodeConfig (Just (WithSeverity Warn (TimeoutDivisor 1), AfterTests))
, symbolOverload = SymbolOverloadConfig (Just (WithSeverity Error (), BeforeTests))
, inlining = InliningConfig (Just (WithSeverity Hint (), AfterTests))
} -> True
Expand Down
Loading