tidying up rule data structures
This commit is contained in:
parent
42ce5a3284
commit
04dcb4e8f7
@ -11,12 +11,10 @@ import Data.Map (Map)
|
|||||||
import Data.Set (Set)
|
import Data.Set (Set)
|
||||||
import Data.Set qualified as Set
|
import Data.Set qualified as Set
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import qualified Data.Text as T
|
|
||||||
import Datalog.DatalogParser(parseDatalog, Literal(..), Statement(..), Term (..), Head (HeadSingle))
|
import Datalog.DatalogParser(parseDatalog, Literal(..), Statement(..), Term (..), Head (HeadSingle))
|
||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Text.Megaparsec (ParseErrorBundle)
|
import Text.Megaparsec (ParseErrorBundle)
|
||||||
import Data.Void
|
import Data.Void
|
||||||
import Control.Exception (Exception)
|
|
||||||
import Control.Exception.Base
|
import Control.Exception.Base
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
@ -36,7 +34,7 @@ data RuleElement = -- entry occurring in a head or body relation - constant or v
|
|||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
data RelationRule = RelationRule {
|
data RelationRule = RelationRule {
|
||||||
headVariables :: [RuleElement],
|
headVariables :: [Text],
|
||||||
body :: [(Relation, [RuleElement])]
|
body :: [(Relation, [RuleElement])]
|
||||||
} deriving (Show, Eq)
|
} deriving (Show, Eq)
|
||||||
|
|
||||||
@ -195,7 +193,9 @@ withFactsAndRules facts rules =
|
|||||||
toConstraintElement :: RuleElement -> ConstraintElement
|
toConstraintElement :: RuleElement -> ConstraintElement
|
||||||
toConstraintElement (RuleElementConstant constant) = ConstraintElementConstant constant
|
toConstraintElement (RuleElementConstant constant) = ConstraintElementConstant constant
|
||||||
toConstraintElement (RuleElementVariable varName) = ConstraintElementIndex (varIndex varName)
|
toConstraintElement (RuleElementVariable varName) = ConstraintElementIndex (varIndex varName)
|
||||||
|
extractVarName :: RuleElement -> Text
|
||||||
|
extractVarName (RuleElementVariable varName) = varName
|
||||||
|
extractVarName (RuleElementConstant constant) = throw $ UnexpectedConstantException constant
|
||||||
-- toBodyConstraint :: (Relation, [RuleElement]) -> BodyConstraint
|
-- toBodyConstraint :: (Relation, [RuleElement]) -> BodyConstraint
|
||||||
-- toBodyConstraint (subRelation, ruleElements) =
|
-- toBodyConstraint (subRelation, ruleElements) =
|
||||||
-- BodyConstraint {
|
-- BodyConstraint {
|
||||||
@ -203,7 +203,7 @@ withFactsAndRules facts rules =
|
|||||||
-- _elements = toConstraintElement <$> ruleElements
|
-- _elements = toConstraintElement <$> ruleElements
|
||||||
-- }
|
-- }
|
||||||
newRule = RelationRule {
|
newRule = RelationRule {
|
||||||
headVariables = variables',
|
headVariables = extractVarName <$> variables',
|
||||||
body = fromBodyConstraint <$> (_bodyConstraints context'')
|
body = fromBodyConstraint <$> (_bodyConstraints context'')
|
||||||
} where
|
} where
|
||||||
fromBodyConstraint :: BodyConstraint -> (Relation, [RuleElement])
|
fromBodyConstraint :: BodyConstraint -> (Relation, [RuleElement])
|
||||||
@ -237,7 +237,8 @@ data NaiveDatabaseException
|
|||||||
NonRuleException Text Statement |
|
NonRuleException Text Statement |
|
||||||
NonQueryException Text Statement |
|
NonQueryException Text Statement |
|
||||||
BadArityException Text Int |
|
BadArityException Text Int |
|
||||||
VariableLookupException Text [RuleElement]
|
VariableLookupException Text [RuleElement] |
|
||||||
|
UnexpectedConstantException Constant
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
instance Exception NaiveDatabaseException
|
instance Exception NaiveDatabaseException
|
||||||
@ -53,11 +53,7 @@ spec = do
|
|||||||
],
|
],
|
||||||
_rules = [
|
_rules = [
|
||||||
RelationRule {
|
RelationRule {
|
||||||
headVariables = [
|
headVariables = [ "X", "Y", "Z" ],
|
||||||
RuleElementVariable "X",
|
|
||||||
RuleElementVariable "Y",
|
|
||||||
RuleElementVariable "Z"
|
|
||||||
],
|
|
||||||
body = [
|
body = [
|
||||||
(
|
(
|
||||||
Relation {
|
Relation {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user