[project @ 1997-03-24 04:42:42 by sof]
[ghc-hetmet.git] / ghc / docs / users_guide / backwards.lit
1 %************************************************************************
2 %*                                                                      *
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}
6 %*                                                                      *
7 %************************************************************************
8
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.26 (a Haskell~1.2 compiler, mostly) to GHC~2.01
12 (a Haskell~1.3 compiler).
13
14 %ToDo: index
15
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.
19
20 For example, in GHC~0.26, \tr{__GLASGOW_HASKELL__} will be 26; for~2.01,
21 it will be 201.  Thus, you can write:
22 \begin{verbatim}
23 #if __HASKELL1__ <= 2
24 main = appendChan stdout "Hello, world!\n" exit done -- 1.2
25 #else
26 # if __GLASGOW_HASKELL__ >= 200
27 import IO
28 main = putStr "Hello, world!"   -- real 1.3
29 # else
30 main = putStr "Hello, world!\n" -- pseudo-1.3 in 0.2x
31 # endif
32 #endif
33 \end{verbatim}
34
35 %************************************************************************
36 %*                                                                      *
37 \subsection{Types}
38 %*                                                                      *
39 %************************************************************************
40
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:
43 \begin{verbatim}
44 map :: Functor f => (a->b) -> f a -> f b
45 \end{verbatim}
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:
49 \begin{code}
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]
53 \end{code}
54 With the type signature this is fine, but if you omit the type signature
55 you'll get:
56 \begin{verbatim}
57   "Color.hs", line 49: No instance for: Prelude.Eq (a{-a18d-} Prelude.Char)
58     "Color.hs", line 49:
59         at a use of an overloaded identifier: `Prelude.meth.Prelude.Eq.=='
60 \end{verbatim}
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.  
65
66 The solution to such messages is to add type signatures.
67
68 %************************************************************************
69 %*                                                                      *
70 \subsection{Lexical matters}
71 %*                                                                      *
72 %************************************************************************
73
74 Old uses of `compose' (\tr{(.)}) can magically turn into qualified
75 names; e.g., \tr{LiteralInt.leStringToInt}; add spaces.
76
77 Leading-underscore names (a Glasgow extension) don't work anymore,
78 even with \tr{-fglasgow-exts}.
79
80 The \tr{Prelude.lex} function doesn't grok Haskell comments any more
81 (a good simplification, but a change nonetheless).
82
83 %************************************************************************
84 %*                                                                      *
85 \subsection{Expressions and patterns}
86 %*                                                                      *
87 %************************************************************************
88
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.
91
92 New keyword: \tr{do}.  Any old variables named \tr{do} will now cause
93 syntax errors.
94
95 %************************************************************************
96 %*                                                                      *
97 \subsection{Converting @Dialogue@ I/O}
98 %*                                                                      *
99 %************************************************************************
100
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.
103
104 \begin{enumerate}
105 \item
106 The type \tr{Dialogue} usually can become \tr{IO ()}.
107 \item
108 Change \tr{readChan stdin} to \tr{getContents}.
109 \item
110 Change \tr{appendChan stdout} to \tr{putStr}, which is equivalent to
111 \tr{hPutStr stdout}.
112 Change \tr{appendChan stderr} to \tr{hPutStr stderr}.
113 \item
114 You need to \tr{import IO} to do any interesting I/O; in particular,
115 anything to do with @Handle@s.
116 \item
117 You need to \tr{import System} if you used @getArgs@, @getEnv@,
118 or @getProgName@.
119 \item
120 Assuming continuation-style @Dialogue@ code, change \tr{... exit done $}
121 to \tr{... >>}.  Change \tr{... exit $ \ foo ->} to \tr{... >>= \ foo ->}.
122 \item
123 Sometimes, when you change your \tr{main} routine to ``do'' notation,
124 you'll get an error message like:
125 \begin{verbatim}
126 "Main.hs", line 32: No instance for: Prelude.MonadZero IO
127     "Main.hs", line 32: in a do statement
128 \end{verbatim}
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:
131 \begin{verbatim}
132 main = do
133     ~[a1] <- getArgs
134     let x = fst (head ((reads::ReadS Int) a1)
135     putStr (show (result x))
136 \end{verbatim}
137 \item
138 If you had any functions named \tr{(>>)}, \tr{(>>=)}, or \tr{return},
139 change them to something else.
140 \item
141 If you used the \tr{StatusFile} I/O request, do something else.  No
142 exact equivalent exists in 1.3.
143 \end{enumerate}
144
145 %************************************************************************
146 %*                                                                      *
147 \subsection{Converting from pre-1.3 monadic I/O}
148 %*                                                                      *
149 %************************************************************************
150
151 GHC~0.26 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.
154
155 What was called \tr{handle} in the draft is now called \tr{catch}.
156
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}.
160
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@.
163
164 All the system modules named \tr{LibSomething} dropped the \tr{Lib}.
165 So: \tr{LibSystem} is now just \tr{System}.
166
167 In~0.26, you could mix-n-match @IO@ with @PrimIO@, simply because the
168 implementation happend to allow it.  Not any more.
169
170 The \tr{IOError} type is now abstract; you cannot see it's
171 constructors.  1.3 provides functions to query errors.
172
173 %************************************************************************
174 %*                                                                      *
175 \subsection{Use of the standard Prelude}
176 %*                                                                      *
177 %************************************************************************
178
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).
185
186 Names that frequently clash with new Prelude names:
187 \tr{fail},
188 \tr{lookup},
189 \tr{seq}.
190
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.  
194
195 As suggested above, any \tr{LibXXX} system modules are now just
196 \tr{XXX}.
197
198 Also: note that
199 Arrays now use ordinary pairs, rather than a separate \tr{Assoc} type.
200 In some modules, we've found it easier to do:
201 \begin{verbatim}
202 infix 1 =:
203 (=:) a b = (a,b)
204 \end{verbatim}
205 and globally replace @:=@ with @=:@.
206 Works fine for expressions; useless for patterns, however.
207
208 For \tr{minInt}/\tr{maxInt} and \tr{minChar}/\tr{maxChar},
209 use the \tr{Bounded} class methods, \tr{minBound} and \tr{maxBound}.
210
211 Replace class \tr{Text} with \tr{Show}; on rare occasions,
212 you may need to do something for \tr{Read}, too.
213
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.
217
218 The functions \tr{even} and \tr{odd} used to be methods of class
219 @Integral@.  They are now ordinary, overloaded functions.
220
221 The \tr{print} function now appends a newline to its output.  This is
222 good, but different.
223
224 \tr{readDec} no longer exists; use \tr{(reads::ReadS Int)}, or similar.
225
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}).
229
230 %************************************************************************
231 %*                                                                      *
232 \subsection{The module system}
233 %*                                                                      *
234 %************************************************************************
235
236 GHC~2.01 is fussier than 0.26 about junk in import lists.  This is a
237 feature.
238
239 \tr{Foo..} (in export lists) must be changed to \tr{module Foo}.
240
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.
243
244 %************************************************************************
245 %*                                                                      *
246 \subsection{Use of Glasgow extensions}
247 %*                                                                      *
248 %************************************************************************
249
250 Leading-underscore names are {\em gone}.  Simon hated them.
251
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....
256
257 You can't dig around inside the @ST@/@IO@ monads quite so freely.
258
259 If you want to provide your own @mainPrimIO@ routine, it must be
260 in a module @GHCmain@, not @Main@.
261
262 The old \tr{PreludePrimIO} interface is DEAD.
263
264 The even-older \tr{PreludeGlaIO} interface is DEADER.
265
266 @returnPrimIO@, @thenPrimIO@, and @seqPrimIO@ are deprecated.  You
267 were warned.
268
269 @foldrPrimIO@ has died.
270
271 @_FILE@, @fclose@, @fdopen@, @fflush@, @fopen@, @fread@, @freopen@,
272 and @fwrite@ are dead.
273
274 @appendChanPrimIO@, @appendFilePrimIO@, @getArgsPrimIO@, and
275 @readChanPrimIO@ are dead (as previously warned).
276
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.26.