2026-01-21 11:24:30 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
|
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
|
|
|
|
|
|
|
|
|
{-# HLINT ignore "Redundant flip" #-}
|
2026-01-21 17:11:13 +00:00
|
|
|
{-# LANGUAGE ImportQualifiedPost #-}
|
2026-01-21 11:24:30 +00:00
|
|
|
module Datalog.NaiveDatabase where
|
|
|
|
|
|
|
|
|
|
import Data.Map (Map)
|
|
|
|
|
import Data.Set (Set)
|
2026-01-21 17:11:13 +00:00
|
|
|
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
|
2026-01-21 11:24:30 +00:00
|
|
|
|
|
|
|
|
data Value =
|
|
|
|
|
ValueInt Int |
|
|
|
|
|
ValueSymbol String
|
|
|
|
|
|
|
|
|
|
data NaiveDatabase = NaiveDatabase {
|
2026-01-21 18:01:06 +00:00
|
|
|
relations :: Map RelationId Relation,
|
2026-01-22 10:57:11 +00:00
|
|
|
constants :: Set Constant
|
2026-01-21 17:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-21 18:01:06 +00:00
|
|
|
data Relation = Relation {
|
|
|
|
|
arity :: Int,
|
2026-01-22 10:57:11 +00:00
|
|
|
tuples :: Set [Constant]
|
2026-01-21 18:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-22 10:57:11 +00:00
|
|
|
-- newtype RelationId = RelationId Text
|
|
|
|
|
-- deriving (Eq, Ord, Show)
|
2026-01-21 18:01:06 +00:00
|
|
|
|
2026-01-22 10:57:11 +00:00
|
|
|
-- Our constants will be the terms of the Datalog grammar - ints/variables/symbols
|
|
|
|
|
type Constant = Term
|
|
|
|
|
type RelationId = Text
|
|
|
|
|
|
|
|
|
|
-- newtype Constant = Constant Text
|
|
|
|
|
-- deriving (Eq, Ord, Show)
|
2026-01-21 18:01:06 +00:00
|
|
|
|
2026-01-21 17:11:13 +00:00
|
|
|
emptyDB :: NaiveDatabase
|
|
|
|
|
emptyDB = NaiveDatabase {
|
|
|
|
|
relations = Map.empty,
|
2026-01-22 10:57:11 +00:00
|
|
|
constants = Set.empty -- the Herbrand universe
|
2026-01-21 17:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2026-01-22 10:57:11 +00:00
|
|
|
addFact (NaiveDatabase relations constants) (Literal neg relationName terms) =
|
|
|
|
|
NaiveDatabase newRelations newConstants where
|
|
|
|
|
newRelations =
|
|
|
|
|
case Map.lookup relationName relations of
|
|
|
|
|
Nothing -> relations
|
|
|
|
|
Just relation -> relations
|
|
|
|
|
newConstants = Set.union constants $ Set.fromList terms
|
2026-01-21 17:11:13 +00:00
|
|
|
|
|
|
|
|
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
|