on master help sign in
add relational equality operator to Tutorial D
f95e64cAgentM
RelationExpr.hs+12−1
@@ -72,6 +72,17 @@ evalRelationalExpr (Restrict predicateExpr relExpr) = do
72 Right rel -> case predicateRestrictionFilter context predicateExpr of
72 Right rel -> case predicateRestrictionFilter context predicateExpr of
73 Left err -> return $ Left err
73 Left err -> return $ Left err
74 Right filterfunc -> return $ restrict filterfunc rel
74 Right filterfunc -> return $ restrict filterfunc rel
75+
76+evalRelationalExpr (Equals relExprA relExprB) = do
77+ evaldA <- evalRelationalExpr relExprA
78+ evaldB <- evalRelationalExpr relExprB
79+ case evaldA of
80+ Left err -> return $ Left err
81+ Right relA -> case evaldB of
82+ Left err -> return $ Left err
83+ Right relB -> return $ Right $ if relA == relB then relationTrue else relationFalse
84+
85+
75
86
76emptyDatabaseContext :: DatabaseContext
87emptyDatabaseContext :: DatabaseContext
77emptyDatabaseContext = DatabaseContext { inclusionDependencies = HS.empty,
88emptyDatabaseContext = DatabaseContext { inclusionDependencies = HS.empty,
@@ -130,7 +141,7 @@ supplierProductsRel = case mkRelation attributes tupleSet of
130 where
141 where
131 attributes = M.fromList [("S#", Attribute "S#" StringAtomType),
142 attributes = M.fromList [("S#", Attribute "S#" StringAtomType),
132 ("P#", Attribute "P#" StringAtomType),
143 ("P#", Attribute "P#" StringAtomType),
133− ("QTY", Attribute "QTY" StringAtomType)]
144+ ("QTY", Attribute "QTY" IntAtomType)]
134 tupleSet = HS.fromList $ mkRelationTuples attributes [
145 tupleSet = HS.fromList $ mkRelationTuples attributes [
135 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P1"), ("QTY", IntAtom 300)],
146 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P1"), ("QTY", IntAtom 300)],
136 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P2"), ("QTY", IntAtom 200)],
147 M.fromList [("S#", StringAtom "S1"), ("P#", StringAtom "P2"), ("QTY", IntAtom 200)],
RelationType.hs+1
@@ -51,6 +51,7 @@ data RelationalExpr where
51 Group :: S.Set AttributeName -> AttributeName -> RelationalExpr -> RelationalExpr
51 Group :: S.Set AttributeName -> AttributeName -> RelationalExpr -> RelationalExpr
52 Ungroup :: AttributeName -> RelationalExpr -> RelationalExpr
52 Ungroup :: AttributeName -> RelationalExpr -> RelationalExpr
53 Restrict :: RestrictionPredicateExpr -> RelationalExpr -> RelationalExpr
53 Restrict :: RestrictionPredicateExpr -> RelationalExpr -> RelationalExpr
54+ Equals :: RelationalExpr -> RelationalExpr -> RelationalExpr
54
55
55{- maybe break this into multiple steps:
56{- maybe break this into multiple steps:
561. check relational types for match (attribute counts) (typechecking step
571. check relational types for match (attribute counts) (typechecking step
TutorialDInterpreter.hs+21−2
@@ -61,7 +61,25 @@ attributeAndType = do
61 case tutDTypeToAtomType attrTypeName of
61 case tutDTypeToAtomType attrTypeName of
62 Just t -> return $ Attribute attrName t
62 Just t -> return $ Attribute attrName t
63 Nothing -> fail (attrTypeName ++ " is not a valid type name.")
63 Nothing -> fail (attrTypeName ++ " is not a valid type name.")
64−
64+
65+tupleP :: Parser RelationTuple
66+tupleP = do
67+ reservedOp "tuple"
68+ attrValues <- braces (sepBy tupleAtomP comma)
69+ return $ RelationTuple (M.fromList attrValues)
70+
71+tupleAtomP :: Parser (AttributeName, Atom)
72+tupleAtomP = do
73+ attributeName <- identifier
74+ attrType <- identifier
75+ atom <- parens (stringAtomP <|> intAtomP)
76+ case tutDTypeToAtomType attrType of
77+ Nothing -> fail (attrType ++ " is not a valid type name.")
78+ Just typeA -> if typeA == atomTypeForAtom atom then
79+ return $ (attributeName, atom)
80+ else
81+ fail "type mismatch in tuple generation"
82+
65--convert Tutorial D type to AtomType
83--convert Tutorial D type to AtomType
66tutDTypeToAtomType :: String -> Maybe AtomType
84tutDTypeToAtomType :: String -> Maybe AtomType
67tutDTypeToAtomType tutDType = case tutDType of
85tutDTypeToAtomType tutDType = case tutDType of
@@ -139,7 +157,8 @@ relOperators = [
139 [Postfix groupP],
157 [Postfix groupP],
140 [Postfix ungroupP],
158 [Postfix ungroupP],
141 [Infix (reservedOp "join" >> return Join) AssocLeft],
159 [Infix (reservedOp "join" >> return Join) AssocLeft],
142− [Infix (reservedOp "union" >> return Union) AssocLeft]
160+ [Infix (reservedOp "union" >> return Union) AssocLeft],
161+ [Infix (reservedOp "=" >> return Equals) AssocNone]
143 ]
162 ]
144
163
145relExpr :: Parser RelationalExpr
164relExpr :: Parser RelationalExpr
TutorialDInterpreterTest.hs+3−1
@@ -28,7 +28,9 @@ main = do
28 ("x:=true where true or false", Right relationTrue),
28 ("x:=true where true or false", Right relationTrue),
29 ("x:=true where false or false", Right relationFalse),
29 ("x:=true where false or false", Right relationFalse),
30 ("x:=true where true and false", Right relationFalse),
30 ("x:=true where true and false", Right relationFalse),
31− ("x:=true where true and true", Right relationTrue)
31+ ("x:=true where true and true", Right relationTrue),
32+ ("x:=true=true", Right relationTrue),
33+ ("x:=true=false", Right relationFalse)
32 ]
34 ]
33 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
35 simpleAAttributes = M.fromList [("a", Attribute "a" IntAtomType)]
34 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]
36 simpleBAttributes = M.fromList [("d", Attribute "d" IntAtomType)]