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