packages feed

Emping-0.4: src/Aux.hs

{- | Emping 0.4 (provisional)

Module Aux provides some general purpose functions, which are used by other modules.
-}
module Aux where

import Data.List ( partition )

-- | AVp is the type of an attribute-value tuple.
type AVp = (Int, Int)

{- | Ant is the type of the Antedent in a rule. 

Warning: A fact is also a list of Int tuples, and so is a disjunction of attribute-value pairs. Use only when it really reprecents an Antedent of a rule.
-}
type Ant = [AVp]

-- | Rule is the type of a rule, a tuple of an Ant and a AVp
type Rule = (Ant,AVp)

-- | checks whether the elements of the first list are all in the second list

isSub :: Eq a => [a] -> [a] -> Bool
isSub [] _ = True
isSub (x:xs) y | not (x `elem` y) = False
               | otherwise = isSub xs y

-- | checks whether two lists are equal as sets (regardless of order)
isEq :: Eq a => [a] -> [a] -> Bool
isEq x y = isSub x y && isSub y x

-- | returns x if it's elements are all in y, otherwise y
minLs :: Eq a => [a] -> [a] -> [a]
minLs x y | x `isSub` y = x
          | otherwise = y

-- | partitions a list according to an equivalence relation
partitionBy :: (a -> a -> Bool) -> [a] -> [[a]]
partitionBy _ [] = []
partitionBy eq ls = x:(partitionBy eq y)  where
                   (x,y) = partition ((head ls) `eq`) ls