{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

--------------------------------------------------------------------------------
--  See end of this file for licence information.
--------------------------------------------------------------------------------
-- |
--  Module      :  Integer
--  Copyright   :  (c) 2003, Graham Klyne, 2009 Vasili I Galchin, 2011, 2012, 2014, 2018, 2022 Douglas Burke
--  License     :  GPL V2
--
--  Maintainer  :  Douglas Burke
--  Stability   :  experimental
--  Portability :  CPP, OverloadedStrings
--
--  This module defines the structures used to represent and
--  manipulate RDF @xsd:integer@ datatyped literals.
--
--------------------------------------------------------------------------------

module Swish.RDF.Datatype.XSD.Integer
    ( rdfDatatypeXsdInteger
    , rdfDatatypeValXsdInteger
    , typeNameXsdInteger, namespaceXsdInteger
    , axiomsXsdInteger, rulesXsdInteger
    )
where

import Swish.Datatype
    ( Datatype(..)
    , DatatypeVal(..)
    , DatatypeRel(..), DatatypeRelPr
    , altArgs
    , UnaryFnTable,    unaryFnApp
    , BinaryFnTable,   binaryFnApp
    , BinMaybeFnTable, binMaybeFnApp
    , DatatypeMod(..) 
    , makeVmod11inv, makeVmod11
    , makeVmod21inv, makeVmod21
    , makeVmod20
    , makeVmod22
    )
import Swish.Namespace (Namespace, ScopedName)
import Swish.Namespace (namespaceToBuilder, makeNSScopedName)
import Swish.QName (LName)
import Swish.Ruleset (makeRuleset)

import Swish.RDF.Datatype (RDFDatatype, RDFDatatypeVal, RDFDatatypeMod)
import Swish.RDF.Datatype (makeRdfDtOpenVarBindingModifiers)
import Swish.RDF.Datatype.XSD.MapInteger (mapXsdInteger)

import Swish.RDF.Ruleset (RDFFormula, RDFRule, RDFRuleset)
import Swish.RDF.Ruleset (makeRDFGraphFromN3Builder, makeRDFFormula)

import Swish.RDF.ClassRestrictionRule (makeRDFDatatypeRestrictionRules)

import Swish.RDF.Vocabulary
    ( namespaceRDF
    , namespaceRDFS
    , namespaceRDFD
    , namespaceXSD
    , namespaceXsdType
    )

import Data.Maybe (maybeToList)

#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Data.Monoid (Monoid(..))
#endif

import qualified Data.Text.Lazy.Builder as B

------------------------------------------------------------
--  Misc values
------------------------------------------------------------

--  Local name for Integer datatype
nameXsdInteger :: LName
nameXsdInteger :: LName
nameXsdInteger = LName
"integer"

-- |Type name for @xsd:integer@ datatype.
typeNameXsdInteger :: ScopedName
typeNameXsdInteger :: ScopedName
typeNameXsdInteger  = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXSD LName
nameXsdInteger

-- |Namespace for @xsd:integer@ datatype functions.
namespaceXsdInteger :: Namespace
namespaceXsdInteger :: Namespace
namespaceXsdInteger = LName -> Namespace
namespaceXsdType LName
nameXsdInteger

-- | The RDFDatatype value for @xsd:integer@.
rdfDatatypeXsdInteger :: RDFDatatype
rdfDatatypeXsdInteger :: RDFDatatype
rdfDatatypeXsdInteger = forall ex lb vn vt. DatatypeVal ex vt lb vn -> Datatype ex lb vn
Datatype RDFDatatypeVal Integer
rdfDatatypeValXsdInteger

--  Integer power (exponentiation) function
--  returns Nothing if exponent is negative.
--
intPower :: Integer -> Integer -> Maybe Integer
intPower :: Integer -> Integer -> Maybe Integer
intPower Integer
a Integer
b = if Integer
b forall a. Ord a => a -> a -> Bool
< Integer
0 then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just (forall {a} {a}. (Integral a, Num a) => a -> a -> a
intPower1 Integer
a Integer
b)
    where
        intPower1 :: a -> a -> a
intPower1 a
x a
y
            | a
q forall a. Eq a => a -> a -> Bool
== a
1           = a
atopsqforall a. Num a => a -> a -> a
*a
x
            | a
p forall a. Eq a => a -> a -> Bool
== a
0           = a
1
            | Bool
otherwise        = a
atopsq
            where
                (a
p,a
q)  = a
y forall a. Integral a => a -> a -> (a, a)
`divMod` a
2
                atop :: a
atop   = a -> a -> a
intPower1 a
x a
p
                atopsq :: a
atopsq = a
atopforall a. Num a => a -> a -> a
*a
atop

------------------------------------------------------------
--  Implmentation of RDFDatatypeVal for xsd:integer
------------------------------------------------------------

-- |Define Datatype value for @xsd:integer@.
--
--  Members of this datatype are positive or negative integer values.
--
--  The lexical form consists of an optional @+@ or @-@
--  followed by a sequence of decimal digits.
--
--  The canonical lexical form has leading zeros and @+@ sign removed.
--
rdfDatatypeValXsdInteger :: RDFDatatypeVal Integer
rdfDatatypeValXsdInteger :: RDFDatatypeVal Integer
rdfDatatypeValXsdInteger = DatatypeVal
    { tvalName :: ScopedName
tvalName      = ScopedName
typeNameXsdInteger
    , tvalRules :: Ruleset RDFGraph
tvalRules     = Ruleset RDFGraph
rdfRulesetXsdInteger  -- Ruleset RDFGraph
    , tvalMkRules :: RDFGraph -> [Rule RDFGraph]
tvalMkRules   = forall vt. RDFDatatypeVal vt -> RDFGraph -> [Rule RDFGraph]
makeRDFDatatypeRestrictionRules RDFDatatypeVal Integer
rdfDatatypeValXsdInteger
                                            -- RDFGraph -> [RDFRules]
    , tvalMkMods :: [OpenVarBindingModify RDFLabel RDFLabel]
tvalMkMods    = forall vt.
RDFDatatypeVal vt -> [OpenVarBindingModify RDFLabel RDFLabel]
makeRdfDtOpenVarBindingModifiers RDFDatatypeVal Integer
rdfDatatypeValXsdInteger
    , tvalMap :: DatatypeMap Integer
tvalMap       = DatatypeMap Integer
mapXsdInteger         -- DatatypeMap Integer
    , tvalRel :: [DatatypeRel Integer]
tvalRel       = [DatatypeRel Integer]
relXsdInteger         -- [DatatypeRel Integer]
    , tvalMod :: [DatatypeMod Integer RDFLabel RDFLabel]
tvalMod       = [DatatypeMod Integer RDFLabel RDFLabel]
modXsdInteger         -- [DatatypeMod Integer]
    }

-- |relXsdInteger contains arithmetic and other relations for xsd:Integer values.
--
--  The functions are inspired by those defined by CWM as math: properties
--  (<http://www.w3.org/2000/10/swap/doc/CwmBuiltins.html>).
--
relXsdInteger :: [DatatypeRel Integer]
relXsdInteger :: [DatatypeRel Integer]
relXsdInteger =
    [ DatatypeRel Integer
relXsdIntegerAbs
    , DatatypeRel Integer
relXsdIntegerNeg
    , DatatypeRel Integer
relXsdIntegerSum
    , DatatypeRel Integer
relXsdIntegerDiff
    , DatatypeRel Integer
relXsdIntegerProd
    , DatatypeRel Integer
relXsdIntegerDivMod
    , DatatypeRel Integer
relXsdIntegerPower
    , DatatypeRel Integer
relXsdIntegerEq
    , DatatypeRel Integer
relXsdIntegerNe
    , DatatypeRel Integer
relXsdIntegerLt
    , DatatypeRel Integer
relXsdIntegerLe
    , DatatypeRel Integer
relXsdIntegerGt
    , DatatypeRel Integer
relXsdIntegerGe
    ]

mkIntRel2 ::
    LName -> DatatypeRelPr Integer -> UnaryFnTable Integer
    -> DatatypeRel Integer
mkIntRel2 :: LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
nam DatatypeRelPr Integer
pr UnaryFnTable Integer
fns = DatatypeRel
    { dtRelName :: ScopedName
dtRelName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
nam
    , dtRelFunc :: DatatypeRelFn Integer
dtRelFunc = forall vt b.
Eq vt =>
DatatypeRelPr vt
-> [(vt -> Bool, [b])]
-> ((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt])
-> DatatypeRelFn vt
altArgs DatatypeRelPr Integer
pr UnaryFnTable Integer
fns forall a. UnaryFnApply a
unaryFnApp
    }

mkIntRel3 ::
    LName -> DatatypeRelPr Integer -> BinaryFnTable Integer
    -> DatatypeRel Integer
mkIntRel3 :: LName
-> DatatypeRelPr Integer
-> BinaryFnTable Integer
-> DatatypeRel Integer
mkIntRel3 LName
nam DatatypeRelPr Integer
pr BinaryFnTable Integer
fns = DatatypeRel
    { dtRelName :: ScopedName
dtRelName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
nam
    , dtRelFunc :: DatatypeRelFn Integer
dtRelFunc = forall vt b.
Eq vt =>
DatatypeRelPr vt
-> [(vt -> Bool, [b])]
-> ((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt])
-> DatatypeRelFn vt
altArgs DatatypeRelPr Integer
pr BinaryFnTable Integer
fns forall a. BinaryFnApply a
binaryFnApp
    }

mkIntRel3maybe ::
    LName -> DatatypeRelPr Integer -> BinMaybeFnTable Integer
    -> DatatypeRel Integer
mkIntRel3maybe :: LName
-> DatatypeRelPr Integer
-> BinMaybeFnTable Integer
-> DatatypeRel Integer
mkIntRel3maybe LName
nam DatatypeRelPr Integer
pr BinMaybeFnTable Integer
fns = DatatypeRel
    { dtRelName :: ScopedName
dtRelName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
nam
    , dtRelFunc :: DatatypeRelFn Integer
dtRelFunc = forall vt b.
Eq vt =>
DatatypeRelPr vt
-> [(vt -> Bool, [b])]
-> ((vt -> Bool) -> b -> [Maybe vt] -> Maybe [vt])
-> DatatypeRelFn vt
altArgs DatatypeRelPr Integer
pr BinMaybeFnTable Integer
fns forall a. BinMaybeFnApply a
binMaybeFnApp
    }

relXsdIntegerAbs :: DatatypeRel Integer
relXsdIntegerAbs :: DatatypeRel Integer
relXsdIntegerAbs = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"abs" (forall a b. a -> b -> a
const Bool
True)
    [ ( (forall a. Ord a => a -> a -> Bool
>= Integer
0),     [ (forall a. Num a => a -> a
abs,Int
1) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ (forall a. a -> a
id,Int
0), (forall a. Num a => a -> a
negate,Int
0) ] )
    ]

relXsdIntegerNeg :: DatatypeRel Integer
relXsdIntegerNeg :: DatatypeRel Integer
relXsdIntegerNeg = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"neg" (forall a b. a -> b -> a
const Bool
True)
    [ ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Num a => a -> a
negate,Int
1) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Num a => a -> a
negate,Int
0) ] )
    ]

relXsdIntegerSum :: DatatypeRel Integer
relXsdIntegerSum :: DatatypeRel Integer
relXsdIntegerSum = LName
-> DatatypeRelPr Integer
-> BinaryFnTable Integer
-> DatatypeRel Integer
mkIntRel3 LName
"sum" (forall a b. a -> b -> a
const Bool
True)
    [ ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Num a => a -> a -> a
(+),Int
1,Int
2) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ ((-),Int
0,Int
2) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ ((-),Int
0,Int
1) ] )
    ]

relXsdIntegerDiff :: DatatypeRel Integer
relXsdIntegerDiff :: DatatypeRel Integer
relXsdIntegerDiff = LName
-> DatatypeRelPr Integer
-> BinaryFnTable Integer
-> DatatypeRel Integer
mkIntRel3 LName
"diff" (forall a b. a -> b -> a
const Bool
True)
    [ ( forall a b. a -> b -> a
const Bool
True, [ ((-),Int
1,Int
2) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Num a => a -> a -> a
(+),Int
0,Int
2) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ ((-),Int
1,Int
0) ] )
    ]

relXsdIntegerProd :: DatatypeRel Integer
relXsdIntegerProd :: DatatypeRel Integer
relXsdIntegerProd = LName
-> DatatypeRelPr Integer
-> BinaryFnTable Integer
-> DatatypeRel Integer
mkIntRel3 LName
"prod" (forall a b. a -> b -> a
const Bool
True)
    [ ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Num a => a -> a -> a
(*),Int
1,Int
2) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Integral a => a -> a -> a
div,Int
0,Int
2) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Integral a => a -> a -> a
div,Int
0,Int
1) ] )
    ]

relXsdIntegerDivMod :: DatatypeRel Integer
relXsdIntegerDivMod :: DatatypeRel Integer
relXsdIntegerDivMod = LName
-> DatatypeRelPr Integer
-> BinaryFnTable Integer
-> DatatypeRel Integer
mkIntRel3 LName
"divmod" (forall a b. a -> b -> a
const Bool
True)
    [ ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Integral a => a -> a -> a
div,Int
2,Int
3) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ (forall a. Integral a => a -> a -> a
mod,Int
2,Int
3) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ ] )
    ]

--  Compose with function of two arguments
c2 :: (b -> c) -> (a -> d -> b) -> a -> d -> c
c2 :: forall b c a d. (b -> c) -> (a -> d -> b) -> a -> d -> c
c2 = forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)

relXsdIntegerPower :: DatatypeRel Integer
relXsdIntegerPower :: DatatypeRel Integer
relXsdIntegerPower = LName
-> DatatypeRelPr Integer
-> BinMaybeFnTable Integer
-> DatatypeRel Integer
mkIntRel3maybe LName
"power" (forall a b. a -> b -> a
const Bool
True)
    [ ( forall a b. a -> b -> a
const Bool
True, [ (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> [a] -> [a]
:[]) forall b c a d. (b -> c) -> (a -> d -> b) -> a -> d -> c
`c2` Integer -> Integer -> Maybe Integer
intPower,Int
1,Int
2) ] )
    , ( forall a b. a -> b -> a
const Bool
True, [ ] )
    , ( (forall a. Ord a => a -> a -> Bool
>= Integer
0),     [ ] )
    ]

liftL2 :: (a->a->Bool) -> ([a]->a) -> ([a]->a) -> [a] -> Bool
liftL2 :: forall a.
(a -> a -> Bool) -> ([a] -> a) -> ([a] -> a) -> [a] -> Bool
liftL2 a -> a -> Bool
p [a] -> a
i1 [a] -> a
i2 [a]
as = a -> a -> Bool
p ([a] -> a
i1 [a]
as) ([a] -> a
i2 [a]
as)

lcomp :: (a->a->Bool) -> [a] -> Bool
lcomp :: forall a. (a -> a -> Bool) -> [a] -> Bool
lcomp a -> a -> Bool
p = forall a.
(a -> a -> Bool) -> ([a] -> a) -> ([a] -> a) -> [a] -> Bool
liftL2 a -> a -> Bool
p forall a. [a] -> a
head (forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> [a]
tail)

-- eq

relXsdIntegerEq :: DatatypeRel Integer
relXsdIntegerEq :: DatatypeRel Integer
relXsdIntegerEq = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"eq" (forall a. (a -> a -> Bool) -> [a] -> Bool
lcomp forall a. Eq a => a -> a -> Bool
(==))
    ( forall a. a -> [a]
repeat (forall a b. a -> b -> a
const Bool
True, []) )

-- ne

relXsdIntegerNe :: DatatypeRel Integer
relXsdIntegerNe :: DatatypeRel Integer
relXsdIntegerNe = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"ne" (forall a. (a -> a -> Bool) -> [a] -> Bool
lcomp forall a. Eq a => a -> a -> Bool
(/=))
    ( forall a. a -> [a]
repeat (forall a b. a -> b -> a
const Bool
True, []) )

-- lt

relXsdIntegerLt :: DatatypeRel Integer
relXsdIntegerLt :: DatatypeRel Integer
relXsdIntegerLt = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"lt" (forall a. (a -> a -> Bool) -> [a] -> Bool
lcomp forall a. Ord a => a -> a -> Bool
(<))
    ( forall a. a -> [a]
repeat (forall a b. a -> b -> a
const Bool
True, []) )

-- le

relXsdIntegerLe :: DatatypeRel Integer
relXsdIntegerLe :: DatatypeRel Integer
relXsdIntegerLe = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"le" (forall a. (a -> a -> Bool) -> [a] -> Bool
lcomp forall a. Ord a => a -> a -> Bool
(<=))
    ( forall a. a -> [a]
repeat (forall a b. a -> b -> a
const Bool
True, []) )

-- gt

relXsdIntegerGt :: DatatypeRel Integer
relXsdIntegerGt :: DatatypeRel Integer
relXsdIntegerGt = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"gt" (forall a. (a -> a -> Bool) -> [a] -> Bool
lcomp forall a. Ord a => a -> a -> Bool
(>))
    ( forall a. a -> [a]
repeat (forall a b. a -> b -> a
const Bool
True, []) )

-- ge

relXsdIntegerGe :: DatatypeRel Integer
relXsdIntegerGe :: DatatypeRel Integer
relXsdIntegerGe = LName
-> DatatypeRelPr Integer
-> UnaryFnTable Integer
-> DatatypeRel Integer
mkIntRel2 LName
"ge" (forall a. (a -> a -> Bool) -> [a] -> Bool
lcomp forall a. Ord a => a -> a -> Bool
(>=))
    ( forall a. a -> [a]
repeat (forall a b. a -> b -> a
const Bool
True, []) )

-- |modXsdInteger contains variable binding modifiers for xsd:Integer values.
--
--  The functions are selected from those defined by CWM as math:
--  properties
--  (<http://www.w3.org/2000/10/swap/doc/CwmBuiltins.html>).
--
modXsdInteger :: [RDFDatatypeMod Integer]
modXsdInteger :: [DatatypeMod Integer RDFLabel RDFLabel]
modXsdInteger =
    [ DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerAbs
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerNeg
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerSum
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerDiff
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerProd
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerDivMod
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerPower
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerEq
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerNe
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerLt
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerLe
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerGt
    , DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerGe
    ]

modXsdIntegerAbs :: RDFDatatypeMod Integer
modXsdIntegerAbs :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerAbs = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
"abs"
    , dmModf :: [ModifierFn Integer]
dmModf = [ forall {a}. (Eq a, Num a) => [a] -> [a]
f0, forall {a}. Num a => [a] -> [a]
f1 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod11
    }
    where
        f0 :: [a] -> [a]
f0 vs :: [a]
vs@[a
v1,a
v2] = if a
v1 forall a. Eq a => a -> a -> Bool
== forall a. Num a => a -> a
abs a
v2 then [a]
vs else []
        f0 [a]
_          = []
        f1 :: [a] -> [a]
f1 [a
v2]       = [forall a. Num a => a -> a
abs a
v2]
        f1 [a]
_          = []

modXsdIntegerNeg :: RDFDatatypeMod Integer
modXsdIntegerNeg :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerNeg = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
"neg"
    , dmModf :: [ModifierFn Integer]
dmModf = [ forall {a}. (Eq a, Num a) => [a] -> [a]
f0, forall {a}. Num a => [a] -> [a]
f1, forall {a}. Num a => [a] -> [a]
f1 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod11inv
    }
    where
        f0 :: [a] -> [a]
f0 vs :: [a]
vs@[a
v1,a
v2] = if a
v1 forall a. Eq a => a -> a -> Bool
== forall a. Num a => a -> a
negate a
v2 then [a]
vs else []
        f0 [a]
_          = []
        f1 :: [a] -> [a]
f1 [a
vi]       = [-a
vi]
        f1 [a]
_          = []

modXsdIntegerSum :: RDFDatatypeMod Integer
modXsdIntegerSum :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerSum = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
"sum"
    , dmModf :: [ModifierFn Integer]
dmModf = [ forall {a}. (Eq a, Num a) => [a] -> [a]
f0, forall {a}. Num a => [a] -> [a]
f1, forall {a}. Num a => [a] -> [a]
f2, forall {a}. Num a => [a] -> [a]
f2 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21inv
    }
    where
        f0 :: [a] -> [a]
f0 vs :: [a]
vs@[a
v1,a
v2,a
v3] = if a
v1 forall a. Eq a => a -> a -> Bool
== a
v2 forall a. Num a => a -> a -> a
+ a
v3 then [a]
vs else []
        f0 [a]
_             = []
        f1 :: [a] -> [a]
f1 [a
v2,a
v3]       = [a
v2 forall a. Num a => a -> a -> a
+ a
v3]
        f1 [a]
_             = []
        f2 :: [a] -> [a]
f2 [a
v1,a
vi]       = [a
v1 forall a. Num a => a -> a -> a
- a
vi]
        f2 [a]
_             = []

modXsdIntegerDiff :: RDFDatatypeMod Integer
modXsdIntegerDiff :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerDiff = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
"diff"
    , dmModf :: [ModifierFn Integer]
dmModf = [ forall {a}. (Eq a, Num a) => [a] -> [a]
f0, forall {a}. Num a => [a] -> [a]
f1, forall {a}. Num a => [a] -> [a]
f2, forall {a}. Num a => [a] -> [a]
f3 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21inv
    }
    where
        f0 :: [a] -> [a]
f0 vs :: [a]
vs@[a
v1,a
v2,a
v3] = if a
v1 forall a. Eq a => a -> a -> Bool
== a
v2 forall a. Num a => a -> a -> a
- a
v3 then [a]
vs else []
        f0 [a]
_             = []
        f1 :: [a] -> [a]
f1 [a
v2,a
v3]       = [a
v2 forall a. Num a => a -> a -> a
- a
v3]
        f1 [a]
_             = []
        f2 :: [a] -> [a]
f2 [a
v1,a
v3]       = [a
v1 forall a. Num a => a -> a -> a
+ a
v3]
        f2 [a]
_             = []
        f3 :: [a] -> [a]
f3 [a
v1,a
v2]       = [a
v2 forall a. Num a => a -> a -> a
- a
v1]
        f3 [a]
_             = []

modXsdIntegerProd :: RDFDatatypeMod Integer
modXsdIntegerProd :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerProd = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
"prod"
    , dmModf :: [ModifierFn Integer]
dmModf = [ forall {a}. (Eq a, Num a) => [a] -> [a]
f0, forall {a}. Num a => [a] -> [a]
f1, forall {a}. Integral a => [a] -> [a]
f2, forall {a}. Integral a => [a] -> [a]
f2 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21inv
    }
    where
        f0 :: [a] -> [a]
f0 vs :: [a]
vs@[a
v1,a
v2,a
v3] = if a
v1 forall a. Eq a => a -> a -> Bool
== a
v2 forall a. Num a => a -> a -> a
* a
v3 then [a]
vs else []
        f0 [a]
_             = []
        f1 :: [a] -> [a]
f1 [a
v2,a
v3]       = [a
v2 forall a. Num a => a -> a -> a
* a
v3]
        f1 [a]
_             = []
        f2 :: [a] -> [a]
f2 [a
v1,a
vi]       = [a
q | a
r forall a. Eq a => a -> a -> Bool
== a
0]
            where (a
q,a
r)  = forall a. Integral a => a -> a -> (a, a)
quotRem a
v1 a
vi
        f2 [a]
_             = []

modXsdIntegerDivMod :: RDFDatatypeMod Integer
modXsdIntegerDivMod :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerDivMod = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
"divmod"
    , dmModf :: [ModifierFn Integer]
dmModf = [ forall {a}. Integral a => [a] -> [a]
f0, forall {a}. Integral a => [a] -> [a]
f1 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod22
    }
    where
        f0 :: [b] -> [b]
f0 vs :: [b]
vs@[b
v1,b
v2,b
v3,b
v4] = if (b
v1,b
v2) forall a. Eq a => a -> a -> Bool
== forall a. Integral a => a -> a -> (a, a)
divMod b
v3 b
v4 then [b]
vs else []
        f0 [b]
_                = []
        f1 :: [a] -> [a]
f1 [a
v3,a
v4]          = [a
v1,a
v2] where (a
v1,a
v2) = forall a. Integral a => a -> a -> (a, a)
divMod a
v3 a
v4
        f1 [a]
_                = []

modXsdIntegerPower :: RDFDatatypeMod Integer
modXsdIntegerPower :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerPower = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
"power"
    , dmModf :: [ModifierFn Integer]
dmModf = [ ModifierFn Integer
f0, ModifierFn Integer
f1 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn. (Ord lb, Ord vn) => ApplyModifier lb vn
makeVmod21
    }
    where
        f0 :: ModifierFn Integer
f0 vs :: [Integer]
vs@[Integer
v1,Integer
v2,Integer
v3] = if forall a. a -> Maybe a
Just Integer
v1 forall a. Eq a => a -> a -> Bool
== Integer -> Integer -> Maybe Integer
intPower Integer
v2 Integer
v3 then [Integer]
vs else []
        f0 [Integer]
_             = []
        f1 :: ModifierFn Integer
f1 [Integer
v2,Integer
v3]       = forall a. Maybe a -> [a]
maybeToList (Integer -> Integer -> Maybe Integer
intPower Integer
v2 Integer
v3)
        f1 [Integer]
_             = []

modXsdIntegerEq, modXsdIntegerNe, modXsdIntegerLt, modXsdIntegerLe, modXsdIntegerGt, modXsdIntegerGe :: RDFDatatypeMod Integer 
modXsdIntegerEq :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerEq = LName
-> (Integer -> Integer -> Bool)
-> DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerCompare LName
"eq" forall a. Eq a => a -> a -> Bool
(==)
modXsdIntegerNe :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerNe = LName
-> (Integer -> Integer -> Bool)
-> DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerCompare LName
"ne" forall a. Eq a => a -> a -> Bool
(/=)
modXsdIntegerLt :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerLt = LName
-> (Integer -> Integer -> Bool)
-> DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerCompare LName
"lt" forall a. Ord a => a -> a -> Bool
(<)
modXsdIntegerLe :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerLe = LName
-> (Integer -> Integer -> Bool)
-> DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerCompare LName
"le" forall a. Ord a => a -> a -> Bool
(<=)
modXsdIntegerGt :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerGt = LName
-> (Integer -> Integer -> Bool)
-> DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerCompare LName
"gt" forall a. Ord a => a -> a -> Bool
(>)
modXsdIntegerGe :: DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerGe = LName
-> (Integer -> Integer -> Bool)
-> DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerCompare LName
"ge" forall a. Ord a => a -> a -> Bool
(>=)

modXsdIntegerCompare ::
    LName -> (Integer->Integer->Bool) -> RDFDatatypeMod Integer
modXsdIntegerCompare :: LName
-> (Integer -> Integer -> Bool)
-> DatatypeMod Integer RDFLabel RDFLabel
modXsdIntegerCompare LName
nam Integer -> Integer -> Bool
rel = DatatypeMod
    { dmName :: ScopedName
dmName = Namespace -> LName -> ScopedName
makeNSScopedName Namespace
namespaceXsdInteger LName
nam
    , dmModf :: [ModifierFn Integer]
dmModf = [ ModifierFn Integer
f0 ]
    , dmAppf :: ApplyModifier RDFLabel RDFLabel
dmAppf = forall lb vn.
(Eq lb, Show lb, Eq vn, Show vn) =>
ApplyModifier lb vn
makeVmod20
    }
    where
        f0 :: ModifierFn Integer
f0 vs :: [Integer]
vs@[Integer
v1,Integer
v2] = if Integer -> Integer -> Bool
rel Integer
v1 Integer
v2 then [Integer]
vs else []
        f0 [Integer]
_          = []

-- |rulesetXsdInteger contains rules and axioms that allow additional
--  deductions when xsd:integer values appear in a graph.
--
--  The rules defined here are concerned with basic integer arithmetic
--  operations: +, -, *, div, rem
--
--  makeRuleset :: Namespace -> [Formula ex] -> [Rule ex] -> Ruleset ex
--
rdfRulesetXsdInteger :: RDFRuleset
rdfRulesetXsdInteger :: Ruleset RDFGraph
rdfRulesetXsdInteger =
    forall ex. Namespace -> [Formula ex] -> [Rule ex] -> Ruleset ex
makeRuleset Namespace
namespaceXsdInteger [RDFFormula]
axiomsXsdInteger [Rule RDFGraph]
rulesXsdInteger

prefixXsdInteger :: B.Builder
prefixXsdInteger :: Builder
prefixXsdInteger = 
  forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Namespace -> Builder
namespaceToBuilder
              [ Namespace
namespaceRDF
              , Namespace
namespaceRDFS
              , Namespace
namespaceRDFD
              , Namespace
namespaceXSD
              , Namespace
namespaceXsdInteger
              ]

mkAxiom :: LName -> B.Builder -> RDFFormula
mkAxiom :: LName -> Builder -> RDFFormula
mkAxiom LName
local Builder
gr =
    Namespace -> LName -> Builder -> RDFFormula
makeRDFFormula Namespace
namespaceXsdInteger LName
local (Builder
prefixXsdInteger forall a. Monoid a => a -> a -> a
`mappend` Builder
gr)

-- | The axioms for @xsd:integer@, which are
--
-- > xsd:integer a rdfs:Datatype .
--
axiomsXsdInteger :: [RDFFormula]
axiomsXsdInteger :: [RDFFormula]
axiomsXsdInteger =
    [ LName -> Builder -> RDFFormula
mkAxiom LName
"dt"      Builder
"xsd:integer rdf:type rdfs:Datatype ."
    ]

-- | The rules for @xsd:integer@.
rulesXsdInteger :: [RDFRule]
rulesXsdInteger :: [Rule RDFGraph]
rulesXsdInteger = forall vt. RDFDatatypeVal vt -> RDFGraph -> [Rule RDFGraph]
makeRDFDatatypeRestrictionRules RDFDatatypeVal Integer
rdfDatatypeValXsdInteger RDFGraph
gr
    where
        gr :: RDFGraph
gr = Builder -> RDFGraph
makeRDFGraphFromN3Builder Builder
rulesXsdIntegerBuilder

--- I have removed the newline which was added between each line
--- to improve the clarity of parser errors.
---
rulesXsdIntegerBuilder :: B.Builder
rulesXsdIntegerBuilder :: Builder
rulesXsdIntegerBuilder = 
  forall a. Monoid a => [a] -> a
mconcat
  [ Builder
prefixXsdInteger
    , Builder
"xsd_integer:Abs a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:abs ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Neg a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:neg ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Sum a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2 rdf:_3) ; "
    , Builder
"  rdfd:constraint xsd_integer:sum ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Diff a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2 rdf:_3) ; "
    , Builder
"  rdfd:constraint xsd_integer:diff ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Prod a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2 rdf:_3) ; "
    , Builder
"  rdfd:constraint xsd_integer:prod ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:DivMod a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2 rdf:_3 rdf:_4) ; "
    , Builder
"  rdfd:constraint xsd_integer:divmod ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Power a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2 rdf:_3) ; "
    , Builder
"  rdfd:constraint xsd_integer:power ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Eq a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:eq ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Ne a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:ne ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Lt a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:lt ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Le a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:le ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Gt a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:gt ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    , Builder
"xsd_integer:Ge a rdfd:GeneralRestriction ; "
    , Builder
"  rdfd:onProperties (rdf:_1 rdf:_2) ; "
    , Builder
"  rdfd:constraint xsd_integer:ge ; "
    , Builder
"  rdfd:maxCardinality \"1\"^^xsd:nonNegativeInteger . "
    ]
  
--------------------------------------------------------------------------------
--
--  Copyright (c) 2003, Graham Klyne, 2009 Vasili I Galchin,
--    2011, 2012, 2014, 2018, 2022 Douglas Burke
--  All rights reserved.
--
--  This file is part of Swish.
--
--  Swish is free software; you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation; either version 2 of the License, or
--  (at your option) any later version.
--
--  Swish is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with Swish; if not, write to:
--    The Free Software Foundation, Inc.,
--    59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
--
--------------------------------------------------------------------------------