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 ( TypecheckedRuleDecl )
14 import TcModule ( TcResults(..) )
16 import Rules ( ProtoCoreRule(..), pprProtoCoreRule )
17 import Subst ( substExpr, mkSubst, mkInScopeSet )
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 )
27 import Bag ( isEmptyBag )
28 import CmdLineOpts ( opt_SccProfilingOn )
29 import CoreLint ( beginPass, endPass )
30 import ErrUtils ( doIfSet, pprBagOfWarnings )
32 import UniqSupply ( UniqSupply )
35 %************************************************************************
37 %* The main function: deSugar
39 %************************************************************************
41 The only trick here is to get the @DsMonad@ stuff off to a good
48 -> IO ([CoreBind], [ProtoCoreRule], SDoc, SDoc, [CoreBndr])
50 deSugar mod_name us (TcResults {tc_env = global_val_env,
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
62 -- Display any warnings
63 doIfSet (not (isEmptyBag ds_warns))
64 (printErrs (pprBagOfWarnings ds_warns))
66 -- Lint result if necessary
67 endPass "Desugar" opt_D_dump_ds ds_binds
69 doIfSet opt_D_dump_ds (printDump (ppr_ds_rules ds_rules))
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) ->
77 ds_binds = fi_binds ++ [Rec core_prs] ++ fe_binds
78 fe_binders = bindersOfBinds fe_binds
79 local_binders = mkVarSet (bindersOfBinds ds_binds)
81 mapDs (dsRule local_binders) rules `thenDs` \ rules' ->
82 returnDs (ds_binds, rules', h_code, c_code, fe_binders)
84 auto_scc | opt_SccProfilingOn = TopLevel
87 ppr_ds_rules [] = empty
89 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
90 vcat (map pprProtoCoreRule rules)
94 %************************************************************************
96 %* Desugaring transformation rules
98 %************************************************************************
101 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM ProtoCoreRule
102 dsRule in_scope (IfaceRuleOut fn rule)
103 = returnDs (ProtoCoreRule False {- non-local -} fn rule)
105 dsRule in_scope (HsRule name sig_tvs vars lhs rhs 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))
112 tpl_vars = sig_tvs ++ [var | RuleBndr var <- vars]
113 all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
117 (dict_binds, body) = case lhs of
118 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
119 other -> (EmptyMonoBinds, lhs)
121 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
122 dsExpr body `thenDs` \ body' ->
124 -- Substitute the dict bindings eagerly,
125 -- and take the body apart into a (f args) form
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
132 subst = mkSubst all_vars subst_env
133 body'' = substExpr subst body'
136 -- Now unpack the resulting body
138 pair = case collectArgs body'' of
139 (Var fn, args) -> (fn, args)
140 other -> pprPanic "dsRule" (ppr lhs)
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' ->