Contribution:
haskellComposition
Headline
Data composition in Haskell with algebraic data types
Characteristics
The data model leverages data composition for companies with departmental nesting. Thus, an algebraic data type is used for departments so that recursive nesting can be expressed. The algebraic data type only needs a single data constructor. Thus, data variation is not exercised, but see Contribution:haskellVariation for an alternative with data variation.
Illustration
The data model leverages an algebraic data type for departments; in this manner recursion is enabled:
{-| A data model for the 101companies System -}
module Company.Data where
-- | A company consists of name and top-level departments
type Company = (Name, [Department])
-- | A department consists of name, manager, sub-departments, and employees
data Department = Department Name Manager [Department] [Employee]
deriving (Eq, Read, Show)
-- | An employee consists of name, address, and salary
type Employee = (Name, Address, Salary)
-- | Managers as employees
type Manager = Employee
-- | Names of companies, departments, and employees
type Name = String
-- | Addresses as strings
type Address = String
-- | Salaries as floats
type Salary = Float
A sample company looks like this:
{- | Sample data of the 101companies System -}
module Company.Sample where
import Company.Data
-- | A sample company useful for basic tests
sampleCompany :: Company
sampleCompany =
( "Acme Corporation",
[
Department "Research"
("Craig", "Redmond", 123456)
[]
[
("Erik", "Utrecht", 12345),
("Ralf", "Koblenz", 1234)
],
Department "Development"
("Ray", "Redmond", 234567)
[
Department "Dev1"
("Klaus", "Boston", 23456)
[
Department "Dev1.1"
("Karl", "Riga", 2345)
[]
[("Joe", "Wifi City", 2344)]
]
[]
]
[]
]
)
Feature:Total is implemented as follows:
{-| The operation of totaling all salaries of all employees in a company -}
module Company.Total where
import Company.Data
-- | Total all salaries in a company
total :: Company -> Float
total (_, ds) = totalDepartments ds
where
-- Total salaries in a list of departments
totalDepartments :: [Department] -> Float
totalDepartments [] = 0
totalDepartments (Department _ m ds es : ds')
= getSalary m
+ totalDepartments ds
+ totalEmployees es
+ totalDepartments ds'
where
-- Total salaries in a list of employees
totalEmployees :: [Employee] -> Float
totalEmployees [] = 0
totalEmployees (e:es)
= getSalary e
+ totalEmployees es
-- Extract the salary from an employee
getSalary :: Employee -> Salary
getSalary (_, _, s) = s
The following salary total is computed for the sample company:
399747.0
Relationships
- See Contribution:haskellVariation for a contribution with a similar data model such that data variation is exercised in addition to data composition.
- See Contribution:haskellEngineer for a contribution with a simple data model without support for departmental nesting. No algebraic data types are leveraged.
- See Contribution:haskellData for a contribution with a simple data model without support for departmental nesting. Algebraic data types are leveraged systematically for all types to distinguish the types nominally.
Architecture
There are these modules:
{-| A data model for the 101companies System -}
module Company.Data where
-- | A company consists of name and top-level departments
type Company = (Name, [Department])
-- | A department consists of name, manager, sub-departments, and employees
data Department = Department Name Manager [Department] [Employee]
deriving (Eq, Read, Show)
-- | An employee consists of name, address, and salary
type Employee = (Name, Address, Salary)
-- | Managers as employees
type Manager = Employee
-- | Names of companies, departments, and employees
type Name = String
-- | Addresses as strings
type Address = String
-- | Salaries as floats
type Salary = Float
{- | Sample data of the 101companies System -}
module Company.Sample where
import Company.Data
-- | A sample company useful for basic tests
sampleCompany :: Company
sampleCompany =
( "Acme Corporation",
[
Department "Research"
("Craig", "Redmond", 123456)
[]
[
("Erik", "Utrecht", 12345),
("Ralf", "Koblenz", 1234)
],
Department "Development"
("Ray", "Redmond", 234567)
[
Department "Dev1"
("Klaus", "Boston", 23456)
[
Department "Dev1.1"
("Karl", "Riga", 2345)
[]
[("Joe", "Wifi City", 2344)]
]
[]
]
[]
]
)
{-| The operation of totaling all salaries of all employees in a company -}
module Company.Total where
import Company.Data
-- | Total all salaries in a company
total :: Company -> Float
total (_, ds) = totalDepartments ds
where
-- Total salaries in a list of departments
totalDepartments :: [Department] -> Float
totalDepartments [] = 0
totalDepartments (Department _ m ds es : ds')
= getSalary m
+ totalDepartments ds
+ totalEmployees es
+ totalDepartments ds'
where
-- Total salaries in a list of employees
totalEmployees :: [Employee] -> Float
totalEmployees [] = 0
totalEmployees (e:es)
= getSalary e
+ totalEmployees es
-- Extract the salary from an employee
getSalary :: Employee -> Salary
getSalary (_, _, s) = s
{-| The operation of cutting all salaries of all employees in a company in half -}
module Company.Cut where
import Company.Data
-- | Cut all salaries in a company
cut :: Company -> Company
cut (n, ds) = (n, (map cutD ds))
where
-- Cut all salaries in a department
cutD :: Department -> Department
cutD (Department n m ds es)
= Department n (cutE m) (map cutD ds) (map cutE es)
where
-- Cut the salary of an employee in half
cutE :: Employee -> Employee
cutE (n, a, s) = (n, a, s/2)
{-| Tests for the 101companies System -}
module Main where
import Company.Data
import Company.Sample
import Company.Total
import Company.Cut
import Test.HUnit
import System.Exit
-- | Compare salary total of sample company with baseline
totalTest = 399747.0 ~=? total sampleCompany
-- | Compare total after cut of sample company with baseline
cutTest = total sampleCompany / 2 ~=? total (cut sampleCompany)
-- | Test for round-tripping of de-/serialization of sample company
serializationTest = sampleCompany ~=? read (show sampleCompany)
-- | The list of tests
tests =
TestList [
TestLabel "total" totalTest,
TestLabel "cut" cutTest,
TestLabel "serialization" serializationTest
]
-- | Run all tests and communicate through exit code
main = do
counts <- runTestTT tests
if (errors counts > 0 || failures counts > 0)
then exitFailure
else exitSuccess
{-| A data model for the 101companies System -}
module Company.Data where
-- | A company consists of name and top-level departments
type Company = (Name, [Department])
-- | A department consists of name, manager, sub-departments, and employees
data Department = Department Name Manager [Department] [Employee]
deriving (Eq, Read, Show)
-- | An employee consists of name, address, and salary
type Employee = (Name, Address, Salary)
-- | Managers as employees
type Manager = Employee
-- | Names of companies, departments, and employees
type Name = String
-- | Addresses as strings
type Address = String
-- | Salaries as floats
type Salary = Float
Usage
See https://github.com/101companies/101haskell/blob/master/README.md.
Backlinks
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.