{-# LANGUAGE OverloadedStrings #-}
module Prolog.Programming.CodeAnalysis.Rules.SymbolOverload where
import Data.Foldable.Extra (notNull)
import Data.Generics (everything, mkQ)
import Data.List (singleton)
import Data.Set (Set)
import qualified Data.Set as Set (
difference,
empty,
insert,
intersection,
singleton,
toList,
union,
unions,
)
import Data.Text.Lazy (pack)
import Language.Prolog (Atom, ProgramClause, Term (Struct))
import Prolog.Programming.CodeAnalysis.Helper (internalErrorProblem)
import Prolog.Programming.CodeAnalysis.Types (Context, Problem (..), ProgramRule)
import Text.PrettyPrint.Leijen.Text (hsep, string)
symbolOverloadChecker :: Context -> ProgramRule
symbolOverloadChecker :: Context -> ProgramRule
symbolOverloadChecker Context
otherDefinitions Context
prog
| Set Atom -> Bool
forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull (Set Atom -> Set Atom -> Set Atom
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection Set Atom
taskPredicates Set Atom
taskDataCons) =
Problem -> [Problem]
forall a. a -> [a]
singleton (Problem -> [Problem]) -> Problem -> [Problem]
forall a b. (a -> b) -> a -> b
$
Atom -> Problem
internalErrorProblem Atom
"Task instance has mixed usage of predicates and data constructors under same name."
| Bool
otherwise =
[[Problem]] -> [Problem]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ (Atom -> Problem) -> Set Atom -> [Problem]
forall {a} {b}. (a -> b) -> Set a -> [b]
toProblems (Atom -> Atom -> Atom -> Problem
toTaskProblem Atom
"data constructor" Atom
"predicate") Set Atom
taskPredicatesAsSubmissionDataCons
, (Atom -> Problem) -> Set Atom -> [Problem]
forall {a} {b}. (a -> b) -> Set a -> [b]
toProblems (Atom -> Atom -> Atom -> Problem
toTaskProblem Atom
"predicate" Atom
"data constructor") Set Atom
taskDataConsAsSubmissionPredicates
, (Atom -> Problem) -> Set Atom -> [Problem]
forall {a} {b}. (a -> b) -> Set a -> [b]
toProblems Atom -> Problem
toSubmissionProblem
(Set Atom -> [Problem]) -> Set Atom -> [Problem]
forall a b. (a -> b) -> a -> b
$ Set Atom -> Set Atom -> Set Atom
forall a. Ord a => Set a -> Set a -> Set a
Set.difference Set Atom
submissionIntersection
(Set Atom -> Set Atom) -> Set Atom -> Set Atom
forall a b. (a -> b) -> a -> b
$ Set Atom -> Set Atom -> Set Atom
forall a. Ord a => Set a -> Set a -> Set a
Set.union Set Atom
taskPredicatesAsSubmissionDataCons Set Atom
taskDataConsAsSubmissionPredicates
]
where
(Set Atom
taskPredicates, Set Atom
taskDataCons) =
Set (Either Atom Atom) -> (Set Atom, Set Atom)
partitionIdentities
(Set (Either Atom Atom) -> (Set Atom, Set Atom))
-> Set (Either Atom Atom) -> (Set Atom, Set Atom)
forall a b. (a -> b) -> a -> b
$ [Set (Either Atom Atom)] -> Set (Either Atom Atom)
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions
([Set (Either Atom Atom)] -> Set (Either Atom Atom))
-> [Set (Either Atom Atom)] -> Set (Either Atom Atom)
forall a b. (a -> b) -> a -> b
$ (ProgramClause -> Set (Either Atom Atom))
-> Context -> [Set (Either Atom Atom)]
forall a b. (a -> b) -> [a] -> [b]
map ProgramClause -> Set (Either Atom Atom)
grabPredicatesAndDataConstructorsFromClause Context
otherDefinitions
(Set Atom
submissionPredicates, Set Atom
submissionDataCons) =
Set (Either Atom Atom) -> (Set Atom, Set Atom)
partitionIdentities
(Set (Either Atom Atom) -> (Set Atom, Set Atom))
-> Set (Either Atom Atom) -> (Set Atom, Set Atom)
forall a b. (a -> b) -> a -> b
$ [Set (Either Atom Atom)] -> Set (Either Atom Atom)
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions
([Set (Either Atom Atom)] -> Set (Either Atom Atom))
-> [Set (Either Atom Atom)] -> Set (Either Atom Atom)
forall a b. (a -> b) -> a -> b
$ (ProgramClause -> Set (Either Atom Atom))
-> Context -> [Set (Either Atom Atom)]
forall a b. (a -> b) -> [a] -> [b]
map ProgramClause -> Set (Either Atom Atom)
grabPredicatesAndDataConstructorsFromClause Context
prog
taskPredicatesAsSubmissionDataCons :: Set Atom
taskPredicatesAsSubmissionDataCons = Set Atom -> Set Atom -> Set Atom
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection Set Atom
taskPredicates Set Atom
submissionDataCons
taskDataConsAsSubmissionPredicates :: Set Atom
taskDataConsAsSubmissionPredicates = Set Atom -> Set Atom -> Set Atom
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection Set Atom
taskDataCons Set Atom
submissionPredicates
submissionIntersection :: Set Atom
submissionIntersection = Set Atom -> Set Atom -> Set Atom
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection Set Atom
submissionPredicates Set Atom
submissionDataCons
toProblems :: (a -> b) -> Set a -> [b]
toProblems a -> b
mapper Set a
s = (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
mapper ([a] -> [b]) -> [a] -> [b]
forall a b. (a -> b) -> a -> b
$ Set a -> [a]
forall a. Set a -> [a]
Set.toList Set a
s
partitionIdentities :: Set (Either Atom Atom) -> (Set Atom, Set Atom)
partitionIdentities = (Either Atom Atom -> (Set Atom, Set Atom) -> (Set Atom, Set Atom))
-> (Set Atom, Set Atom)
-> Set (Either Atom Atom)
-> (Set Atom, Set Atom)
forall a b. (a -> b -> b) -> b -> Set a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Either Atom Atom -> (Set Atom, Set Atom) -> (Set Atom, Set Atom)
forall {a} {a}.
(Ord a, Ord a) =>
Either a a -> (Set a, Set a) -> (Set a, Set a)
partition (Set Atom
forall a. Set a
Set.empty, Set Atom
forall a. Set a
Set.empty)
partition :: Either a a -> (Set a, Set a) -> (Set a, Set a)
partition (Left a
sig) (Set a
ps, Set a
ds) = (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
sig Set a
ps, Set a
ds)
partition (Right a
sig) (Set a
ps, Set a
ds) = (Set a
ps, a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
sig Set a
ds)
grabPredicatesAndDataConstructorsFromClause :: ProgramClause -> Set (Either Atom Atom)
grabPredicatesAndDataConstructorsFromClause :: ProgramClause -> Set (Either Atom Atom)
grabPredicatesAndDataConstructorsFromClause (Atom
name, [Term]
args, [Term]
goals) =
Set (Either Atom Atom)
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a. Ord a => Set a -> Set a -> Set a
Set.union Set (Either Atom Atom)
fromClauseHead (Set (Either Atom Atom) -> Set (Either Atom Atom))
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a b. (a -> b) -> a -> b
$ [Set (Either Atom Atom)] -> Set (Either Atom Atom)
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions ([Set (Either Atom Atom)] -> Set (Either Atom Atom))
-> [Set (Either Atom Atom)] -> Set (Either Atom Atom)
forall a b. (a -> b) -> a -> b
$ (Term -> Set (Either Atom Atom))
-> [Term] -> [Set (Either Atom Atom)]
forall a b. (a -> b) -> [a] -> [b]
map Term -> Set (Either Atom Atom)
grabPredicatesAndDataConstructors [Term]
goals
where
fromClauseHead :: Set (Either Atom Atom)
fromClauseHead =
Either Atom Atom
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a. Ord a => a -> Set a -> Set a
Set.insert (Atom -> Either Atom Atom
forall a b. a -> Either a b
Left Atom
name) (Set (Either Atom Atom) -> Set (Either Atom Atom))
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a b. (a -> b) -> a -> b
$
(Set (Either Atom Atom)
-> Set (Either Atom Atom) -> Set (Either Atom Atom))
-> GenericQ (Set (Either Atom Atom))
-> GenericQ (Set (Either Atom Atom))
forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything Set (Either Atom Atom)
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Set (Either Atom Atom)
-> (Term -> Set (Either Atom Atom)) -> a -> Set (Either Atom Atom)
forall a b r. (Typeable a, Typeable b) => r -> (b -> r) -> a -> r
mkQ Set (Either Atom Atom)
forall a. Set a
Set.empty Term -> Set (Either Atom Atom)
forall a. Term -> Set (Either a Atom)
structToDataCons) [Term]
args
grabPredicatesAndDataConstructors :: Term -> Set (Either Atom Atom)
grabPredicatesAndDataConstructors :: Term -> Set (Either Atom Atom)
grabPredicatesAndDataConstructors (Struct Atom
name [Term]
args) =
Either Atom Atom
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a. Ord a => a -> Set a -> Set a
Set.insert (Atom -> Either Atom Atom
forall a b. a -> Either a b
Left Atom
name) (Set (Either Atom Atom) -> Set (Either Atom Atom))
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a b. (a -> b) -> a -> b
$ case (Atom
name, [Term]
args) of
(Atom
n, [Term
arg]) | Atom
n Atom -> [Atom] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Atom
"\\+", Atom
"not"] -> Term -> Set (Either Atom Atom)
grabPredicatesAndDataConstructors Term
arg
(Atom
n, [Term
l, Term
r])
| Atom
n Atom -> [Atom] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Atom
";", Atom
","] ->
Set (Either Atom Atom)
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Term -> Set (Either Atom Atom)
grabPredicatesAndDataConstructors Term
l) (Term -> Set (Either Atom Atom)
grabPredicatesAndDataConstructors Term
r)
(Atom
_, [Term]
args') -> (Set (Either Atom Atom)
-> Set (Either Atom Atom) -> Set (Either Atom Atom))
-> GenericQ (Set (Either Atom Atom))
-> GenericQ (Set (Either Atom Atom))
forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything Set (Either Atom Atom)
-> Set (Either Atom Atom) -> Set (Either Atom Atom)
forall a. Ord a => Set a -> Set a -> Set a
Set.union (Set (Either Atom Atom)
-> (Term -> Set (Either Atom Atom)) -> a -> Set (Either Atom Atom)
forall a b r. (Typeable a, Typeable b) => r -> (b -> r) -> a -> r
mkQ Set (Either Atom Atom)
forall a. Set a
Set.empty Term -> Set (Either Atom Atom)
forall a. Term -> Set (Either a Atom)
structToDataCons) [Term]
args'
grabPredicatesAndDataConstructors Term
_ = Set (Either Atom Atom)
forall a. Set a
Set.empty
structToDataCons :: Term -> Set (Either a Atom)
structToDataCons :: forall a. Term -> Set (Either a Atom)
structToDataCons (Struct Atom
structName [Term]
_) = Either a Atom -> Set (Either a Atom)
forall a. a -> Set a
Set.singleton (Either a Atom -> Set (Either a Atom))
-> Either a Atom -> Set (Either a Atom)
forall a b. (a -> b) -> a -> b
$ Atom -> Either a Atom
forall a b. b -> Either a b
Right Atom
structName
structToDataCons Term
_ = Set (Either a Atom)
forall a. Set a
Set.empty
toTaskProblem :: String -> String -> Atom -> Problem
toTaskProblem :: Atom -> Atom -> Atom -> Problem
toTaskProblem Atom
usedAs Atom
shouldBe Atom
name =
Doc -> Problem
Problem (Doc -> Problem) -> Doc -> Problem
forall a b. (a -> b) -> a -> b
$
[Doc] -> Doc
hsep
[ Doc
"Your program contains the symbol with name"
, Text -> Doc
string (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ Atom -> Text
pack (Atom -> Text) -> Atom -> Text
forall a b. (a -> b) -> a -> b
$ Atom -> Atom
forall a. Show a => a -> Atom
show Atom
name
, Doc
"which is used as a"
, Text -> Doc
string (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ Atom -> Text
pack Atom
usedAs
, Doc
"but should really be used as a"
, Text -> Doc
string (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ Atom -> Text
pack Atom
shouldBe
, Doc
"."
]
toSubmissionProblem :: Atom -> Problem
toSubmissionProblem :: Atom -> Problem
toSubmissionProblem Atom
name =
Doc -> Problem
Problem (Doc -> Problem) -> Doc -> Problem
forall a b. (a -> b) -> a -> b
$
[Doc] -> Doc
hsep
[ Doc
"Your program contains the symbol with name"
, Text -> Doc
string (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ Atom -> Text
pack (Atom -> Text) -> Atom -> Text
forall a b. (a -> b) -> a -> b
$ Atom -> Atom
forall a. Show a => a -> Atom
show Atom
name
, Doc
"which is used both as a predicate and as a data constructor."
]