Created
January 21, 2018 18:41
-
-
Save DavidEichmann/6dbe7d4fffff9f55e03bbfd36a7f891c to your computer and use it in GitHub Desktop.
LiquidHaskell Company and Groups
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
module Main where | |
import Data.Set (Set) | |
import qualified Data.Set as S | |
main :: IO () | |
main | |
. removePerson (Person 1) | |
. addPerson (Person 1) | |
$ emptyCompany | |
newtype Person = Person { personId :: Int } | |
deriving (Show, Eq, Ord) | |
{-@ | |
emptyCompany :: { c : Company | people c == S.empty } | |
@-} | |
emptyCompany :: Company | |
emptyCompany = Company S.empty S.empty | |
{-@ | |
data Company = Company | |
{ people :: Set Person | |
, groups :: Set (Set { p : Person | S.member p people }) | |
} | |
@-} | |
data Company | |
= Company | |
{ people :: Set Person | |
, groups :: Set (Set Person) | |
} deriving (Show) | |
memberOfCompany :: Person -> Company -> Bool | |
memberOfCompany person company = person `S.member` (people company) | |
{-@ addPerson | |
:: person : Person | |
-> company : { c : Company | not S.member person (people c) } | |
-> { c' : Company | people c' == S.union (S.singleton person) (people company) } | |
@-} | |
addPerson :: Person -> Company -> Company | |
addPerson person company = company { people = S.insert person (people company) } | |
removePerson :: Person -> Company -> Company | |
removePerson person company = company | |
{ people = S.delete person (people company) | |
, groups = S.map (S.delete person) (groups company) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment