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` \ (fe_binders, foreign_binds, h_code, c_code) ->
77 ds_binds = [Rec (foreign_binds ++ core_prs)]
78 -- Notice that we put the whole lot in a big Rec, even the foreign binds
79 -- When compiling PrelFloat, which defines data Float = F# Float#
80 -- we want F# to be in scope in the foreign marshalling code!
81 -- You might think it doesn't matter, but the simplifier brings all top-level
82 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
84 local_binders = mkVarSet (bindersOfBinds ds_binds)
86 mapDs (dsRule local_binders) rules `thenDs` \ rules' ->
87 returnDs (ds_binds, rules', h_code, c_code, fe_binders)
89 auto_scc | opt_SccProfilingOn = TopLevel
92 ppr_ds_rules [] = empty
94 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
95 vcat (map pprProtoCoreRule rules)
99 %************************************************************************
101 %* Desugaring transformation rules
103 %************************************************************************
106 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM ProtoCoreRule
107 dsRule in_scope (IfaceRuleOut fn rule)
108 = returnDs (ProtoCoreRule False {- non-local -} fn rule)
110 dsRule in_scope (HsRule name sig_tvs vars lhs rhs loc)
112 ds_lhs all_vars lhs `thenDs` \ (fn, args) ->
113 dsExpr rhs `thenDs` \ core_rhs ->
114 returnDs (ProtoCoreRule True {- local -} fn
115 (Rule name tpl_vars args core_rhs))
117 tpl_vars = sig_tvs ++ [var | RuleBndr var <- vars]
118 all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
122 (dict_binds, body) = case lhs of
123 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
124 other -> (EmptyMonoBinds, lhs)
126 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
127 dsExpr body `thenDs` \ body' ->
129 -- Substitute the dict bindings eagerly,
130 -- and take the body apart into a (f args) form
132 subst_env = mkSubstEnv [id | (id,rhs) <- dict_binds']
133 [ContEx subst_env rhs | (id,rhs) <- dict_binds']
134 -- Note recursion here... substitution won't terminate
135 -- if there is genuine recursion... which there isn't
137 subst = mkSubst all_vars subst_env
138 body'' = substExpr subst body'
141 -- Now unpack the resulting body
143 pair = case collectArgs body'' of
144 (Var fn, args) -> (fn, args)
145 other -> pprPanic "dsRule" (ppr lhs)
149 ds_dict_binds EmptyMonoBinds = returnDs []
150 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1 `thenDs` \ env1 ->
151 ds_dict_binds b2 `thenDs` \ env2 ->
152 returnDs (env1 ++ env2)
153 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs `thenDs` \ rhs' ->