hlint-induced tidyings
This commit is contained in:
parent
9fc3cc9fa0
commit
80024a533e
@ -107,7 +107,7 @@ data BodyConstraint = BodyConstraint
|
|||||||
data RuleContext = RuleContext
|
data RuleContext = RuleContext
|
||||||
{ __relation :: Relation
|
{ __relation :: Relation
|
||||||
, -- _variableNames :: [Text],
|
, -- _variableNames :: [Text],
|
||||||
_headVariables :: [RuleElement]
|
_headEntries :: [RuleElement]
|
||||||
, _bodyConstraints :: [BodyConstraint]
|
, _bodyConstraints :: [BodyConstraint]
|
||||||
, _db :: NaiveDatabase
|
, _db :: NaiveDatabase
|
||||||
}
|
}
|
||||||
@ -122,7 +122,7 @@ withFactsAndRules facts rules =
|
|||||||
where
|
where
|
||||||
extractRule :: Text -> (Literal, [Literal])
|
extractRule :: Text -> (Literal, [Literal])
|
||||||
extractRule ruleText =
|
extractRule ruleText =
|
||||||
case (parseDatalog ruleText) of
|
case parseDatalog ruleText of
|
||||||
Right (Rule (HeadSingle lhs) rhs) -> (lhs, rhs)
|
Right (Rule (HeadSingle lhs) rhs) -> (lhs, rhs)
|
||||||
Right otherStatement -> throw $ NonRuleException ruleText otherStatement
|
Right otherStatement -> throw $ NonRuleException ruleText otherStatement
|
||||||
Left ex -> throw $ CannotParseStatementException ruleText ex
|
Left ex -> throw $ CannotParseStatementException ruleText ex
|
||||||
@ -130,7 +130,7 @@ withFactsAndRules facts rules =
|
|||||||
digestHead db relation (Literal neg relationName terms) =
|
digestHead db relation (Literal neg relationName terms) =
|
||||||
RuleContext
|
RuleContext
|
||||||
{ __relation = relation
|
{ __relation = relation
|
||||||
, _headVariables = variables'
|
, _headEntries = variables'
|
||||||
, _bodyConstraints = []
|
, _bodyConstraints = []
|
||||||
, _db = NaiveDatabase relationMap' constants'
|
, _db = NaiveDatabase relationMap' constants'
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ withFactsAndRules facts rules =
|
|||||||
digestBody context (Literal neg subRelationName terms) =
|
digestBody context (Literal neg subRelationName terms) =
|
||||||
RuleContext
|
RuleContext
|
||||||
{ __relation = relation
|
{ __relation = relation
|
||||||
, _headVariables = variables'
|
, _headEntries = variables'
|
||||||
, _bodyConstraints = constraints'
|
, _bodyConstraints = constraints'
|
||||||
, _db = NaiveDatabase relationMap' constants'
|
, _db = NaiveDatabase relationMap' constants'
|
||||||
}
|
}
|
||||||
@ -156,8 +156,8 @@ withFactsAndRules facts rules =
|
|||||||
relationMap :: Map RelationId Relation = relations (_db context)
|
relationMap :: Map RelationId Relation = relations (_db context)
|
||||||
relationMap' = Map.insert subRelationName subRelation relationMap
|
relationMap' = Map.insert subRelationName subRelation relationMap
|
||||||
extraVariables = toElement <$> terms
|
extraVariables = toElement <$> terms
|
||||||
extraConstants = catMaybes $ maybeConstant <$> extraVariables
|
extraConstants = mapMaybe maybeConstant extraVariables
|
||||||
variables' = nub $ _headVariables context ++ extraVariables
|
variables' = nub $ _headEntries context ++ extraVariables
|
||||||
constants' = Set.union (constants (_db context)) $ Set.fromList extraConstants
|
constants' = Set.union (constants (_db context)) $ Set.fromList extraConstants
|
||||||
constraints = _bodyConstraints context
|
constraints = _bodyConstraints context
|
||||||
newConstraint = BodyConstraint subRelation (toConstraint <$> terms)
|
newConstraint = BodyConstraint subRelation (toConstraint <$> terms)
|
||||||
@ -182,14 +182,14 @@ withFactsAndRules facts rules =
|
|||||||
context'' = foldl digestBody context' body
|
context'' = foldl digestBody context' body
|
||||||
db' = _db context''
|
db' = _db context''
|
||||||
relationMap = relations db'
|
relationMap = relations db'
|
||||||
variables' = _headVariables context''
|
variables' = _headEntries context''
|
||||||
extractVarName :: RuleElement -> Maybe Text
|
extractVarName :: RuleElement -> Maybe Text
|
||||||
extractVarName (RuleElementVariable varName) = Just varName
|
extractVarName (RuleElementVariable varName) = Just varName
|
||||||
extractVarName (RuleElementConstant constant) = Nothing
|
extractVarName (RuleElementConstant constant) = Nothing
|
||||||
newRule =
|
newRule =
|
||||||
RelationRule
|
RelationRule
|
||||||
{ headVariables = catMaybes $ extractVarName <$> variables'
|
{ headVariables = mapMaybe extractVarName variables'
|
||||||
, bodyElements = fromBodyConstraint <$> (_bodyConstraints context'')
|
, bodyElements = fromBodyConstraint <$> _bodyConstraints context''
|
||||||
}
|
}
|
||||||
where
|
where
|
||||||
fromBodyConstraint :: BodyConstraint -> RuleBodyElement
|
fromBodyConstraint :: BodyConstraint -> RuleBodyElement
|
||||||
@ -206,7 +206,7 @@ withFactsAndRules facts rules =
|
|||||||
{ _name = _name relation
|
{ _name = _name relation
|
||||||
, _arity = newArity
|
, _arity = newArity
|
||||||
, _tuples = _tuples relation
|
, _tuples = _tuples relation
|
||||||
, _rules = (_rules relation) ++ [newRule]
|
, _rules = newRule : _rules relation
|
||||||
}
|
}
|
||||||
newRelationMap = Map.insert relationName relation' relationMap
|
newRelationMap = Map.insert relationName relation' relationMap
|
||||||
constants' = constants db'
|
constants' = constants db'
|
||||||
@ -216,7 +216,7 @@ withFactsAndRules facts rules =
|
|||||||
|
|
||||||
query :: NaiveDatabase -> Text -> Text
|
query :: NaiveDatabase -> Text -> Text
|
||||||
query db qText =
|
query db qText =
|
||||||
case (parseDatalog qText) of
|
case parseDatalog qText of
|
||||||
Right (Query texts literals) -> "#NYI"
|
Right (Query texts literals) -> "#NYI"
|
||||||
Right otherStatement -> throw $ NonQueryException qText otherStatement
|
Right otherStatement -> throw $ NonQueryException qText otherStatement
|
||||||
Left ex -> throw $ CannotParseStatementException qText ex
|
Left ex -> throw $ CannotParseStatementException qText ex
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
{-# LANGUAGE NoFieldSelectors #-}
|
{-# LANGUAGE NoFieldSelectors #-}
|
||||||
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
||||||
{-# LANGUAGE BlockArguments #-}
|
{-# LANGUAGE BlockArguments #-}
|
||||||
|
{-# LANGUAGE NoMonomorphismRestriction #-}
|
||||||
|
|
||||||
module Test.Datalog.NaiveDatabaseSpec where
|
module Test.Datalog.NaiveDatabaseSpec where
|
||||||
|
|
||||||
@ -35,7 +36,7 @@ spec = do
|
|||||||
`shouldBe` Map.fromList
|
`shouldBe` Map.fromList
|
||||||
[
|
[
|
||||||
( "parent"
|
( "parent"
|
||||||
, Relation "parent" 2 (Set.fromList $ map (Sym <$>) [["alice", "bob"], ["bob", "carol"]]) []
|
, Relation "parent" 2 (Set.fromList $ Sym <<$>> [["alice", "bob"], ["bob", "carol"]]) []
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
it "can ingest facts and rules" do
|
it "can ingest facts and rules" do
|
||||||
@ -84,7 +85,7 @@ spec = do
|
|||||||
, _rules = [ancestorRule]
|
, _rules = [ancestorRule]
|
||||||
}
|
}
|
||||||
constants db
|
constants db
|
||||||
`shouldBe` (Set.fromList $ Sym <$> ["alice", "bob", "carol"])
|
`shouldBe` Set.fromList (Sym <$> ["alice", "bob", "carol"])
|
||||||
relations db
|
relations db
|
||||||
`shouldBe` Map.fromList
|
`shouldBe` Map.fromList
|
||||||
[ ("ancestor", ancestorRelation)
|
[ ("ancestor", ancestorRelation)
|
||||||
@ -114,7 +115,7 @@ spec = do
|
|||||||
]
|
]
|
||||||
|
|
||||||
constants db
|
constants db
|
||||||
`shouldBe` (Set.fromList $ Sym <$> ["patriarch"])
|
`shouldBe` Set.fromList (Sym <$> ["patriarch"])
|
||||||
|
|
||||||
it "can do basic queries" do
|
it "can do basic queries" do
|
||||||
let db =
|
let db =
|
||||||
@ -123,3 +124,13 @@ spec = do
|
|||||||
, "parent(\"bob\", \"carol\")."
|
, "parent(\"bob\", \"carol\")."
|
||||||
]
|
]
|
||||||
query db "?- parent(alice,X)." `shouldBe` "#NYI" -- ideally, 'bob'
|
query db "?- parent(alice,X)." `shouldBe` "#NYI" -- ideally, 'bob'
|
||||||
|
|
||||||
|
(<<$>>) :: (Functor f1, Functor f2) => (a -> b) -> f1 (f2 a) -> f1 (f2 b)
|
||||||
|
(<<$>>) = fmap fmap fmap
|
||||||
|
|
||||||
|
-- (<<<$>>>) :: (Functor f1, Functor f2, Functor f3) => f1 (f2 (a -> b)) -> f1 (f2 (f3 a -> f3 b))
|
||||||
|
-- (<<<$>>>) :: Functor f => (a1 -> b) -> (a2 -> a1) -> f a2 -> f b
|
||||||
|
-- (<<<$>>>) :: (Functor f1, Functor f2) => (a1 -> a2 -> b) -> f1 a1 -> f1 (f2 a2 -> f2 b)
|
||||||
|
-- (<<<$>>>) :: (Functor f1, Functor f2, Functor f3) => f1 (a -> b) -> f1 (f2 (f3 a) -> f2 (f3 b))
|
||||||
|
(<<<$>>>) :: (Functor f1, Functor f2, Functor f3) => (a -> b) -> f1 (f2 (f3 a)) -> f1 (f2 (f3 b))
|
||||||
|
(<<<$>>>) = fmap fmap fmap fmap fmap fmap fmap fmap
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user