{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
module Swish.VarBinding
( VarBinding(..), nullVarBinding
, boundVars, subBinding, makeVarBinding
, applyVarBinding, joinVarBindings, addVarBinding
, VarBindingModify(..), OpenVarBindingModify
, openVbmName
, vbmCompatibility, vbmCompose
, composeSequence, findCompositions, findComposition
, VarBindingFilter(..)
, makeVarFilterModify
, makeVarTestFilter, makeVarCompareFilter
, varBindingId, nullVarBindingModify
, varFilterDisjunction, varFilterConjunction
, varFilterEQ, varFilterNE
)
where
import qualified Data.Map as M
import qualified Data.Set as S
#if (!defined(__GLASGOW_HASKELL__)) || (__GLASGOW_HASKELL__ < 710)
import Control.Applicative ((<$>), (<*>))
import Data.Monoid (Monoid(..), mconcat)
#endif
#if !(MIN_VERSION_base(4, 11, 0))
import Data.Semigroup
#endif
import Control.Monad (mplus)
import Data.Foldable(Foldable(..))
import Data.Function (on)
import Data.List (find, intersect, union, (\\), permutations)
import Data.Maybe (mapMaybe, fromMaybe, isJust, fromJust, listToMaybe)
import Data.Ord (comparing)
import Prelude hiding (Foldable(..))
import Swish.Namespace (ScopedName, getScopeLocal)
import Swish.QName (newLName, getLName)
import Swish.RDF.Vocabulary (swishName)
import Swish.Utils.ListHelpers (flist)
data VarBinding a b = VarBinding
{ forall a b. VarBinding a b -> a -> Maybe b
vbMap :: a -> Maybe b
, forall a b. VarBinding a b -> Set (a, b)
vbEnum :: S.Set (a,b)
, forall a b. VarBinding a b -> Bool
vbNull :: Bool
}
instance (Ord a, Ord b) => Eq (VarBinding a b) where
== :: VarBinding a b -> VarBinding a b -> Bool
(==) = Set (a, b) -> Set (a, b) -> Bool
forall a. Eq a => a -> a -> Bool
(==) (Set (a, b) -> Set (a, b) -> Bool)
-> (VarBinding a b -> Set (a, b))
-> VarBinding a b
-> VarBinding a b
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` VarBinding a b -> Set (a, b)
forall a b. VarBinding a b -> Set (a, b)
vbEnum
instance (Ord a, Ord b) => Ord (VarBinding a b) where
compare :: VarBinding a b -> VarBinding a b -> Ordering
compare = (VarBinding a b -> Set (a, b))
-> VarBinding a b -> VarBinding a b -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing VarBinding a b -> Set (a, b)
forall a b. VarBinding a b -> Set (a, b)
vbEnum
instance (Ord a, Ord b) => Semigroup (VarBinding a b) where
<> :: VarBinding a b -> VarBinding a b -> VarBinding a b
(<>) = VarBinding a b -> VarBinding a b -> VarBinding a b
forall a b.
(Ord a, Ord b) =>
VarBinding a b -> VarBinding a b -> VarBinding a b
joinVarBindings
instance (Ord a, Ord b) => Monoid (VarBinding a b) where
mempty :: VarBinding a b
mempty = VarBinding a b
forall a b. VarBinding a b
nullVarBinding
#if !(MIN_VERSION_base(4, 11, 0))
mappend = (<>)
#endif
instance (Show a, Show b) => Show (VarBinding a b) where
show :: VarBinding a b -> String
show = [(a, b)] -> String
forall a. Show a => a -> String
show ([(a, b)] -> String)
-> (VarBinding a b -> [(a, b)]) -> VarBinding a b -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set (a, b) -> [(a, b)]
forall a. Set a -> [a]
S.toList (Set (a, b) -> [(a, b)])
-> (VarBinding a b -> Set (a, b)) -> VarBinding a b -> [(a, b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarBinding a b -> Set (a, b)
forall a b. VarBinding a b -> Set (a, b)
vbEnum
nullVarBinding :: VarBinding a b
nullVarBinding :: forall a b. VarBinding a b
nullVarBinding = VarBinding
{ vbMap :: a -> Maybe b
vbMap = Maybe b -> a -> Maybe b
forall a b. a -> b -> a
const Maybe b
forall a. Maybe a
Nothing
, vbEnum :: Set (a, b)
vbEnum = Set (a, b)
forall a. Set a
S.empty
, vbNull :: Bool
vbNull = Bool
True
}
boundVars :: (Ord a, Ord b) => VarBinding a b -> S.Set a
boundVars :: forall a b. (Ord a, Ord b) => VarBinding a b -> Set a
boundVars = ((a, b) -> a) -> Set (a, b) -> Set a
forall b a. Ord b => (a -> b) -> Set a -> Set b
S.map (a, b) -> a
forall a b. (a, b) -> a
fst (Set (a, b) -> Set a)
-> (VarBinding a b -> Set (a, b)) -> VarBinding a b -> Set a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarBinding a b -> Set (a, b)
forall a b. VarBinding a b -> Set (a, b)
vbEnum
subBinding :: (Ord a, Ord b) => VarBinding a b -> VarBinding a b -> Bool
subBinding :: forall a b.
(Ord a, Ord b) =>
VarBinding a b -> VarBinding a b -> Bool
subBinding = Set (a, b) -> Set (a, b) -> Bool
forall a. Ord a => Set a -> Set a -> Bool
S.isSubsetOf (Set (a, b) -> Set (a, b) -> Bool)
-> (VarBinding a b -> Set (a, b))
-> VarBinding a b
-> VarBinding a b
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` VarBinding a b -> Set (a, b)
forall a b. VarBinding a b -> Set (a, b)
vbEnum
makeVarBinding :: (Ord a, Ord b) => [(a,b)] -> VarBinding a b
makeVarBinding :: forall a b. (Ord a, Ord b) => [(a, b)] -> VarBinding a b
makeVarBinding [] = VarBinding a b
forall a b. VarBinding a b
nullVarBinding
makeVarBinding [(a, b)]
vrbs =
VarBinding
{ vbMap :: a -> Maybe b
vbMap = (a -> Map a b -> Maybe b) -> Map a b -> a -> Maybe b
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Map a b -> Maybe b
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ([(a, b)] -> Map a b
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(a, b)]
vrbs)
, vbEnum :: Set (a, b)
vbEnum = [(a, b)] -> Set (a, b)
forall a. Ord a => [a] -> Set a
S.fromList [(a, b)]
vrbs
, vbNull :: Bool
vbNull = Bool
False
}
applyVarBinding :: VarBinding a a -> a -> a
applyVarBinding :: forall a. VarBinding a a -> a -> a
applyVarBinding VarBinding a a
vbind a
v = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
v (VarBinding a a -> a -> Maybe a
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding a a
vbind a
v)
joinVarBindings :: (Ord a, Ord b) => VarBinding a b -> VarBinding a b -> VarBinding a b
joinVarBindings :: forall a b.
(Ord a, Ord b) =>
VarBinding a b -> VarBinding a b -> VarBinding a b
joinVarBindings VarBinding a b
vb1 VarBinding a b
vb2
| VarBinding a b -> Bool
forall a b. VarBinding a b -> Bool
vbNull VarBinding a b
vb1 = VarBinding a b
vb2
| VarBinding a b -> Bool
forall a b. VarBinding a b -> Bool
vbNull VarBinding a b
vb2 = VarBinding a b
vb1
| Bool
otherwise = VarBinding
{ vbMap :: a -> Maybe b
vbMap = a -> Maybe b
mv12
, vbEnum :: Set (a, b)
vbEnum = (a -> (a, b)) -> Set a -> Set (a, b)
forall b a. Ord b => (a -> b) -> Set a -> Set b
S.map (\a
v -> (a
v,Maybe b -> b
forall a. HasCallStack => Maybe a -> a
fromJust (a -> Maybe b
mv12 a
v))) Set a
bv12
, vbNull :: Bool
vbNull = Bool
False
}
where
mv12 :: a -> Maybe b
mv12 a
n = VarBinding a b -> a -> Maybe b
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding a b
vb1 a
n Maybe b -> Maybe b -> Maybe b
forall a. Maybe a -> Maybe a -> Maybe a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` VarBinding a b -> a -> Maybe b
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding a b
vb2 a
n
bv12 :: Set a
bv12 = VarBinding a b -> Set a
forall a b. (Ord a, Ord b) => VarBinding a b -> Set a
boundVars VarBinding a b
vb1 Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
`S.union` VarBinding a b -> Set a
forall a b. (Ord a, Ord b) => VarBinding a b -> Set a
boundVars VarBinding a b
vb2
addVarBinding ::
(Ord a, Ord b)
=> a
-> b
-> VarBinding a b
-> VarBinding a b
addVarBinding :: forall a b.
(Ord a, Ord b) =>
a -> b -> VarBinding a b -> VarBinding a b
addVarBinding a
lb b
val VarBinding a b
vbind = VarBinding a b -> VarBinding a b -> VarBinding a b
forall a b.
(Ord a, Ord b) =>
VarBinding a b -> VarBinding a b -> VarBinding a b
joinVarBindings VarBinding a b
vbind (VarBinding a b -> VarBinding a b)
-> VarBinding a b -> VarBinding a b
forall a b. (a -> b) -> a -> b
$ [(a, b)] -> VarBinding a b
forall a b. (Ord a, Ord b) => [(a, b)] -> VarBinding a b
makeVarBinding [(a
lb,b
val)]
data VarBindingModify a b = VarBindingModify
{ forall a b. VarBindingModify a b -> ScopedName
vbmName :: ScopedName
, forall a b.
VarBindingModify a b -> [VarBinding a b] -> [VarBinding a b]
vbmApply :: [VarBinding a b] -> [VarBinding a b]
, forall a b. VarBindingModify a b -> [a]
vbmVocab :: [a]
, forall a b. VarBindingModify a b -> [[a]]
vbmUsage :: [[a]]
}
type OpenVarBindingModify lb vn = [lb] -> VarBindingModify lb vn
openVbmName :: OpenVarBindingModify lb vn -> ScopedName
openVbmName :: forall lb vn. OpenVarBindingModify lb vn -> ScopedName
openVbmName OpenVarBindingModify lb vn
ovbm = VarBindingModify lb vn -> ScopedName
forall a b. VarBindingModify a b -> ScopedName
vbmName (OpenVarBindingModify lb vn
ovbm (String -> [lb]
forall a. HasCallStack => String -> a
error String
"Undefined labels in variable binding"))
instance Show (OpenVarBindingModify a b)
where
show :: OpenVarBindingModify a b -> String
show = ScopedName -> String
forall a. Show a => a -> String
show (ScopedName -> String)
-> (OpenVarBindingModify a b -> ScopedName)
-> OpenVarBindingModify a b
-> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OpenVarBindingModify a b -> ScopedName
forall lb vn. OpenVarBindingModify lb vn -> ScopedName
openVbmName
vbmCompatibility :: (Eq a) => VarBindingModify a b -> [a] -> Maybe [a]
vbmCompatibility :: forall a b. Eq a => VarBindingModify a b -> [a] -> Maybe [a]
vbmCompatibility VarBindingModify a b
vbm [a]
vars = ([a] -> Bool) -> [[a]] -> Maybe [a]
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find [a] -> Bool
compat (VarBindingModify a b -> [[a]]
forall a b. VarBindingModify a b -> [[a]]
vbmUsage VarBindingModify a b
vbm)
where
compat :: [a] -> Bool
compat = [a] -> [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> [a] -> Bool
vbmCompatibleVars [a]
vars (VarBindingModify a b -> [a]
forall a b. VarBindingModify a b -> [a]
vbmVocab VarBindingModify a b
vbm)
vbmCompatibleVars ::
(Eq a)
=> [a]
-> [a]
-> [a]
-> Bool
vbmCompatibleVars :: forall a. Eq a => [a] -> [a] -> [a] -> Bool
vbmCompatibleVars [a]
bvars [a]
vocab [a]
ovars =
[a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([a]
ivars [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
`intersect` [a]
ovars) Bool -> Bool -> Bool
&&
[a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (([a]
vocab [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
\\ [a]
ovars) [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
\\ [a]
ivars)
where
ivars :: [a]
ivars = [a]
bvars [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
`intersect` [a]
vocab
vbmCompose :: (Eq a) => VarBindingModify a b -> VarBindingModify a b
-> Maybe (VarBindingModify a b)
vbmCompose :: forall a b.
Eq a =>
VarBindingModify a b
-> VarBindingModify a b -> Maybe (VarBindingModify a b)
vbmCompose
(VarBindingModify ScopedName
nam1 [VarBinding a b] -> [VarBinding a b]
app1 [a]
voc1 [[a]]
use1)
(VarBindingModify ScopedName
nam2 [VarBinding a b] -> [VarBinding a b]
app2 [a]
voc2 [[a]]
use2)
| Bool -> Bool
not ([[a]] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[a]]
use12) = VarBindingModify a b -> Maybe (VarBindingModify a b)
forall a. a -> Maybe a
Just VarBindingModify
{ vbmName :: ScopedName
vbmName = ScopedName
name
, vbmApply :: [VarBinding a b] -> [VarBinding a b]
vbmApply = [VarBinding a b] -> [VarBinding a b]
app2 ([VarBinding a b] -> [VarBinding a b])
-> ([VarBinding a b] -> [VarBinding a b])
-> [VarBinding a b]
-> [VarBinding a b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [VarBinding a b] -> [VarBinding a b]
app1
, vbmVocab :: [a]
vbmVocab = [a]
voc1 [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
`union` [a]
voc2
, vbmUsage :: [[a]]
vbmUsage = [[a]]
use12
}
| Bool
otherwise = Maybe (VarBindingModify a b)
forall a. Maybe a
Nothing
where
use12 :: [[a]]
use12 = [a] -> [[a]] -> [[a]] -> [[a]]
forall a. Eq a => [a] -> [[a]] -> [[a]] -> [[a]]
compatibleUsage [a]
voc1 [[a]]
use1 [[a]]
use2
getName :: ScopedName -> Text
getName = LName -> Text
getLName (LName -> Text) -> (ScopedName -> LName) -> ScopedName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedName -> LName
getScopeLocal
name :: ScopedName
name = LName -> ScopedName
swishName (LName -> ScopedName) -> LName -> ScopedName
forall a b. (a -> b) -> a -> b
$ Maybe LName -> LName
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe LName -> LName) -> Maybe LName -> LName
forall a b. (a -> b) -> a -> b
$ Text -> Maybe LName
newLName (Text -> Maybe LName) -> Text -> Maybe LName
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [Text
"_", ScopedName -> Text
getName ScopedName
nam1, Text
"_", ScopedName -> Text
getName ScopedName
nam2, Text
"_"]
compatibleUsage ::
(Eq a)
=> [a]
-> [[a]]
-> [[a]]
-> [[a]]
compatibleUsage :: forall a. Eq a => [a] -> [[a]] -> [[a]] -> [[a]]
compatibleUsage [a]
voc1 [[a]]
use1 [[a]]
use2 =
[ [a]
u1 [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
u2 | [a]
u2 <- [[a]]
use2, [a] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([a]
voc1 [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
`intersect` [a]
u2), [a]
u1 <- [[a]]
use1 ]
findCompositions ::
(Eq a) => [VarBindingModify a b]
-> [a]
-> [VarBindingModify a b]
findCompositions :: forall a b.
Eq a =>
[VarBindingModify a b] -> [a] -> [VarBindingModify a b]
findCompositions [VarBindingModify a b]
vbms [a]
vars =
([VarBindingModify a b] -> Maybe (VarBindingModify a b))
-> [[VarBindingModify a b]] -> [VarBindingModify a b]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ([a] -> [VarBindingModify a b] -> Maybe (VarBindingModify a b)
forall a b.
Eq a =>
[a] -> [VarBindingModify a b] -> Maybe (VarBindingModify a b)
composeCheckSequence [a]
vars) ([VarBindingModify a b] -> [[VarBindingModify a b]]
forall a. [a] -> [[a]]
permutations [VarBindingModify a b]
vbms)
composeCheckSequence :: (Eq a) => [a] -> [VarBindingModify a b]
-> Maybe (VarBindingModify a b)
composeCheckSequence :: forall a b.
Eq a =>
[a] -> [VarBindingModify a b] -> Maybe (VarBindingModify a b)
composeCheckSequence [a]
vars [VarBindingModify a b]
vbms = [a] -> Maybe (VarBindingModify a b) -> Maybe (VarBindingModify a b)
forall {a} {b}.
Eq a =>
[a] -> Maybe (VarBindingModify a b) -> Maybe (VarBindingModify a b)
useWith [a]
vars (Maybe (VarBindingModify a b) -> Maybe (VarBindingModify a b))
-> Maybe (VarBindingModify a b) -> Maybe (VarBindingModify a b)
forall a b. (a -> b) -> a -> b
$ [VarBindingModify a b] -> Maybe (VarBindingModify a b)
forall a b.
Eq a =>
[VarBindingModify a b] -> Maybe (VarBindingModify a b)
composeSequence [VarBindingModify a b]
vbms
where
useWith :: [a] -> Maybe (VarBindingModify a b) -> Maybe (VarBindingModify a b)
useWith [a]
_ Maybe (VarBindingModify a b)
Nothing = Maybe (VarBindingModify a b)
forall a. Maybe a
Nothing
useWith [a]
vs v :: Maybe (VarBindingModify a b)
v@(Just VarBindingModify a b
vbm)
| Maybe [a] -> Bool
forall a. Maybe a -> Bool
isJust (Maybe [a] -> Bool) -> Maybe [a] -> Bool
forall a b. (a -> b) -> a -> b
$ VarBindingModify a b -> [a] -> Maybe [a]
forall a b. Eq a => VarBindingModify a b -> [a] -> Maybe [a]
vbmCompatibility VarBindingModify a b
vbm [a]
vs = Maybe (VarBindingModify a b)
v
| Bool
otherwise = Maybe (VarBindingModify a b)
forall a. Maybe a
Nothing
composeSequence :: (Eq a) => [VarBindingModify a b]
-> Maybe (VarBindingModify a b)
composeSequence :: forall a b.
Eq a =>
[VarBindingModify a b] -> Maybe (VarBindingModify a b)
composeSequence [] = VarBindingModify a b -> Maybe (VarBindingModify a b)
forall a. a -> Maybe a
Just VarBindingModify a b
forall a b. VarBindingModify a b
varBindingId
composeSequence (VarBindingModify a b
vbm:[VarBindingModify a b]
vbms) = (Maybe (VarBindingModify a b)
-> VarBindingModify a b -> Maybe (VarBindingModify a b))
-> Maybe (VarBindingModify a b)
-> [VarBindingModify a b]
-> Maybe (VarBindingModify a b)
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Maybe (VarBindingModify a b)
-> VarBindingModify a b -> Maybe (VarBindingModify a b)
forall a b.
Eq a =>
Maybe (VarBindingModify a b)
-> VarBindingModify a b -> Maybe (VarBindingModify a b)
composePair (VarBindingModify a b -> Maybe (VarBindingModify a b)
forall a. a -> Maybe a
Just VarBindingModify a b
vbm) [VarBindingModify a b]
vbms
composePair :: (Eq a) => Maybe (VarBindingModify a b) -> VarBindingModify a b
-> Maybe (VarBindingModify a b)
composePair :: forall a b.
Eq a =>
Maybe (VarBindingModify a b)
-> VarBindingModify a b -> Maybe (VarBindingModify a b)
composePair Maybe (VarBindingModify a b)
Nothing VarBindingModify a b
_ = Maybe (VarBindingModify a b)
forall a. Maybe a
Nothing
composePair (Just VarBindingModify a b
vbm1) VarBindingModify a b
vbm2 = VarBindingModify a b
-> VarBindingModify a b -> Maybe (VarBindingModify a b)
forall a b.
Eq a =>
VarBindingModify a b
-> VarBindingModify a b -> Maybe (VarBindingModify a b)
vbmCompose VarBindingModify a b
vbm1 VarBindingModify a b
vbm2
findComposition :: (Eq a) => [VarBindingModify a b] -> [a]
-> Maybe (VarBindingModify a b)
findComposition :: forall a b.
Eq a =>
[VarBindingModify a b] -> [a] -> Maybe (VarBindingModify a b)
findComposition = [VarBindingModify a b] -> Maybe (VarBindingModify a b)
forall a. [a] -> Maybe a
listToMaybe ([VarBindingModify a b] -> Maybe (VarBindingModify a b))
-> ([VarBindingModify a b] -> [a] -> [VarBindingModify a b])
-> [VarBindingModify a b]
-> [a]
-> Maybe (VarBindingModify a b)
forall {b} {c} {a} {a}. (b -> c) -> (a -> a -> b) -> a -> a -> c
`c2` [VarBindingModify a b] -> [a] -> [VarBindingModify a b]
forall a b.
Eq a =>
[VarBindingModify a b] -> [a] -> [VarBindingModify a b]
findCompositions
where
c2 :: (b -> c) -> (a -> a -> b) -> a -> a -> c
c2 = ((a -> b) -> a -> c) -> (a -> a -> b) -> a -> a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) (((a -> b) -> a -> c) -> (a -> a -> b) -> a -> a -> c)
-> ((b -> c) -> (a -> b) -> a -> c)
-> (b -> c)
-> (a -> a -> b)
-> a
-> a
-> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> c) -> (a -> b) -> a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)
varBindingId :: VarBindingModify a b
varBindingId :: forall a b. VarBindingModify a b
varBindingId = VarBindingModify
{ vbmName :: ScopedName
vbmName = LName -> ScopedName
swishName LName
"varBindingId"
, vbmApply :: [VarBinding a b] -> [VarBinding a b]
vbmApply = [VarBinding a b] -> [VarBinding a b]
forall a. a -> a
id
, vbmVocab :: [a]
vbmVocab = []
, vbmUsage :: [[a]]
vbmUsage = [[]]
}
nullVarBindingModify :: OpenVarBindingModify a b
nullVarBindingModify :: forall a b. OpenVarBindingModify a b
nullVarBindingModify [a]
lbs = VarBindingModify
{ vbmName :: ScopedName
vbmName = LName -> ScopedName
swishName LName
"nullVarBindingModify"
, vbmApply :: [VarBinding a b] -> [VarBinding a b]
vbmApply = [VarBinding a b] -> [VarBinding a b]
forall a. a -> a
id
, vbmVocab :: [a]
vbmVocab = [a]
lbs
, vbmUsage :: [[a]]
vbmUsage = [[]]
}
data VarBindingFilter a b = VarBindingFilter
{ forall a b. VarBindingFilter a b -> ScopedName
vbfName :: ScopedName
, forall a b. VarBindingFilter a b -> [a]
vbfVocab :: [a]
, forall a b. VarBindingFilter a b -> VarBinding a b -> Bool
vbfTest :: VarBinding a b -> Bool
}
makeVarFilterModify :: VarBindingFilter a b -> VarBindingModify a b
makeVarFilterModify :: forall a b. VarBindingFilter a b -> VarBindingModify a b
makeVarFilterModify VarBindingFilter a b
vbf = VarBindingModify
{ vbmName :: ScopedName
vbmName = VarBindingFilter a b -> ScopedName
forall a b. VarBindingFilter a b -> ScopedName
vbfName VarBindingFilter a b
vbf
, vbmApply :: [VarBinding a b] -> [VarBinding a b]
vbmApply = (VarBinding a b -> Bool) -> [VarBinding a b] -> [VarBinding a b]
forall a. (a -> Bool) -> [a] -> [a]
filter (VarBindingFilter a b -> VarBinding a b -> Bool
forall a b. VarBindingFilter a b -> VarBinding a b -> Bool
vbfTest VarBindingFilter a b
vbf)
, vbmVocab :: [a]
vbmVocab = VarBindingFilter a b -> [a]
forall a b. VarBindingFilter a b -> [a]
vbfVocab VarBindingFilter a b
vbf
, vbmUsage :: [[a]]
vbmUsage = [[]]
}
makeVarTestFilter ::
ScopedName -> (b -> Bool) -> a -> VarBindingFilter a b
makeVarTestFilter :: forall b a. ScopedName -> (b -> Bool) -> a -> VarBindingFilter a b
makeVarTestFilter ScopedName
nam b -> Bool
vtest a
var = VarBindingFilter
{ vbfName :: ScopedName
vbfName = ScopedName
nam
, vbfVocab :: [a]
vbfVocab = [a
var]
, vbfTest :: VarBinding a b -> Bool
vbfTest = \VarBinding a b
vb -> Bool -> (b -> Bool) -> Maybe b -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False b -> Bool
vtest (VarBinding a b -> a -> Maybe b
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding a b
vb a
var)
}
makeVarCompareFilter ::
ScopedName -> (b -> b -> Bool) -> a -> a -> VarBindingFilter a b
makeVarCompareFilter :: forall b a.
ScopedName -> (b -> b -> Bool) -> a -> a -> VarBindingFilter a b
makeVarCompareFilter ScopedName
nam b -> b -> Bool
vcomp a
v1 a
v2 = VarBindingFilter
{ vbfName :: ScopedName
vbfName = ScopedName
nam
, vbfVocab :: [a]
vbfVocab = [a
v1,a
v2]
, vbfTest :: VarBinding a b -> Bool
vbfTest = \VarBinding a b
vb -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== (b -> b -> Bool
vcomp (b -> b -> Bool) -> Maybe b -> Maybe (b -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VarBinding a b -> a -> Maybe b
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding a b
vb a
v1 Maybe (b -> Bool) -> Maybe b -> Maybe Bool
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> VarBinding a b -> a -> Maybe b
forall a b. VarBinding a b -> a -> Maybe b
vbMap VarBinding a b
vb a
v2)
}
varFilterEQ :: (Eq b) => a -> a -> VarBindingFilter a b
varFilterEQ :: forall b a. Eq b => a -> a -> VarBindingFilter a b
varFilterEQ =
ScopedName -> (b -> b -> Bool) -> a -> a -> VarBindingFilter a b
forall b a.
ScopedName -> (b -> b -> Bool) -> a -> a -> VarBindingFilter a b
makeVarCompareFilter (LName -> ScopedName
swishName LName
"varFilterEQ") b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==)
varFilterNE :: (Eq b) => a -> a -> VarBindingFilter a b
varFilterNE :: forall b a. Eq b => a -> a -> VarBindingFilter a b
varFilterNE =
ScopedName -> (b -> b -> Bool) -> a -> a -> VarBindingFilter a b
forall b a.
ScopedName -> (b -> b -> Bool) -> a -> a -> VarBindingFilter a b
makeVarCompareFilter (LName -> ScopedName
swishName LName
"varFilterNE") b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(/=)
varFilterDisjunction :: (Eq a) => [VarBindingFilter a b]
-> VarBindingFilter a b
varFilterDisjunction :: forall a b. Eq a => [VarBindingFilter a b] -> VarBindingFilter a b
varFilterDisjunction [VarBindingFilter a b]
vbfs = VarBindingFilter
{ vbfName :: ScopedName
vbfName = LName -> ScopedName
swishName LName
"varFilterDisjunction"
, vbfVocab :: [a]
vbfVocab = ([a] -> [a] -> [a]) -> [[a]] -> [a]
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
union ((VarBindingFilter a b -> [a]) -> [VarBindingFilter a b] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map VarBindingFilter a b -> [a]
forall a b. VarBindingFilter a b -> [a]
vbfVocab [VarBindingFilter a b]
vbfs)
, vbfTest :: VarBinding a b -> Bool
vbfTest = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or ([Bool] -> Bool)
-> (VarBinding a b -> [Bool]) -> VarBinding a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [VarBinding a b -> Bool] -> VarBinding a b -> [Bool]
forall a b. [a -> b] -> a -> [b]
flist ((VarBindingFilter a b -> VarBinding a b -> Bool)
-> [VarBindingFilter a b] -> [VarBinding a b -> Bool]
forall a b. (a -> b) -> [a] -> [b]
map VarBindingFilter a b -> VarBinding a b -> Bool
forall a b. VarBindingFilter a b -> VarBinding a b -> Bool
vbfTest [VarBindingFilter a b]
vbfs)
}
varFilterConjunction :: (Eq a) => [VarBindingFilter a b]
-> VarBindingFilter a b
varFilterConjunction :: forall a b. Eq a => [VarBindingFilter a b] -> VarBindingFilter a b
varFilterConjunction [VarBindingFilter a b]
vbfs = VarBindingFilter
{ vbfName :: ScopedName
vbfName = LName -> ScopedName
swishName LName
"varFilterConjunction"
, vbfVocab :: [a]
vbfVocab = ([a] -> [a] -> [a]) -> [[a]] -> [a]
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
union ((VarBindingFilter a b -> [a]) -> [VarBindingFilter a b] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map VarBindingFilter a b -> [a]
forall a b. VarBindingFilter a b -> [a]
vbfVocab [VarBindingFilter a b]
vbfs)
, vbfTest :: VarBinding a b -> Bool
vbfTest = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool)
-> (VarBinding a b -> [Bool]) -> VarBinding a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [VarBinding a b -> Bool] -> VarBinding a b -> [Bool]
forall a b. [a -> b] -> a -> [b]
flist ((VarBindingFilter a b -> VarBinding a b -> Bool)
-> [VarBindingFilter a b] -> [VarBinding a b -> Bool]
forall a b. (a -> b) -> [a] -> [b]
map VarBindingFilter a b -> VarBinding a b -> Bool
forall a b. VarBindingFilter a b -> VarBinding a b -> Bool
vbfTest [VarBindingFilter a b]
vbfs)
}