Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Task Set 3 (Query Language & Graphs) ====== ===== Query Language overview ===== The objective of this task set is to: * allow programmers to **combine** table processing functions in a flexible way * create a **separation** between what functions **do** and how they are implemented. This is helpful for a number of reasons: * **integration** of new table functionality later on * continuous code **upgrade** (new algorithms for different table processing functions can be implemented without altering the rest of the project) * debugging and testing You will implement a **query language** which **represents** a variety of table transformations, some of which you have already implemented as table functions. A ''Query'' is **sequence** (in some cases, a **combination**) of such transformations. You will also implement an evaluation function which performs a ''Query'' on a given table. The query language to be implemented is shown below: <code haskell> data Query = FromCSV CSV | ToCSV Query | AsList String Query | Sort String Query | ValueMap (Value -> Value) Query | RowMap (Row -> Row) [String] Query | VUnion Query Query | HUnion Query Query | TableJoin String Query Query | Cartesian (Row -> Row -> Row) [String] Query Query | Projection [String] Query | forall a. Filter (FilterCondition a) Query | Graph EdgeOp query </code> Filtering operations are important, for which reason ... <code haskell> data FilterCondition a = Eq String a | Lt String a | Gt String a | In String [a] | FNot (FilterCondition a) | FieldEq String String -- where EdgeOp is defined: type EdgeOp = Row -> Row -> Value </code> **Don't worry about Graph or Filter queries yet.** === Query Evaluation === Each query defined above should evaluate to either a String, a Table or a list of Strings. Thus we define QResult. Enroll QResult in show. For Table, use write_csv. For List and String, use default. <code haskell> data QResult = CSV CSV | Table Table | List [String] -- don't confuse first 'CSV' and second 'CSV': first refers to constructor name, -- second refers to type CSV (defined in taskset 1); same with 'Table'. instance Show QResult where ... </code> We define class Eval, which offers function **eval**. Your job is to enroll Query in this class. For each constructor from Query, we will explain what eval should produce. <code haskell> class Eval a where eval :: a -> QResult instance Eval Query where ... </code> - **FromCSV str**: converts string str to a Table. - **ToCSV query**: converts Table obtained from the evaluation of query to a string in CSV format. - **AsList colname query**: returns values from column colname as a list. - **Sort colname query**: sorts table by column colname. - **ValueMap op query**: maps all values from table, using op. - **RowMap op colnames query**: maps all rows from table, using op. - **VUnion query1 query2**: vertical union of the 2 tables obtained through the evaluations of query1 and query2. - **HUnion query1 query2**: horizontal union of the 2 tables. - **TableJoin colname query1 query2**: table join with respect to column colname. - **Cartesian op colnames query1 query2**: cartesian product. - **Projection colnames query**: extract specified columns from table. === FilterCondition Evaluation === Let's take a look at FilterCondition. It is used in a Filter query, in order to filter the entries (rows) in a table, based on a condition. We are going to define class FEval, which contains function feval, through which we evaluate a FilterCondition to a function of type FilterOp (defined also below). In order to do so, feval will also receive the column names (the table head). <code haskell> class FEval a where feval :: [String] -> (FilterCondition a) -> FilterOp type FilterOp = Row -> Bool </code> - **Eq colname ref**: checks if value from column colname is equal to ref. - **Lt colname ref**: checks if value from column colname is less than ref. - **Gt colname ref**: checks if value from column colname is greater than ref. - **In colname list**: checks if value from column colname is in list. - **FNot cond**: negates condition. - **FieldEq colname1 colname2**: checks if values from columns colname1 and colname2 are equal. Your task is to write the instances for (FEval Float) and (FEval String). <code haskell> instance FEval Float where ... instance FEval String where ... </code> Now you can write the evaluation for Filter query (**eval**). === Graph Query === We define a graph as a table with column names: ["From", "To", "Value"]. Each row defines a weighted edge between node "From" and node "To". - **Graph edgeop query**: creates a graph starting from the table query evaluates to. The nodes are the rows in table T. The weight of an edge between 2 nodes is given by edgeop. We will only keep the edge between row1 and row2 if (edgeop row1 row2) > 0. In the resulting table, a row describes an edge between node_i and node_j and will have the values: "From" = first column from node_i "To" = first column from node_j "Value" = edgeop node_i node_j The edge node_i-node_j is the same as node_j-node_i, so it should only appear once. "From" value should be lexicographically before "To". === Similarities graph, using queries === We want to check the similarities between students lecture points. For that, we want to obtain a graph where "From" and "To" are students' emails and "Value" is the distance between the 2 students' points. We define the distance between stud1 and stud2 as the sum of questions where they both received the same points. Also, the edges in the resulting graph (the rows in the resulting table) should be sorted by the "Value" column. Keep only the rows with distance >= 5. If email is missing, ignore that entry. Your task is to write **similarities_query** as a sequence of queries, that once evaluated results in the graph described above. === TL;DR Tasks === 1. Enroll Query in class Eval (without Filter or Graph). 0.2p 2. Enroll FilterCondition in class FEval and implement eval for Filter query. 0.2p 3. Implement eval for Graph query. 0.2p 4. Get graph for similarities. 0.3p === Checker === === Submit === **Deadline**: 16.05, 23:50. **Vmchecker**: TBA.