{-# LANGUAGE OverloadedStrings #-} module Prolog.Programming.CodeAnalysis.Rules.ForbiddenPredicates (forbiddenPredicatesChecker) where import Data.List (intercalate) import Data.Set (Set) import qualified Data.Set as Set (intersection, toList) import Data.Text.Lazy (pack) import Language.Prolog (Clause (..), ProgramClause, Signature, prettifyClause) import Prolog.Programming.CodeAnalysis.Helper (clauseIdentities) import Prolog.Programming.CodeAnalysis.Types (ClauseRule, Problem (..)) import Text.PrettyPrint.Leijen.Text (empty, indent, string, vsep) forbiddenPredicatesChecker :: Set Signature -> ClauseRule forbiddenPredicatesChecker :: Set Signature -> ClauseRule forbiddenPredicatesChecker Set Signature forbidden c :: ProgramClause c@([Char] name, [Term] args, [Term] goals) | [Signature] -> Bool forall a. [a] -> Bool forall (t :: * -> *) a. Foldable t => t a -> Bool null [Signature] usedForbidden = [] | Bool otherwise = [Maybe [Char] -> [Signature] -> Clause -> Problem toProblem Maybe [Char] forall a. Maybe a Nothing [Signature] usedForbidden Clause clause] where clause :: Clause clause = [Char] -> [Term] -> [Term] -> Clause Clause [Char] name [Term] args [Term] goals usedForbidden :: [Signature] usedForbidden = Set Signature -> [Signature] forall a. Set a -> [a] Set.toList (Set Signature -> [Signature]) -> Set Signature -> [Signature] forall a b. (a -> b) -> a -> b $ Set Signature -> ProgramClause -> Set Signature forbiddenInClause Set Signature forbidden ProgramClause c forbiddenInClause :: Set Signature -> ProgramClause -> Set Signature forbiddenInClause :: Set Signature -> ProgramClause -> Set Signature forbiddenInClause Set Signature forbidden ProgramClause clause = Set Signature forbidden Set Signature -> Set Signature -> Set Signature forall a. Ord a => Set a -> Set a -> Set a `Set.intersection` (Signature, Set Signature) -> Set Signature forall a b. (a, b) -> b snd (Bool -> ProgramClause -> (Signature, Set Signature) clauseIdentities Bool True ProgramClause clause) toProblem :: Maybe String -> [Signature] -> Clause -> Problem toProblem :: Maybe [Char] -> [Signature] -> Clause -> Problem toProblem Maybe [Char] msg [Signature] predicates Clause clause = Problem { problemDisplay :: Doc problemDisplay = [Doc] -> Doc vsep [ Text -> Doc string Text "Your clause" , Int -> Doc -> Doc indent Int 2 (Doc -> Doc) -> Doc -> Doc forall a b. (a -> b) -> a -> b $ Text -> Doc string (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ [Char] -> Text pack ([Char] -> Text) -> [Char] -> Text forall a b. (a -> b) -> a -> b $ Clause -> [Char] prettifyClause Clause clause , Text -> Doc string (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ [Char] -> Text pack ([Char] -> Text) -> [Char] -> Text forall a b. (a -> b) -> a -> b $ [Char] "makes use of forbidden predicates: " [Char] -> [Char] -> [Char] forall a. [a] -> [a] -> [a] ++ [Char] -> [[Char]] -> [Char] forall a. [a] -> [[a]] -> [a] intercalate [Char] ", " ((Signature -> [Char]) -> [Signature] -> [[Char]] forall a b. (a -> b) -> [a] -> [b] map Signature -> [Char] forall a. Show a => a -> [Char] show [Signature] predicates) , Doc -> ([Char] -> Doc) -> Maybe [Char] -> Doc forall b a. b -> (a -> b) -> Maybe a -> b maybe Doc empty (Text -> Doc string (Text -> Doc) -> ([Char] -> Text) -> [Char] -> Doc forall b c a. (b -> c) -> (a -> b) -> a -> c . [Char] -> Text pack) Maybe [Char] msg ] }