@@ -1,9 +1,9 @@
1−module RelationExpr where
2−import Relation
3−import RelationTuple
4−import RelationTupleSet
5−import RelationType
6−import RelationalError
1+module ProjectM36.RelationalExpression where
2+import ProjectM36.Relation
3+import ProjectM36.Tuple
4+import ProjectM36.TupleSet
5+import ProjectM36.Base
6+import ProjectM36.Error
77import qualified Data.Map as M
88import qualified Data.HashSet as HS
99import Control.Monad.State hiding (join)
@@ -20,7 +20,7 @@ evalRelationalExpr (RelationVariable name) = do
2020evalRelationalExpr (Project attrNameSet expr) = do
2121 rel <- evalRelationalExpr expr
2222 case rel of
23− Right rel -> return $ project attrNameSet rel
23+ Right rel2 -> return $ project attrNameSet rel2
2424 Left err -> return $ Left err
2525
2626evalRelationalExpr (Union exprA exprB) = do
@@ -28,21 +28,21 @@ evalRelationalExpr (Union exprA exprB) = do
2828 relB <- evalRelationalExpr exprB
2929 case relA of
3030 Left err -> return $ Left err
31− Right relA -> case relB of
31+ Right relA2 -> case relB of
3232 Left err -> return $ Left err
33− Right relB -> return $ union relA relB
33+ Right relB2 -> return $ union relA2 relB2
3434
3535evalRelationalExpr (Join exprA exprB) = do
3636 relA <- evalRelationalExpr exprA
3737 relB <- evalRelationalExpr exprB
3838 case relA of
3939 Left err -> return $ Left err
40− Right relA -> case relB of
40+ Right relA2 -> case relB of
4141 Left err -> return $ Left err
42− Right relB -> return $ join relA relB
42+ Right relB2 -> return $ join relA2 relB2
4343
44−evalRelationalExpr (MakeStaticRelation attributes tupleSet) = do
45− case mkRelation attributes tupleSet of
44+evalRelationalExpr (MakeStaticRelation attributeSet tupleSet) = do
45+ case mkRelation attributeSet tupleSet of
4646 Right rel -> return $ Right rel
4747 Left err -> return $ Left err
4848
@@ -102,31 +102,33 @@ dateExamples = DatabaseContext { inclusionDependencies = HS.empty, -- add foreig
102102 products = productsRel
103103 supplierProducts = supplierProductsRel
104104
105−suppliersRel = case mkRelation attributes tupleSet of
106− Left err -> undefined
105+suppliersRel :: Relation
106+suppliersRel = case mkRelation attributeSet tupleSet of
107+ Left _ -> undefined
107108 Right rel -> rel
108109 where
109− attributes = M.fromList [("S#", Attribute "S#" StringAtomType),
110+ attributeSet = M.fromList [("S#", Attribute "S#" StringAtomType),
110111 ("SNAME", Attribute "SNAME" StringAtomType),
111112 ("STATUS", Attribute "STATUS" IntAtomType),
112113 ("CITY", Attribute "CITY" StringAtomType)]
113− tupleSet = HS.fromList $ mkRelationTuples attributes [
114+ tupleSet = HS.fromList $ mkRelationTuples attributeSet [
114115 M.fromList [("S#", StringAtom "S1") , ("SNAME", StringAtom "Smith"), ("STATUS", IntAtom 20) , ("CITY", StringAtom "London")],
115116 M.fromList [("S#", StringAtom "S2"), ("SNAME", StringAtom "Jones"), ("STATUS", IntAtom 10), ("CITY", StringAtom "Paris")],
116117 M.fromList [("S#", StringAtom "S3"), ("SNAME", StringAtom "Blake"), ("STATUS", IntAtom 30), ("CITY", StringAtom "Paris")],
117118 M.fromList [("S#", StringAtom "S4"), ("SNAME", StringAtom "Clark"), ("STATUS", IntAtom 20), ("CITY", StringAtom "London")],
118119 M.fromList [("S#", StringAtom "S5"), ("SNAME", StringAtom "Adams"), ("STATUS", IntAtom 30), ("CITY", StringAtom "Athens")]]
119120
120−productsRel = case mkRelation attributes tupleSet of
121− Left err -> undefined
121+productsRel :: Relation
122+productsRel = case mkRelation attributeSet tupleSet of
123+ Left _ -> undefined
122124 Right rel -> rel
123125 where
124− attributes = M.fromList [("P#", Attribute "P#" StringAtomType),
126+ attributeSet = M.fromList [("P#", Attribute "P#" StringAtomType),
125127 ("PNAME", Attribute "PNAME" StringAtomType),
126128 ("COLOR", Attribute "COLOR" StringAtomType),
127129 ("WEIGHT", Attribute "WEIGHT" IntAtomType),
128130 ("CITY", Attribute "CITY" StringAtomType)]
129− tupleSet = HS.fromList $ mkRelationTuples attributes [
131+ tupleSet = HS.fromList $ mkRelationTuples attributeSet [
130132 M.fromList [("P#", StringAtom "P1"), ("PNAME", StringAtom "Nut"), ("COLOR", StringAtom "Red"), ("WEIGHT", IntAtom 12), ("CITY", StringAtom "London")],
131133 M.fromList [("P#", StringAtom "P2"), ("PNAME", StringAtom "Bolt"), ("COLOR", StringAtom "Green"), ("WEIGHT", IntAtom 17), ("CITY", StringAtom "Paris")],
132134 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
135137 M.fromList [("P#", StringAtom "P6"), ("PNAME", StringAtom "Cog"), ("COLOR", StringAtom "Red"), ("WEIGHT", IntAtom 19), ("CITY", StringAtom "London")]
136138
137139 ]
138−
139−supplierProductsRel = case mkRelation attributes tupleSet of
140− Left err -> undefined
140+
141+supplierProductsRel :: Relation
142+supplierProductsRel = case mkRelation attributeSet tupleSet of
143+ Left _ -> undefined
141144 Right rel -> rel
142145 where
143− attributes = M.fromList [("S#", Attribute "S#" StringAtomType),
146+ attributeSet = M.fromList [("S#", Attribute "S#" StringAtomType),
144147 ("P#", Attribute "P#" StringAtomType),
145148 ("QTY", Attribute "QTY" IntAtomType)]
146− tupleSet = HS.fromList $ mkRelationTuples attributes [
149+ tupleSet = HS.fromList $ mkRelationTuples attributeSet [
147150 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P1"), ("QTY", IntAtom 300)],
148151 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P2"), ("QTY", IntAtom 200)],
149152 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P3"), ("QTY", IntAtom 400)],
@@ -164,20 +167,20 @@ supplierProductsRel = case mkRelation attributes tupleSet of
164167--helper function to process relation variable creation/assignment
165168setRelVar :: String -> Relation -> DatabaseState (Maybe RelationalError)
166169setRelVar relVarName rel = do
167− state <- get
168− let newRelVars = M.insert relVarName rel $ relationVariables state
169− case checkConstraints state of
170+ currstate <- get
171+ let newRelVars = M.insert relVarName rel $ relationVariables currstate
172+ case checkConstraints currstate of
170173 Just err -> return $ Just err
171174 Nothing -> do
172− put $ DatabaseContext (inclusionDependencies state) newRelVars
175+ put $ DatabaseContext (inclusionDependencies currstate) newRelVars
173176 return Nothing
174177
175178-- 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
176179deleteRelVar :: String -> DatabaseState (Maybe RelationalError)
177180deleteRelVar relVarName = do
178− state <- get
179− let newRelVars = M.delete relVarName (relationVariables state)
180− put $ DatabaseContext (inclusionDependencies state) newRelVars
181+ currstate <- get
182+ let newRelVars = M.delete relVarName (relationVariables currstate)
183+ put $ DatabaseContext (inclusionDependencies currstate) newRelVars
181184 return Nothing
182185
183186evalContextExpr :: DatabaseExpr -> DatabaseState (Maybe RelationalError)
@@ -217,9 +220,9 @@ evalContextExpr (Delete relVarName predicate) = do
217220
218221--union of restricted+updated portion and the unrestricted+unupdated portion
219222evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = do
220− state <- get
221− let relVarTable = relationVariables state
222− case predicateRestrictionFilter state restrictionPredicateExpr of
223+ currstate <- get
224+ let relVarTable = relationVariables currstate
225+ case predicateRestrictionFilter currstate restrictionPredicateExpr of
223226 Left err -> return $ Just err
224227 Right predicateFunc -> do
225228 case M.lookup relVarName relVarTable of
@@ -228,18 +231,18 @@ evalContextExpr (Update relVarName attrAssignments restrictionPredicateExpr) = d
228231 Left err -> return $ Just err
229232 Right updatedRel -> setRelVar relVarName updatedRel
230233 where
231− makeUpdatedRel rel = do
232− restrictedPortion <- restrict predicateFunc rel
233− unrestrictedPortion <- restrict (not . predicateFunc) rel
234+ makeUpdatedRel relin = do
235+ restrictedPortion <- restrict predicateFunc relin
236+ unrestrictedPortion <- restrict (not . predicateFunc) relin
234237 updatedPortion <- relMap tupleUpdater restrictedPortion
235238 union updatedPortion unrestrictedPortion
236239 tupleUpdater = \(RelationTuple tupMap) -> RelationTuple $ M.union attrAssignments tupMap
237240
238241
239242evalContextExpr (AddInclusionDependency dep) = do
240− state <- get
241− let newDeps = HS.insert dep (inclusionDependencies state)
242− put $ DatabaseContext newDeps (relationVariables state)
243+ currstate <- get
244+ let newDeps = HS.insert dep (inclusionDependencies currstate)
245+ put $ DatabaseContext newDeps (relationVariables currstate)
243246 return Nothing
244247
245248evalContextExpr (MultipleExpr exprs) = do
@@ -248,7 +251,7 @@ evalContextExpr (MultipleExpr exprs) = do
248251 --some lifting magic needed here
249252 case catMaybes evald of
250253 [] -> return $ Nothing
251− err:l -> return $ Just err
254+ err:_ -> return $ Just err
252255
253256-- restrict relvar to get affected tuples, update tuples, delete restriction from relvar, relvar = relvar union updated tuples
254257--evalRelVarExpr (Update relVarName updateMap) = do
@@ -257,7 +260,7 @@ evalContextExpr (MultipleExpr exprs) = do
257260checkConstraints :: DatabaseContext -> Maybe RelationalError
258261checkConstraints context = case failures of
259262 [] -> Nothing
260− l:ls -> Just l
263+ l:_ -> Just l
261264 where
262265 failures = catMaybes $ map checkIncDep (HS.toList deps)
263266 deps = inclusionDependencies context
@@ -292,9 +295,9 @@ checkConstraint (InclusionDependency name subDep superDep) = do
292295
293296typeForRelationalExpr :: RelationalExpr -> DatabaseState (Either RelationalError Relation)
294297typeForRelationalExpr expr = do
295− state <- get
298+ currstate <- get
296299 --replace the relationVariables context element with a cloned set of relation devoid of tuples
297− put $ contextWithEmptyTupleSets state
300+ put $ contextWithEmptyTupleSets currstate
298301 evalRelationalExpr expr
299302
300303--returns a database context with all tuples removed
@@ -315,7 +318,7 @@ predicateRestrictionFilter context (OrPredicate expr1 expr2) = do
315318 expr2v <- predicateRestrictionFilter context expr2
316319 return $ \x -> expr1v x || expr2v x
317320
318−predicateRestrictionFilter context TruePredicate = Right $ \x -> True
321+predicateRestrictionFilter _ TruePredicate = Right $ \_ -> True
319322
320323predicateRestrictionFilter context (NotPredicate expr) = do
321324 exprv <- predicateRestrictionFilter context expr
@@ -324,13 +327,13 @@ predicateRestrictionFilter context (NotPredicate expr) = do
324327predicateRestrictionFilter context (RelationalExprPredicate relExpr) = case runState (evalRelationalExpr relExpr) context of
325328 (Left err, _) -> Left err
326329 (Right rel, _) -> if rel == relationTrue then
327− Right $ \x -> True
330+ Right $ \_ -> True
328331 else if rel == relationFalse then
329− Right $ \x -> False
332+ Right $ \_ -> False
330333 else
331334 Left $ PredicateExpressionError "Relational restriction filter must evaluate to 'true' or 'false'"
332335
333−predicateRestrictionFilter context (AttributeEqualityPredicate attrName atom) = Right $ \(RelationTuple tupMap) -> tupValue tupMap == atom
336+predicateRestrictionFilter _ (AttributeEqualityPredicate attrName atom) = Right $ \(RelationTuple tupMap) -> tupValue tupMap == atom
334337 where
335338 tupValue tupMap = tupMap M.! attrName
336339