1 %************************************************************************
3 \section[backwards]{Backwards compatibility: Converting from GHC 0.xx and Haskell~1.2}
4 \index{GHC vs the Haskell 1.2 language}
5 \index{Haskell 1.2 language vs GHC}
7 %************************************************************************
9 This part of the guide is to help people upgrading from a
10 previous version of GHC. Right now, it is mostly to help people
11 switching from GHC~0.29 (a Haskell~1.2 compiler, mostly) to GHC~2.04
12 (a Haskell~1.4 compiler).
16 If you need to maintain Haskell code that will work for multiple
17 versions of GHC, you can use the \tr{-cpp} flag and the
18 \tr{__GLASGOW_HASKELL__} pre-processor variable.
20 For example, in GHC~0.29, \tr{__GLASGOW_HASKELL__} will be 29; for~2.04,
21 it will be 204. Thus, you can write:
24 main = appendChan stdout "Hello, world!\n" exit done -- 1.2
26 # if __GLASGOW_HASKELL__ >= 200
28 main = putStr "Hello, world!" -- real 1.3
30 main = putStr "Hello, world!\n" -- pseudo-1.3 in 0.2x
35 %************************************************************************
39 %************************************************************************
41 A big new thing in Haskell~1.3 is constructor classes. Humble old functions
42 such as @map@ now have an exciting new type:
44 map :: Functor f => (a->b) -> f a -> f b
46 These new overloadings, expecially where it's the type constructor
47 that's overloaded (as in @map@) can give rise to some puzzling error
48 messages. For example:
50 lookupColor :: String -> [(String, (a, b, c))] -> (a, b, c)
51 lookupColor colorName colorTable =
52 head [(r,g,b) | (c,(r,g,b)) <- colorTable, c == map toLower colorName]
54 With the type signature this is fine, but if you omit the type signature
57 "Color.hs", line 49: No instance for: Prelude.Eq (a{-a18d-} Prelude.Char)
59 at a use of an overloaded identifier: `Prelude.meth.Prelude.Eq.=='
61 @map@ no longer says that @colorName@ has to be a list; it could
62 be any type of the form (t @Char@). Unfortunately, lookupColor has to
63 take equality over these (t @Char@) things, so it gets stuck trying to figure
64 out how to resolve (@Eq@ (t @Char@)) when it knows nothing about t.
66 The solution to such messages is to add type signatures.
68 %************************************************************************
70 \subsection{Lexical matters}
72 %************************************************************************
74 Old uses of `compose' (\tr{(.)}) can magically turn into qualified
75 names; e.g., \tr{LiteralInt.leStringToInt}; add spaces.
77 Leading-underscore names (a Glasgow extension) don't work anymore,
78 even with \tr{-fglasgow-exts}.
80 The \tr{Prelude.lex} function doesn't grok Haskell comments any more
81 (a good simplification, but a change nonetheless).
83 %************************************************************************
85 \subsection{Expressions and patterns}
87 %************************************************************************
89 You used to be able to define non-binary functions in a pseudo-infix-y
90 say; e.g., \tr{(a `op` b) c = ...}. Illegal now.
92 New keyword: \tr{do}. Any old variables named \tr{do} will now cause
95 %************************************************************************
97 \subsection{Converting @Dialogue@ I/O}
99 %************************************************************************
101 In most cases, it's really easy to convert from Haskell~1.2's I/O to
102 the monadic I/O of Haskell~1.3.
106 The type \tr{Dialogue} usually can become \tr{IO ()}.
108 Change \tr{readChan stdin} to \tr{getContents}.
110 Change \tr{appendChan stdout} to \tr{putStr}, which is equivalent to
112 Change \tr{appendChan stderr} to \tr{hPutStr stderr}.
114 You need to \tr{import IO} to do any interesting I/O; in particular,
115 anything to do with @Handle@s.
117 You need to \tr{import System} if you used @getArgs@, @getEnv@,
120 Assuming continuation-style @Dialogue@ code, change \tr{... exit done $}
121 to \tr{... >>}. Change \tr{... exit $ \ foo ->} to \tr{... >>= \ foo ->}.
123 Sometimes, when you change your \tr{main} routine to ``do'' notation,
124 you'll get an error message like:
126 "Main.hs", line 32: No instance for: Prelude.MonadZero IO
127 "Main.hs", line 32: in a do statement
129 This probably means that you need to ``twiddle'' some patterns; e.g.,
130 I added the twiddle to the \tr{getArgs}-related pattern here:
134 let x = fst (head ((reads::ReadS Int) a1)
135 putStr (show (result x))
138 If you had any functions named \tr{(>>)}, \tr{(>>=)}, or \tr{return},
139 change them to something else.
141 If you used the \tr{StatusFile} I/O request, do something else. No
142 exact equivalent exists in 1.3.
145 %************************************************************************
147 \subsection{Converting from pre-1.3 monadic I/O}
149 %************************************************************************
151 GHC~0.29 supported an early DRAFT of the Haskell~1.3 monadic I/O
152 facilities. Inevitably, what Really Made It into 1.3 is not quite
153 what was in the draft.
155 What was called \tr{handle} in the draft is now called \tr{catch}.
157 The type of the function \tr{fail} changed between draft and real-thing.
158 Old: \tr{fail x}; new: \tr{fail (userError x)}.
159 Also, what used to be \tr{failWith x} is now just \tr{fail x}.
161 The function \tr{try} didn't make it into 1.3 I/O. GHC supplies it
162 (at least for now) as @GHCio.tryIO@.
164 All the system modules named \tr{LibSomething} dropped the \tr{Lib}.
165 So: \tr{LibSystem} is now just \tr{System}.
167 In~0.29, you could mix-n-match @IO@ with @PrimIO@, simply because the
168 implementation happend to allow it. Not any more.
170 The \tr{IOError} type is now abstract; you cannot see it's
171 constructors. 1.3 provides functions to query errors.
173 %************************************************************************
175 \subsection{Use of the standard Prelude}
177 %************************************************************************
179 As with any previous Prelude/standard-modules changes, if you have
180 top-level functions that name-clash with imported entities, you'll get
181 compiler errors. So, for example, if your code defines a function
182 called \tr{seq} (entirely cool in Haskell~1.2), you will now get a
183 compiler error because there is now a Prelude entity called \tr{seq}.
184 (You may, of course, \tr{import Prelude hiding (seq)}, if you wish).
186 Names that frequently clash with new Prelude names:
191 Add \tr{import Ratio} if you use \tr{Rationals} at all.
192 Ditto: \tr{import Complex} if you use complex numbers.
193 Ditto: \tr{import Array} if you use arrays.
195 As suggested above, any \tr{LibXXX} system modules are now just
199 Arrays now use ordinary pairs, rather than a separate \tr{Assoc} type.
200 In some modules, we've found it easier to do:
205 and globally replace @:=@ with @=:@.
206 Works fine for expressions; useless for patterns, however.
208 For \tr{minInt}/\tr{maxInt} and \tr{minChar}/\tr{maxChar},
209 use the \tr{Bounded} class methods, \tr{minBound} and \tr{maxBound}.
211 Replace class \tr{Text} with \tr{Show}; on rare occasions,
212 you may need to do something for \tr{Read}, too.
214 The functions \tr{ord} and \tr{chr} have been replaced by
215 the class methods \tr{fromEnum} and \tr{toEnum}, respectively.
216 The changes, however, can lead to ambiguous overloading.
218 The functions \tr{even} and \tr{odd} used to be methods of class
219 @Integral@. They are now ordinary, overloaded functions.
221 The \tr{print} function now appends a newline to its output. This is
224 \tr{readDec} no longer exists; use \tr{(reads::ReadS Int)}, or similar.
226 If you relied on \tr{take}, \tr{drop}, \tr{splitAt}, etc., being
227 overloaded, you will need to switch to \tr{genericTake},
228 \tr{genericDrop}, etc., (imported from \tr{List}).
230 %************************************************************************
232 \subsection{The module system}
234 %************************************************************************
236 GHC~2.04 is fussier than 0.29 about junk in import lists. This is a
239 \tr{Foo..} (in export lists) must be changed to \tr{module Foo}.
241 Type synonyms may be imported/exported with or without \tr{(..)}---it
242 was required in Haskell~1.2--but you get a warning if you do it with.
244 %************************************************************************
246 \subsection{Use of Glasgow extensions}
248 %************************************************************************
250 Leading-underscore names are {\em gone}. Simon hated them.
252 To get access to GHC innards, you will need to import system modules
253 with names of the form \tr{GHCxxx}. We plan to restrict access to
254 such interfaces in the future; and, of course, we reserve the right to
255 make horrible changes to \tr{GHC*} modules....
257 You can't dig around inside the @ST@/@IO@ monads quite so freely.
259 If you want to provide your own @mainPrimIO@ routine, it must be
260 in a module @GHCmain@, not @Main@.
262 The old \tr{PreludePrimIO} interface is DEAD.
264 The even-older \tr{PreludeGlaIO} interface is DEADER.
266 @returnPrimIO@, @thenPrimIO@, and @seqPrimIO@ are deprecated. You
269 @foldrPrimIO@ has died.
271 @_FILE@, @fclose@, @fdopen@, @fflush@, @fopen@, @fread@, @freopen@,
272 and @fwrite@ are dead.
274 @appendChanPrimIO@, @appendFilePrimIO@, @getArgsPrimIO@, and
275 @readChanPrimIO@ are dead (as previously warned).
277 The \tr{LibPosix} stuff didn't make it into 1.3 I/O, so it has become
278 a ``system library'' (\tr{-syslib posix}). Other than dropping the
279 \tr{Lib*} prefix, everything should be the same as in 0.29.