-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a database implementation with bags (#37)
* Add LANGUAGE extensions for the library in cabal * Add file to hold example from appendix * Remove old test file * Add types for the example * Define cartesian product for bags * Start example with cartesian product * Add test cases for bag cartesian product * Add file to hold database operations * Add table with overview of relational algebra in bags * Implement empty table * Define singleton table and bag * Fix organisation of BagSpec tests * Test the singleton Bag function * Define singleton bag * Test singleton table function * Define table union * Define cartesian product for database tables * Define neutral element for a table * Implement projection * Implement selection and filtering * Add aggregation to bag implementation of database Also declare instance of related monoids as commutative monoids to use within the test case. * Rename selection function to select * Implement equijoin by cartesian product for bags * Explain design choice with commutative monoids * Fix some spellings in implementation report * Fix spellings * Test CMonoid implementations of Boolean operations
- Loading branch information
Showing
14 changed files
with
283 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Main where | ||
|
||
import Data.Bag | ||
|
||
-- Define types used in example | ||
type Identifier = String | ||
type Name = String | ||
type Date = String | ||
type Amount = Float | ||
|
||
data Customer = C { cid :: Identifier, name :: Name} | ||
data Invoice = I { iid :: Identifier, cust :: Identifier, due :: Date, amount :: Amount} | ||
|
||
-- Cartesian product of both databases | ||
exampleWithCP :: Bag Customer -> Bag Invoice -> Bag (Customer, Invoice) | ||
exampleWithCP cs is = cp cs is | ||
|
||
main :: IO () | ||
main = do | ||
putStrLn "Example from appendix" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ lhs2 | |
theoreticalanalysis | ||
documenttype | ||
secondmarker | ||
BagRelAlgOps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Database.Bag where | ||
|
||
import qualified Data.Bag as Bag | ||
import Data.CMonoid | ||
|
||
type Table = Bag.Bag | ||
|
||
empty :: Table a | ||
empty = Bag.empty | ||
|
||
single :: a -> Table a | ||
single = Bag.single | ||
|
||
union :: Table a -> Table a -> Table a | ||
union = Bag.union | ||
|
||
cp :: Table a -> Table b -> Table (a, b) | ||
cp = Bag.cp | ||
|
||
-- TODO:: check | ||
neutral :: Table () | ||
neutral = single () | ||
|
||
projection :: (a -> b) -> Table a -> Table b | ||
projection = fmap | ||
|
||
select :: (a -> Bool) -> Table a -> Table a | ||
select p = Bag.Bag . filter p . Bag.elements | ||
|
||
aggregate :: CMonoid a => Table a -> a | ||
aggregate = Bag.reduceBag | ||
|
||
equijoinWithCp :: Eq c => (a -> c) -> (b -> c) -> Table a -> Table b -> Table (a, b) | ||
equijoinWithCp fa fb as bs = select equality (cp as bs) | ||
where | ||
equality (a, b) = fa a == fb b | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.