2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Desugar]{@deSugar@: the main function}
7 module Desugar ( deSugar, deSugarExpr,
10 #include "HsVersions.h"
12 import CmdLineOpts ( DynFlags, DynFlag(..), dopt, opt_SccProfilingOn )
13 import HscTypes ( ModDetails(..), TypeEnv )
14 import HsSyn ( MonoBinds, RuleDecl(..), RuleBndr(..),
15 HsExpr(..), HsBinds(..), MonoBinds(..) )
16 import TcHsSyn ( TypecheckedRuleDecl, TypecheckedHsExpr,
18 import TcModule ( TcResults(..) )
21 import PprCore ( pprIdRules, pprCoreExpr )
22 import Subst ( substExpr, mkSubst, mkInScopeSet )
24 import DsExpr ( dsExpr )
25 import DsBinds ( dsMonoBinds, AutoScc(..) )
26 import DsForeign ( dsForeigns )
27 import DsExpr () -- Forces DsExpr to be compiled; DsBinds only
28 -- depends on DsExpr.hi-boot.
29 import Module ( Module )
31 import NameEnv ( lookupNameEnv )
34 import Bag ( isEmptyBag )
35 import CoreLint ( showPass, endPass )
36 import ErrUtils ( doIfSet, dumpIfSet_dyn, pprBagOfWarnings )
38 import UniqSupply ( mkSplitUniqSupply )
39 import HscTypes ( HomeSymbolTable, PersistentCompilerState(..), TyThing(..), lookupType, )
42 %************************************************************************
44 %* The main function: deSugar
46 %************************************************************************
48 The only trick here is to get the @DsMonad@ stuff off to a good
53 -> PersistentCompilerState -> HomeSymbolTable
54 -> Module -> PrintUnqualified
56 -> IO (ModDetails, (SDoc, SDoc, [FAST_STRING], [CoreBndr]))
58 deSugar dflags pcs hst mod_name unqual
59 (TcResults {tc_env = type_env,
64 = do { showPass dflags "Desugar"
65 ; us <- mkSplitUniqSupply 'd'
68 ; let (ds_result, ds_warns) = initDs dflags us lookup mod_name
69 (dsProgram mod_name all_binds rules fo_decls)
71 (ds_binds, ds_rules, foreign_stuff) = ds_result
73 mod_details = ModDetails { md_types = type_env,
78 -- Display any warnings
79 ; doIfSet (not (isEmptyBag ds_warns))
80 (printErrs unqual (pprBagOfWarnings ds_warns))
82 -- Lint result if necessary
83 ; endPass dflags "Desugar" Opt_D_dump_ds ds_binds
86 ; doIfSet (dopt Opt_D_dump_ds dflags)
87 (printDump (ppr_ds_rules ds_rules))
89 ; return (mod_details, foreign_stuff)
93 -- The lookup function passed to initDs is used for well-known Ids,
94 -- such as fold, build, cons etc, so the chances are
95 -- it'll be found in the package symbol table. That's
96 -- why we don't merge all these tables
98 lookup n = case lookupType hst pte n of {
101 case lookupNameEnv type_env n of
103 other -> pprPanic "Desugar: lookup:" (ppr n)
106 deSugarExpr :: DynFlags
107 -> PersistentCompilerState -> HomeSymbolTable
108 -> Module -> PrintUnqualified
111 deSugarExpr dflags pcs hst mod_name unqual tc_expr
112 = do { showPass dflags "Desugar"
113 ; us <- mkSplitUniqSupply 'd'
116 ; let (core_expr, ds_warns) = initDs dflags us lookup mod_name (dsExpr tc_expr)
118 -- Display any warnings
119 ; doIfSet (not (isEmptyBag ds_warns))
120 (printErrs unqual (pprBagOfWarnings ds_warns))
123 ; dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr core_expr)
129 lookup n = case lookupType hst pte n of
131 other -> pprPanic "Desugar: lookup:" (ppr n)
133 dsProgram mod_name all_binds rules fo_decls
134 = dsMonoBinds auto_scc all_binds [] `thenDs` \ core_prs ->
135 dsForeigns mod_name fo_decls `thenDs` \ (fe_binders, foreign_binds, h_code, c_code, headers) ->
137 ds_binds = [Rec (foreign_binds ++ core_prs)]
138 -- Notice that we put the whole lot in a big Rec, even the foreign binds
139 -- When compiling PrelFloat, which defines data Float = F# Float#
140 -- we want F# to be in scope in the foreign marshalling code!
141 -- You might think it doesn't matter, but the simplifier brings all top-level
142 -- things into the in-scope set before simplifying; so we get no unfolding for F#!
144 local_binders = mkVarSet (bindersOfBinds ds_binds)
146 mapDs (dsRule local_binders) rules `thenDs` \ rules' ->
147 returnDs (ds_binds, rules', (h_code, c_code, headers, fe_binders))
149 auto_scc | opt_SccProfilingOn = TopLevel
152 ppr_ds_rules [] = empty
154 = text "" $$ text "-------------- DESUGARED RULES -----------------" $$
158 Simplest thing in the world, desugaring External Core:
161 deSugarCore :: (TypeEnv, [TypecheckedCoreBind], [TypecheckedRuleDecl])
162 -> IO (ModDetails, (SDoc, SDoc, [FAST_STRING], [CoreBndr]))
163 deSugarCore (type_env, pairs, rules)
164 = return (mod_details, no_foreign_stuff)
166 mod_details = ModDetails { md_types = type_env
168 , md_rules = ds_rules
169 , md_binds = ds_binds }
170 ds_binds = [Rec pairs]
171 ds_rules = [(fun,rule) | IfaceRuleOut fun rule <- rules]
173 no_foreign_stuff = (empty,empty,[],[])
177 %************************************************************************
179 %* Desugaring transformation rules
181 %************************************************************************
184 dsRule :: IdSet -> TypecheckedRuleDecl -> DsM (Id, CoreRule)
185 dsRule in_scope (IfaceRuleOut fun rule) -- Built-in rules come this way
186 = returnDs (fun, rule)
188 dsRule in_scope (HsRule name act vars lhs rhs loc)
190 ds_lhs all_vars lhs `thenDs` \ (fn, args) ->
191 dsExpr rhs `thenDs` \ core_rhs ->
192 returnDs (fn, Rule name act tpl_vars args core_rhs)
194 tpl_vars = [var | RuleBndr var <- vars]
195 all_vars = mkInScopeSet (in_scope `unionVarSet` mkVarSet tpl_vars)
199 (dict_binds, body) = case lhs of
200 (HsLet (MonoBind dict_binds _ _) body) -> (dict_binds, body)
201 other -> (EmptyMonoBinds, lhs)
203 ds_dict_binds dict_binds `thenDs` \ dict_binds' ->
204 dsExpr body `thenDs` \ body' ->
206 -- Substitute the dict bindings eagerly,
207 -- and take the body apart into a (f args) form
209 subst_env = mkSubstEnv [id | (id,rhs) <- dict_binds']
210 [ContEx subst_env rhs | (id,rhs) <- dict_binds']
211 -- Note recursion here... substitution won't terminate
212 -- if there is genuine recursion... which there isn't
214 subst = mkSubst all_vars subst_env
215 body'' = substExpr subst body'
218 -- Now unpack the resulting body
220 pair = case collectArgs body'' of
221 (Var fn, args) -> (fn, args)
222 other -> pprPanic "dsRule" (ppr lhs)
226 ds_dict_binds EmptyMonoBinds = returnDs []
227 ds_dict_binds (AndMonoBinds b1 b2) = ds_dict_binds b1 `thenDs` \ env1 ->
228 ds_dict_binds b2 `thenDs` \ env2 ->
229 returnDs (env1 ++ env2)
230 ds_dict_binds (VarMonoBind id rhs) = dsExpr rhs `thenDs` \ rhs' ->