[project @ 1999-06-17 09:51:16 by simonmar]
[ghc-hetmet.git] / ghc / compiler / deSugar / Desugar.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[Desugar]{@deSugar@: the main function}
5
6 \begin{code}
7 module Desugar ( deSugar ) where
8
9 #include "HsVersions.h"
10
11 import CmdLineOpts      ( opt_D_dump_ds )
12 import HsSyn            ( MonoBinds, RuleDecl(..), RuleBndr(..), HsExpr(..), HsBinds(..), MonoBinds(..) )
13 import HsCore           ( UfRuleBody(..) )
14 import TcHsSyn          ( TypecheckedMonoBinds, TypecheckedForeignDecl, TypecheckedRuleDecl )
15 import TcModule         ( TcResults(..) )
16 import CoreSyn
17 import Rules            ( ProtoCoreRule(..), pprProtoCoreRule )
18 import Subst            ( substExpr, mkSubst )
19 import DsMonad
20 import DsExpr           ( dsExpr )
21 import DsBinds          ( dsMonoBinds, AutoScc(..) )
22 import DsForeign        ( dsForeigns )
23 import DsUtils
24 import DsExpr           ()      -- Forces DsExpr to be compiled; DsBinds only
25                                 -- depends on DsExpr.hi-boot.
26 import Module           ( Module, moduleString )
27 import Id               ( Id )
28 import Name             ( isLocallyDefined )
29 import VarEnv
30 import VarSet
31 import Bag              ( isEmptyBag, unionBags )
32 import CmdLineOpts      ( opt_SccGroup, opt_SccProfilingOn )
33 import CoreLint         ( beginPass, endPass )
34 import ErrUtils         ( doIfSet, pprBagOfWarnings )
35 import Outputable
36 import UniqSupply       ( splitUniqSupply, UniqSupply )
37 \end{code}
38
39 %************************************************************************
40 %*                                                                      *
41 %*              The main function: deSugar
42 %*                                                                      *
43 %************************************************************************
44
45 The only trick here is to get the @DsMonad@ stuff off to a good
46 start.
47
48 \begin{code}
49 deSugar :: Module 
50         -> UniqSupply
51         -> TcResults
52         -> IO ([CoreBind], [ProtoCoreRule], SDoc, SDoc)
53
54 deSugar mod_name us (TcResults {tc_env = global_val_env,
55                                 tc_binds = all_binds,
56                                 tc_rules = rules,
57                                 tc_fords = fo_decls})
58   = do
59         beginPass "Desugar"
60         -- Do desugaring
61         let (result, ds_warns) = initDs us global_val_env module_and_group 
62                                         (dsProgram mod_name all_binds rules fo_decls)    
63             (ds_binds, ds_rules, _, _) = result
64
65          -- Display any warnings
66         doIfSet (not (isEmptyBag ds_warns))
67                 (printErrs (pprBagOfWarnings ds_warns))
68
69          -- Lint result if necessary
70         endPass "Desugar" opt_D_dump_ds ds_binds
71
72         doIfSet opt_D_dump_ds (printDump (ppr_ds_rules ds_rules))
73
74         return result
75   where
76     module_and_group = (mod_name, grp_name)
77     grp_name  = case opt_SccGroup of
78                   Just xx -> _PK_ xx
79                   Nothing -> _PK_ (moduleString mod_name) -- default: module name
80
81 dsProgram mod_name all_binds rules fo_decls
82   = dsMonoBinds auto_scc all_binds []   `thenDs` \ core_prs ->
83     dsForeigns mod_name fo_decls        `thenDs` \ (fi_binds, fe_binds, h_code, c_code) ->
84     mapDs dsRule rules                  `thenDs` \ rules' ->
85     let 
86         ds_binds = fi_binds ++ [Rec core_prs] ++ fe_binds
87     in
88     returnDs (ds_binds, rules', h_code, c_code)
89   where
90     auto_scc | opt_SccProfilingOn = TopLevel
91              | otherwise          = NoSccs
92
93 ppr_ds_rules [] = empty
94 ppr_ds_rules rules
95   = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
96     vcat (map pprProtoCoreRule rules)
97 \end{code}
98
99
100 %************************************************************************
101 %*                                                                      *
102 %*              Desugaring transformation rules
103 %*                                                                      *
104 %************************************************************************
105
106 \begin{code}
107 dsRule :: TypecheckedRuleDecl -> DsM ProtoCoreRule
108 dsRule (IfaceRuleDecl fn (CoreRuleBody name all_vars args rhs) loc)
109   = returnDs (ProtoCoreRule False {- non-local -} fn 
110                             (Rule name all_vars args rhs))
111     
112 dsRule (RuleDecl name sig_tvs vars lhs rhs loc)
113   = putSrcLocDs loc             $
114     ds_lhs all_vars lhs         `thenDs` \ (fn, args) ->
115     dsExpr rhs                  `thenDs` \ core_rhs ->
116     returnDs (ProtoCoreRule True {- local -} fn
117                             (Rule name all_vars args core_rhs))
118   where
119     all_vars = sig_tvs ++ [var | RuleBndr var <- vars]
120
121 ds_lhs all_vars lhs
122   = let
123         (dict_binds, body) = case lhs of
124                 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
125                 other                                  -> (EmptyMonoBinds, lhs)
126     in
127     ds_dict_binds dict_binds    `thenDs` \ dict_binds' ->
128     dsExpr body                 `thenDs` \ body' ->
129
130         -- Substitute the dict bindings eagerly,
131         -- and take the body apart into a (f args) form
132     let
133         subst_env = mkSubstEnv [id                   | (id,rhs) <- dict_binds']
134                                [ContEx subst_env rhs | (id,rhs) <- dict_binds']
135                         -- Note recursion here... substitution won't terminate
136                         -- if there is genuine recursion... which there isn't
137
138         subst = mkSubst (mkVarSet all_vars) subst_env
139         body'' = substExpr subst body'
140     in
141         
142         -- Now unpack the resulting body
143     let
144         pair = case collectArgs body'' of
145                         (Var fn, args) -> (fn, args)
146                         other          -> pprPanic "dsRule" (ppr lhs)
147     in
148     returnDs pair
149
150 ds_dict_binds EmptyMonoBinds       = returnDs []
151 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1   `thenDs` \ env1 ->
152                                      ds_dict_binds b2   `thenDs` \ env2 ->
153                                      returnDs (env1 ++ env2)
154 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs         `thenDs` \ rhs' ->
155                                      returnDs [(id,rhs')]
156 \end{code}