{-# LANGUAGE OverloadedStrings #-}

module Prolog.Programming.CodeAnalysis.Rules.InconsistentArities (inconsistentAritiesChecker) where

import Control.Monad (foldM)
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.List.NonEmpty as NE (groupWith, head, map)
import Data.Map (Map)
import qualified Data.Map as Map (empty, insert, lookup)
import Data.Maybe (mapMaybe)
import qualified Data.Set as Set (filter, insert, unions)
import Data.Text.Lazy (pack)
import Prolog.Programming.CodeAnalysis.Helper (clauseIdentities, internalErrorProblem)
import Prolog.Programming.CodeAnalysis.Types (
  Context,
  IgnoredPredicates (..),
  Problem (..),
  ProgramRule,
 )
import Prolog.Programming.Helper (predicateArity, predicateName)
import Text.PrettyPrint.Leijen.Text (string)

inconsistentAritiesChecker :: Context -> IgnoredPredicates -> ProgramRule
inconsistentAritiesChecker :: Context -> IgnoredPredicates -> ProgramRule
inconsistentAritiesChecker Context
otherDefinitions (IgnoredPredicates [[Char]]
ignore) Context
clauses =
  case [([Char], NonEmpty Int)] -> Maybe (Map [Char] Int)
forcedIdentities (Context -> [([Char], NonEmpty Int)]
identities Context
otherDefinitions) of
    Maybe (Map [Char] Int)
Nothing ->
      [ [Char] -> Problem
internalErrorProblem [Char]
"Task instance does not satisfy arity constraint."
      ]
    Just Map [Char] Int
forced ->
      (([Char], NonEmpty Int) -> Maybe Problem)
-> [([Char], NonEmpty Int)] -> [Problem]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
        ((Result -> Problem) -> Maybe Result -> Maybe Problem
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Result -> Problem
toProblem (Maybe Result -> Maybe Problem)
-> (([Char], NonEmpty Int) -> Maybe Result)
-> ([Char], NonEmpty Int)
-> Maybe Problem
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map [Char] Int -> ([Char], NonEmpty Int) -> Maybe Result
compareWithForced Map [Char] Int
forced)
        ([([Char], NonEmpty Int)] -> [Problem])
-> [([Char], NonEmpty Int)] -> [Problem]
forall a b. (a -> b) -> a -> b
$ Context -> [([Char], NonEmpty Int)]
identities Context
clauses
  where
    identities :: Context -> [([Char], NonEmpty Int)]
identities =
      (NonEmpty Signature -> ([Char], NonEmpty Int))
-> [NonEmpty Signature] -> [([Char], NonEmpty Int)]
forall a b. (a -> b) -> [a] -> [b]
map
        (\NonEmpty Signature
group -> (Signature -> [Char]
predicateName (NonEmpty Signature -> Signature
forall a. NonEmpty a -> a
NE.head NonEmpty Signature
group), (Signature -> Int) -> NonEmpty Signature -> NonEmpty Int
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map Signature -> Int
predicateArity NonEmpty Signature
group))
        ([NonEmpty Signature] -> [([Char], NonEmpty Int)])
-> (Context -> [NonEmpty Signature])
-> Context
-> [([Char], NonEmpty Int)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Signature -> [Char]) -> Set Signature -> [NonEmpty Signature]
forall (f :: * -> *) b a.
(Foldable f, Eq b) =>
(a -> b) -> f a -> [NonEmpty a]
NE.groupWith Signature -> [Char]
predicateName
        (Set Signature -> [NonEmpty Signature])
-> (Context -> Set Signature) -> Context -> [NonEmpty Signature]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Set Signature] -> Set Signature
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions
        ([Set Signature] -> Set Signature)
-> (Context -> [Set Signature]) -> Context -> Set Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ProgramClause -> Set Signature) -> Context -> [Set Signature]
forall a b. (a -> b) -> [a] -> [b]
map ((Signature -> Bool) -> Set Signature -> Set Signature
forall a. (a -> Bool) -> Set a -> Set a
Set.filter (([Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [[Char]]
ignore) ([Char] -> Bool) -> (Signature -> [Char]) -> Signature -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> [Char]
predicateName) (Set Signature -> Set Signature)
-> (ProgramClause -> Set Signature)
-> ProgramClause
-> Set Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Signature -> Set Signature -> Set Signature)
-> (Signature, Set Signature) -> Set Signature
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Signature -> Set Signature -> Set Signature
forall a. Ord a => a -> Set a -> Set a
Set.insert ((Signature, Set Signature) -> Set Signature)
-> (ProgramClause -> (Signature, Set Signature))
-> ProgramClause
-> Set Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> ProgramClause -> (Signature, Set Signature)
clauseIdentities Bool
True)

data Result = MultipleArities String | InconsistentWithForced String Int

compareWithForced :: Map String Int -> (String, NonEmpty Int) -> Maybe Result
compareWithForced :: Map [Char] Int -> ([Char], NonEmpty Int) -> Maybe Result
compareWithForced Map [Char] Int
forced ([Char]
name, NonEmpty Int
arities) =
  case NonEmpty Int
arities of
    Int
arity :| [] ->
      case [Char] -> Map [Char] Int -> Maybe Int
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup [Char]
name Map [Char] Int
forced of
        Just Int
expected
          | Int
expected Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
arity ->
              Result -> Maybe Result
forall a. a -> Maybe a
Just (Result -> Maybe Result) -> Result -> Maybe Result
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> Result
InconsistentWithForced [Char]
name Int
expected
        Maybe Int
_ -> Maybe Result
forall a. Maybe a
Nothing
    NonEmpty Int
_ -> Result -> Maybe Result
forall a. a -> Maybe a
Just (Result -> Maybe Result) -> Result -> Maybe Result
forall a b. (a -> b) -> a -> b
$ [Char] -> Result
MultipleArities [Char]
name

forcedIdentities :: [(String, NonEmpty Int)] -> Maybe (Map String Int)
forcedIdentities :: [([Char], NonEmpty Int)] -> Maybe (Map [Char] Int)
forcedIdentities = (Map [Char] Int
 -> ([Char], NonEmpty Int) -> Maybe (Map [Char] Int))
-> Map [Char] Int
-> [([Char], NonEmpty Int)]
-> Maybe (Map [Char] Int)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ((([Char], NonEmpty Int)
 -> Map [Char] Int -> Maybe (Map [Char] Int))
-> Map [Char] Int
-> ([Char], NonEmpty Int)
-> Maybe (Map [Char] Int)
forall a b c. (a -> b -> c) -> b -> a -> c
flip ([Char], NonEmpty Int) -> Map [Char] Int -> Maybe (Map [Char] Int)
forall {k} {a}.
Ord k =>
(k, NonEmpty a) -> Map k a -> Maybe (Map k a)
addIdentity) Map [Char] Int
forall k a. Map k a
Map.empty
  where
    addIdentity :: (k, NonEmpty a) -> Map k a -> Maybe (Map k a)
addIdentity (k
name, NonEmpty a
arities) =
      case NonEmpty a
arities of
        a
arity :| [] -> Map k a -> Maybe (Map k a)
forall a. a -> Maybe a
Just (Map k a -> Maybe (Map k a))
-> (Map k a -> Map k a) -> Map k a -> Maybe (Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
name a
arity
        NonEmpty a
_ -> Maybe (Map k a) -> Map k a -> Maybe (Map k a)
forall a b. a -> b -> a
const Maybe (Map k a)
forall a. Maybe a
Nothing

toProblem :: Result -> Problem
toProblem :: Result -> Problem
toProblem Result
res =
  Problem {
    problemDisplay :: Doc
problemDisplay =
      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
$
          case Result
res of
            MultipleArities [Char]
name ->
              [Char]
"Your program contains the predicate " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
name [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" which is used/defined with different arities."
            InconsistentWithForced [Char]
name Int
correctArity ->
              [Char]
"Your program contains the predicate "
                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
name
                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
" which is used/defined with wrong arity. It should be "
                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
name
                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"/"
                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
correctArity
                [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"."
    }