{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Prolog.Programming.CodeAnalysis.Helper (
  clauseIdentities,
  countVariables,
  containsCut,
  grabPredicateIdentities,
  internalErrorProblem,
) where

import Data.Data (Data)
import Data.Generics (everything, mkQ)
import Data.Map (Map)
import qualified Data.Map as Map (empty, singleton, unionWith)
import Data.Set (Set)
import qualified Data.Set as Set (empty, insert, singleton, unions)
import Data.Text.Lazy (pack)
import Language.Prolog (ProgramClause, Signature (Signature), Term (..), VariableName (..))
import Prolog.Programming.CodeAnalysis.Types (Problem (..))
import Text.PrettyPrint.Leijen.Text (string, vcat)

-- | Return predicate information (name and arity) for left- and right-hand side of a program clause
clauseIdentities :: Bool -> ProgramClause -> (Signature, Set Signature)
clauseIdentities :: Bool -> ProgramClause -> (Signature, Set Signature)
clauseIdentities Bool
includeWrapperPredicates (Atom
name, [Term]
args, [Term]
rs) =
  (Atom -> Int -> Signature
Signature Atom
name ([Term] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Term]
args), [Set Signature] -> Set Signature
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions ([Set Signature] -> Set Signature)
-> [Set Signature] -> Set Signature
forall a b. (a -> b) -> a -> b
$ (Term -> Set Signature) -> [Term] -> [Set Signature]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Term -> Set Signature
grabPredicateIdentities Bool
includeWrapperPredicates) [Term]
rs)

grabPredicateIdentities :: Bool -> Term -> Set Signature
grabPredicateIdentities :: Bool -> Term -> Set Signature
grabPredicateIdentities Bool
includeWrapperPredicates (Struct Atom
name [Term]
args)
  | Atom
name Atom -> [Atom] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Atom
",", Atom
";", Atom
"\\+", Atom
"not"] =
      (if Bool
includeWrapperPredicates then Signature -> Set Signature -> Set Signature
forall a. Ord a => a -> Set a -> Set a
Set.insert Signature
sig else Set Signature -> Set Signature
forall a. a -> a
id)
        (Set Signature -> Set Signature) -> Set Signature -> Set Signature
forall a b. (a -> b) -> a -> b
$ [Set Signature] -> Set Signature
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions
        ([Set Signature] -> Set Signature)
-> [Set Signature] -> Set Signature
forall a b. (a -> b) -> a -> b
$ (Term -> Set Signature) -> [Term] -> [Set Signature]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> Term -> Set Signature
grabPredicateIdentities Bool
includeWrapperPredicates) [Term]
args
  | Bool
otherwise = Signature -> Set Signature
forall a. a -> Set a
Set.singleton Signature
sig
  where
    sig :: Signature
sig = Atom -> Int -> Signature
Signature Atom
name ([Term] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Term]
args)
grabPredicateIdentities Bool
_ Term
_ = Set Signature
forall a. Set a
Set.empty

-- | Return variables and their amount of occurrences in the given structure
countVariables :: Data a => a -> Map String Int
countVariables :: forall a. Data a => a -> Map Atom Int
countVariables = (Map Atom Int -> Map Atom Int -> Map Atom Int)
-> (forall a. Data a => a -> Map Atom Int)
-> forall a. Data a => a -> Map Atom Int
forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything ((Int -> Int -> Int) -> Map Atom Int -> Map Atom Int -> Map Atom Int
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+)) ((forall a. Data a => a -> Map Atom Int)
 -> forall a. Data a => a -> Map Atom Int)
-> (forall a. Data a => a -> Map Atom Int)
-> forall a. Data a => a -> Map Atom Int
forall a b. (a -> b) -> a -> b
$ Map Atom Int -> (Term -> Map Atom Int) -> a -> Map Atom Int
forall a b r. (Typeable a, Typeable b) => r -> (b -> r) -> a -> r
mkQ Map Atom Int
forall k a. Map k a
Map.empty Term -> Map Atom Int
count
  where
    count :: Term -> Map String Int
    count :: Term -> Map Atom Int
count (Var (VariableName Int
_ Atom
name)) = Atom -> Int -> Map Atom Int
forall k a. k -> a -> Map k a
Map.singleton Atom
name Int
1
    count Term
_ = Map Atom Int
forall k a. Map k a
Map.empty

-- | Check whether a term in the structure represents the cut operator
containsCut :: Data a => a -> Bool
containsCut :: forall a. Data a => a -> Bool
containsCut = (Bool -> Bool -> Bool)
-> (forall a. Data a => a -> Bool) -> forall a. Data a => a -> Bool
forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything Bool -> Bool -> Bool
(||) ((forall a. Data a => a -> Bool) -> forall a. Data a => a -> Bool)
-> (forall a. Data a => a -> Bool) -> forall a. Data a => a -> Bool
forall a b. (a -> b) -> a -> b
$ Bool -> (Term -> Bool) -> a -> Bool
forall a b r. (Typeable a, Typeable b) => r -> (b -> r) -> a -> r
mkQ Bool
False ((Term -> Bool) -> a -> Bool) -> (Term -> Bool) -> a -> Bool
forall a b. (a -> b) -> a -> b
$ \case
  Cut Int
_ -> Bool
True
  Term
_ -> Bool
False

internalErrorProblem :: String -> Problem
internalErrorProblem :: Atom -> Problem
internalErrorProblem Atom
message =
  Doc -> Problem
Problem (Doc -> Problem) -> Doc -> Problem
forall a b. (a -> b) -> a -> b
$
    [Doc] -> Doc
vcat
      [ Doc
"An unexpected error occurred."
      , Doc
"This is usually not caused by a fault within your submission."
      , Doc
"Please contact your lecturers, providing the following error message:"
      , Text -> Doc
string (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ Atom -> Text
pack Atom
message
      ]