Improve Safe Haskell bounds for changes to base over time
[ghc.git] / libraries / base / Data / Version.hs
1 {-# LANGUAGE Safe #-}
2 {-# LANGUAGE AutoDeriveTypeable #-}
3 {-# LANGUAGE NoImplicitPrelude #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : Data.Version
8 -- Copyright : (c) The University of Glasgow 2004
9 -- License : BSD-style (see the file libraries/base/LICENSE)
10 --
11 -- Maintainer : libraries@haskell.org
12 -- Stability : experimental
13 -- Portability : non-portable (local universal quantification in ReadP)
14 --
15 -- A general library for representation and manipulation of versions.
16 --
17 -- Versioning schemes are many and varied, so the version
18 -- representation provided by this library is intended to be a
19 -- compromise between complete generality, where almost no common
20 -- functionality could reasonably be provided, and fixing a particular
21 -- versioning scheme, which would probably be too restrictive.
22 --
23 -- So the approach taken here is to provide a representation which
24 -- subsumes many of the versioning schemes commonly in use, and we
25 -- provide implementations of 'Eq', 'Ord' and conversion to\/from 'String'
26 -- which will be appropriate for some applications, but not all.
27 --
28 -----------------------------------------------------------------------------
29
30 module Data.Version (
31 -- * The @Version@ type
32 Version(..),
33 -- * A concrete representation of @Version@
34 showVersion, parseVersion,
35 ) where
36
37 import Control.Monad ( Monad(..), liftM )
38 import Data.Bool ( (&&) )
39 import Data.Char ( isDigit, isAlphaNum )
40 import Data.Eq
41 import Data.Int ( Int )
42 import Data.List
43 import Data.Ord
44 import Data.String ( String )
45 import Data.Typeable ( Typeable )
46 import GHC.Read
47 import GHC.Show
48 import Text.ParserCombinators.ReadP
49 import Text.Read ( read )
50
51 {- |
52 A 'Version' represents the version of a software entity.
53
54 An instance of 'Eq' is provided, which implements exact equality
55 modulo reordering of the tags in the 'versionTags' field.
56
57 An instance of 'Ord' is also provided, which gives lexicographic
58 ordering on the 'versionBranch' fields (i.e. 2.1 > 2.0, 1.2.3 > 1.2.2,
59 etc.). This is expected to be sufficient for many uses, but note that
60 you may need to use a more specific ordering for your versioning
61 scheme. For example, some versioning schemes may include pre-releases
62 which have tags @\"pre1\"@, @\"pre2\"@, and so on, and these would need to
63 be taken into account when determining ordering. In some cases, date
64 ordering may be more appropriate, so the application would have to
65 look for @date@ tags in the 'versionTags' field and compare those.
66 The bottom line is, don't always assume that 'compare' and other 'Ord'
67 operations are the right thing for every 'Version'.
68
69 Similarly, concrete representations of versions may differ. One
70 possible concrete representation is provided (see 'showVersion' and
71 'parseVersion'), but depending on the application a different concrete
72 representation may be more appropriate.
73 -}
74 data Version =
75 Version { versionBranch :: [Int],
76 -- ^ The numeric branch for this version. This reflects the
77 -- fact that most software versions are tree-structured; there
78 -- is a main trunk which is tagged with versions at various
79 -- points (1,2,3...), and the first branch off the trunk after
80 -- version 3 is 3.1, the second branch off the trunk after
81 -- version 3 is 3.2, and so on. The tree can be branched
82 -- arbitrarily, just by adding more digits.
83 --
84 -- We represent the branch as a list of 'Int', so
85 -- version 3.2.1 becomes [3,2,1]. Lexicographic ordering
86 -- (i.e. the default instance of 'Ord' for @[Int]@) gives
87 -- the natural ordering of branches.
88
89 versionTags :: [String] -- really a bag
90 -- ^ A version can be tagged with an arbitrary list of strings.
91 -- The interpretation of the list of tags is entirely dependent
92 -- on the entity that this version applies to.
93 }
94 deriving (Read,Show,Typeable)
95
96 instance Eq Version where
97 v1 == v2 = versionBranch v1 == versionBranch v2
98 && sort (versionTags v1) == sort (versionTags v2)
99 -- tags may be in any order
100
101 instance Ord Version where
102 v1 `compare` v2 = versionBranch v1 `compare` versionBranch v2
103
104 -- -----------------------------------------------------------------------------
105 -- A concrete representation of 'Version'
106
107 -- | Provides one possible concrete representation for 'Version'. For
108 -- a version with 'versionBranch' @= [1,2,3]@ and 'versionTags'
109 -- @= [\"tag1\",\"tag2\"]@, the output will be @1.2.3-tag1-tag2@.
110 --
111 showVersion :: Version -> String
112 showVersion (Version branch tags)
113 = concat (intersperse "." (map show branch)) ++
114 concatMap ('-':) tags
115
116 -- | A parser for versions in the format produced by 'showVersion'.
117 --
118 parseVersion :: ReadP Version
119 parseVersion = do branch <- sepBy1 (liftM read (munch1 isDigit)) (char '.')
120 tags <- many (char '-' >> munch1 isAlphaNum)
121 return Version{versionBranch=branch, versionTags=tags}