initial 'naive DB' API implementation

This commit is contained in:
Felix Dilke 2026-01-21 17:11:13 +00:00
parent 2900e781a1
commit 73aec57151
2 changed files with 52 additions and 2 deletions

View File

@ -3,16 +3,60 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Redundant flip" #-}
{-# LANGUAGE ImportQualifiedPost #-}
module Datalog.NaiveDatabase where
import Data.Map (Map)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import qualified Data.Text as T
import Datalog.DatalogParser
import qualified Data.Map as Map
import Text.Megaparsec (ParseErrorBundle)
import Data.Void
import Control.Exception (Exception)
import Control.Exception.Base
data Value =
ValueInt Int |
ValueSymbol String
data NaiveDatabase = NaiveDatabase {
relations :: Map String Int,
relations :: Map Text Int,
values :: Set Value
}
}
emptyDB :: NaiveDatabase
emptyDB = NaiveDatabase {
relations = Map.empty,
values = Set.empty
}
withFacts :: [Text] -> NaiveDatabase
withFacts facts =
foldl addFact emptyDB (extractFact <$> facts) where
extractFact:: Text -> Literal
extractFact factText =
case (parseDatalog factText) of
Right (Fact fact) -> fact
Right otherStatement -> throw $ NonFactException factText otherStatement
Left ex -> throw $ CannotParseStatementException factText ex
addFact :: NaiveDatabase -> Literal -> NaiveDatabase
addFact db lit =
db
query :: NaiveDatabase -> Text -> Text
query db qText =
case (parseDatalog qText) of
Right (Query texts literals) -> "#NYI"
Right otherStatement -> throw $ NonQueryException qText otherStatement
Left ex -> throw $ CannotParseStatementException qText ex
data NaiveDatabaseException
= CannotParseStatementException Text (ParseErrorBundle Text Void) |
NonFactException Text Statement |
NonQueryException Text Statement
deriving (Show)
instance Exception NaiveDatabaseException

View File

@ -13,6 +13,7 @@ module Test.Datalog.NaiveDatabaseSpec where
import Test.Hspec
import Datalog.NaiveDatabase
import qualified Datalog.NaiveDatabase as NaiveDatabase
-- checkParse :: String -> Expr -> Expectation
-- checkParse text expectedExpr =
@ -28,3 +29,8 @@ spec = do
describe "dummy test" $ do
it "..." $ do
1 `shouldBe` (1 :: Int)
it "can accept facts and do basic queries" $ do
let twig = [ "xx", "yy "]
let pig = [ "parent(\"alice\", \"bob\").", "parent(\"bob\", \"carol\")." ]
let db = NaiveDatabase.withFacts [ "parent(\"alice\", \"bob\").", "parent(\"bob\", \"carol\")." ]
query db"?- parent(alice,X)." `shouldBe` "#NYI" -- ideally, 'bob'