2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Desugar]{@deSugar@: the main function}
7 module Desugar ( deSugar ) where
9 #include "HsVersions.h"
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(..) )
17 import Rules ( ProtoCoreRule(..), pprProtoCoreRule )
18 import Subst ( substExpr, mkSubst )
20 import DsExpr ( dsExpr )
21 import DsBinds ( dsMonoBinds, AutoScc(..) )
22 import DsForeign ( dsForeigns )
24 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
25 -- depends on DsExpr.hi-boot.
26 import Module ( Module, moduleString )
28 import Name ( isLocallyDefined )
31 import Bag ( isEmptyBag, unionBags )
32 import CmdLineOpts ( opt_SccProfilingOn )
33 import CoreLint ( beginPass, endPass )
34 import ErrUtils ( doIfSet, pprBagOfWarnings )
36 import UniqSupply ( splitUniqSupply, UniqSupply )
39 %************************************************************************
41 %* The main function: deSugar
43 %************************************************************************
45 The only trick here is to get the @DsMonad@ stuff off to a good
52 -> IO ([CoreBind], [ProtoCoreRule], SDoc, SDoc, [CoreBndr])
54 deSugar mod_name us (TcResults {tc_env = global_val_env,
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
66 -- Display any warnings
67 doIfSet (not (isEmptyBag ds_warns))
68 (printErrs (pprBagOfWarnings ds_warns))
70 -- Lint result if necessary
71 endPass "Desugar" opt_D_dump_ds ds_binds
73 doIfSet opt_D_dump_ds (printDump (ppr_ds_rules ds_rules))
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' ->
82 ds_binds = fi_binds ++ [Rec core_prs] ++ fe_binds
83 fe_binders = bindersOfBinds fe_binds
85 returnDs (ds_binds, rules', h_code, c_code, fe_binders)
87 auto_scc | opt_SccProfilingOn = TopLevel
90 ppr_ds_rules [] = empty
92 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
93 vcat (map pprProtoCoreRule rules)
97 %************************************************************************
99 %* Desugaring transformation rules
101 %************************************************************************
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))
109 dsRule (RuleDecl name sig_tvs vars lhs rhs 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))
116 all_vars = sig_tvs ++ [var | RuleBndr var <- vars]
120 (dict_binds, body) = case lhs of
121 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
122 other -> (EmptyMonoBinds, lhs)
124 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
125 dsExpr body `thenDs` \ body' ->
127 -- Substitute the dict bindings eagerly,
128 -- and take the body apart into a (f args) form
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
135 subst = mkSubst (mkVarSet all_vars) subst_env
136 body'' = substExpr subst body'
139 -- Now unpack the resulting body
141 pair = case collectArgs body'' of
142 (Var fn, args) -> (fn, args)
143 other -> pprPanic "dsRule" (ppr lhs)
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' ->