on master help sign in
add support to tutd for Maybe Int and Maybe Text types
commit6dae00ed511e841fb58912ce755e4eb3ddd4af00 authorAgentM <agentm@themactionfaction.com> dateFri 16 Oct 2015, 11:47 (11 years ago) parentff2a98b
2 files changed+31−2
TutorialD/Interpreter/RelationalExpr.hs+22−1 test/TutorialD/Interpreter.hs+9−1
TutorialD/Interpreter/RelationalExpr.hs+22−1
@@ -226,7 +226,15 @@ nakedAtomExprP :: Parser AtomExpr
226226nakedAtomExprP = NakedAtomExpr <$> atomP
227227
228228atomP :: Parser Atom
229−atomP = dateTimeAtomP <|> dateAtomP <|> stringAtomP <|> doubleAtomP <|> intAtomP <|> boolAtomP <|> relationAtomP
229+atomP = dateTimeAtomP <|>
230+ dateAtomP <|>
231+ maybeTextAtomP <|>
232+ maybeIntAtomP <|>
233+ stringAtomP <|>
234+ doubleAtomP <|>
235+ intAtomP <|>
236+ boolAtomP <|>
237+ relationAtomP
230238
231239functionAtomExprP :: Parser AtomExpr
232240functionAtomExprP = do
@@ -240,6 +248,19 @@ relationalAtomExprP = RelationAtomExpr <$> relExprP
240248stringAtomP :: Parser Atom
241249stringAtomP = liftA (Atom . T.pack) quotedString
242250
251+--until polymorphic type constructors and algebraic data types are properly supported, such constructs must be unfortunately hard-coded
252+maybeTextAtomP :: Parser Atom
253+maybeTextAtomP = do
254+ maybeText <- try $ ((Just . T.pack <$> (reserved "Just" *> quotedString)) <|>
255+ (reserved "nothing" *> return Nothing)) <* reserved "::maybe char"
256+ return $ Atom maybeText
257+
258+maybeIntAtomP :: Parser Atom
259+maybeIntAtomP = do
260+ maybeInt <- try $ ((Just . fromIntegral <$> (reserved "Just" *> integer)) <|>
261+ (reserved "nothing" *> return Nothing)) <* reserved "::maybe int"
262+ return $ Atom (maybeInt :: Maybe Int)
263+
243264dateTimeAtomP :: Parser Atom
244265dateTimeAtomP = do
245266 dateTimeString' <- try $ do
test/TutorialD/Interpreter.hs+9−1
@@ -18,6 +18,8 @@ import System.Exit
1818import Data.Maybe (isJust)
1919import System.IO
2020import qualified Data.Vector as V
21+import Data.Typeable (Proxy(..))
22+import Data.Text (Text)
2123
2224--urgent: add group and ungroup tests- I missed the group relation type bug
2325main :: IO ()
@@ -59,6 +61,8 @@ main = do
5961 simpleBAttributes = A.attributesFromList [Attribute "d" intAtomType]
6062 simpleCAttributes = A.attributesFromList [Attribute "a" stringAtomType, Attribute "b" intAtomType]
6163 simpleDAttributes = A.attributesFromList [Attribute "a" intAtomType, Attribute "b" intAtomType]
64+ simpleMaybeTextAttributes = A.attributesFromList [Attribute "a" $ atomTypeForProxy (Proxy :: Proxy (Maybe Text))]
65+ simpleMaybeIntAttributes = A.attributesFromList [Attribute "a" $ atomTypeForProxy (Proxy :: Proxy (Maybe Int))]
6266 simpleProjectionAttributes = A.attributesFromList [Attribute "c" intAtomType]
6367 nestedRelationAttributes = A.attributesFromList [Attribute "a" intAtomType, Attribute "b" (RelationAtomType $ A.attributesFromList [Attribute "a" intAtomType])]
6468 extendTestAttributes = A.attributesFromList [Attribute "a" intAtomType, Attribute "b" $ RelationAtomType (attributes suppliersRel)]
@@ -89,7 +93,11 @@ main = do
8993 ("x:=S{all but S#} = S{CITY,SNAME,STATUS}", Right $ relationTrue),
9094 --test key syntax
9195 ("x:=S; key testconstraint {S#,CITY} x; insert x relation{tuple{CITY \"London\", S# \"S1\", SNAME \"gonk\", STATUS 50}}", Left (InclusionDependencyCheckError "testconstraint")),
92− ("y:=S; key testconstraint {S#} y; insert y relation{tuple{CITY \"London\", S# \"S6\", SNAME \"gonk\", STATUS 50}}; x:=y{S#} = S{S#} union relation{tuple{S# \"S6\"}}", Right $ relationTrue)
96+ ("y:=S; key testconstraint {S#} y; insert y relation{tuple{CITY \"London\", S# \"S6\", SNAME \"gonk\", STATUS 50}}; x:=y{S#} = S{S#} union relation{tuple{S# \"S6\"}}", Right $ relationTrue),
97+ --test Maybe Text
98+ ("x:=relation{tuple{a Just \"spam\"::maybe char}}", mkRelationFromList simpleMaybeTextAttributes [[Atom (Just "spam" :: Maybe Text)]]),
99+ --test Maybe Int
100+ ("x:=relation{tuple{a Just 3::maybe int}}", mkRelationFromList simpleMaybeIntAttributes [[Atom (Just 3 :: Maybe Int)]])
93101 ]
94102
95103