From 219ff26a908ba15c11d63b3e39a1057b4dfc7201 Mon Sep 17 00:00:00 2001 From: Felix Dilke Date: Mon, 26 Jan 2026 12:20:19 +0000 Subject: [PATCH] added names for relations --- haskell-experiments/src/Datalog/NaiveDatabase.hs | 6 ++++-- haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/haskell-experiments/src/Datalog/NaiveDatabase.hs b/haskell-experiments/src/Datalog/NaiveDatabase.hs index 99e98cd..9c5860f 100644 --- a/haskell-experiments/src/Datalog/NaiveDatabase.hs +++ b/haskell-experiments/src/Datalog/NaiveDatabase.hs @@ -39,6 +39,7 @@ data RelationRule = RelationRule { } deriving (Show, Eq) data Relation = Relation { + _name :: Text, _arity :: Int, _tuples :: Set [Constant], _rules :: [RelationRule] @@ -80,10 +81,10 @@ withFacts facts = lookupRelation :: RelationId -> Map RelationId Relation -> Int -> [Term] -> Relation lookupRelation relationName relationMap newArity terms = case Map.lookup relationName relationMap of - Nothing -> Relation newArity (Set.singleton terms) [] + Nothing -> Relation relationName newArity (Set.singleton terms) [] Just relation -> if (_arity relation == newArity) - then Relation (length terms) (Set.singleton terms) [] + then Relation relationName (length terms) (Set.singleton terms) [] else throw $ BadArityException relationName newArity data ConstraintElement = -- entry occurring in a rule body constraint - constant or variable index @@ -217,6 +218,7 @@ withFactsAndRules facts rules = -- _relation :: Relation, -- _elements :: [ConstraintElement] relation' = Relation { + _name = _name relation, _arity = newArity, _tuples = _tuples relation, _rules = (_rules relation) ++ [newRule] diff --git a/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs b/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs index 873b152..f59ee66 100644 --- a/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs +++ b/haskell-experiments/test/Test/Datalog/NaiveDatabaseSpec.hs @@ -33,7 +33,7 @@ spec = do relations db `shouldBe` Map.fromList [ ("parent", - Relation 2 (Set.fromList ([Sym <$> ["bob", "carol"]])) [] ) + Relation "parent" 2 (Set.fromList ([Sym <$> ["bob", "carol"]])) [] ) ] it "can ingest facts and rules" $ do let db = NaiveDatabase.withFactsAndRules @@ -48,6 +48,7 @@ spec = do ("ancestor", Relation { _arity = 2, + _name = "ancestor", _tuples = Set.fromList [ [Var "X", Var "Y"] ], @@ -57,6 +58,7 @@ spec = do body = [ ( Relation { + _name = "parent", _arity = 2, _tuples = Set.fromList [ [Var "X",Var "Z" ] @@ -68,6 +70,7 @@ spec = do ] ),( Relation { + _name = "ancestor", _arity = 2, _tuples = Set.fromList [ [Var "Z",Var "Y"] @@ -85,6 +88,7 @@ spec = do ), ("parent", Relation { + _name = "parent", _arity = 2, _tuples = Set.fromList [ [Var "X",Var "Z"]