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