2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[RenameMonad12]{The monad used by the renamer passes 1 and 2}
7 #include "HsVersions.h"
11 initRn12, thenRn12, returnRn12,
12 mapRn12, zipWithRn12, foldrRn12,
13 addErrRn12, getModuleNameRn12, recoverQuietlyRn12,
15 -- and to make the interface self-sufficient...
16 Bag, Pretty(..), PprStyle, PrettyRep
22 import Pretty -- for type Pretty
23 import Util -- for pragmas only
28 In this monad, we pass down the name of the module we are working on,
29 and we thread the collected errors.
33 = FAST_STRING{-module name-}
35 -> (result, Bag Error)
37 #ifdef __GLASGOW_HASKELL__
38 {-# INLINE thenRn12 #-}
39 {-# INLINE returnRn12 #-}
42 initRn12 :: FAST_STRING{-module name-} -> Rn12M a -> (a, Bag Error)
43 initRn12 mod action = action mod emptyBag
45 thenRn12 :: Rn12M a -> (a -> Rn12M b) -> Rn12M b
46 thenRn12 expr continuation mod errs_so_far
47 = case (expr mod errs_so_far) of
48 (res1, errs1) -> continuation res1 mod errs1
50 returnRn12 :: a -> Rn12M a
51 returnRn12 x mod errs_so_far = (x, errs_so_far)
53 mapRn12 :: (a -> Rn12M b) -> [a] -> Rn12M [b]
55 mapRn12 f [] = returnRn12 []
57 = f x `thenRn12` \ r ->
58 mapRn12 f xs `thenRn12` \ rs ->
61 zipWithRn12 :: (a -> b -> Rn12M c) -> [a] -> [b] -> Rn12M [c]
63 zipWithRn12 f [] [] = returnRn12 []
64 zipWithRn12 f (x:xs) (y:ys)
65 = f x y `thenRn12` \ r ->
66 zipWithRn12 f xs ys `thenRn12` \ rs ->
69 foldrRn12 :: (a -> b -> Rn12M b) -> b -> [a] -> Rn12M b
71 foldrRn12 f z [] = returnRn12 z
73 = foldrRn12 f z xs `thenRn12` \ rest ->
76 addErrRn12 :: Error -> Rn12M ()
77 addErrRn12 err mod errs_so_far
78 = ( (), errs_so_far `snocBag` err )
80 getModuleNameRn12 :: Rn12M FAST_STRING
81 getModuleNameRn12 mod errs_so_far = (mod, errs_so_far)
85 recoverQuietlyRn12 :: a -> Rn12M a -> Rn12M a
87 recoverQuietlyRn12 use_this_if_err action mod errs_so_far
90 = case (action mod emptyBag{-no errors-}) of { (res, errs) ->
91 if isEmptyBag errs then
92 (res, errs_so_far) -- retain incoming errs
94 (use_this_if_err, errs_so_far)