initial 'naive DB' API implementation
This commit is contained in:
parent
2900e781a1
commit
73aec57151
@ -3,16 +3,60 @@
|
|||||||
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
||||||
|
|
||||||
{-# HLINT ignore "Redundant flip" #-}
|
{-# HLINT ignore "Redundant flip" #-}
|
||||||
|
{-# LANGUAGE ImportQualifiedPost #-}
|
||||||
module Datalog.NaiveDatabase where
|
module Datalog.NaiveDatabase where
|
||||||
|
|
||||||
import Data.Map (Map)
|
import Data.Map (Map)
|
||||||
import Data.Set (Set)
|
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 =
|
data Value =
|
||||||
ValueInt Int |
|
ValueInt Int |
|
||||||
ValueSymbol String
|
ValueSymbol String
|
||||||
|
|
||||||
data NaiveDatabase = NaiveDatabase {
|
data NaiveDatabase = NaiveDatabase {
|
||||||
relations :: Map String Int,
|
relations :: Map Text Int,
|
||||||
values :: Set Value
|
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
|
||||||
@ -13,6 +13,7 @@ module Test.Datalog.NaiveDatabaseSpec where
|
|||||||
|
|
||||||
import Test.Hspec
|
import Test.Hspec
|
||||||
import Datalog.NaiveDatabase
|
import Datalog.NaiveDatabase
|
||||||
|
import qualified Datalog.NaiveDatabase as NaiveDatabase
|
||||||
|
|
||||||
-- checkParse :: String -> Expr -> Expectation
|
-- checkParse :: String -> Expr -> Expectation
|
||||||
-- checkParse text expectedExpr =
|
-- checkParse text expectedExpr =
|
||||||
@ -28,3 +29,8 @@ spec = do
|
|||||||
describe "dummy test" $ do
|
describe "dummy test" $ do
|
||||||
it "..." $ do
|
it "..." $ do
|
||||||
1 `shouldBe` (1 :: Int)
|
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'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user