137 lines
4.6 KiB
Haskell
Raw Normal View History

2026-01-21 11:24:30 +00:00
{-# LANGUAGE DuplicateRecordFields #-}
2026-01-27 16:41:33 +00:00
{-# LANGUAGE ImportQualifiedPost #-}
2026-01-21 11:24:30 +00:00
{-# LANGUAGE OverloadedRecordDot #-}
2026-01-27 16:41:33 +00:00
{-# LANGUAGE OverloadedStrings #-}
2026-01-21 11:24:30 +00:00
{-# HLINT ignore "Use const" #-}
{-# HLINT ignore "Unused LANGUAGE pragma" #-}
{-# HLINT ignore "Avoid lambda" #-}
{-# LANGUAGE ScopedTypeVariables #-}
2026-01-27 16:41:33 +00:00
{-# LANGUAGE TypeApplications #-}
2026-01-21 11:24:30 +00:00
{-# LANGUAGE NoFieldSelectors #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
2026-01-27 16:41:33 +00:00
{-# LANGUAGE BlockArguments #-}
2026-01-27 17:27:18 +00:00
{-# LANGUAGE NoMonomorphismRestriction #-}
2026-01-21 11:24:30 +00:00
module Test.Datalog.NaiveDatabaseSpec where
2026-01-27 16:41:33 +00:00
import Data.Map qualified as Map
2026-01-22 14:25:09 +00:00
import Data.Set qualified as Set
import Datalog.DatalogParser
2026-01-27 16:41:33 +00:00
import Datalog.NaiveDatabase
import Datalog.NaiveDatabase qualified as NaiveDatabase
import Test.Hspec
2026-01-21 11:24:30 +00:00
spec :: Spec
spec = do
2026-01-27 16:41:33 +00:00
describe "NaiveDatabase operations" do
2026-01-22 14:25:09 +00:00
it "can ingest facts into relations & a universe" $ do
2026-01-27 16:41:33 +00:00
let db =
NaiveDatabase.withFacts
[ "parent(\"alice\", \"bob\")."
, "parent(\"bob\", \"carol\")."
]
constants db
`shouldBe` Set.fromList (Sym <$> ["alice", "bob", "carol"])
relations db
`shouldBe` Map.fromList
[
( "parent"
2026-01-27 17:27:18 +00:00
, Relation "parent" 2 (Set.fromList $ Sym <<$>> [["alice", "bob"], ["bob", "carol"]]) []
2026-01-27 16:41:33 +00:00
)
]
it "can ingest facts and rules" do
let db =
NaiveDatabase.withFactsAndRules
[ "parent(\"alice\", \"bob\")."
, "parent(\"bob\", \"carol\")."
]
[ "ancestor(X,Y) :- parent(X,Y)."
, "ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y)."
]
parentRelation =
Relation
{ _name = "parent"
, _arity = 2
, _tuples =
Set.fromList $
map (Sym <$>) [["alice", "bob"], ["bob", "carol"]]
, _rules = []
}
ancestorRule =
RelationRule
{ headVariables = ["X", "Y", "Z"]
, bodyElements =
[ RuleBodyElement
{ _subRelationId = "parent"
, _ruleElements =
[ RuleElementVariable "X"
, RuleElementVariable "Z"
]
}
, RuleBodyElement
{ _subRelationId = "ancestor"
, _ruleElements =
[ RuleElementVariable "Z"
, RuleElementVariable "Y"
]
}
]
2026-01-27 12:41:23 +00:00
}
2026-01-27 16:41:33 +00:00
ancestorRelation =
Relation
{ _arity = 2
, _name = "ancestor"
, _tuples = Set.empty
, _rules = [ancestorRule]
}
constants db
2026-01-27 17:27:18 +00:00
`shouldBe` Set.fromList (Sym <$> ["alice", "bob", "carol"])
2026-01-27 16:41:33 +00:00
relations db
`shouldBe` Map.fromList
[ ("ancestor", ancestorRelation)
, ("parent", parentRelation)
]
2026-01-22 14:25:09 +00:00
2026-01-27 16:41:33 +00:00
it "can ingest facts and rules with constants" do
let db =
NaiveDatabase.withFactsAndRules
[]
["ancestor(X,\"patriarch\") :- ."]
ancestorRule =
RelationRule
{ headVariables = ["X"]
, bodyElements = []
}
ancestorRelation =
Relation
{ _arity = 2
, _name = "ancestor"
, _tuples = Set.empty
, _rules = [ancestorRule]
}
relations db
`shouldBe` Map.fromList
[ ("ancestor", ancestorRelation)
]
2026-01-27 16:41:33 +00:00
constants db
2026-01-27 17:27:18 +00:00
`shouldBe` Set.fromList (Sym <$> ["patriarch"])
2026-01-27 16:41:33 +00:00
it "can do basic queries" do
let db =
NaiveDatabase.withFacts
[ "parent(\"alice\", \"bob\")."
, "parent(\"bob\", \"carol\")."
]
2026-01-22 14:25:09 +00:00
query db "?- parent(alice,X)." `shouldBe` "#NYI" -- ideally, 'bob'
2026-01-27 17:27:18 +00:00
(<<$>>) :: (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