[project @ 2000-10-26 16:51:44 by sewardj]
[ghc-hetmet.git] / ghc / compiler / main / CmdLineOpts.lhs
1 %
2 % (c) The University of Glasgow, 1996-2000
3 %
4 \section[CmdLineOpts]{Things to do with command-line options}
5
6 \begin{code}
7
8 module CmdLineOpts (
9         CoreToDo(..),
10         SimplifierSwitch(..), isAmongSimpl,
11         StgToDo(..),
12         SwitchResult(..),
13         HscLang(..),
14         DynFlag(..),    -- needed non-abstractly by DriverFlags
15         DynFlags(..),
16
17         v_Static_hsc_opts,
18
19         intSwitchSet,
20         switchIsOn,
21         isStaticHscFlag,
22
23         opt_PprStyle_NoPrags,
24         opt_PprUserLength,
25         opt_PprStyle_Debug,
26
27         dopt,
28
29         -- other dynamic flags
30         dopt_CoreToDo,
31         dopt_StgToDo,
32         dopt_HscLang,
33         dopt_OutName,
34
35         -- profiling opts
36         opt_AutoSccsOnAllToplevs,
37         opt_AutoSccsOnExportedToplevs,
38         opt_AutoSccsOnIndividualCafs,
39         opt_AutoSccsOnDicts,
40         opt_SccProfilingOn,
41         opt_DoTickyProfiling,
42
43         -- language opts
44         opt_AllStrict,
45         opt_DictsStrict,
46         opt_MaxContextReductionDepth,
47         opt_IrrefutableTuples,
48         opt_NumbersStrict,
49         opt_Parallel,
50         opt_SMP,
51
52         -- optimisation opts
53         opt_DoSemiTagging,
54         opt_FoldrBuildOn,
55         opt_LiberateCaseThreshold,
56         opt_StgDoLetNoEscapes,
57         opt_UnfoldCasms,
58         opt_UsageSPOn,
59         opt_UnboxStrictFields,
60         opt_SimplNoPreInlining,
61         opt_SimplDoEtaReduction,
62         opt_SimplDoLambdaEtaExpansion,
63         opt_SimplCaseOfCase,
64         opt_SimplCaseMerge,
65         opt_SimplPedanticBottoms,
66         opt_SimplExcessPrecision,
67
68         -- Unfolding control
69         opt_UF_HiFileThreshold,
70         opt_UF_CreationThreshold,
71         opt_UF_UseThreshold,
72         opt_UF_FunAppDiscount,
73         opt_UF_KeenessFactor,
74         opt_UF_UpdateInPlace,
75         opt_UF_CheapOp,
76         opt_UF_DearOp,
77
78         -- misc opts
79         opt_InPackage,
80         opt_EmitCExternDecls,
81         opt_EnsureSplittableC,
82         opt_GranMacros,
83         opt_HiVersion,
84         opt_HistorySize,
85         opt_IgnoreAsserts,
86         opt_IgnoreIfacePragmas,
87         opt_NoHiCheck,
88         opt_NoImplicitPrelude,
89         opt_OmitBlackHoling,
90         opt_OmitInterfacePragmas,
91         opt_NoPruneTyDecls,
92         opt_NoPruneDecls,
93         opt_Static,
94         opt_Unregisterised,
95         opt_Verbose
96     ) where
97
98 #include "HsVersions.h"
99
100 import Array    ( array, (//) )
101 import GlaExts
102 import IOExts   ( IORef, readIORef )
103 import Argv
104 import Constants        -- Default values for some flags
105 import Util
106 import FastTypes
107
108 import Maybes           ( firstJust )
109 import Panic            ( panic )
110
111 #if __GLASGOW_HASKELL__ < 301
112 import ArrBase  ( Array(..) )
113 #else
114 import PrelArr  ( Array(..) )
115 #endif
116 \end{code}
117
118 %************************************************************************
119 %*                                                                      *
120 \subsection{Command-line options}
121 %*                                                                      *
122 %************************************************************************
123
124 The hsc command-line options are split into two categories:
125
126   - static flags
127   - dynamic flags
128
129 Static flags are represented by top-level values of type Bool or Int,
130 for example.  They therefore have the same value throughout the
131 invocation of hsc.
132
133 Dynamic flags are represented by an abstract type, DynFlags, which is
134 passed into hsc by the compilation manager for every compilation.
135 Dynamic flags are those that change on a per-compilation basis,
136 perhaps because they may be present in the OPTIONS pragma at the top
137 of a module.
138
139 Other flag-related blurb:
140
141 A list of {\em ToDo}s is things to be done in a particular part of
142 processing.  A (fictitious) example for the Core-to-Core simplifier
143 might be: run the simplifier, then run the strictness analyser, then
144 run the simplifier again (three ``todos'').
145
146 There are three ``to-do processing centers'' at the moment.  In the
147 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
148 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
149 (\tr{simplStg/SimplStg.lhs}).
150
151 %************************************************************************
152 %*                                                                      *
153 \subsection{Datatypes associated with command-line options}
154 %*                                                                      *
155 %************************************************************************
156
157 \begin{code}
158 data SwitchResult
159   = SwBool      Bool            -- on/off
160   | SwString    FAST_STRING     -- nothing or a String
161   | SwInt       Int             -- nothing or an Int
162 \end{code}
163
164 \begin{code}
165 data CoreToDo           -- These are diff core-to-core passes,
166                         -- which may be invoked in any order,
167                         -- as many times as you like.
168
169   = CoreDoSimplify      -- The core-to-core simplifier.
170         (SimplifierSwitch -> SwitchResult)
171                         -- Each run of the simplifier can take a different
172                         -- set of simplifier-specific flags.
173   | CoreDoFloatInwards
174   | CoreDoFloatOutwards Bool    -- True <=> float lambdas to top level
175   | CoreLiberateCase
176   | CoreDoPrintCore
177   | CoreDoStaticArgs
178   | CoreDoStrictness
179   | CoreDoWorkerWrapper
180   | CoreDoSpecialising
181   | CoreDoUSPInf
182   | CoreDoCPResult
183   | CoreDoGlomBinds
184   | CoreCSE
185
186   | CoreDoNothing        -- useful when building up lists of these things
187 \end{code}
188
189 \begin{code}
190 data StgToDo
191   = StgDoStaticArgs
192   | StgDoLambdaLift
193   | StgDoMassageForProfiling  -- should be (next to) last
194   -- There's also setStgVarInfo, but its absolute "lastness"
195   -- is so critical that it is hardwired in (no flag).
196   | D_stg_stats
197 \end{code}
198
199 \begin{code}
200 data SimplifierSwitch
201   = MaxSimplifierIterations Int
202   | SimplInlinePhase Int
203   | DontApplyRules
204   | NoCaseOfCase
205   | SimplLetToCase
206 \end{code}
207
208 %************************************************************************
209 %*                                                                      *
210 \subsection{Dynamic command-line options}
211 %*                                                                      *
212 %************************************************************************
213
214 \begin{code}
215 data DynFlag
216
217    -- debugging flags
218    = Opt_D_dump_all
219    | Opt_D_dump_most
220    | Opt_D_dump_absC
221    | Opt_D_dump_asm
222    | Opt_D_dump_cpranal
223    | Opt_D_dump_deriv
224    | Opt_D_dump_ds
225    | Opt_D_dump_flatC
226    | Opt_D_dump_foreign
227    | Opt_D_dump_inlinings
228    | Opt_D_dump_occur_anal
229    | Opt_D_dump_parsed
230    | Opt_D_dump_realC
231    | Opt_D_dump_rn
232    | Opt_D_dump_simpl
233    | Opt_D_dump_simpl_iterations
234    | Opt_D_dump_spec
235    | Opt_D_dump_stg
236    | Opt_D_dump_stranal
237    | Opt_D_dump_tc
238    | Opt_D_dump_types
239    | Opt_D_dump_rules
240    | Opt_D_dump_usagesp
241    | Opt_D_dump_cse
242    | Opt_D_dump_worker_wrapper
243    | Opt_D_show_passes
244    | Opt_D_dump_rn_trace
245    | Opt_D_dump_rn_stats
246    | Opt_D_dump_stix
247    | Opt_D_dump_simpl_stats
248    | Opt_D_source_stats
249    | Opt_D_verbose_core2core
250    | Opt_D_verbose_stg2stg
251    | Opt_D_dump_hi_diffs
252    | Opt_D_dump_minimal_imports
253    | Opt_DoCoreLinting
254    | Opt_DoStgLinting
255    | Opt_DoUSPLinting
256
257    | Opt_WarnDuplicateExports
258    | Opt_WarnHiShadows
259    | Opt_WarnIncompletePatterns
260    | Opt_WarnMissingFields
261    | Opt_WarnMissingMethods
262    | Opt_WarnMissingSigs
263    | Opt_WarnNameShadowing
264    | Opt_WarnOverlappingPatterns
265    | Opt_WarnSimplePatterns
266    | Opt_WarnTypeDefaults
267    | Opt_WarnUnusedBinds
268    | Opt_WarnUnusedImports
269    | Opt_WarnUnusedMatches
270    | Opt_WarnDeprecations
271
272    -- language opts
273    | Opt_AllowOverlappingInstances
274    | Opt_AllowUndecidableInstances
275    | Opt_GlasgowExts
276    | Opt_Generics
277
278    -- misc
279    | Opt_ReportCompile
280    deriving (Eq)
281
282 data DynFlags = DynFlags {
283   coreToDo   :: [CoreToDo],
284   stgToDo    :: [StgToDo],
285   hscLang    :: HscLang,
286   hscOutName :: String,  -- name of the file in which to place output
287   flags      :: [DynFlag]
288  }
289
290 dopt :: DynFlag -> DynFlags -> Bool
291 dopt f dflags  = f `elem` (flags dflags)
292
293 dopt_CoreToDo :: DynFlags -> [CoreToDo]
294 dopt_CoreToDo = coreToDo
295
296 dopt_StgToDo :: DynFlags -> [StgToDo]
297 dopt_StgToDo = stgToDo
298
299 dopt_OutName :: DynFlags -> String
300 dopt_OutName = hscOutName
301
302 data HscLang
303   = HscC
304   | HscAsm
305   | HscJava
306   | HscInterpreted
307     deriving Eq
308
309 dopt_HscLang :: DynFlags -> HscLang
310 dopt_HscLang = hscLang
311 \end{code}
312
313 %************************************************************************
314 %*                                                                      *
315 \subsection{Classifying command-line options}
316 %*                                                                      *
317 %************************************************************************
318
319 \begin{code}
320 -- v_Statis_hsc_opts is here to avoid a circular dependency with
321 -- main/DriverState.
322 GLOBAL_VAR(v_Static_hsc_opts, [], [String])
323
324 lookUp           :: FAST_STRING -> Bool
325 lookup_int       :: String -> Maybe Int
326 lookup_def_int   :: String -> Int -> Int
327 lookup_def_float :: String -> Float -> Float
328 lookup_str       :: String -> Maybe String
329
330 unpacked_static_opts = unsafePerformIO (readIORef v_Static_hsc_opts)
331 packed_static_opts   = map _PK_ unpacked_static_opts
332
333 lookUp     sw = sw `elem` packed_static_opts
334         
335 lookup_str sw = firstJust (map (startsWith sw) unpacked_static_opts)
336
337 lookup_int sw = case (lookup_str sw) of
338                   Nothing -> Nothing
339                   Just xx -> Just (read xx)
340
341 lookup_def_int sw def = case (lookup_str sw) of
342                             Nothing -> def              -- Use default
343                             Just xx -> read xx
344
345 lookup_def_char sw def = case (lookup_str sw) of
346                             Just (xx:_) -> xx
347                             _           -> def          -- Use default
348
349 lookup_def_float sw def = case (lookup_str sw) of
350                             Nothing -> def              -- Use default
351                             Just xx -> read xx
352
353
354 {-
355  Putting the compiler options into temporary at-files
356  may turn out to be necessary later on if we turn hsc into
357  a pure Win32 application where I think there's a command-line
358  length limit of 255. unpacked_opts understands the @ option.
359
360 unpacked_opts :: [String]
361 unpacked_opts =
362   concat $
363   map (expandAts) $
364   map _UNPK_ argv  -- NOT ARGV any more: v_Static_hsc_opts
365   where
366    expandAts ('@':fname) = words (unsafePerformIO (readFile fname))
367    expandAts l = [l]
368 -}
369 \end{code}
370
371 %************************************************************************
372 %*                                                                      *
373 \subsection{Static options}
374 %*                                                                      *
375 %************************************************************************
376
377 \begin{code}
378 -- debugging opts
379 opt_PprStyle_NoPrags            = lookUp  SLIT("-dppr-noprags")
380 opt_PprStyle_Debug              = lookUp  SLIT("-dppr-debug")
381 opt_PprUserLength               = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
382
383 -- profiling opts
384 opt_AutoSccsOnAllToplevs        = lookUp  SLIT("-fauto-sccs-on-all-toplevs")
385 opt_AutoSccsOnExportedToplevs   = lookUp  SLIT("-fauto-sccs-on-exported-toplevs")
386 opt_AutoSccsOnIndividualCafs    = lookUp  SLIT("-fauto-sccs-on-individual-cafs")
387 opt_AutoSccsOnDicts             = lookUp  SLIT("-fauto-sccs-on-dicts")
388 opt_SccProfilingOn              = lookUp  SLIT("-fscc-profiling")
389 opt_DoTickyProfiling            = lookUp  SLIT("-fticky-ticky")
390
391 -- language opts
392 opt_AllStrict                   = lookUp  SLIT("-fall-strict")
393 opt_DictsStrict                 = lookUp  SLIT("-fdicts-strict")
394 opt_IrrefutableTuples           = lookUp  SLIT("-firrefutable-tuples")
395 opt_MaxContextReductionDepth    = lookup_def_int "-fcontext-stack" mAX_CONTEXT_REDUCTION_DEPTH
396 opt_NumbersStrict               = lookUp  SLIT("-fnumbers-strict")
397 opt_Parallel                    = lookUp  SLIT("-fparallel")
398 opt_SMP                         = lookUp  SLIT("-fsmp")
399
400 -- optimisation opts
401 opt_DoSemiTagging               = lookUp  SLIT("-fsemi-tagging")
402 opt_FoldrBuildOn                = lookUp  SLIT("-ffoldr-build-on")
403 opt_LiberateCaseThreshold       = lookup_def_int "-fliberate-case-threshold" (10::Int)
404 opt_StgDoLetNoEscapes           = lookUp  SLIT("-flet-no-escape")
405 opt_UnfoldCasms                 = lookUp SLIT("-funfold-casms-in-hi-file")
406 opt_UsageSPOn                   = lookUp  SLIT("-fusagesp-on")
407 opt_UnboxStrictFields           = lookUp  SLIT("-funbox-strict-fields")
408
409 {-
410    The optional '-inpackage=P' flag tells what package
411    we are compiling this module for.
412    The Prelude, for example is compiled with '-package prelude'
413 -}
414 opt_InPackage                   = case lookup_str "-inpackage=" of
415                                     Just p  -> _PK_ p
416                                     Nothing -> SLIT("Main")     -- The package name if none is specified
417
418 opt_EmitCExternDecls            = lookUp  SLIT("-femit-extern-decls")
419 opt_EnsureSplittableC           = lookUp  SLIT("-fglobalise-toplev-names")
420 opt_GranMacros                  = lookUp  SLIT("-fgransim")
421 opt_HiVersion                   = lookup_def_int "-fhi-version=" 0 -- what version we're compiling.
422 opt_HistorySize                 = lookup_def_int "-fhistory-size" 20
423 opt_IgnoreAsserts               = lookUp  SLIT("-fignore-asserts")
424 opt_IgnoreIfacePragmas          = lookUp  SLIT("-fignore-interface-pragmas")
425 opt_NoHiCheck                   = lookUp  SLIT("-fno-hi-version-check")
426 opt_NoImplicitPrelude           = lookUp  SLIT("-fno-implicit-prelude")
427 opt_OmitBlackHoling             = lookUp  SLIT("-dno-black-holing")
428 opt_OmitInterfacePragmas        = lookUp  SLIT("-fomit-interface-pragmas")
429
430 -- Simplifier switches
431 opt_SimplNoPreInlining          = lookUp SLIT("-fno-pre-inlining")
432         -- NoPreInlining is there just to see how bad things
433         -- get if you don't do it!
434 opt_SimplDoEtaReduction         = lookUp SLIT("-fdo-eta-reduction")
435 opt_SimplDoLambdaEtaExpansion   = lookUp SLIT("-fdo-lambda-eta-expansion")
436 opt_SimplCaseOfCase             = lookUp SLIT("-fcase-of-case")
437 opt_SimplCaseMerge              = lookUp SLIT("-fcase-merge")
438 opt_SimplPedanticBottoms        = lookUp SLIT("-fpedantic-bottoms")
439 opt_SimplExcessPrecision        = lookUp SLIT("-fexcess-precision")
440
441 -- Unfolding control
442 opt_UF_HiFileThreshold          = lookup_def_int "-funfolding-interface-threshold" (45::Int)
443 opt_UF_CreationThreshold        = lookup_def_int "-funfolding-creation-threshold"  (45::Int)
444 opt_UF_UseThreshold             = lookup_def_int "-funfolding-use-threshold"       (8::Int)     -- Discounts can be big
445 opt_UF_FunAppDiscount           = lookup_def_int "-funfolding-fun-discount"        (6::Int)     -- It's great to inline a fn
446 opt_UF_KeenessFactor            = lookup_def_float "-funfolding-keeness-factor"    (1.5::Float)
447 opt_UF_UpdateInPlace            = lookUp  SLIT("-funfolding-update-in-place")
448
449 opt_UF_CheapOp  = ( 1 :: Int)   -- Only one instruction; and the args are charged for
450 opt_UF_DearOp   = ( 4 :: Int)
451                         
452 opt_NoPruneDecls                = lookUp SLIT("-fno-prune-decls")
453 opt_NoPruneTyDecls              = lookUp SLIT("-fno-prune-tydecls")
454 opt_Static                      = lookUp SLIT("-static")
455 opt_Unregisterised              = lookUp SLIT("-funregisterised")
456 opt_Verbose                     = lookUp SLIT("-v")
457 \end{code}
458
459 %************************************************************************
460 %*                                                                      *
461 \subsection{List of static hsc flags}
462 %*                                                                      *
463 %************************************************************************
464
465 \begin{code}
466 isStaticHscFlag f =
467   f `elem` [
468         "-fauto-sccs-on-all-toplevs",
469         "-fauto-sccs-on-exported-toplevs",
470         "-fauto-sccs-on-individual-cafs",
471         "-fauto-sccs-on-dicts",
472         "-fscc-profiling",
473         "-fticky-ticky",
474         "-fall-strict",
475         "-fdicts-strict",
476         "-fgenerics",
477         "-firrefutable-tuples",
478         "-fnumbers-strict",
479         "-fparallel",
480         "-fsmp",
481         "-fsemi-tagging",
482         "-ffoldr-build-on",
483         "-flet-no-escape",
484         "-funfold-casms-in-hi-file",
485         "-fusagesp-on",
486         "-funbox-strict-fields",
487         "-femit-extern-decls",
488         "-fglobalise-toplev-names",
489         "-fgransim",
490         "-fignore-asserts",
491         "-fignore-interface-pragmas",
492         "-fno-hi-version-check",
493         "-fno-implicit-prelude",
494         "-dno-black-holing",
495         "-fomit-interface-pragmas",
496         "-fno-pre-inlining",
497         "-fdo-eta-reduction",
498         "-fdo-lambda-eta-expansion",
499         "-fcase-of-case",
500         "-fcase-merge",
501         "-fpedantic-bottoms",
502         "-fexcess-precision",
503         "-funfolding-update-in-place",
504         "-freport-compile",
505         "-fno-prune-decls",
506         "-fno-prune-tydecls",
507         "-static",
508         "-funregisterised",
509         "-v" ]
510   || any (flip prefixMatch f) [
511         "-fcontext-stack",
512         "-fliberate-case-threshold",
513         "-fhi-version=",
514         "-fhistory-size",
515         "-funfolding-interface-threshold",
516         "-funfolding-creation-threshold",
517         "-funfolding-use-threshold",
518         "-funfolding-fun-discount",
519         "-funfolding-keeness-factor"
520      ]
521 \end{code}
522
523 %************************************************************************
524 %*                                                                      *
525 \subsection{Switch ordering}
526 %*                                                                      *
527 %************************************************************************
528
529 These things behave just like enumeration types.
530
531 \begin{code}
532 instance Eq SimplifierSwitch where
533     a == b = tagOf_SimplSwitch a ==# tagOf_SimplSwitch b
534
535 instance Ord SimplifierSwitch where
536     a <  b  = tagOf_SimplSwitch a <# tagOf_SimplSwitch b
537     a <= b  = tagOf_SimplSwitch a <=# tagOf_SimplSwitch b
538
539
540 tagOf_SimplSwitch (SimplInlinePhase _)          = _ILIT(1)
541 tagOf_SimplSwitch (MaxSimplifierIterations _)   = _ILIT(2)
542 tagOf_SimplSwitch DontApplyRules                = _ILIT(3)
543 tagOf_SimplSwitch SimplLetToCase                = _ILIT(4)
544 tagOf_SimplSwitch NoCaseOfCase                  = _ILIT(5)
545
546 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
547
548 lAST_SIMPL_SWITCH_TAG = 5
549 \end{code}
550
551 %************************************************************************
552 %*                                                                      *
553 \subsection{Switch lookup}
554 %*                                                                      *
555 %************************************************************************
556
557 \begin{code}
558 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
559 isAmongSimpl on_switches                -- Switches mentioned later occur *earlier*
560                                         -- in the list; defaults right at the end.
561   = let
562         tidied_on_switches = foldl rm_dups [] on_switches
563                 -- The fold*l* ensures that we keep the latest switches;
564                 -- ie the ones that occur earliest in the list.
565
566         sw_tbl :: Array Int SwitchResult
567         sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
568                         all_undefined)
569                  // defined_elems
570
571         all_undefined = [ (i, SwBool False) | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
572
573         defined_elems = map mk_assoc_elem tidied_on_switches
574     in
575     -- (avoid some unboxing, bounds checking, and other horrible things:)
576 #if __GLASGOW_HASKELL__ < 405
577     case sw_tbl of { Array bounds_who_needs_'em stuff ->
578 #else
579     case sw_tbl of { Array _ _ stuff ->
580 #endif
581     \ switch ->
582         case (indexArray# stuff (tagOf_SimplSwitch switch)) of
583 #if __GLASGOW_HASKELL__ < 400
584           Lift v -> v
585 #elif __GLASGOW_HASKELL__ < 403
586           (# _, v #) -> v
587 #else
588           (# v #) -> v
589 #endif
590     }
591   where
592     mk_assoc_elem k@(MaxSimplifierIterations lvl)
593         = (iBox (tagOf_SimplSwitch k), SwInt lvl)
594     mk_assoc_elem k@(SimplInlinePhase n)
595         = (iBox (tagOf_SimplSwitch k), SwInt n)
596     mk_assoc_elem k
597         = (iBox (tagOf_SimplSwitch k), SwBool True) -- I'm here, Mom!
598
599     -- cannot have duplicates if we are going to use the array thing
600     rm_dups switches_so_far switch
601       = if switch `is_elem` switches_so_far
602         then switches_so_far
603         else switch : switches_so_far
604       where
605         sw `is_elem` []     = False
606         sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) ==# (tagOf_SimplSwitch s)
607                             || sw `is_elem` ss
608 \end{code}
609
610 Default settings for simplifier switches
611
612 \begin{code}
613 defaultSimplSwitches = [MaxSimplifierIterations 1]
614 \end{code}
615
616 %************************************************************************
617 %*                                                                      *
618 \subsection{Misc functions for command-line options}
619 %*                                                                      *
620 %************************************************************************
621
622
623 \begin{code}
624 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
625
626 switchIsOn lookup_fn switch
627   = case (lookup_fn switch) of
628       SwBool False -> False
629       _            -> True
630
631 intSwitchSet :: (switch -> SwitchResult)
632              -> (Int -> switch)
633              -> Maybe Int
634
635 intSwitchSet lookup_fn switch
636   = case (lookup_fn (switch (panic "intSwitchSet"))) of
637       SwInt int -> Just int
638       _         -> Nothing
639 \end{code}
640
641 \begin{code}
642 startsWith :: String -> String -> Maybe String
643 -- startsWith pfx (pfx++rest) = Just rest
644
645 startsWith []     str = Just str
646 startsWith (c:cs) (s:ss)
647   = if c /= s then Nothing else startsWith cs ss
648 startsWith  _     []  = Nothing
649
650 endsWith  :: String -> String -> Maybe String
651 endsWith cs ss
652   = case (startsWith (reverse cs) (reverse ss)) of
653       Nothing -> Nothing
654       Just rs -> Just (reverse rs)
655 \end{code}