From 7855e5ce20c4a0a6bb4fd6546cfd6a0e92ad7af8 Mon Sep 17 00:00:00 2001 From: Felix Dilke Date: Tue, 27 Jan 2026 12:41:23 +0000 Subject: [PATCH] added relation references --- .../src/Datalog/NaiveDatabase.hs | 16 ++++-- .../test/Test/Datalog/NaiveDatabaseSpec.hs | 56 +++++++++---------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/haskell-experiments/src/Datalog/NaiveDatabase.hs b/haskell-experiments/src/Datalog/NaiveDatabase.hs index f874e71..ade7f27 100644 --- a/haskell-experiments/src/Datalog/NaiveDatabase.hs +++ b/haskell-experiments/src/Datalog/NaiveDatabase.hs @@ -33,9 +33,14 @@ data RuleElement = -- entry occurring in a head or body relation - constant or v RuleElementVariable Text deriving (Show, Eq) +data RuleBodyElement = RuleBodyElement { + _subRelationName :: Text, + _ruleElements :: [RuleElement] +} deriving (Show, Eq) + data RelationRule = RelationRule { headVariables :: [Text], - body :: [(Relation, [RuleElement])] + bodyElements :: [RuleBodyElement] } deriving (Show, Eq) data Relation = Relation { @@ -201,11 +206,14 @@ withFactsAndRules facts rules = extractVarName (RuleElementConstant constant) = throw $ UnexpectedConstantException constant newRule = RelationRule { headVariables = extractVarName <$> variables', - body = fromBodyConstraint <$> (_bodyConstraints context'') + bodyElements = fromBodyConstraint <$> (_bodyConstraints context'') } where - fromBodyConstraint :: BodyConstraint -> (Relation, [RuleElement]) + fromBodyConstraint :: BodyConstraint -> RuleBodyElement fromBodyConstraint (BodyConstraint subRelation elements) = - (subRelation, toRuleElement <$> elements) + RuleBodyElement { + _subRelationName = _name subRelation, + _ruleElements = toRuleElement <$> elements + } toRuleElement :: ConstraintElement -> RuleElement toRuleElement (ConstraintElementConstant constant) = RuleElementConstant constant toRuleElement (ConstraintElementIndex index) = variables' !! index diff --git a/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs b/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs index 9105d74..e18a460 100644 --- a/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs +++ b/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs @@ -50,40 +50,34 @@ spec = do map (Sym <$>) [["alice", "bob"], ["bob", "carol"]], _rules = [] } + let ancestorRule = RelationRule { + headVariables = [ "X", "Y", "Z" ], + bodyElements = [ + RuleBodyElement { + _subRelationName = "parent", + _ruleElements = [ + RuleElementVariable "X", RuleElementVariable "Z" + ] + }, + RuleBodyElement { + _subRelationName = "ancestor", + _ruleElements = [ + RuleElementVariable "Z",RuleElementVariable "Y" + ] + } + ] + } let ancestorRelation = Relation { - _arity = 2, - _name = "ancestor", - _tuples = Set.empty, - _rules = [] - } + _arity = 2, + _name = "ancestor", + _tuples = Set.empty, + _rules = [ ancestorRule ] + } relations db `shouldBe` Map.fromList [ - ("ancestor", - Relation { - _arity = 2, - _name = "ancestor", - _tuples = Set.empty, - _rules = [ - RelationRule { - headVariables = [ "X", "Y", "Z" ], - body = [ - ( - parentRelation, [ - RuleElementVariable "X", - RuleElementVariable "Z" - ] - ),( - ancestorRelation,[ - RuleElementVariable "Z", - RuleElementVariable "Y" - ] - ) - ] - } - ] - } ), - ("parent", parentRelation ) - ] + ("ancestor", ancestorRelation), + ("parent", parentRelation ) + ] it "can do basic queries" $ do let db = NaiveDatabase.withFacts