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.xx
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 All the system modules named \tr{LibSomething} dropped the \tr{Lib}.
162 So: \tr{LibSystem} is now just \tr{System}.
164 As of 2.09, GHC doesn't have @PrimIO@, and all @_ccall_@s now return
165 type @IO a@, where @a@ is the return type of the @_ccall_@ itself.
167 The \tr{IOError} type is now abstract; you cannot see it's
168 constructors. 1.3 provides functions to query errors.
170 %************************************************************************
172 \subsection{Use of the standard Prelude}
174 %************************************************************************
176 As with any previous Prelude/standard-modules changes, if you have
177 top-level functions that name-clash with imported entities, you'll get
178 compiler errors. So, for example, if your code defines a function
179 called \tr{seq} (entirely cool in Haskell~1.2), you will now get a
180 compiler error because there is now a Prelude entity called \tr{seq}.
181 (You may, of course, \tr{import Prelude hiding (seq)}, if you wish).
183 Names that frequently clash with new Prelude names:
188 Add \tr{import Ratio} if you use \tr{Rationals} at all.
189 Ditto: \tr{import Complex} if you use complex numbers.
190 Ditto: \tr{import Array} if you use arrays.
192 As suggested above, any \tr{LibXXX} system modules are now just
196 Arrays now use ordinary pairs, rather than a separate \tr{Assoc} type.
197 In some modules, we've found it easier to do:
202 and globally replace @:=@ with @=:@.
203 Works fine for expressions; useless for patterns, however.
205 For \tr{minInt}/\tr{maxInt} and \tr{minChar}/\tr{maxChar},
206 use the \tr{Bounded} class methods, \tr{minBound} and \tr{maxBound}.
208 Replace class \tr{Text} with \tr{Show}; on rare occasions,
209 you may need to do something for \tr{Read}, too.
211 The functions \tr{ord} and \tr{chr} have been replaced by
212 the class methods \tr{fromEnum} and \tr{toEnum}, respectively.
213 The changes, however, can lead to ambiguous overloading.
215 The functions \tr{even} and \tr{odd} used to be methods of class
216 @Integral@. They are now ordinary, overloaded functions.
218 The \tr{print} function now appends a newline to its output. This is
221 \tr{readDec} no longer exists; use \tr{(reads::ReadS Int)}, or similar.
223 If you relied on \tr{take}, \tr{drop}, \tr{splitAt}, etc., being
224 overloaded, you will need to switch to \tr{genericTake},
225 \tr{genericDrop}, etc., (imported from \tr{List}).
227 %************************************************************************
229 \subsection{The module system}
231 %************************************************************************
233 GHC~2.04 is fussier than 0.29 about junk in import lists. This is a
236 \tr{Foo..} (in export lists) must be changed to \tr{module Foo}.
238 Type synonyms may be imported/exported with or without \tr{(..)}---it
239 was required in Haskell~1.2--but you get a warning if you do it with.
241 %************************************************************************
243 \subsection{Use of Glasgow extensions}
245 %************************************************************************
247 Leading-underscore names are {\em gone}. Simon hated them.
249 To get access to GHC innards, you will need to import system modules
250 with names of the form \tr{GHCxxx}. We plan to restrict access to
251 such interfaces in the future; and, of course, we reserve the right to
252 make horrible changes to \tr{GHC*} modules....
254 You can't dig around inside the @ST@/@IO@ monads quite so freely.
256 The old \tr{PreludePrimIO} interface is DEAD.
258 The even-older \tr{PreludeGlaIO} interface is DEADER.
260 @returnPrimIO@, @thenPrimIO@, and @seqPrimIO@ are deprecated. You
263 @foldrPrimIO@ has died.
265 @_FILE@, @fclose@, @fdopen@, @fflush@, @fopen@, @fread@, @freopen@,
266 and @fwrite@ are dead.
268 @appendChanPrimIO@, @appendFilePrimIO@, @getArgsPrimIO@, and
269 @readChanPrimIO@ are dead (as previously warned).
271 The \tr{LibPosix} stuff didn't make it into 1.3 I/O, so it has become
272 a ``system library'' (\tr{-syslib posix}). Other than dropping the
273 \tr{Lib*} prefix, everything should be the same as in 0.29.
275 As of GHC 2.09, @mainPrimIO@ is now called @mainIO@, to reflect the
276 fact that @PrimIO@ doesn't exist anymore. If you want to provide your
277 own @mainIO@ routine, it must be in a module @GHCmain@, not @Main@.