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 TcHsSyn ( TypecheckedMonoBinds, TypecheckedForeignDecl, TypecheckedRuleDecl )
14 import TcModule ( TcResults(..) )
16 import Rules ( ProtoCoreRule(..), pprProtoCoreRule )
17 import Subst ( substExpr, mkSubst )
19 import DsExpr ( dsExpr )
20 import DsBinds ( dsMonoBinds, AutoScc(..) )
21 import DsForeign ( dsForeigns )
23 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
24 -- depends on DsExpr.hi-boot.
25 import Module ( Module, moduleString )
27 import Name ( isLocallyDefined )
30 import Bag ( isEmptyBag, unionBags )
31 import CmdLineOpts ( opt_SccProfilingOn )
32 import CoreLint ( beginPass, endPass )
33 import ErrUtils ( doIfSet, pprBagOfWarnings )
35 import UniqSupply ( splitUniqSupply, UniqSupply )
38 %************************************************************************
40 %* The main function: deSugar
42 %************************************************************************
44 The only trick here is to get the @DsMonad@ stuff off to a good
51 -> IO ([CoreBind], [ProtoCoreRule], SDoc, SDoc, [CoreBndr])
53 deSugar mod_name us (TcResults {tc_env = global_val_env,
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
65 -- Display any warnings
66 doIfSet (not (isEmptyBag ds_warns))
67 (printErrs (pprBagOfWarnings ds_warns))
69 -- Lint result if necessary
70 endPass "Desugar" opt_D_dump_ds ds_binds
72 doIfSet opt_D_dump_ds (printDump (ppr_ds_rules ds_rules))
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) ->
80 ds_binds = fi_binds ++ [Rec core_prs] ++ fe_binds
81 fe_binders = bindersOfBinds fe_binds
82 local_binders = mkVarSet (bindersOfBinds ds_binds)
84 mapDs (dsRule local_binders) rules `thenDs` \ rules' ->
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 :: IdSet -> TypecheckedRuleDecl -> DsM ProtoCoreRule
105 dsRule in_scope (IfaceRuleOut fn rule)
106 = returnDs (ProtoCoreRule False {- non-local -} fn rule)
108 dsRule in_scope (HsRule name sig_tvs vars lhs rhs 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))
115 tpl_vars = sig_tvs ++ [var | RuleBndr var <- vars]
116 all_vars = in_scope `unionVarSet` mkVarSet tpl_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 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' ->