From 7d89b4aec001ade4dcb21770d933e9576ed5c4f9 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Fri, 2 Nov 2007 11:41:59 +0000 Subject: [PATCH] Added the implementation of an ExSet type that can easily represent the set of everything --- transformations/RainUsageCheck.hs | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/transformations/RainUsageCheck.hs b/transformations/RainUsageCheck.hs index a468901..f202ad2 100644 --- a/transformations/RainUsageCheck.hs +++ b/transformations/RainUsageCheck.hs @@ -25,7 +25,7 @@ module RainUsageCheck where import Control.Monad.Identity import Data.Generics import Data.Graph.Inductive -import Data.List +import Data.List hiding (union) import qualified Data.Map as Map import Data.Maybe import qualified Data.Set as Set @@ -249,6 +249,36 @@ parUsageCheck proc subscriptedArrays' _ = Nothing -} +-- | A custom Set wrapper that allows for easy representation of the "everything" set. +-- In most instances, we could actually build the everything set, but +-- representing it this way is easier, more efficient, and more readable. +-- As you would expect, Everything `intersection` x = x, and Everything `union` x = Everything. +data Ord a => ExSet a = Everything | NormalSet (Set.Set a) deriving (Eq, Show) + +intersection :: Ord a => ExSet a -> ExSet a -> ExSet a +intersection Everything x = x +intersection x Everything = x +intersection (NormalSet a) (NormalSet b) = NormalSet (Set.intersection a b) + +union :: Ord a => ExSet a -> ExSet a -> ExSet a +union Everything _ = Everything +union _ Everything = Everything +union (NormalSet a) (NormalSet b) = NormalSet (Set.union a b) + +unions :: Ord a => [ExSet a] -> ExSet a +unions [] = emptySet +unions ss = foldl1 union ss + +emptySet :: Ord a => ExSet a +emptySet = NormalSet (Set.empty) + +isSubsetOf :: Ord a => ExSet a -> ExSet a -> Bool +-- Clause order is important here. Everything is a subset of Everything so this must come first: +isSubsetOf _ Everything = True +isSubsetOf Everything _ = False +isSubsetOf (NormalSet a) (NormalSet b) = Set.isSubsetOf a b + + -- TODO have some sort of error-message return if the check fails or if the code fails checkInitVar :: FlowGraph (Maybe Decl, Vars) -> Node -> Bool