packages feed

WeakSets 1.3.2.0 → 1.3.3.0

raw patch · 3 files changed

+42/−14 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Math.PureSet: maybeFirst :: PureSet -> Maybe PureSet
+ Math.PureSet: maybeSecond :: PureSet -> Maybe PureSet

Files

CHANGELOG.md view
@@ -58,4 +58,8 @@ 
 # 1.3.2.0 -- 2022-08-30
 
-* Bug correction in formatPureSet.+* Bug correction in formatPureSet.
+
+# 1.3.3.0 -- 2022-08-30
+
+* Bug correction in pairs.
WeakSets.cabal view
@@ -14,7 +14,7 @@ -- PVP summary:      +-+------- breaking API changes
 --                   | | +----- non-breaking API additions
 --                   | | | +--- code changes with no API change
-version:            1.3.2.0
+version:            1.3.3.0
 
 -- A short (one-line) description of the package.
 synopsis:
src/Math/PureSet.hs view
@@ -26,6 +26,8 @@     pair,
     first,
     second,
+    maybeFirst,
+    maybeSecond,
     cartesianProduct,
     numberToSet,
     (||||),
@@ -71,8 +73,9 @@     -- | Return the first element of a pair according to Kuratowski's definition of a tuple.
     first :: PureSet -> PureSet
     first (PureSet s)
-        | length l /= 2 = error $ "Math.PureSet.first : malformed pair " ++ (show s)
-        | otherwise = anElement f
+        | length l == 2 = anElement f
+        | length l == 1 = if card (anElement s) == 1 then (anElement.pureSetToSet.anElement $ s) else  (error $ "Math.PureSet.first : malformed pair " ++ (show s))
+        | otherwise = error $ "Math.PureSet.first : malformed pair " ++ (show s)
         where
             l = setToList s
             a = l !! 0
@@ -82,8 +85,9 @@     -- | Return the second element of a pair according to Kuratowski's definition of a tuple.
     second :: PureSet -> PureSet
     second i@(PureSet s)
-        | length l /= 2 = error $ "Math.PureSet.second : malformed pair " ++ (show s)
-        | otherwise = if r2 !! 0 == first i then r2 !! 1 else r2 !! 0
+        | length l == 1 = if card (anElement s) == 1 then (anElement.pureSetToSet.anElement $ s) else  (error $ "Math.PureSet.second : malformed pair " ++ (show s))
+        | length l == 2 = if r2 !! 0 == first i then r2 !! 1 else (if r2 !! 1 == first i then r2 !! 0 else (error $ "Math.PureSet.second : malformed pair " ++ (show s)))
+        | otherwise = error $ "Math.PureSet.second : malformed pair " ++ (show s)
         where
             l = setToList s
             a = l !! 0
@@ -91,6 +95,31 @@             PureSet r = if card a == 2 then a else b
             r2 = setToList r
     
+    -- | Return the first element of a pair if possible.
+    maybeFirst :: PureSet -> Maybe PureSet
+    maybeFirst (PureSet s)
+        | length l == 2 = Just $ anElement f
+        | length l == 1 = if card (anElement s) == 1 then Just (anElement.pureSetToSet.anElement $ s) else Nothing
+        | otherwise = Nothing
+        where
+            l = setToList s
+            a = l !! 0
+            b = l !! 1
+            PureSet f = if card a == 1 then a else b
+    
+    -- | Return the second element of a pair if possible.
+    maybeSecond :: PureSet -> Maybe PureSet
+    maybeSecond i@(PureSet s)
+        | length l == 1 = if card (anElement s) == 1 then Just (anElement.pureSetToSet.anElement $ s) else Nothing
+        | length l == 2 = if r2 !! 0 == first i then Just (r2 !! 1) else (if r2 !! 1 == first i then Just (r2 !! 0) else Nothing)
+        | otherwise = Nothing
+        where
+            l = setToList s
+            a = l !! 0
+            b = l !! 1
+            PureSet r = if card a == 2 then a else b
+            r2 = setToList r
+    
     -- | Construct the cartesian product of two sets.
     cartesianProduct :: PureSet -> PureSet -> PureSet
     cartesianProduct (PureSet xs) (PureSet ys) = pureSet $ [pair x y | x <- setToList xs, y <- setToList ys]
@@ -149,12 +178,7 @@                                         maxNb = maximum $ catMaybes numbers
                                     in 
                                         if (not anyMissing) && (set (Just <$> [0..maxNb])) == (set numbers) then Just (maxNb + 1) else Nothing
-                toPair x@(PureSet xs)
-                    | cardinal xs == 2 = 
-                        case () of
-                         () | ((card $ (setToList xs) !! 0) == 1 && (card $ (setToList xs) !! 1) == 2) && (first x) `isInP` ((setToList xs) !! 1) -> Just $ "(" ++ (formatPureSet.first $ x) ++ "," ++ (formatPureSet.second $ x) ++ ")"
-                            | ((card $ (setToList xs) !! 1) == 1 && (card $ (setToList xs) !! 0) == 2) && (first x) `isInP` ((setToList xs) !! 0) -> Just $ "(" ++ (formatPureSet.first $ x) ++ "," ++ (formatPureSet.second $ x) ++ ")"
-                            | otherwise -> Nothing
-                    | otherwise = Nothing
-    
+                toPair x
+                    | null (maybeSecond x) = Nothing
+                    | otherwise = Just $ "(" ++ (formatPureSet.first $ x) ++ "," ++ (formatPureSet.second $ x) ++ ")"