@@ -37,7 +37,7 @@ data Atom = IntegerAtom Integer |
37 BoolAtom Bool |
37 BoolAtom Bool |
38 RelationAtom Relation |
38 RelationAtom Relation |
39 ConstructedAtom DataConstructorName AtomType [Atom]
39 ConstructedAtom DataConstructorName AtomType [Atom]
40− deriving (Eq, Show, Binary, Typeable, NFData, Generic)
40+ deriving (Eq, Ord, Show, Binary, Typeable, NFData, Generic)
42instance Hashable Atom where
42instance Hashable Atom where
43 hashWithSalt salt (ConstructedAtom dConsName _ atoms) = salt `hashWithSalt` atoms
43 hashWithSalt salt (ConstructedAtom dConsName _ atoms) = salt `hashWithSalt` atoms
@@ -77,7 +77,9 @@ data AtomType = IntAtomType |
77 TypeVariableType TypeVarName
77 TypeVariableType TypeVarName
78 --wildcard used in Atom Functions and tuples for data constructors which don't provide all arguments to the type constructor
78 --wildcard used in Atom Functions and tuples for data constructors which don't provide all arguments to the type constructor
79 deriving (Eq, NFData, Generic, Binary, Show)
79 deriving (Eq, NFData, Generic, Binary, Show)
81+instance Ord AtomType where
81type TypeVarMap = M.Map TypeVarName AtomType
83type TypeVarMap = M.Map TypeVarName AtomType
83instance Hashable TypeVarMap where
85instance Hashable TypeVarMap where
@@ -122,6 +124,16 @@ instance Eq RelationTupleSet where
123instance NFData RelationTupleSet where rnf = genericRnf
125instance NFData RelationTupleSet where rnf = genericRnf
127+sortDataFrameBy :: Attribute -> DataFrame -> DataFrame
128+sortDataFrameBy attr (DataFrame attrs tupSet) =
129+ case attr `elem` attrs of
130+ True -> DataFrame attrs $ sortTupleSetBy (compareRelationTupleBy attr) tupSet
131+ False -> error $ "No such attribute: " ++ show attr
133+sortTupleSetBy :: (RelationTuple -> RelationTuple -> Ordering) -> RelationTupleSet -> RelationTupleSet
134+sortTupleSetBy compare' (RelationTupleSet tuples) =
135+ RelationTupleSet $ L.sortBy compare' tuples
125--the same hash must be generated for equal tuples so that the hashset equality works
137--the same hash must be generated for equal tuples so that the hashset equality works
126instance Hashable RelationTuple where
138instance Hashable RelationTuple where
127 --sanity check the tuple for attribute and tuple counts
139 --sanity check the tuple for attribute and tuple counts
@@ -145,15 +157,32 @@ instance Binary RelationTuple
145instance Eq RelationTuple where
157instance Eq RelationTuple where
146 (==) tuple1@(RelationTuple attrs1 _) tuple2@(RelationTuple attrs2 _) = attributesEqual attrs1 attrs2 && atomsEqual
158 (==) tuple1@(RelationTuple attrs1 _) tuple2@(RelationTuple attrs2 _) = attributesEqual attrs1 attrs2 && atomsEqual
148− atomForAttribute attr (RelationTuple attrs tupVec) = case V.findIndex (== attr) attrs of
150− Just index -> tupVec V.!? index
151 atomsEqual = V.all (== True) $ V.map (\attr -> atomForAttribute attr tuple1 == atomForAttribute attr tuple2) attrs1
160 atomsEqual = V.all (== True) $ V.map (\attr -> atomForAttribute attr tuple1 == atomForAttribute attr tuple2) attrs1
162+atomForAttribute attr (RelationTuple attrs tupVec) =
163+ case V.findIndex (== attr) attrs of
165+ Just index -> tupVec V.!? index
153instance NFData RelationTuple where rnf = genericRnf
167instance NFData RelationTuple where rnf = genericRnf
169+data DataFrameTuple = DataFrameTuple Attributes (V.Vector Atom) deriving (Eq, Show, Generic)
171+compareRelationTupleBy :: Attribute -> RelationTuple -> RelationTuple -> Ordering
172+compareRelationTupleBy attr tuple1@(RelationTuple attrs1 tupVec1) tuple2@(RelationTuple attrs2 tupVec2) =
173+ case attr `elem` attrs1 && attr `elem` attrs2 of
174+ True -> compare (atomForAttribute attr tuple1) (atomForAttribute attr tuple2)
175+ False -> error "Comparing two RelationTuples by an non-existent common attribute."
155data Relation = Relation Attributes RelationTupleSet deriving (Show, Generic,Typeable)
177data Relation = Relation Attributes RelationTupleSet deriving (Show, Generic,Typeable)
179+instance Ord Relation where
180+ compare (Relation attrs1 tupSet1) (Relation attrs2 tupSet2) = undefined
157instance Eq Relation where
186instance Eq Relation where
158 Relation attrs1 tupSet1 == Relation attrs2 tupSet2 = attributesEqual attrs1 attrs2 && tupSet1 == tupSet2
187 Relation attrs1 tupSet1 == Relation attrs2 tupSet2 = attributesEqual attrs1 attrs2 && tupSet1 == tupSet2
@@ -167,7 +196,15 @@ instance Hashable Relation where
167 sortedAttrs = map snd (sortedAttributesIndices attrs)
196 sortedAttrs = map snd (sortedAttributesIndices attrs)
169instance Binary Relation
198instance Binary Relation
200+data DataFrame = DataFrame Attributes RelationTupleSet deriving (Show, Generic, Typeable)
202+toDataFrame :: Relation -> DataFrame
203+toDataFrame (Relation attr tupSet) = DataFrame attr tupSet
205+fromDataFrame :: DataFrame -> Relation
206+fromDataFrame (DataFrame attr tupSet) = Relation attr tupSet
171-- | Used to represent the number of tuples in a relation.
208-- | Used to represent the number of tuples in a relation.
172data RelationCardinality = Countable | Finite Int deriving (Eq, Show, Generic, Ord)
209data RelationCardinality = Countable | Finite Int deriving (Eq, Show, Generic, Ord)