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