@@ -1,9 +1,9 @@
1−module RelationExpr where
1+module ProjectM36.RelationalExpression where
2−import Relation
2+import ProjectM36.Relation
3−import RelationTuple
3+import ProjectM36.Tuple
4−import RelationTupleSet
4+import ProjectM36.TupleSet
5−import RelationType
5+import ProjectM36.Base
6−import RelationalError
6+import ProjectM36.Error
7import qualified Data.Map as M
7import qualified Data.Map as M
8import qualified Data.HashSet as HS
8import qualified Data.HashSet as HS
9import Control.Monad.State hiding (join)
9import Control.Monad.State hiding (join)
@@ -20,7 +20,7 @@ evalRelationalExpr (RelationVariable name) = do
20evalRelationalExpr (Project attrNameSet expr) = do
20evalRelationalExpr (Project attrNameSet expr) = do
21 rel <- evalRelationalExpr expr
21 rel <- evalRelationalExpr expr
22 case rel of
22 case rel of
23− Right rel -> return $ project attrNameSet rel
23+ Right rel2 -> return $ project attrNameSet rel2
24 Left err -> return $ Left err
24 Left err -> return $ Left err
26evalRelationalExpr (Union exprA exprB) = do
26evalRelationalExpr (Union exprA exprB) = do
@@ -28,21 +28,21 @@ evalRelationalExpr (Union exprA exprB) = do
28 relB <- evalRelationalExpr exprB
28 relB <- evalRelationalExpr exprB
29 case relA of
29 case relA of
30 Left err -> return $ Left err
30 Left err -> return $ Left err
31− Right relA -> case relB of
31+ Right relA2 -> case relB of
32 Left err -> return $ Left err
32 Left err -> return $ Left err
33− Right relB -> return $ union relA relB
33+ Right relB2 -> return $ union relA2 relB2
35evalRelationalExpr (Join exprA exprB) = do
35evalRelationalExpr (Join exprA exprB) = do
36 relA <- evalRelationalExpr exprA
36 relA <- evalRelationalExpr exprA
37 relB <- evalRelationalExpr exprB
37 relB <- evalRelationalExpr exprB
38 case relA of
38 case relA of
39 Left err -> return $ Left err
39 Left err -> return $ Left err
40− Right relA -> case relB of
40+ Right relA2 -> case relB of
41 Left err -> return $ Left err
41 Left err -> return $ Left err
42− Right relB -> return $ join relA relB
42+ Right relB2 -> return $ join relA2 relB2
44−evalRelationalExpr (MakeStaticRelation attributes tupleSet) = do
44+evalRelationalExpr (MakeStaticRelation attributeSet tupleSet) = do
45− case mkRelation attributes tupleSet of
45+ case mkRelation attributeSet tupleSet of
46 Right rel -> return $ Right rel
46 Right rel -> return $ Right rel
47 Left err -> return $ Left err
47 Left err -> return $ Left err
@@ -102,31 +102,33 @@ dateExamples = DatabaseContext { inclusionDependencies = HS.empty, -- add foreig
102 products = productsRel
102 products = productsRel
103 supplierProducts = supplierProductsRel
103 supplierProducts = supplierProductsRel
105+suppliersRel :: Relation
105−suppliersRel = case mkRelation attributes tupleSet of
106+suppliersRel = case mkRelation attributeSet tupleSet of
106− Left err -> undefined
107+ Left _ -> undefined
107 Right rel -> rel
108 Right rel -> rel
109− attributes = M.fromList [("S#", Attribute "S#" StringAtomType),
110+ attributeSet = M.fromList [("S#", Attribute "S#" StringAtomType),
110 ("SNAME", Attribute "SNAME" StringAtomType),
111 ("SNAME", Attribute "SNAME" StringAtomType),
111 ("STATUS", Attribute "STATUS" IntAtomType),
112 ("STATUS", Attribute "STATUS" IntAtomType),
112 ("CITY", Attribute "CITY" StringAtomType)]
113 ("CITY", Attribute "CITY" StringAtomType)]
113− tupleSet = HS.fromList $ mkRelationTuples attributes [
114+ tupleSet = HS.fromList $ mkRelationTuples attributeSet [
114 M.fromList [("S#", StringAtom "S1") , ("SNAME", StringAtom "Smith"), ("STATUS", IntAtom 20) , ("CITY", StringAtom "London")],
115 M.fromList [("S#", StringAtom "S1") , ("SNAME", StringAtom "Smith"), ("STATUS", IntAtom 20) , ("CITY", StringAtom "London")],
115 M.fromList [("S#", StringAtom "S2"), ("SNAME", StringAtom "Jones"), ("STATUS", IntAtom 10), ("CITY", StringAtom "Paris")],
116 M.fromList [("S#", StringAtom "S2"), ("SNAME", StringAtom "Jones"), ("STATUS", IntAtom 10), ("CITY", StringAtom "Paris")],
116 M.fromList [("S#", StringAtom "S3"), ("SNAME", StringAtom "Blake"), ("STATUS", IntAtom 30), ("CITY", StringAtom "Paris")],
117 M.fromList [("S#", StringAtom "S3"), ("SNAME", StringAtom "Blake"), ("STATUS", IntAtom 30), ("CITY", StringAtom "Paris")],
117 M.fromList [("S#", StringAtom "S4"), ("SNAME", StringAtom "Clark"), ("STATUS", IntAtom 20), ("CITY", StringAtom "London")],
118 M.fromList [("S#", StringAtom "S4"), ("SNAME", StringAtom "Clark"), ("STATUS", IntAtom 20), ("CITY", StringAtom "London")],
118 M.fromList [("S#", StringAtom "S5"), ("SNAME", StringAtom "Adams"), ("STATUS", IntAtom 30), ("CITY", StringAtom "Athens")]]
119 M.fromList [("S#", StringAtom "S5"), ("SNAME", StringAtom "Adams"), ("STATUS", IntAtom 30), ("CITY", StringAtom "Athens")]]
121+productsRel :: Relation
120−productsRel = case mkRelation attributes tupleSet of
122+productsRel = case mkRelation attributeSet tupleSet of
121− Left err -> undefined
123+ Left _ -> undefined
122 Right rel -> rel
124 Right rel -> rel
124− attributes = M.fromList [("P#", Attribute "P#" StringAtomType),
126+ attributeSet = M.fromList [("P#", Attribute "P#" StringAtomType),
125 ("PNAME", Attribute "PNAME" StringAtomType),
127 ("PNAME", Attribute "PNAME" StringAtomType),
126 ("COLOR", Attribute "COLOR" StringAtomType),
128 ("COLOR", Attribute "COLOR" StringAtomType),
127 ("WEIGHT", Attribute "WEIGHT" IntAtomType),
129 ("WEIGHT", Attribute "WEIGHT" IntAtomType),
128 ("CITY", Attribute "CITY" StringAtomType)]
130 ("CITY", Attribute "CITY" StringAtomType)]
129− tupleSet = HS.fromList $ mkRelationTuples attributes [
131+ tupleSet = HS.fromList $ mkRelationTuples attributeSet [
130 M.fromList [("P#", StringAtom "P1"), ("PNAME", StringAtom "Nut"), ("COLOR", StringAtom "Red"), ("WEIGHT", IntAtom 12), ("CITY", StringAtom "London")],
132 M.fromList [("P#", StringAtom "P1"), ("PNAME", StringAtom "Nut"), ("COLOR", StringAtom "Red"), ("WEIGHT", IntAtom 12), ("CITY", StringAtom "London")],
131 M.fromList [("P#", StringAtom "P2"), ("PNAME", StringAtom "Bolt"), ("COLOR", StringAtom "Green"), ("WEIGHT", IntAtom 17), ("CITY", StringAtom "Paris")],
133 M.fromList [("P#", StringAtom "P2"), ("PNAME", StringAtom "Bolt"), ("COLOR", StringAtom "Green"), ("WEIGHT", IntAtom 17), ("CITY", StringAtom "Paris")],
132 M.fromList [("P#", StringAtom "P3"), ("PNAME", StringAtom "Screw"), ("COLOR", StringAtom "Blue"), ("WEIGHT", IntAtom 17), ("CITY", StringAtom "Oslo")],
134 M.fromList [("P#", StringAtom "P3"), ("PNAME", StringAtom "Screw"), ("COLOR", StringAtom "Blue"), ("WEIGHT", IntAtom 17), ("CITY", StringAtom "Oslo")],
@@ -135,15 +137,16 @@ productsRel = case mkRelation attributes tupleSet of
135 M.fromList [("P#", StringAtom "P6"), ("PNAME", StringAtom "Cog"), ("COLOR", StringAtom "Red"), ("WEIGHT", IntAtom 19), ("CITY", StringAtom "London")]
137 M.fromList [("P#", StringAtom "P6"), ("PNAME", StringAtom "Cog"), ("COLOR", StringAtom "Red"), ("WEIGHT", IntAtom 19), ("CITY", StringAtom "London")]
139−supplierProductsRel = case mkRelation attributes tupleSet of
141+supplierProductsRel :: Relation
142+supplierProductsRel = case mkRelation attributeSet tupleSet of
140− Left err -> undefined
143+ Left _ -> undefined
141 Right rel -> rel
144 Right rel -> rel
143− attributes = M.fromList [("S#", Attribute "S#" StringAtomType),
146+ attributeSet = M.fromList [("S#", Attribute "S#" StringAtomType),
144 ("P#", Attribute "P#" StringAtomType),
147 ("P#", Attribute "P#" StringAtomType),
145 ("QTY", Attribute "QTY" IntAtomType)]
148 ("QTY", Attribute "QTY" IntAtomType)]
146− tupleSet = HS.fromList $ mkRelationTuples attributes [
149+ tupleSet = HS.fromList $ mkRelationTuples attributeSet [
147 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P1"), ("QTY", IntAtom 300)],
150 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P1"), ("QTY", IntAtom 300)],
148 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P2"), ("QTY", IntAtom 200)],
151 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P2"), ("QTY", IntAtom 200)],
149 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P3"), ("QTY", IntAtom 400)],
152 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P3"), ("QTY", IntAtom 400)],
@@ -164,20 +167,20 @@ supplierProductsRel = case mkRelation attributes tupleSet of
164--helper function to process relation variable creation/assignment
167--helper function to process relation variable creation/assignment
165setRelVar :: String -> Relation -> DatabaseState (Maybe RelationalError)
168setRelVar :: String -> Relation -> DatabaseState (Maybe RelationalError)
166setRelVar relVarName rel = do
169setRelVar relVarName rel = do
167− state <- get
170+ currstate <- get
168− let newRelVars = M.insert relVarName rel $ relationVariables state
171+ let newRelVars = M.insert relVarName rel $ relationVariables currstate
169− case checkConstraints state of
172+ case checkConstraints currstate of
170 Just err -> return $ Just err
173 Just err -> return $ Just err
171 Nothing -> do
174 Nothing -> do
172− put $ DatabaseContext (inclusionDependencies state) newRelVars
175+ put $ DatabaseContext (inclusionDependencies currstate) newRelVars
173 return Nothing
176 return Nothing
175-- it is not an error to delete a relvar which does not exist, just like it is not an error to insert a pre-existing tuple into a relation
178-- it is not an error to delete a relvar which does not exist, just like it is not an error to insert a pre-existing tuple into a relation
176deleteRelVar :: String -> DatabaseState (Maybe RelationalError)
179deleteRelVar :: String -> DatabaseState (Maybe RelationalError)
177deleteRelVar relVarName = do
180deleteRelVar relVarName = do
178− state <- get
181+ currstate <- get
179− let newRelVars = M.delete relVarName (relationVariables state)
182+ let newRelVars = M.delete relVarName (relationVariables currstate)
180− put $ DatabaseContext (inclusionDependencies state) newRelVars
183+ put $ DatabaseContext (inclusionDependencies currstate) newRelVars
181 return Nothing
184 return Nothing
183evalContextExpr :: DatabaseExpr -> DatabaseState (Maybe RelationalError)
186evalContextExpr :: DatabaseExpr -> DatabaseState (Maybe RelationalError)
@@ -217,9 +220,9 @@ evalContextExpr (Delete relVarName predicate) = do
218--union of restricted+updated portion and the unrestricted+unupdated portion
221--union of restricted+updated portion and the unrestricted+unupdated portion
219evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = do
222evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = do
220− state <- get
223+ currstate <- get
221− let relVarTable = relationVariables state
224+ let relVarTable = relationVariables currstate
222− case predicateRestrictionFilter state restrictionPredicateExpr of
225+ case predicateRestrictionFilter currstate restrictionPredicateExpr of
223 Left err -> return $ Just err
226 Left err -> return $ Just err
224 Right predicateFunc -> do
227 Right predicateFunc -> do
225 case M.lookup relVarName relVarTable of
228 case M.lookup relVarName relVarTable of
@@ -228,18 +231,18 @@ evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = d
228 Left err -> return $ Just err
231 Left err -> return $ Just err
229 Right updatedRel -> setRelVar relVarName updatedRel
232 Right updatedRel -> setRelVar relVarName updatedRel
231− makeUpdatedRel rel = do
234+ makeUpdatedRel relin = do
232− restrictedPortion <- restrict predicateFunc rel
235+ restrictedPortion <- restrict predicateFunc relin
233− unrestrictedPortion <- restrict (not . predicateFunc) rel
236+ unrestrictedPortion <- restrict (not . predicateFunc) relin
234 updatedPortion <- relMap tupleUpdater restrictedPortion
237 updatedPortion <- relMap tupleUpdater restrictedPortion
235 union updatedPortion unrestrictedPortion
238 union updatedPortion unrestrictedPortion
236 tupleUpdater = \(RelationTuple tupMap) -> RelationTuple $ M.union attrAssignments tupMap
239 tupleUpdater = \(RelationTuple tupMap) -> RelationTuple $ M.union attrAssignments tupMap
239evalContextExpr (AddInclusionDependency dep) = do
242evalContextExpr (AddInclusionDependency dep) = do
240− state <- get
243+ currstate <- get
241− let newDeps = HS.insert dep (inclusionDependencies state)
244+ let newDeps = HS.insert dep (inclusionDependencies currstate)
242− put $ DatabaseContext newDeps (relationVariables state)
245+ put $ DatabaseContext newDeps (relationVariables currstate)
243 return Nothing
246 return Nothing
245evalContextExpr (MultipleExpr exprs) = do
248evalContextExpr (MultipleExpr exprs) = do
@@ -248,7 +251,7 @@ evalContextExpr (MultipleExpr exprs) = do
248 --some lifting magic needed here
251 --some lifting magic needed here
249 case catMaybes evald of
252 case catMaybes evald of
250 [] -> return $ Nothing
253 [] -> return $ Nothing
251− err:l -> return $ Just err
254+ err:_ -> return $ Just err
253-- restrict relvar to get affected tuples, update tuples, delete restriction from relvar, relvar = relvar union updated tuples
256-- restrict relvar to get affected tuples, update tuples, delete restriction from relvar, relvar = relvar union updated tuples
254--evalRelVarExpr (Update relVarName updateMap) = do
257--evalRelVarExpr (Update relVarName updateMap) = do
@@ -257,7 +260,7 @@ evalContextExpr (MultipleExpr exprs) = do
257checkConstraints :: DatabaseContext -> Maybe RelationalError
260checkConstraints :: DatabaseContext -> Maybe RelationalError
258checkConstraints context = case failures of
261checkConstraints context = case failures of
259 [] -> Nothing
262 [] -> Nothing
260− l:ls -> Just l
263+ l:_ -> Just l
262 failures = catMaybes $ map checkIncDep (HS.toList deps)
265 failures = catMaybes $ map checkIncDep (HS.toList deps)
263 deps = inclusionDependencies context
266 deps = inclusionDependencies context
@@ -292,9 +295,9 @@ checkConstraint (InclusionDependency name subDep superDep) = do
293typeForRelationalExpr :: RelationalExpr -> DatabaseState (Either RelationalError Relation)
296typeForRelationalExpr :: RelationalExpr -> DatabaseState (Either RelationalError Relation)
294typeForRelationalExpr expr = do
297typeForRelationalExpr expr = do
295− state <- get
298+ currstate <- get
296 --replace the relationVariables context element with a cloned set of relation devoid of tuples
299 --replace the relationVariables context element with a cloned set of relation devoid of tuples
297− put $ contextWithEmptyTupleSets state
300+ put $ contextWithEmptyTupleSets currstate
298 evalRelationalExpr expr
301 evalRelationalExpr expr
300--returns a database context with all tuples removed
303--returns a database context with all tuples removed
@@ -315,7 +318,7 @@ predicateRestrictionFilter context (OrPredicate expr1 expr2) = do
315 expr2v <- predicateRestrictionFilter context expr2
318 expr2v <- predicateRestrictionFilter context expr2
316 return $ \x -> expr1v x || expr2v x
319 return $ \x -> expr1v x || expr2v x
318−predicateRestrictionFilter context TruePredicate = Right $ \x -> True
321+predicateRestrictionFilter _ TruePredicate = Right $ \_ -> True
320predicateRestrictionFilter context (NotPredicate expr) = do
323predicateRestrictionFilter context (NotPredicate expr) = do
321 exprv <- predicateRestrictionFilter context expr
324 exprv <- predicateRestrictionFilter context expr
@@ -324,13 +327,13 @@ predicateRestrictionFilter context (NotPredicate expr) = do
324predicateRestrictionFilter context (RelationalExprPredicate relExpr) = case runState (evalRelationalExpr relExpr) context of
327predicateRestrictionFilter context (RelationalExprPredicate relExpr) = case runState (evalRelationalExpr relExpr) context of
325 (Left err, _) -> Left err
328 (Left err, _) -> Left err
326 (Right rel, _) -> if rel == relationTrue then
329 (Right rel, _) -> if rel == relationTrue then
327− Right $ \x -> True
330+ Right $ \_ -> True
328 else if rel == relationFalse then
331 else if rel == relationFalse then
329− Right $ \x -> False
332+ Right $ \_ -> False
331 Left $ PredicateExpressionError "Relational restriction filter must evaluate to 'true' or 'false'"
334 Left $ PredicateExpressionError "Relational restriction filter must evaluate to 'true' or 'false'"
333−predicateRestrictionFilter context (AttributeEqualityPredicate attrName atom) = Right $ \(RelationTuple tupMap) -> tupValue tupMap == atom
336+predicateRestrictionFilter _ (AttributeEqualityPredicate attrName atom) = Right $ \(RelationTuple tupMap) -> tupValue tupMap == atom
335 tupValue tupMap = tupMap M.! attrName
338 tupValue tupMap = tupMap M.! attrName