packages feed

WeakSets (empty) → 0.1.0.0

raw patch · 9 files changed

+706/−0 lines, 9 filesdep +WeakSetsdep +base

Dependencies added: WeakSets, base

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for Sets
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,165 @@+                  GNU LESSER GENERAL PUBLIC LICENSE
+                       Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+  This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+  0. Additional Definitions.
+
+  As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+  "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+  An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+  A "Combined Work" is a work produced by combining or linking an
+Application with the Library.  The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+  The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+  The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+  1. Exception to Section 3 of the GNU GPL.
+
+  You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+  2. Conveying Modified Versions.
+
+  If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+   a) under this License, provided that you make a good faith effort to
+   ensure that, in the event an Application does not supply the
+   function or data, the facility still operates, and performs
+   whatever part of its purpose remains meaningful, or
+
+   b) under the GNU GPL, with none of the additional permissions of
+   this License applicable to that copy.
+
+  3. Object Code Incorporating Material from Library Header Files.
+
+  The object code form of an Application may incorporate material from
+a header file that is part of the Library.  You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+   a) Give prominent notice with each copy of the object code that the
+   Library is used in it and that the Library and its use are
+   covered by this License.
+
+   b) Accompany the object code with a copy of the GNU GPL and this license
+   document.
+
+  4. Combined Works.
+
+  You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+   a) Give prominent notice with each copy of the Combined Work that
+   the Library is used in it and that the Library and its use are
+   covered by this License.
+
+   b) Accompany the Combined Work with a copy of the GNU GPL and this license
+   document.
+
+   c) For a Combined Work that displays copyright notices during
+   execution, include the copyright notice for the Library among
+   these notices, as well as a reference directing the user to the
+   copies of the GNU GPL and this license document.
+
+   d) Do one of the following:
+
+       0) Convey the Minimal Corresponding Source under the terms of this
+       License, and the Corresponding Application Code in a form
+       suitable for, and under terms that permit, the user to
+       recombine or relink the Application with a modified version of
+       the Linked Version to produce a modified Combined Work, in the
+       manner specified by section 6 of the GNU GPL for conveying
+       Corresponding Source.
+
+       1) Use a suitable shared library mechanism for linking with the
+       Library.  A suitable mechanism is one that (a) uses at run time
+       a copy of the Library already present on the user's computer
+       system, and (b) will operate properly with a modified version
+       of the Library that is interface-compatible with the Linked
+       Version.
+
+   e) Provide Installation Information, but only if you would otherwise
+   be required to provide such information under section 6 of the
+   GNU GPL, and only to the extent that such information is
+   necessary to install and execute a modified version of the
+   Combined Work produced by recombining or relinking the
+   Application with a modified version of the Linked Version. (If
+   you use option 4d0, the Installation Information must accompany
+   the Minimal Corresponding Source and Corresponding Application
+   Code. If you use option 4d1, you must provide the Installation
+   Information in the manner specified by section 6 of the GNU GPL
+   for conveying Corresponding Source.)
+
+  5. Combined Libraries.
+
+  You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+   a) Accompany the combined library with a copy of the same work based
+   on the Library, uncombined with any other library facilities,
+   conveyed under the terms of this License.
+
+   b) Give prominent notice with the combined library that part of it
+   is a work based on the Library, and explaining where to find the
+   accompanying uncombined form of the same work.
+
+  6. Revised Versions of the GNU Lesser General Public License.
+
+  The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+  Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+  If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
+ Readme.md view
@@ -0,0 +1,46 @@+# WeakSets++This is a Haskell package which defines sets in a more general way than Data.Set.++Data.Set only allows to create sets of types which implements the Ord typeclass, this package introduces HomogeneousSet which does not require the typeclass Ord.++The PureSet type allows to create nested sets easily. It may be used to study set theory for example.++## General info++The homogenous sets implemented in this package require the type contained in the set to implement the typeclass Eq. It ensures there are no duplicate element in the set and that the order of elements does not matter when testing equality. It is slower than Data.Set because we do not require the Ord typeclass, if you only use types which are orderable, use Data.Set instead.++The pure set type implemented in this package is a tree like structure where the order of branches does not matter. It allows arbitrary nesting of sets which is useful to do set theoretic constructions.++## Installation++`cabal install WeakSets`++## Usage++Example usage of homogenous sets :++```haskell+ghci> import HomogeneousSet+ghci> data Foo = Foo Int Char deriving (Eq) -- an arbitrary type which is not required to implement >Ord typeclass+ghci> s1 = set [Foo 3 'a', Foo 2 'c', Foo 3 'a']+ghci> s2 = set [Foo 2 'c', Foo 3 'a']+ghci> s1 == s2+True+```++Example usage of pure sets :++```haskell+ghci> import PureSet+ghci> numberToSet 3+(pureSet [(pureSet []),(pureSet [(pureSet [])]),(pureSet [(pureSet []),(pureSet [(pureSet [])])])])+ghci> putStrLn.prettify $ numberToSet 3+{{}, {{}}, {{}, {{}}}}+```++## Contribution++Any input is appreciated ! Send an email for any remark or question.+The git repository : https://gitlab.utc.fr/gsabbagh/sets+
+ WeakSets.cabal view
@@ -0,0 +1,88 @@+cabal-version:      2.4
+
+-- Initial package description 'WeakSets.cabal' generated by
+-- 'cabal init'. For further documentation, see:
+--   http://haskell.org/cabal/users-guide/
+-- 
+-- The name of the package.
+name:               WeakSets
+
+-- The package version.
+-- See the Haskell package versioning policy (PVP) for standards
+-- guiding when and how versions should be incremented.
+-- https://pvp.haskell.org
+-- PVP summary:      +-+------- breaking API changes
+--                   | | +----- non-breaking API additions
+--                   | | | +--- code changes with no API change
+version:            0.1.0.0
+
+-- A short (one-line) description of the package.
+synopsis:
+    Simple set types. Useful to create sets of arbitrary types and nested sets.
+
+-- A longer description of the package.
+description: This package answers two problems : how to make sets of types which does not implement the Ord typeclass and how to make arbitrarily nested sets as set theory allows. The first problem is resolved thanks to `HomogeneousSet` which is a list where duplicates elements are not allowed and the order of elements is forgotten. The second problem is resolved thanks to `PureSet`, it is a tree structure where the order of the branches does not matter.
+
+-- URL for the project homepage or repository.
+homepage: https://gitlab.utc.fr/gsabbagh/sets
+
+-- A URL where users can report bugs.
+-- bug-reports:
+
+-- The license under which the package is released.
+license:            LGPL-3.0-or-later
+
+-- The file containing the license text.
+license-file:       LICENSE
+
+-- The package author(s).
+author:             Guillaume Sabbagh
+
+-- An email address to which users can send suggestions, bug reports, and patches.
+maintainer:         guillaumesabbagh@protonmail.com
+
+-- A copyright notice.
+-- copyright:
+category:           Math, Data
+
+-- Extra files to be distributed with the package, such as examples or a README.
+extra-source-files: 
+    CHANGELOG.md
+    Readme.md
+
+library
+    -- Modules exported by the library.
+    exposed-modules:  HomogeneousSet, PureSet
+
+    -- Modules included in this library but not exported.
+    -- other-modules:
+
+    -- LANGUAGE extensions used by modules in this package.
+    -- other-extensions:
+
+    -- Other library packages from which modules are imported.
+    build-depends:    base ^>=4.15.0.0
+
+    -- Directories containing source files.
+    hs-source-dirs:   src
+
+    -- Base language which the package is written in.
+    default-language: Haskell2010
+
+test-suite WeakSets-test
+    -- Base language which the package is written in.
+    default-language: Haskell2010
+
+    -- The interface type and version of the test suite.
+    type:             exitcode-stdio-1.0
+
+    -- Directories containing source files.
+    hs-source-dirs:   test
+
+    -- The entrypoint to the test suite.
+    main-is:          AllTests.hs
+
+    -- Test dependencies.
+    build-depends:    base ^>=4.15.0.0, WeakSets
+    
+    other-modules:    TestHomogeneousSet, TestPureSet
+ src/HomogeneousSet.hs view
@@ -0,0 +1,202 @@+{-| Module  : WeakSets
+Description : Homogeneous sets are sets which can contain only one type of values. They are more flexible than Data.Set because they do not require the objects contained to be orderable.
+Copyright   : Guillaume Sabbagh 2022
+License     : LGPL-3.0-or-later
+Maintainer  : guillaumesabbagh@protonmail.com
+Stability   : experimental
+Portability : portable
+
+Homogeneous sets are sets which can contain only one type of values.
+
+They are more flexible than Data.Set because they do not require the objects contained to be orderable.
+
+The datatype only assumes its components are equatable, it is therefore slower than the Data.Set datatype.
+
+We use this datatype because most of the datatypes we care about are not orderable.
+
+Inline functions related to homogeneous sets are written between pipes @|@.
+-}
+
+module HomogeneousSet
+(
+    -- * Set datatype and smart constructor
+    Set, -- abstract type, the smart constructor is `set`
+    set, -- the smart constructor for `Set`
+    -- * Set related functions
+    setToList,
+    isIncludedIn,
+    cardinal,
+    isIn,
+    (|<$>|),
+    (|&|),
+    (|||),
+    (|*|),
+    (|+|),
+    (|-|),
+    powerSet,
+    -- * Function datatype and smart constructor
+    AssociationList(..),
+    Function, -- abstract type, the smart constructor is `function`
+    function, -- the smart constructor for `Function`
+    -- * Function related functions
+    functionToSet,
+    domain,
+    image,
+    (|$|),
+    (|!|),
+    findWithDefault,
+    (|.|),
+    memorizeFunction,
+)
+where
+    import Data.List (intercalate, nub, nubBy, intersect, union, (\\), subsequences)
+    
+    -- | A homogeneous set is a list of values.
+    --
+    -- The only differences are that we don't want duplicate elements and we don't need the order of the list elements.
+    --
+    -- To force these constraints, the `Set` constructor is abstract and is not exported. The only way to construct a set is to use the smart constructor `set` which ensures the previous conditions.
+    data Set a = Set [a]
+    
+    -- | The smart constructor of sets. This is the only way of instantiating a `Set`.
+    --
+    -- If several elements are equal, only the first element is kept.
+    set :: (Eq a) => [a] -> Set a
+    set xs = Set (nub xs)
+    
+    instance (Show a) => Show (Set a) where
+        show (Set xs) = "(set "++show xs++")"
+    
+    -- | Returns a boolean indicating if a `Set` is included in another one.
+    isIncludedIn :: (Eq a) => Set a -> Set a -> Bool
+    (Set []) `isIncludedIn` _ = True
+    (Set (x:xs)) `isIncludedIn` (Set ys)
+        | x `elem` ys = (Set xs) `isIncludedIn` (Set ys)
+        | otherwise = False
+    
+    instance (Eq a) => Eq (Set a) where
+        x == y = x `isIncludedIn` y && y `isIncludedIn` x
+        
+
+    instance (Eq a) => Semigroup (Set a) where
+        (Set xs) <> (Set ys) = set $ xs <> ys
+        
+    instance (Eq a) => Monoid (Set a) where
+        mempty = Set []
+    
+    instance Foldable Set where
+        foldr f d (Set xs) = foldr f d xs
+
+    -- | Transforms a `Set` back into a list, the list returned does not have duplicate elements, the order of the original list holds.
+    setToList :: Set a -> [a]
+    setToList (Set xs) = xs
+
+    -- | Maps a function to every element of the set. We can't instantiate `Functor` because we would have to add a contraint @Eq@ to the type parameter of `Set`.
+    (|<$>|) :: (Eq b) => (a -> b) -> Set a -> Set b
+    (|<$>|) f (Set xs) = set $ f <$> xs
+    
+    -- | Unsafe map of a funtion to every element of a set. The function should be injective. This function is not exported.
+    unsafeMap :: (a -> b) -> Set a -> Set b
+    unsafeMap f (Set xs) = Set $ f <$> xs
+    
+    -- | Size of a set.
+    cardinal :: Set a -> Int
+    cardinal = length.setToList
+    
+    -- | Returns wether an element is in a set.
+    isIn :: (Eq a) => a -> Set a -> Bool
+    isIn x = (elem x).setToList
+    
+    -- | Returns the intersection of two sets.
+    (|&|) :: (Eq a) => Set a -> Set a -> Set a
+    (|&|) (Set xs) (Set ys) = Set $ xs `intersect` ys
+    
+    -- | Returns the union of two sets.
+    (|||) ::  (Eq a) => Set a -> Set a -> Set a
+    (|||) (Set xs) (Set ys) = Set $ xs `union` ys
+    
+    -- | Unsafe union where we do not check duplicates. This function is not exported.
+    (|!) :: Set a -> Set a -> Set a
+    (|!) (Set xs) (Set ys) = Set $ xs ++ ys
+    
+    -- | Returns the cartesian product of two sets.
+    (|*|) ::  Set a -> Set b -> Set (a,b)
+    (|*|) (Set xs) (Set ys) = Set $ [(x,y) | x <- xs, y <- ys]
+    
+    -- | Returns the disjoint union of two sets.
+    (|+|) :: Set a -> Set b -> Set (Either a b)
+    (|+|) (Set xs) (Set ys) = Set $ [Left x | x <- xs] ++ [Right y | y <- ys]
+    
+    -- | Returns the difference of two sets.
+    (|-|) :: (Eq a) => Set a -> Set a -> Set a
+    (|-|) (Set xs) (Set ys) = Set $ xs \\ ys
+    
+    -- | Returns the set of all subsets of a given set.
+    powerSet :: Set a -> Set (Set a)
+    powerSet (Set xs) = Set $ Set <$> subsequences xs
+    
+    -- | A function of homogeneous sets. It is a set of pairs (key,value) such that their should only be one pair with a given key.
+    --
+    -- It is an abstract type, the smart constructor is `function`.
+    data Function a b = Function (Set (a,b)) deriving (Eq)
+    
+    instance (Show a, Show b) => Show (Function a b) where
+        show (Function al) = "(function "++show al++")"
+    
+    -- | An association list is a list of pairs (key,value).
+    type AssociationList a b = [(a,b)]
+    
+    -- | The smart constructor of functions. This is the only way of instantiating a `Function`.
+    --
+    -- Takes an association list and returns a function which maps to each key the value associated.
+    --
+    -- If several pairs have the same keys, the first pair is kept.
+    function :: (Eq a) => AssociationList a b -> Function a b
+    function al = Function $ Set $ nubBy (\x y -> (fst x) == (fst y)) al
+    
+    -- | Transforms a function back into its underlying association list.
+    functionToSet :: Function a b -> Set (a,b)
+    functionToSet (Function al) = al
+    
+    -- | Returns the domain of a function.
+    domain :: Function a b -> Set a
+    domain = (unsafeMap fst).functionToSet
+    
+    -- | Returns the image of a function. The image of a function is the set of values which are reachable by applying the function.
+    image :: Function a b -> Set b
+    image = (unsafeMap snd).functionToSet
+        
+    -- | Apply a function to a given value. If the function is not defined on the given value returns `Nothing`, otherwise returns `Just` the image.
+    --
+    -- This function is like `lookup` in Data.Map for function (the order of the argument are reversed though).
+    (|$|) :: (Eq a) => Function a b -> a -> Maybe b
+    (|$|) (Function (Set [])) _ = Nothing
+    (|$|) (Function (Set ((k,v):xs))) x
+        | x == k = Just v
+        | otherwise = (Function (Set xs)) |$| x
+    
+    -- | Unsafe version of `(|$|)`.
+    --
+    -- This function is like `(!)` in Data.Map for function.
+    (|!|) :: (Eq a) => Function a b -> a -> b
+    (|!|) (Function (Set [])) _ = error "Function applied on a value not in the domain."
+    (|!|) (Function (Set ((k,v):xs))) x
+        | x == k = v
+        | otherwise = (Function (Set xs)) |!| x
+    
+    -- | Apply a function to a given value, if the value is in the domain returns the image, otherwise return a default value.
+    --
+    -- This function is like `findWithDefault` in Data.Map for function (the order of the argument are reversed though).
+    findWithDefault :: (Eq a) => Function a b -> b -> a -> b
+    findWithDefault (Function (Set [])) d _ = d
+    findWithDefault (Function (Set ((k,v):xs))) d x
+        | x == k = v
+        | otherwise = findWithDefault (Function (Set xs)) d x
+   
+    -- | Composes two functions. If the two functions are not composable, strips the functions until they can compose.
+    (|.|) :: (Eq a, Eq b) => Function b c -> Function a b -> Function a c
+    (|.|) f2 f1 = Function $ Set [(k,(f2 |!| (f1 |!| k))) | k <- (setToList.domain $ f1), f1 |!| k `isIn` (domain f2)]
+    
+    -- | Memorize a Haskell function on a given finite domain.
+    memorizeFunction :: (a -> b) -> Set a -> Function a b
+    memorizeFunction f (Set xs) = Function $ Set [(k, f k) | k <- xs]
+ src/PureSet.hs view
@@ -0,0 +1,136 @@+{-| Module  : WeakSets
+Description : Pure sets are nested sets which only contain other sets all the way down. They allow to explore basic set theory.
+Copyright   : Guillaume Sabbagh 2021
+License     : GPL-3
+Maintainer  : guillaumesabbagh@protonmail.com
+Stability   : experimental
+Portability : portable
+
+Pure sets are nested sets which only contain other sets all the way down. They allow to explore basic set theory.
+
+Every mathematical object is a set, usual constructions such as Von Neumann numbers and Kuratowski pairs are implemented.
+
+It is a tree where the order of the branches does not matter.
+
+Functions with the same name as homogeneous set functions are suffixed with the letter 'P' for pure to avoid name collision.
+-}
+
+module PureSet 
+(
+    -- * `PureSet` datatype
+    PureSet(..),
+    pureSet,
+    -- * Mathematical constructions using sets
+    emptySet,
+    singleton,
+    pair,
+    cartesianProduct,
+    numberToSet,
+    (||||),
+    (&&&&),
+    isInP,
+    isIncludedInP,
+    card,
+    powerSetP,
+    -- * Formatting functions
+    prettify,
+    formatPureSet,
+)
+where
+    import HomogeneousSet
+    import Data.List    (intersect, nub, intercalate, subsequences)
+    import Data.Maybe   (fromJust, catMaybes)
+    
+    -- | A `PureSet` is a `Set` of other pure sets.
+    data PureSet = PureSet (Set PureSet) deriving (Eq)
+    
+    instance Show PureSet where
+        show (PureSet xs) = "(pureSet "++ show (setToList xs) ++")"
+    
+    -- | Constructs a `PureSet` from a list of pure sets.
+    pureSet :: [PureSet] -> PureSet
+    pureSet = (PureSet).set
+    
+    -- | Peels a `PureSet` into a `Set`.
+    pureSetToSet :: PureSet -> Set PureSet
+    pureSetToSet (PureSet xs) = xs
+    
+    -- | Constructs the empty set.
+    emptySet :: PureSet
+    emptySet = pureSet []
+  
+    -- | Constructs the singleton containing a given set.
+    singleton :: PureSet -> PureSet
+    singleton x = pureSet $ [x]
+    
+    -- | Constructs an ordered pair from two sets according to Kuratowski's definition of a tuple.
+    pair :: PureSet -> PureSet -> PureSet
+    pair x y = PureSet $ set [singleton x, pureSet $ [x,y]]
+    
+    -- | Constructs 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]
+    
+    -- | Union of two pure sets.
+    (||||) :: PureSet -> PureSet -> PureSet
+    (||||) (PureSet xs) (PureSet ys) = PureSet $ xs ||| ys
+    
+    -- | Intersection of two pure sets.
+    (&&&&) :: PureSet -> PureSet -> PureSet
+    (&&&&) (PureSet xs) (PureSet ys) = PureSet $ xs |&| ys
+    
+    -- | Difference of two pure sets.
+    (\\\\) :: PureSet -> PureSet -> PureSet
+    (\\\\) (PureSet xs) (PureSet ys) = PureSet $ xs |-| ys
+   
+    -- | Transforms a number into its Von Neumann construction
+    numberToSet :: (Num a, Eq a) => a -> PureSet
+    numberToSet 0 = emptySet
+    numberToSet n = (numberToSet (n-1)) |||| (singleton (numberToSet (n-1)))
+    
+    -- | Returns wether a pure set is in another one.
+    isInP :: PureSet -> PureSet -> Bool
+    isInP x (PureSet xs) = x `isIn` xs
+    
+    -- | Returns wether a pure set is included in another one.
+    isIncludedInP :: PureSet -> PureSet -> Bool
+    isIncludedInP (PureSet xs) (PureSet ys) = xs `isIncludedIn` ys
+    
+    -- | Returns the size of a pure set.
+    card :: PureSet -> Int
+    card (PureSet xs) = cardinal xs
+    
+    -- | Returns the set of subsets of a given set.
+    powerSetP :: PureSet -> PureSet
+    powerSetP (PureSet xs) = PureSet $ PureSet |<$>| powerSet xs
+    
+    -- | Prettiffies a pure set according to usual mathematical notation.
+    prettify :: PureSet -> String
+    prettify (PureSet xs)
+        | cardinal xs == 0 = "{}"
+        | otherwise = "{" ++ (intercalate ", " $ prettify <$> setToList xs) ++ "}"
+        
+    -- | Format pure sets such that if numbers are recognized, they are transformed into integer and if pairs are recognized, they are transformed into pairs.
+    formatPureSet :: PureSet -> String
+    formatPureSet x
+        | (not.null) $ toNumber x = show.fromJust $ toNumber x
+        | (not.null) $ toPair x = fromJust.toPair $ x
+        | otherwise = "{"++intercalate "," (formatPureSet <$> (setToList.pureSetToSet $ x))++"}"
+            where
+                toNumber s@(PureSet xs)
+                    | s == emptySet = Just 0
+                    | otherwise =   let
+                                        numbers = setToList $ toNumber |<$>| xs
+                                        anyMissing = null $ foldr1 (>>) numbers
+                                        maxNb = maximum $ catMaybes numbers
+                                    in 
+                                        if (not anyMissing) && (set (Just <$> [0..maxNb])) == (set numbers) then Just (maxNb + 1) else Nothing
+                toPair (PureSet xs)
+                    | cardinal xs == 2 = 
+                        case () of
+                         () | ((card $ (setToList xs) !! 0) == 1 && (card $ (setToList xs) !! 1) == 2) && ((setToList xs) !! 0) `isInP` ((setToList xs) !! 1) -> Just $ "(" ++ (formatPureSet.head.setToList.pureSetToSet $ ((setToList xs) !! 0)) ++ "," ++ (formatPureSet.head.setToList.pureSetToSet $ (((setToList xs) !! 1) \\\\ ((setToList xs) !! 0))) ++ ")"
+                            | ((card $ (setToList xs) !! 1) == 1 && (card $ (setToList xs) !! 0) == 2) && ((setToList xs) !! 1) `isInP` ((setToList xs) !! 0) -> Just $ "(" ++ (formatPureSet.head.setToList.pureSetToSet $ ((setToList xs) !! 1)) ++ "," ++ (formatPureSet.head.setToList.pureSetToSet $ (((setToList xs) !! 0) \\\\ ((setToList xs) !! 1))) ++ ")"
+                            | otherwise -> Nothing
+                    | otherwise = Nothing
+    
+    
+ test/AllTests.hs view
@@ -0,0 +1,10 @@+module Main (main) where
+    import qualified TestHomogeneousSet as HSET
+    import qualified TestPureSet as PSET
+
+    main :: IO ()
+    main = do
+        putStrLn "Beginning of the test."
+        HSET.main
+        PSET.main
+        putStrLn "End of the tests."
+ test/TestHomogeneousSet.hs view
@@ -0,0 +1,36 @@+module TestHomogeneousSet where
+    import HomogeneousSet
+    
+    -- | Tests all functions related to homogeneous sets.
+    main :: IO ()
+    main = do
+            putStrLn "Beginning of TestHomogeneousSet"
+            let s1 = set [1,2,3] 
+            let s2 = set [3,4,5] 
+            putStrLn $ show $ s1
+            putStrLn $ show $ setToList s1
+            putStrLn $ show $ s2 `isIncludedIn` s1
+            putStrLn $ show $ (set [1,2]) `isIncludedIn` s1
+            putStrLn $ show $ cardinal s1
+            putStrLn $ show $ 3 `isIn` s1
+            putStrLn $ show $ (+1) |<$>| s1
+            putStrLn $ show $ s1 |&| s2
+            putStrLn $ show $ s1 ||| s2
+            putStrLn $ show $ s1 |*| s2
+            putStrLn $ show $ s1 |+| s2
+            putStrLn $ show $ s1 |-| s2
+            putStrLn $ show $ powerSet s1
+            let f = function $ zip (setToList s1) (setToList s2)
+            let g = function $ zip (setToList s2) (setToList s1)
+            putStrLn $ show $ f
+            putStrLn $ show $ g
+            putStrLn $ show $ functionToSet $ f
+            putStrLn $ show $ domain $ f
+            putStrLn $ show $ image $ f
+            putStrLn $ show $ f |$| 1
+            putStrLn $ show $ f |!| 1
+            putStrLn $ show $ f |.| g
+            putStrLn $ show $ g |.| f
+            putStrLn $ show $ memorizeFunction (*3) (set [1..10])
+            putStrLn "End of TestHomogeneousSet"
+    
+ test/TestPureSet.hs view
@@ -0,0 +1,18 @@+module TestPureSet where
+    import HomogeneousSet
+    import PureSet
+    import Data.List (intercalate)
+    
+    -- | Tests all functions related to pure sets.
+    main :: IO ()
+    main = do
+            putStrLn "Beginning of TestPureSet"
+            putStrLn.show $ emptySet
+            putStrLn.show $ singleton $ emptySet
+            putStrLn.show $ pair emptySet emptySet
+            putStrLn.show $ numberToSet <$> [0,3]
+            putStrLn $ intercalate "\n" $ formatPureSet <$> numberToSet <$> [0..3]
+            putStrLn.prettify $ pureSet [pureSet [pureSet []],pureSet [], pureSet [],pureSet [pureSet [],pureSet [pureSet [pureSet [pureSet [pureSet []]]]]]]
+            putStrLn.formatPureSet $ pureSet [pureSet [pureSet []],pureSet [], pureSet [],pureSet [pureSet [],pureSet [pureSet [pureSet [pureSet [pureSet []]]]]]]
+            putStrLn "End of TestPureSet"
+