module Prolog.Programming.CodeAnalysis.Other.RedundantCode ( checkForRedundantCode, ) where import Data.Text.Lazy (pack) import Language.Prolog (Clause (..), ProgramClause, prettifyClause, prettifyTerm) import Prolog.Programming.CodeAnalysis.Types ( Problem (..), ) import Prolog.Programming.Mutation (findFirstAliveMutation) import Prolog.Programming.Mutation.Mutators (dropClauseMutator, dropGoalMutator) import Prolog.Programming.Mutation.Types (Mutation (..)) import Text.PrettyPrint.Leijen.Text (indent, text, vsep) checkForRedundantCode :: ([ProgramClause] -> IO Bool) -> [ProgramClause] -> IO (Maybe Problem) checkForRedundantCode :: ([ProgramClause] -> IO Bool) -> [ProgramClause] -> IO (Maybe Problem) checkForRedundantCode [ProgramClause] -> IO Bool checker [ProgramClause] prog = do let toResult :: Maybe Mutation -> IO (Maybe Problem) toResult = Maybe Problem -> IO (Maybe Problem) forall a. a -> IO a forall (f :: * -> *) a. Applicative f => a -> f a pure (Maybe Problem -> IO (Maybe Problem)) -> (Maybe Mutation -> Maybe Problem) -> Maybe Mutation -> IO (Maybe Problem) forall b c a. (b -> c) -> (a -> b) -> a -> c . (Mutation -> Problem) -> Maybe Mutation -> Maybe Problem forall a b. (a -> b) -> Maybe a -> Maybe b forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmap Mutation -> Problem toProblem dropClausesResult <- ([ProgramClause] -> IO Bool) -> [Mutant] -> IO (Maybe Mutation) findFirstAliveMutation [ProgramClause] -> IO Bool checker (Mutator dropClauseMutator [ProgramClause] prog) case dropClausesResult of Just Mutation _ -> Maybe Mutation -> IO (Maybe Problem) toResult Maybe Mutation dropClausesResult Maybe Mutation Nothing -> do dropGoalsResult <- ([ProgramClause] -> IO Bool) -> [Mutant] -> IO (Maybe Mutation) findFirstAliveMutation [ProgramClause] -> IO Bool checker (Mutator dropGoalMutator [ProgramClause] prog) toResult dropGoalsResult toProblem :: Mutation -> Problem toProblem :: Mutation -> Problem toProblem = Doc -> Problem Problem (Doc -> Problem) -> (Mutation -> Doc) -> Mutation -> Problem forall b c a. (b -> c) -> (a -> b) -> a -> c . Mutation -> Doc displaySuggestion where displaySuggestion :: Mutation -> Doc displaySuggestion (DropClause (Atom n, [Term] a, [Term] g)) = [Doc] -> Doc vsep [ Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack Atom "The clause" , Int -> Doc -> Doc indent Int 2 (Doc -> Doc) -> Doc -> Doc forall a b. (a -> b) -> a -> b $ Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack (Atom -> Text) -> Atom -> Text forall a b. (a -> b) -> a -> b $ Clause -> Atom prettifyClause (Clause -> Atom) -> Clause -> Atom forall a b. (a -> b) -> a -> b $ Atom -> [Term] -> [Term] -> Clause Clause Atom n [Term] a [Term] g , Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack Atom "seems redundant and can be removed." ] displaySuggestion (DropGoal (Atom n, [Term] a, [Term] g) Term goal) = [Doc] -> Doc vsep [ Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack Atom "The goal" , Int -> Doc -> Doc indent Int 2 (Doc -> Doc) -> Doc -> Doc forall a b. (a -> b) -> a -> b $ Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack (Atom -> Text) -> Atom -> Text forall a b. (a -> b) -> a -> b $ Term -> Atom prettifyTerm Term goal , Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack Atom "in clause" , Int -> Doc -> Doc indent Int 2 (Doc -> Doc) -> Doc -> Doc forall a b. (a -> b) -> a -> b $ Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack (Atom -> Text) -> Atom -> Text forall a b. (a -> b) -> a -> b $ Clause -> Atom prettifyClause (Clause -> Atom) -> Clause -> Atom forall a b. (a -> b) -> a -> b $ Atom -> [Term] -> [Term] -> Clause Clause Atom n [Term] a [Term] g , Text -> Doc text (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ Atom -> Text pack Atom "seems redundant and can be removed." ]