{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.RDF.ProofContext ( rulesetRDF
, rulesetRDFS
, rulesetRDFD) where
import Swish.Datatype (typeMkCanonicalForm)
import Swish.Namespace (Namespace, makeNSScopedName)
import Swish.QName (LName)
import Swish.Ruleset (makeRuleset)
import Swish.VarBinding (VarBindingModify(..))
import Swish.VarBinding (applyVarBinding, addVarBinding, makeVarFilterModify, varFilterDisjunction)
import Swish.RDF.BuiltIn.Datatypes (findRDFDatatype)
import Swish.RDF.Proof (makeRdfSubgraphEntailmentRule
, makeRdfSimpleEntailmentRule )
import Swish.RDF.Ruleset
( RDFFormula, RDFRule, RDFRuleset
, makeRDFFormula
, makeN3ClosureRule
, makeN3ClosureSimpleRule
, makeN3ClosureModifyRule
, makeN3ClosureAllocatorRule
, makeNodeAllocTo )
import Swish.RDF.VarBinding
( RDFVarBinding
, RDFVarBindingModify
, RDFVarBindingFilter
, rdfVarBindingUriRef, rdfVarBindingBlank
, rdfVarBindingLiteral
, rdfVarBindingUntypedLiteral
, rdfVarBindingXMLLiteral, rdfVarBindingDatatyped
, rdfVarBindingMemberProp
)
import Swish.RDF.Graph (RDFLabel(..), isUri)
import Swish.RDF.Vocabulary
( namespaceRDFD
, scopeRDF
, scopeRDFS
, scopeRDFD
)
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Data.Monoid (Monoid(..))
#endif
import qualified Data.Text.Lazy.Builder as B
makeFormula :: Namespace -> LName -> B.Builder -> RDFFormula
makeFormula = makeRDFFormula
requireAny :: [RDFVarBindingFilter] -> RDFVarBindingFilter
requireAny = varFilterDisjunction
isLiteralV :: String -> RDFVarBindingFilter
isLiteralV = rdfVarBindingLiteral . Var
isUntypedLitV :: String -> RDFVarBindingFilter
isUntypedLitV = rdfVarBindingUntypedLiteral . Var
isXMLLitV :: String -> RDFVarBindingFilter
isXMLLitV = rdfVarBindingXMLLiteral . Var
isUriRefV :: String -> RDFVarBindingFilter
isUriRefV = rdfVarBindingUriRef . Var
isBlankV :: String -> RDFVarBindingFilter
isBlankV = rdfVarBindingBlank . Var
isDatatypedV :: String -> String -> RDFVarBindingFilter
isDatatypedV d l = rdfVarBindingDatatyped (Var d) (Var l)
isMemberPropV :: String -> RDFVarBindingFilter
isMemberPropV = rdfVarBindingMemberProp . Var
allocateTo :: String -> String -> [RDFLabel] -> RDFVarBindingModify
allocateTo bv av = makeNodeAllocTo (Var bv) (Var av)
valueSame :: String -> String -> String -> String -> RDFVarBindingModify
valueSame val1 typ1 val2 typ2 =
sameDatatypedValue (Var val1) (Var typ1) (Var val2) (Var typ2)
sameDatatypedValue ::
RDFLabel -> RDFLabel -> RDFLabel -> RDFLabel -> RDFVarBindingModify
sameDatatypedValue val1 typ1 val2 typ2 = VarBindingModify
{ vbmName = makeNSScopedName namespaceRDFD "sameValue"
, vbmApply = sameDatatypedValueApplyAll val1 typ1 val2 typ2
, vbmVocab = [val1,typ1,val2,typ2]
, vbmUsage = [[val2]]
}
sameDatatypedValueApplyAll ::
RDFLabel -> RDFLabel -> RDFLabel -> RDFLabel
-> [RDFVarBinding]
-> [RDFVarBinding]
sameDatatypedValueApplyAll val1 typ1 val2 typ2 =
concatMap (sameDatatypedValueApply val1 typ1 val2 typ2)
sameDatatypedValueApply ::
RDFLabel -> RDFLabel -> RDFLabel -> RDFLabel
-> RDFVarBinding
-> [RDFVarBinding]
sameDatatypedValueApply val1 typ1 val2 typ2 vbind =
result
where
v1 = applyVarBinding vbind val1
t1 = applyVarBinding vbind typ1
t2 = applyVarBinding vbind typ2
sametype = getCanonical v1 t1 t2
result =
if isUri t1 && isUri t2 then
if t1 == t2 then
case sametype of
Just st -> [addVarBinding val2 st vbind]
_ -> []
else
error "subtype conversions not yet defined"
else
[]
getCanonical :: RDFLabel -> RDFLabel -> RDFLabel -> Maybe RDFLabel
getCanonical (TypedLit v dt) (Res dqn1) (Res dqn2) =
if dt == dqn1
then case findRDFDatatype dqn1 of
Just dt1 -> flip TypedLit dqn2 `fmap` typeMkCanonicalForm dt1 v
_ -> Nothing
else Nothing
getCanonical _ _ _ = Nothing
rdfa1 :: RDFFormula
rdfa1 = makeFormula scopeRDF "a1" "rdf:type rdf:type rdf:Property ."
rdfa2 :: RDFFormula
rdfa2 = makeFormula scopeRDF "a2" "rdf:subject rdf:type rdf:Property ."
rdfa3 :: RDFFormula
rdfa3 = makeFormula scopeRDF "a3" "rdf:predicate rdf:type rdf:Property ."
rdfa4 :: RDFFormula
rdfa4 = makeFormula scopeRDF "a4" "rdf:object rdf:type rdf:Property ."
rdfa5 :: RDFFormula
rdfa5 = makeFormula scopeRDF "a5" "rdf:first rdf:type rdf:Property ."
rdfa6 :: RDFFormula
rdfa6 = makeFormula scopeRDF "a6" "rdf:rest rdf:type rdf:Property ."
rdfa7 :: RDFFormula
rdfa7 = makeFormula scopeRDF "a7" "rdf:value rdf:type rdf:Property ."
rdfa8 :: RDFFormula
rdfa8 = makeFormula scopeRDF "a8" "rdf:nil rdf:type rdf:List ."
axiomsRDF :: [RDFFormula]
axiomsRDF =
[ rdfa1, rdfa2, rdfa3, rdfa4, rdfa5
, rdfa6, rdfa7, rdfa8
]
rdfsub :: RDFRule
rdfsub = makeRdfSubgraphEntailmentRule (makeNSScopedName scopeRDF "sub")
rdfse :: RDFRule
rdfse = makeRdfSimpleEntailmentRule (makeNSScopedName scopeRDF "se")
rdflg :: RDFRule
rdflg = makeN3ClosureAllocatorRule scopeRDF "lg"
"?x ?a ?l . "
"?x ?a ?b . ?b rdf:_allocatedTo ?l ."
(makeVarFilterModify $ isLiteralV "l")
(allocateTo "b" "l")
rdfgl :: RDFRule
rdfgl = makeN3ClosureSimpleRule scopeRDF "gl"
"?x ?a ?l . ?b rdf:_allocatedTo ?l . "
"?x ?a ?b ."
rdfr1 :: RDFRule
rdfr1 = makeN3ClosureSimpleRule scopeRDF "r1"
"?x ?a ?y ."
"?a rdf:type rdf:Property ."
rdfr2 :: RDFRule
rdfr2 = makeN3ClosureRule scopeRDF "r2"
"?x ?a ?b . ?b rdf:_allocatedTo ?l . "
"?b rdf:type rdf:XMLLiteral ."
(makeVarFilterModify $ isXMLLitV "l")
rdfcp1 :: RDFRule
rdfcp1 = makeN3ClosureRule scopeRDF "cp1"
"?x ?c ?y . "
"?c rdf:type rdf:Property ."
(makeVarFilterModify $ isMemberPropV "c")
rdfcp2 :: RDFRule
rdfcp2 = makeN3ClosureRule scopeRDF "cp2"
"?c ?p ?y . "
"?c rdf:type rdf:Property ."
(makeVarFilterModify $ isMemberPropV "c")
rdfcp3 :: RDFRule
rdfcp3 = makeN3ClosureRule scopeRDF "cp3"
"?x ?p ?c . "
"?c rdf:type rdf:Property ."
(makeVarFilterModify $ isMemberPropV "c")
rulesRDF :: [RDFRule]
rulesRDF =
[ rdfsub, rdfse
, rdflg, rdfgl
, rdfr1, rdfr2
, rdfcp1, rdfcp2, rdfcp3
]
rulesetRDF :: RDFRuleset
rulesetRDF = makeRuleset scopeRDF axiomsRDF rulesRDF
rdfsa01 :: RDFFormula
rdfsa01 = makeFormula scopeRDFS "a01"
"rdf:type rdfs:domain rdfs:Resource ."
rdfsa02 :: RDFFormula
rdfsa02 = makeFormula scopeRDFS "a02"
"rdf:type rdfs:range rdfs:Class ."
rdfsa03 :: RDFFormula
rdfsa03 = makeFormula scopeRDFS "a03"
"rdfs:domain rdfs:domain rdf:Property ."
rdfsa04 :: RDFFormula
rdfsa04 = makeFormula scopeRDFS "a04"
"rdfs:domain rdfs:range rdfs:Class ."
rdfsa05 :: RDFFormula
rdfsa05 = makeFormula scopeRDFS "a05"
"rdfs:range rdfs:domain rdf:Property ."
rdfsa06 :: RDFFormula
rdfsa06 = makeFormula scopeRDFS "a06"
"rdfs:range rdfs:range rdfs:Class ."
rdfsa07 :: RDFFormula
rdfsa07 = makeFormula scopeRDFS "a07"
"rdfs:subPropertyOf rdfs:domain rdf:Property ."
rdfsa08 :: RDFFormula
rdfsa08 = makeFormula scopeRDFS "a08"
"rdfs:subPropertyOf rdfs:range rdf:Property ."
rdfsa09 :: RDFFormula
rdfsa09 = makeFormula scopeRDFS "a09"
"rdfs:subClassOf rdfs:domain rdfs:Class ."
rdfsa10 :: RDFFormula
rdfsa10 = makeFormula scopeRDFS "a10"
"rdfs:subClassOf rdfs:range rdfs:Class ."
rdfsa11 :: RDFFormula
rdfsa11 = makeFormula scopeRDFS "a11"
"rdf:subject rdfs:domain rdf:Statement ."
rdfsa12 :: RDFFormula
rdfsa12 = makeFormula scopeRDFS "a12"
"rdf:subject rdfs:range rdfs:Resource ."
rdfsa13 :: RDFFormula
rdfsa13 = makeFormula scopeRDFS "a13"
"rdf:predicate rdfs:domain rdf:Statement ."
rdfsa14 :: RDFFormula
rdfsa14 = makeFormula scopeRDFS "a14"
"rdf:predicate rdfs:range rdfs:Resource ."
rdfsa15 :: RDFFormula
rdfsa15 = makeFormula scopeRDFS "a15"
"rdf:object rdfs:domain rdf:Statement ."
rdfsa16 :: RDFFormula
rdfsa16 = makeFormula scopeRDFS "a16"
"rdf:object rdfs:range rdfs:Resource ."
rdfsa17 :: RDFFormula
rdfsa17 = makeFormula scopeRDFS "a17"
"rdfs:member rdfs:domain rdfs:Resource ."
rdfsa18 :: RDFFormula
rdfsa18 = makeFormula scopeRDFS "a18"
"rdfs:member rdfs:range rdfs:Resource ."
rdfsa19 :: RDFFormula
rdfsa19 = makeFormula scopeRDFS "a19"
"rdf:first rdfs:domain rdf:List ."
rdfsa20 :: RDFFormula
rdfsa20 = makeFormula scopeRDFS "a20"
"rdf:first rdfs:range rdfs:Resource ."
rdfsa21 :: RDFFormula
rdfsa21 = makeFormula scopeRDFS "a21"
"rdf:rest rdfs:domain rdf:List ."
rdfsa22 :: RDFFormula
rdfsa22 = makeFormula scopeRDFS "a22"
"rdf:rest rdfs:range rdf:List ."
rdfsa23 :: RDFFormula
rdfsa23 = makeFormula scopeRDFS "a23"
"rdfs:seeAlso rdfs:domain rdfs:Resource ."
rdfsa24 :: RDFFormula
rdfsa24 = makeFormula scopeRDFS "a24"
"rdfs:seeAlso rdfs:range rdfs:Resource ."
rdfsa25 :: RDFFormula
rdfsa25 = makeFormula scopeRDFS "a25"
"rdfs:isDefinedBy rdfs:domain rdfs:Resource ."
rdfsa26 :: RDFFormula
rdfsa26 = makeFormula scopeRDFS "a26"
"rdfs:isDefinedBy rdfs:range rdfs:Resource ."
rdfsa27 :: RDFFormula
rdfsa27 = makeFormula scopeRDFS "a27"
"rdfs:isDefinedBy rdfs:subPropertyOf rdfs:seeAlso ."
rdfsa28 :: RDFFormula
rdfsa28 = makeFormula scopeRDFS "a28"
"rdfs:comment rdfs:domain rdfs:Resource ."
rdfsa29 :: RDFFormula
rdfsa29 = makeFormula scopeRDFS "a29"
"rdfs:comment rdfs:range rdfs:Literal ."
rdfsa30 :: RDFFormula
rdfsa30 = makeFormula scopeRDFS "a30"
"rdfs:label rdfs:domain rdfs:Resource ."
rdfsa31 :: RDFFormula
rdfsa31 = makeFormula scopeRDFS "a31"
"rdfs:label rdfs:range rdfs:Literal ."
rdfsa32 :: RDFFormula
rdfsa32 = makeFormula scopeRDFS "a32"
"rdf:value rdfs:domain rdfs:Resource ."
rdfsa33 :: RDFFormula
rdfsa33 = makeFormula scopeRDFS "a33"
"rdf:value rdfs:range rdfs:Resource ."
rdfsa34 :: RDFFormula
rdfsa34 = makeFormula scopeRDFS "a34"
"rdf:Alt rdfs:subClassOf rdfs:Container ."
rdfsa35 :: RDFFormula
rdfsa35 = makeFormula scopeRDFS "a35"
"rdf:Bag rdfs:subClassOf rdfs:Container ."
rdfsa36 :: RDFFormula
rdfsa36 = makeFormula scopeRDFS "a36"
"rdf:Seq rdfs:subClassOf rdfs:Container ."
rdfsa37 :: RDFFormula
rdfsa37 = makeFormula scopeRDFS "a37"
"rdfs:ContainerMembershipProperty rdfs:subClassOf rdf:Property ."
rdfsa38 :: RDFFormula
rdfsa38 = makeFormula scopeRDFS "a38"
"rdf:XMLLiteral rdf:type rdfs:Datatype ."
rdfsa39 :: RDFFormula
rdfsa39 = makeFormula scopeRDFS "a39"
"rdf:XMLLiteral rdfs:subClassOf rdfs:Literal ."
rdfsa40 :: RDFFormula
rdfsa40 = makeFormula scopeRDFS "a40"
"rdfs:Datatype rdfs:subClassOf rdfs:Class ."
axiomsRDFS :: [RDFFormula]
axiomsRDFS =
[ rdfsa01, rdfsa02, rdfsa03, rdfsa04
, rdfsa05, rdfsa06, rdfsa07, rdfsa08, rdfsa09
, rdfsa10, rdfsa11, rdfsa12, rdfsa13, rdfsa14
, rdfsa15, rdfsa16, rdfsa17, rdfsa18, rdfsa19
, rdfsa20, rdfsa21, rdfsa22, rdfsa23, rdfsa24
, rdfsa25, rdfsa26, rdfsa27, rdfsa28, rdfsa29
, rdfsa30, rdfsa31, rdfsa32, rdfsa33, rdfsa34
, rdfsa35, rdfsa36, rdfsa37, rdfsa38, rdfsa39
, rdfsa40
]
rdfsr1 :: RDFRule
rdfsr1 = makeN3ClosureRule scopeRDFS "r1"
"?x ?a ?b . ?b rdf:_allocatedTo ?l . "
"?b rdf:type rdfs:Literal ."
(makeVarFilterModify $ isUntypedLitV "l" )
rdfsr2 :: RDFRule
rdfsr2 = makeN3ClosureSimpleRule scopeRDFS "r2"
"?x ?a ?y . ?a rdfs:domain ?z ."
"?x rdf:type ?z ."
rdfsr3 :: RDFRule
rdfsr3 = makeN3ClosureRule scopeRDFS "r3"
"?u ?a ?v . ?a rdfs:range ?z ."
"?v rdf:type ?z ."
(makeVarFilterModify $ requireAny [isUriRefV "v",isBlankV "v"])
rdfsr4a :: RDFRule
rdfsr4a = makeN3ClosureSimpleRule scopeRDFS "r4a"
"?x ?a ?y ."
"?x rdf:type rdfs:Resource ."
rdfsr4b :: RDFRule
rdfsr4b = makeN3ClosureRule scopeRDFS "r4b"
"?x ?a ?u ."
"?u rdf:type rdfs:Resource ."
(makeVarFilterModify $ requireAny [isUriRefV "u",isBlankV "u"])
rdfsr5 :: RDFRule
rdfsr5 = makeN3ClosureSimpleRule scopeRDFS "r5"
"?a rdfs:subPropertyOf ?b . ?b rdfs:subPropertyOf ?c ."
"?a rdfs:subPropertyOf ?c ."
rdfsr6 :: RDFRule
rdfsr6 = makeN3ClosureSimpleRule scopeRDFS "r6"
"?x rdf:type rdf:Property ."
"?x rdfs:subPropertyOf ?x ."
rdfsr7 :: RDFRule
rdfsr7 = makeN3ClosureSimpleRule scopeRDFS "r7"
"?x ?a ?y . ?a rdfs:subPropertyOf ?b ."
"?x ?b ?y ."
rdfsr8 :: RDFRule
rdfsr8 = makeN3ClosureSimpleRule scopeRDFS "r8"
"?x rdf:type rdfs:Class ."
"?x rdfs:subClassOf rdfs:Resource ."
rdfsr9 :: RDFRule
rdfsr9 = makeN3ClosureSimpleRule scopeRDFS "r9"
"?x rdfs:subClassOf ?y . ?a rdf:type ?x ."
"?a rdf:type ?y ."
rdfsr10 :: RDFRule
rdfsr10 = makeN3ClosureSimpleRule scopeRDFS "r10"
"?x rdf:type rdfs:Class ."
"?x rdfs:subClassOf ?x ."
rdfsr11 :: RDFRule
rdfsr11 = makeN3ClosureSimpleRule scopeRDFS "r11"
"?x rdfs:subClassOf ?y . ?y rdfs:subClassOf ?z ."
"?x rdfs:subClassOf ?z ."
rdfsr12 :: RDFRule
rdfsr12 = makeN3ClosureSimpleRule scopeRDFS "r12"
"?x rdf:type rdfs:ContainerMembershipProperty ."
"?x rdfs:subPropertyOf rdfs:member ."
rdfsr13 :: RDFRule
rdfsr13 = makeN3ClosureSimpleRule scopeRDFS "r13"
"?x rdf:type rdfs:Datatype ."
"?x rdfs:subClassOf rdfs:Literal ."
rdfscp11 :: RDFRule
rdfscp11 = makeN3ClosureRule scopeRDFS "cp11"
"?x ?c ?y . "
"?c rdf:type rdfs:ContainerMembershipProperty ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp12 :: RDFRule
rdfscp12 = makeN3ClosureRule scopeRDFS "cp12"
"?c ?p ?y . "
"?c rdf:type rdfs:ContainerMembershipProperty ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp13 :: RDFRule
rdfscp13 = makeN3ClosureRule scopeRDFS "cp13"
"?x ?p ?c . "
"?c rdf:type rdfs:ContainerMembershipProperty ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp21 :: RDFRule
rdfscp21 = makeN3ClosureRule scopeRDFS "cp21"
"?x ?c ?y . "
"?c rdfs:domain rdfs:Resource ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp22 :: RDFRule
rdfscp22 = makeN3ClosureRule scopeRDFS "cp22"
"?c ?p ?y . "
"?c rdfs:domain rdfs:Resource ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp23 :: RDFRule
rdfscp23 = makeN3ClosureRule scopeRDFS "cp23"
"?x ?p ?c . "
"?c rdfs:domain rdfs:Resource ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp31 :: RDFRule
rdfscp31 = makeN3ClosureRule scopeRDFS "cp31"
"?x ?c ?y . "
"?c rdfs:range rdfs:Resource ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp32 :: RDFRule
rdfscp32 = makeN3ClosureRule scopeRDFS "cp32"
"?c ?p ?y . "
"?c rdfs:range rdfs:Resource ."
(makeVarFilterModify $ isMemberPropV "c")
rdfscp33 :: RDFRule
rdfscp33 = makeN3ClosureRule scopeRDFS "cp33"
"?x ?p ?c . "
"?c rdfs:range rdfs:Resource ."
(makeVarFilterModify $ isMemberPropV "c")
rulesRDFS :: [RDFRule]
rulesRDFS =
[ rdfsr1, rdfsr2, rdfsr3, rdfsr4a, rdfsr4b
, rdfsr5, rdfsr6, rdfsr7, rdfsr8, rdfsr9
, rdfsr10, rdfsr11, rdfsr12, rdfsr13
, rdfscp11, rdfscp12, rdfscp13
, rdfscp21, rdfscp22, rdfscp23
, rdfscp31, rdfscp32, rdfscp33
]
rulesetRDFS :: RDFRuleset
rulesetRDFS = makeRuleset scopeRDFS axiomsRDFS rulesRDFS
axiomsRDFD :: [RDFFormula]
axiomsRDFD =
[
]
rdfdr1 :: RDFRule
rdfdr1 = makeN3ClosureRule scopeRDFD "r1"
"?d rdf:type rdfs:Datatype . ?a ?p ?l . ?b rdf:_allocatedTo ?l . "
"?b rdf:type ?d ."
(makeVarFilterModify $ isDatatypedV "d" "l")
rdfdr2 :: RDFRule
rdfdr2 = makeN3ClosureRule scopeRDFD "r2"
"?d rdf:type rdfs:Datatype . ?a ?p ?s ."
"?a ?p ?t ."
(valueSame "s" "d" "t" "d")
rdfdr3 :: RDFRule
rdfdr3 = makeN3ClosureModifyRule scopeRDFD "r3"
( "?d rdf:type rdfs:Datatype . ?e rdf:type rdfs:Datatype . " `mappend`
"?a ?p ?s ." )
"?a ?p ?t ."
(makeVarFilterModify $ isDatatypedV "s" "d")
(valueSame "s" "d" "t" "e")
rulesRDFD :: [RDFRule]
rulesRDFD =
[ rdfdr1, rdfdr2, rdfdr3
]
rulesetRDFD :: RDFRuleset
rulesetRDFD = makeRuleset scopeRDFD axiomsRDFD rulesRDFD