on fd05d50 help sign in
project-m36/src/lib/ProjectM36/AttributeNames.hs
38 lines 2.3 kB history raw
1module ProjectM36.AttributeNames where
2import qualified ProjectM36.Attribute as A
3import ProjectM36.Base
4import ProjectM36.Error
5import qualified Data.Set as S
6--AttributeNames is a data structure which can represent inverted projection attributes
8empty :: AttributeNames
9empty = AttributeNames S.empty
11all :: AttributeNames
12all = InvertedAttributeNames S.empty
14--check that the attribute names are actually in the attributes
15projectionAttributesForAttributeNames :: Attributes -> AttributeNames -> Either RelationalError Attributes
16projectionAttributesForAttributeNames attrs (AttributeNames attrNameSet) = do
17 let nonExistentAttributeNames = A.attributeNamesNotContained attrNameSet (A.attributeNameSet attrs)
18 if not $ S.null nonExistentAttributeNames then
19 Left $ AttributeNamesMismatchError nonExistentAttributeNames
20 else
21 return $ A.attributesForNames attrNameSet attrs
22projectionAttributesForAttributeNames attrs (InvertedAttributeNames unselectedAttrNameSet) = do
23 let nonExistentAttributeNames = A.attributeNamesNotContained unselectedAttrNameSet (A.attributeNameSet attrs)
24 if not $ S.null nonExistentAttributeNames then
25 Left $ AttributeNamesMismatchError nonExistentAttributeNames
26 else
27 return $ A.attributesForNames (A.nonMatchingAttributeNameSet unselectedAttrNameSet (A.attributeNameSet attrs)) attrs
28projectionAttributesForAttributeNames attrs (UnionAttributeNames namesA namesB) = do
29 attrsA <- projectionAttributesForAttributeNames attrs namesA
30 attrsB <- projectionAttributesForAttributeNames attrs namesB
31 pure (A.union attrsA attrsB)
32projectionAttributesForAttributeNames attrs (IntersectAttributeNames namesA namesB) = A.intersection <$> projectionAttributesForAttributeNames attrs namesA <*> projectionAttributesForAttributeNames attrs namesB
34invertAttributeNames :: AttributeNames -> AttributeNames
35invertAttributeNames (AttributeNames names) = InvertedAttributeNames names
36invertAttributeNames (InvertedAttributeNames names) = AttributeNames names
37invertAttributeNames (UnionAttributeNames namesA namesB) = IntersectAttributeNames (invertAttributeNames namesA) (invertAttributeNames namesB)
38invertAttributeNames (IntersectAttributeNames namesA namesB) = UnionAttributeNames (invertAttributeNames namesA) (invertAttributeNames namesB)