Implement "TypeLevelReasoning" proposal at wiki:TypeLevelReasoning
[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 import Text.ParserCombinators.ReadP
39
40 #if !__GLASGOW_HASKELL__
41 import Data.Typeable ( Typeable, TyCon, mkTyCon, mkTyConApp )
42 #else
43 import Data.Typeable ( Typeable )
44 #endif
45
46 import Data.List ( intersperse, sort )
47 import Control.Monad ( liftM )
48 import Data.Char ( isDigit, isAlphaNum )
49
50 {- |
51 A 'Version' represents the version of a software entity.
52
53 An instance of 'Eq' is provided, which implements exact equality
54 modulo reordering of the tags in the 'versionTags' field.
55
56 An instance of 'Ord' is also provided, which gives lexicographic
57 ordering on the 'versionBranch' fields (i.e. 2.1 > 2.0, 1.2.3 > 1.2.2,
58 etc.). This is expected to be sufficient for many uses, but note that
59 you may need to use a more specific ordering for your versioning
60 scheme. For example, some versioning schemes may include pre-releases
61 which have tags @\"pre1\"@, @\"pre2\"@, and so on, and these would need to
62 be taken into account when determining ordering. In some cases, date
63 ordering may be more appropriate, so the application would have to
64 look for @date@ tags in the 'versionTags' field and compare those.
65 The bottom line is, don't always assume that 'compare' and other 'Ord'
66 operations are the right thing for every 'Version'.
67
68 Similarly, concrete representations of versions may differ. One
69 possible concrete representation is provided (see 'showVersion' and
70 'parseVersion'), but depending on the application a different concrete
71 representation may be more appropriate.
72 -}
73 data Version =
74 Version { versionBranch :: [Int],
75 -- ^ The numeric branch for this version. This reflects the
76 -- fact that most software versions are tree-structured; there
77 -- is a main trunk which is tagged with versions at various
78 -- points (1,2,3...), and the first branch off the trunk after
79 -- version 3 is 3.1, the second branch off the trunk after
80 -- version 3 is 3.2, and so on. The tree can be branched
81 -- arbitrarily, just by adding more digits.
82 --
83 -- We represent the branch as a list of 'Int', so
84 -- version 3.2.1 becomes [3,2,1]. Lexicographic ordering
85 -- (i.e. the default instance of 'Ord' for @[Int]@) gives
86 -- the natural ordering of branches.
87
88 versionTags :: [String] -- really a bag
89 -- ^ A version can be tagged with an arbitrary list of strings.
90 -- The interpretation of the list of tags is entirely dependent
91 -- on the entity that this version applies to.
92 }
93 deriving (Read,Show
94 #if __GLASGOW_HASKELL__
95 ,Typeable
96 #endif
97 )
98
99 #if !__GLASGOW_HASKELL__
100 versionTc :: TyCon
101 versionTc = mkTyCon "Version"
102
103 instance Typeable Version where
104 typeOf _ = mkTyConApp versionTc []
105 #endif
106
107 instance Eq Version where
108 v1 == v2 = versionBranch v1 == versionBranch v2
109 && sort (versionTags v1) == sort (versionTags v2)
110 -- tags may be in any order
111
112 instance Ord Version where
113 v1 `compare` v2 = versionBranch v1 `compare` versionBranch v2
114
115 -- -----------------------------------------------------------------------------
116 -- A concrete representation of 'Version'
117
118 -- | Provides one possible concrete representation for 'Version'. For
119 -- a version with 'versionBranch' @= [1,2,3]@ and 'versionTags'
120 -- @= [\"tag1\",\"tag2\"]@, the output will be @1.2.3-tag1-tag2@.
121 --
122 showVersion :: Version -> String
123 showVersion (Version branch tags)
124 = concat (intersperse "." (map show branch)) ++
125 concatMap ('-':) tags
126
127 -- | A parser for versions in the format produced by 'showVersion'.
128 --
129 #if __GLASGOW_HASKELL__ || __HUGS__
130 parseVersion :: ReadP Version
131 #else
132 parseVersion :: ReadP r Version
133 #endif
134 parseVersion = do branch <- sepBy1 (liftM read $ munch1 isDigit) (char '.')
135 tags <- many (char '-' >> munch1 isAlphaNum)
136 return Version{versionBranch=branch, versionTags=tags}