[project @ 2000-03-08 17:48:24 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_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, [CoreBndr])
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) = 
62                 initDs us global_val_env mod_name
63                         (dsProgram mod_name all_binds rules fo_decls)    
64             (ds_binds, ds_rules, _, _, _) = result
65
66          -- Display any warnings
67         doIfSet (not (isEmptyBag ds_warns))
68                 (printErrs (pprBagOfWarnings ds_warns))
69
70          -- Lint result if necessary
71         endPass "Desugar" opt_D_dump_ds ds_binds
72
73         doIfSet opt_D_dump_ds (printDump (ppr_ds_rules ds_rules))
74
75         return result
76
77 dsProgram mod_name all_binds rules fo_decls
78   = dsMonoBinds auto_scc all_binds []   `thenDs` \ core_prs ->
79     dsForeigns mod_name fo_decls        `thenDs` \ (fi_binds, fe_binds, h_code, c_code) ->
80     mapDs dsRule rules                  `thenDs` \ rules' ->
81     let 
82         ds_binds = fi_binds ++ [Rec core_prs] ++ fe_binds
83         fe_binders = bindersOfBinds fe_binds
84     in
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 :: TypecheckedRuleDecl -> DsM ProtoCoreRule
105 dsRule (IfaceRuleDecl fn (CoreRuleBody name all_vars args rhs) loc)
106   = returnDs (ProtoCoreRule False {- non-local -} fn 
107                             (Rule name all_vars args rhs))
108     
109 dsRule (RuleDecl name sig_tvs vars lhs rhs loc)
110   = putSrcLocDs loc             $
111     ds_lhs all_vars lhs         `thenDs` \ (fn, args) ->
112     dsExpr rhs                  `thenDs` \ core_rhs ->
113     returnDs (ProtoCoreRule True {- local -} fn
114                             (Rule name all_vars args core_rhs))
115   where
116     all_vars = sig_tvs ++ [var | RuleBndr var <- 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 (mkVarSet 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}