{-# LANGUAGE OverloadedStrings #-}

module Prolog.Programming.CodeAnalysis.Rules.EarlyRecursion (earlyRecursionChecker) where

import qualified Data.Set as Set (member)
import Data.Text.Lazy (pack)
import Language.Prolog (Clause (..), Signature (Signature), Term (..), prettifyClause)
import Prolog.Programming.CodeAnalysis.Helper (grabPredicateIdentities)
import Prolog.Programming.CodeAnalysis.Types (AdditionalMessage (AdditionalMessage), ClauseRule, Problem (..))
import Text.PrettyPrint.Leijen.Text (empty, indent, string, vsep)

earlyRecursionChecker :: AdditionalMessage -> ClauseRule
earlyRecursionChecker :: AdditionalMessage -> ClauseRule
earlyRecursionChecker (AdditionalMessage Maybe String
msg) (String
name, [Term]
args, [Term]
goals) =
  [Maybe String -> Clause -> Problem
toProblem Maybe String
msg (String -> [Term] -> [Term] -> Clause
Clause String
name [Term]
args [Term]
goals) | Signature -> [Term] -> Bool
hasViolation Signature
definedPredicate [Term]
goals]
  where
    definedPredicate :: Signature
definedPredicate = String -> Int -> Signature
Signature String
name (Int -> Signature) -> Int -> Signature
forall a b. (a -> b) -> a -> b
$ [Term] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Term]
args

hasViolation :: Signature -> [Term] -> Bool
hasViolation :: Signature -> [Term] -> Bool
hasViolation Signature
p = Bool -> [Term] -> Bool
go Bool
False
  where
    containsPredicate :: Term -> Bool
containsPredicate = Signature -> Set Signature -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member Signature
p (Set Signature -> Bool) -> (Term -> Set Signature) -> Term -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Term -> Set Signature
grabPredicateIdentities Bool
False
    isPredicate :: String -> t a -> Bool
isPredicate String
name t a
args = Signature
p Signature -> Signature -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Int -> Signature
Signature String
name (t a -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
args)

    go :: Bool -> [Term] -> Bool
go Bool
_ [] = Bool
False
    go Bool
seen [Struct String
m [Term
arg]]
      | String
m String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String
"not", String
"\\+"] = Bool -> [Term] -> Bool
go Bool
seen [Term
arg]
    go Bool
seen [Struct String
"," [Term
l, Term
r]] = Bool -> [Term] -> Bool
go (Bool
seen Bool -> Bool -> Bool
|| Term -> Bool
containsPredicate Term
l) [Term
r]
    go Bool
seen [Struct String
";" [Term
l, Term
r]] = Bool -> [Term] -> Bool
go Bool
seen [Term
l] Bool -> Bool -> Bool
|| Bool -> [Term] -> Bool
go Bool
seen [Term
r]
    go Bool
seen [Struct String
name [Term]
args] = Bool
seen Bool -> Bool -> Bool
&& Bool -> Bool
not (String -> [Term] -> Bool
forall {t :: * -> *} {a}. Foldable t => String -> t a -> Bool
isPredicate String
name [Term]
args)
    go Bool
seen (Term
t : [Term]
ts) = Bool -> [Term] -> Bool
go (Bool
seen Bool -> Bool -> Bool
|| Term -> Bool
containsPredicate Term
t) [Term]
ts

toProblem :: Maybe String -> Clause -> Problem
toProblem :: Maybe String -> Clause -> Problem
toProblem Maybe String
msg Clause
clause =
  Problem {
    problemDisplay :: Doc
problemDisplay =
      [Doc] -> Doc
vsep
        [ Text -> Doc
string Text
"The last goal in (an execution trace for) 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
$ String -> Text
pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Clause -> String
prettifyClause Clause
clause
        , Text -> Doc
string Text
"doesn't seem to be a recursive call despite one being used in it."
        , Doc -> (String -> Doc) -> Maybe String -> Doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc
empty (Text -> Doc
string (Text -> Doc) -> (String -> Text) -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack) Maybe String
msg
        ]
    }