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