added names for relations

This commit is contained in:
Felix Dilke 2026-01-26 12:20:19 +00:00
parent 04dcb4e8f7
commit 219ff26a90
2 changed files with 9 additions and 3 deletions

View File

@ -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]

View File

@ -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"]