Last active
November 11, 2016 21:16
-
-
Save cogumbreiro/7158e53e20af4a20008a365f2e17939e to your computer and use it in GitHub Desktop.
Known-set implementation in PureScript
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 Prelude | |
import Data.Set as S | |
import Control.Monad.Eff (Eff) | |
import Control.Monad.Eff.Console (CONSOLE, log) | |
import Data.Set (Set, empty) | |
type Task = Int | |
type Spawns = Set Task | |
data Tree = Leaf Spawns | Branch Tree Tree | |
type KnownSet = {spawns:: Spawns, joins:: Tree} | |
make :: KnownSet | |
make = {spawns: S.empty, joins: Leaf S.empty} | |
set_copy :: Spawns -> Spawns | |
set_copy x = x | |
copy :: KnownSet -> KnownSet | |
copy ks = { | |
spawns: S.empty, | |
joins: Branch (Leaf (set_copy ks.spawns)) ks.joins | |
} | |
add :: KnownSet -> Task -> KnownSet | |
add ks x = {spawns: S.insert x ks.spawns, joins: ks.joins } | |
contains :: KnownSet -> Int -> Boolean | |
contains ks x = x `S.member` ks.spawns || isIn ks.joins | |
where | |
isIn :: Tree -> Boolean | |
isIn (Leaf s) = x `S.member` s | |
isIn (Branch l r) = isIn l || isIn r | |
union :: KnownSet -> Tree -> KnownSet | |
union ks j = {spawns: ks.spawns, joins: Branch j ks.joins} | |
main :: forall e. Eff (console :: CONSOLE | e) Unit | |
main = do | |
log (show (contains (add (copy (add make 1)) 2) 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment