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