2module TutorialDInterpreter where
9import Text.Parsec.String
10import Text.Parsec.Expr
11import Text.Parsec.Language
12import qualified Text.Parsec.Token as Token
13import qualified Data.Set as S
14import qualified Data.HashSet as HS
15import qualified Data.Map as M
16import qualified Data.List as L
17import Control.Applicative (liftA, (<*))
18import Control.Monad.State
19import System.Console.Readline
22lexer :: Token.TokenParser ()
23lexer = Token.makeTokenParser tutD
24 where tutD = emptyDef {
25 Token.reservedOpNames = ["join", "where", "union", "group", "ungroup"],
26 Token.reservedNames = [],
27 Token.identStart = letter <|> char '_',
28 Token.identLetter = alphaNum <|> char '_'}
30parens = Token.parens lexer
31reservedOp = Token.reservedOp lexer
32reserved = Token.reserved lexer
33braces = Token.braces lexer
34identifier = Token.identifier lexer
35comma = Token.comma lexer
36semi = Token.semi lexer
39attributeList :: Parser [AttributeName]
40attributeList = sepBy identifier comma
42makeRelation :: Parser RelationalExpr
45 attrs <- makeAttributes
46 return $ MakeStaticRelation attrs HS.empty
48--used in relation creation
49makeAttributes :: Parser Attributes
51 attrList <- braces (sepBy attributeAndType comma)
52 return $ M.fromList $ map toAttributeAssocList attrList
54 toAttributeAssocList attr@(Attribute attrName _) = (attrName, attr)
56attributeAndType :: Parser Attribute
58 attrName <- identifier
59 attrTypeName <- identifier
60 --convert type name into type
61 case tutDTypeToAtomType attrTypeName of
62 Just t -> return $ Attribute attrName t
63 Nothing -> fail (attrTypeName ++ " is not a valid type name.")
65--convert Tutorial D type to AtomType
66tutDTypeToAtomType :: String -> Maybe AtomType
67tutDTypeToAtomType tutDType = case tutDType of
68 "char" -> Just StringAtomType
69 "int" -> Just IntAtomType
72atomTypeToTutDType :: AtomType -> Maybe String
73atomTypeToTutDType atomType = case atomType of
74 StringAtomType -> Just "char"
75 IntAtomType -> Just "int"
76 --RelationAtomType rel ->
79relVarP :: Parser RelationalExpr
80relVarP = liftA RelationVariable identifier
82relTerm = parens relExpr
87 attrs <- braces attributeList
88 return $ Project (S.fromList attrs)
90assignP :: Parser DatabaseExpr
92 relVarName <- identifier
95 return $ Assign relVarName expr
100 newAttr <- identifier
101 return $ (oldAttr, newAttr)
103renameP :: Parser (RelationalExpr -> RelationalExpr)
106 (oldAttr, newAttr) <- braces renameClause
107 return $ Rename oldAttr newAttr
110 attrs <- braces attributeList
112 newAttrName <- identifier
113 return $ (attrs, newAttrName)
115groupP :: Parser (RelationalExpr -> RelationalExpr)
118 (groupAttrList, groupAttrName) <- parens groupClause
119 return $ Group (S.fromList groupAttrList) groupAttrName
121--in "Time and Relational Theory" (2014), Date's Tutorial D grammar for ungroup takes one attribute, while in previous books, it take multiple arguments. Let us assume that nested ungroups are the same as multiple attributes.
122ungroupP :: Parser (RelationalExpr -> RelationalExpr)
125 rvaAttrName <- identifier
126 return $ Ungroup rvaAttrName
133 [Infix (reservedOp "join" >> return Join) AssocLeft],
134 [Infix (reservedOp "union" >> return Union) AssocLeft]
137relExpr :: Parser RelationalExpr
138relExpr = buildExpressionParser relOperators relTerm
140databaseExpr :: Parser DatabaseExpr
141databaseExpr = insertP
148multipleDatabaseExpr :: Parser DatabaseExpr
149multipleDatabaseExpr = do
150 exprs <- sepBy1 databaseExpr semi
151 return $ MultipleExpr exprs
153insertP :: Parser DatabaseExpr
158 return $ Insert relvar expr
160defineP :: Parser DatabaseExpr
162 relVarName <- identifier
164 attributes <- makeAttributes
165 return $ Define relVarName attributes
167deleteP :: Parser DatabaseExpr
170 relVarName <- identifier
171 return $ Delete relVarName
173updateP :: Parser DatabaseExpr
176 relVarName <- identifier
178 attributeAssignments <- liftM M.fromList $ parens (sepBy attributeAssignment comma)
179 return $ Update relVarName attributeAssignments
181constraintP :: Parser DatabaseExpr
183 reservedOp "constraint"
184 constraintName <- identifier
188 return $ AddInclusionDependency (InclusionDependency constraintName subset superset)
190attributeAssignment :: Parser (String, RelationalExpr)
191attributeAssignment = do
192 attrName <- identifier
195 return $ (attrName, relExpr)
197parseString :: String -> Either RelationalError DatabaseExpr
198parseString str = case parse multipleDatabaseExpr "" str of
199 Left err -> Left $ ParseError (show err)
202data TutorialDOperator where
203 ShowRelation :: RelationalExpr -> TutorialDOperator
204 ShowRelationVariableType :: String -> TutorialDOperator
205 ShowConstraint :: String -> TutorialDOperator
208typeP :: Parser TutorialDOperator
211 relVarName <- identifier
212 return $ ShowRelationVariableType relVarName
214showRelP :: Parser TutorialDOperator
218 return $ ShowRelation expr
220showConstraintsP :: Parser TutorialDOperator
223 constraintName <- option "" identifier
224 return $ ShowConstraint constraintName
226interpreterOps :: Parser TutorialDOperator
227interpreterOps = typeP
231showRelationAttributes :: Relation -> String
232showRelationAttributes rel = "{" ++ concat (L.intersperse ", " $ map showAttribute attrs) ++ "}"
234 showAttribute (Attribute name atomType) = name ++ " " ++ case atomTypeToTutDType atomType of
237 attrs = values (attributes rel)
238 values m = map snd (M.toAscList m)
240evalTutorialDOp :: DatabaseContext -> TutorialDOperator -> String
241evalTutorialDOp context (ShowRelationVariableType relVarName) = case M.lookup relVarName (relationVariables context) of
242 Just rel -> showRelationAttributes rel
243 Nothing -> relVarName ++ " not defined"
245evalTutorialDOp context (ShowRelation expr) = do
246 case runState (evalRelationalExpr expr) context of
247 (Left err, _) -> show err
248 (Right rel, _) -> showRelation rel
250evalTutorialDOp context (ShowConstraint name) = do
253 deps = inclusionDependencies context
254 filteredDeps = case name of
256 name -> HS.filter (\(InclusionDependency n _ _) -> n == name) deps
258example1 = "relA {a,b, c}"
259example2 = "relA join relB"
260example3 = "relA join relB {x,y,z}"
261example4 = "(relA) {x,y,z}"
262example5 = "relA union relB"
263example6 = "rv1 := true"
264example7 = "rv1 := relA union relB"
265example8 = "relA := true; relB := false"
266example9 = "relA := relation { SNO CHAR }"
268interpret :: DatabaseContext -> String -> (Maybe RelationalError, DatabaseContext)
269interpret context tutdstring = case parseString tutdstring of
270 Left err -> (Just err, context)
271 Right parsed -> runState (evalContextExpr parsed) context
273-- for interpreter-specific operations
274interpretOps :: DatabaseContext -> String -> Maybe String
275interpretOps context instring = case parse interpreterOps "" instring of
277 Right ops -> Just $ evalTutorialDOp context ops
279reprLoop :: DatabaseContext -> IO ()
281 maybeLine <- readline "TutorialD: "
287 case interpretOps context line of
293 let (value, contextup) = interpret context line
295 Nothing -> reprLoop contextup
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list