Headline

Dealing with large bananas in Haskell

Characteristics

Large bananas are put to work as a means of generic programming so that operations for cutting or totaling salaries are implemented concisely. Only problem-specific aspects need to be specified. The overall data traversal is provided the generalized folds and suitably predefined fold algebras.

Illustration

See the concept of large bananas for an illustration that actually involves the present contribution.

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 data Company = Company Name [Department] deriving (Eq, Read, Show) -- | 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 data Employee = Employee Name Address Salary deriving (Eq, Show, Read) -- | 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 data model for Feature:Hierarchical company
  • module Company.Algebra where
import Company.Data import Data.Monoid -- An algebra type for companies data CompanyAlgebra c d e = CompanyAlgebra { atCompany :: Name -> [d] -> c, atDepartment :: Name -> e -> [d] -> [e] -> d, atEmployee :: Name -> Address -> Salary -> e } -- A family of fold functions for companies and constituents foldCompany :: CompanyAlgebra c d e -> Company -> c foldCompany f (Company n ds) = atCompany f n ds' where ds' = map (foldDepartment f) ds foldDepartment :: CompanyAlgebra c d e -> Department -> d foldDepartment f (Department n m ds es) = atDepartment f n m' ds' es' where m' = foldEmployee f m ds' = map (foldDepartment f) ds es' = map (foldEmployee f) es foldEmployee :: CompanyAlgebra c d e -> Employee -> e foldEmployee f (Employee n w s) = atEmployee f n w s -- An algebra for deep identity mapCompany :: CompanyAlgebra Company Department Employee mapCompany = CompanyAlgebra { atCompany = Company, atDepartment = Department, atEmployee = Employee } -- An algebra for deep mconcat of monoid mconcatCompany :: Monoid m => CompanyAlgebra m m m mconcatCompany = CompanyAlgebra { atCompany = \n ds -> mconcat ds, atDepartment = \n m ds es -> mappend m (mappend (mconcat ds) (mconcat es)), atEmployee = \n w s -> mempty }
: support for large bananas
  • {- | Sample data of the 101companies System -}
module Company.Sample where import Company.Data -- | A sample company useful for basic tests sampleCompany :: Company sampleCompany = Company "Acme Corporation" [ Department "Research" (Employee "Craig" "Redmond" 123456) [] [ (Employee "Erik" "Utrecht" 12345), (Employee "Ralf" "Koblenz" 1234) ], Department "Development" (Employee "Ray" "Redmond" 234567) [ Department "Dev1" (Employee "Klaus" "Boston" 23456) [ Department "Dev1.1" (Employee "Karl" "Riga" 2345) [] [(Employee "Joe" "Wifi City" 2344)] ] [] ] [] ]
: a sample company
  • module Company.Total where
import Company.Data import Company.Algebra import Data.Monoid total :: Company -> Float total = getSum . foldCompany totalAlgebra where totalAlgebra = mconcatCompany { atEmployee = \n a s -> Sum s }
: the implementation of Feature:Total import Company.Data import Company.Algebra cut :: Company -> Company cut = foldCompany cutAlgebra where cutAlgebra = mapCompany { atEmployee = \n a s -> Employee n a (s/2) } -- For comparison, without using mapCompany. cut' :: Company -> Company cut' = foldCompany cutAlgebra where cutAlgebra :: CompanyAlgebra Company Department Employee cutAlgebra = CompanyAlgebra { atCompany = Company, atDepartment = Department, atEmployee = \n a s -> Employee n a (s/2) }
: the implementation of Feature:Cut import Company.Data import Company.Algebra import Data.Monoid depth :: Company -> Int depth = getPosMax . foldCompany depthAlgebra where depthAlgebra :: CompanyAlgebra (PosMax Int) (PosMax Int) (PosMax Int) depthAlgebra = mconcatCompany { atCompany = \n ds -> mconcat ds, atDepartment = \n _ ds es -> PosMax (1 + getPosMax (mconcat ds)) } -- The monoid for maxima on non-negative numbers data (Ord x, Num x) => PosMax x = PosMax { getPosMax :: x } instance (Ord x, Num x) => Monoid (PosMax x) where mempty = PosMax 0 x `mappend` y = PosMax (getPosMax x `max` getPosMax y)
: the implementation of Feature:Depth module Main where import Company.Data import Company.Sample import Company.Total import Company.Cut import Company.Depth 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) -- | Compare depth of sample company with baseline depthTest = 3 ~=? depth 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 "depth" depthTest, 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 : Tests

Usage

See Contribution:haskellComposition.

Metadata

Metadata


There are no revisions for this page.

User contributions

This user never has never made submissions.

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.