module Prolog.Programming.CodeAnalysis.Rules.UngroupedDefinitions (ungroupedDefinitionsChecker) where import Data.List.Extra (anySame) import qualified Data.List.NonEmpty as NE (groupWith, head, toList) import Data.List.Ordered (isSorted) import Data.Text.Lazy (pack) import Prolog.Programming.CodeAnalysis.Helper (clauseIdentities) import Prolog.Programming.CodeAnalysis.Types (Problem (..), ProgramRule) import Prolog.Programming.Helper (predicateArity, predicateName) import Text.PrettyPrint.Leijen.Text (string) ungroupedDefinitionsChecker :: ProgramRule ungroupedDefinitionsChecker :: ProgramRule ungroupedDefinitionsChecker [ProgramClause] clauses = [String -> Problem toProblem String "Your code does not group predicate definitions by predicate name." | Bool duplicateExists] [Problem] -> [Problem] -> [Problem] forall a. [a] -> [a] -> [a] ++ [ String -> Problem toProblem String "Your code does not sort the predicate definitions ascending by arity (per group)." | Bool -> Bool not ((NonEmpty Signature -> Bool) -> [NonEmpty Signature] -> Bool forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool all ([Arity] -> Bool forall a. Ord a => [a] -> Bool isSorted ([Arity] -> Bool) -> (NonEmpty Signature -> [Arity]) -> NonEmpty Signature -> Bool forall b c a. (b -> c) -> (a -> b) -> a -> c . (Signature -> Arity) -> [Signature] -> [Arity] forall a b. (a -> b) -> [a] -> [b] map Signature -> Arity predicateArity ([Signature] -> [Arity]) -> (NonEmpty Signature -> [Signature]) -> NonEmpty Signature -> [Arity] forall b c a. (b -> c) -> (a -> b) -> a -> c . NonEmpty Signature -> [Signature] forall a. NonEmpty a -> [a] NE.toList) [NonEmpty Signature] groupedByName) ] where groupedByName :: [NonEmpty Signature] groupedByName = (Signature -> String) -> [Signature] -> [NonEmpty Signature] forall (f :: * -> *) b a. (Foldable f, Eq b) => (a -> b) -> f a -> [NonEmpty a] NE.groupWith Signature -> String predicateName ([Signature] -> [NonEmpty Signature]) -> [Signature] -> [NonEmpty Signature] forall a b. (a -> b) -> a -> b $ (ProgramClause -> Signature) -> [ProgramClause] -> [Signature] forall a b. (a -> b) -> [a] -> [b] map ((Signature, Set Signature) -> Signature forall a b. (a, b) -> a fst ((Signature, Set Signature) -> Signature) -> (ProgramClause -> (Signature, Set Signature)) -> ProgramClause -> Signature forall b c a. (b -> c) -> (a -> b) -> a -> c . Bool -> ProgramClause -> (Signature, Set Signature) clauseIdentities Bool forall a. HasCallStack => a undefined) [ProgramClause] clauses duplicateExists :: Bool duplicateExists = [String] -> Bool forall a. Eq a => [a] -> Bool anySame ([String] -> Bool) -> [String] -> Bool forall a b. (a -> b) -> a -> b $ (NonEmpty Signature -> String) -> [NonEmpty Signature] -> [String] forall a b. (a -> b) -> [a] -> [b] map (Signature -> String predicateName (Signature -> String) -> (NonEmpty Signature -> Signature) -> NonEmpty Signature -> String forall b c a. (b -> c) -> (a -> b) -> a -> c . NonEmpty Signature -> Signature forall a. NonEmpty a -> a NE.head) [NonEmpty Signature] groupedByName toProblem :: String -> Problem toProblem :: String -> Problem toProblem String msg = Problem { problemDisplay :: Doc problemDisplay = Text -> Doc string (Text -> Doc) -> Text -> Doc forall a b. (a -> b) -> a -> b $ String -> Text pack String msg }