[project @ 2003-10-09 11:58:39 by simonpj]
authorsimonpj <unknown>
Thu, 9 Oct 2003 11:59:43 +0000 (11:59 +0000)
committersimonpj <unknown>
Thu, 9 Oct 2003 11:59:43 +0000 (11:59 +0000)
commit98688c6e8fd33f31c51218cf93cbf03fe3a5e73d
tree417682357fcd0733cd447f69e9b9032474348291
parent79c93a8a30aaaa6bd940c0677d6f3c57eb727fa2
[project @ 2003-10-09 11:58:39 by simonpj]
-------------------------
GHC heart/lung transplant
-------------------------

This major commit changes the way that GHC deals with importing
types and functions defined in other modules, during renaming and
typechecking.  On the way I've changed or cleaned up numerous other
things, including many that I probably fail to mention here.

Major benefit: GHC should suck in many fewer interface files when
compiling (esp with -O).  (You can see this with -ddump-rn-stats.)

It's also some 1500 lines of code shorter than before.

** So expect bugs!  I can do a 3-stage bootstrap, and run
** the test suite, but you may be doing stuff I havn't tested.
**  Don't update if you are relying on a working HEAD.

In particular, (a) External Core and (b) GHCi are very little tested.

But please, please DO test this version!

------------------------
Big things
------------------------

Interface files, version control, and importing declarations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* There is a totally new data type for stuff that lives in interface files:
Original names IfaceType.IfaceExtName
Types IfaceType.IfaceType
Declarations (type,class,id) IfaceSyn.IfaceDecl
Unfoldings IfaceSyn.IfaceExpr
  (Previously we used HsSyn for type/class decls, and UfExpr for unfoldings.)
  The new data types are in iface/IfaceType and iface/IfaceSyn.  They are
  all instances of Binary, so they can be written into interface files.
  Previous engronkulation concering the binary instance of RdrName has
  gone away -- RdrName is not an instance of Binary any more.  Nor does
  Binary.lhs need to know about the ``current module'' which it used to,
  which made it specialised to GHC.

  A good feature of this is that the type checker for source code doesn't
  need to worry about the possibility that we might be typechecking interface
  file stuff.  Nor does it need to do renaming; we can typecheck direct from
  IfaceSyn, saving a whole pass (module TcIface)

* Stuff from interface files is sucked in *lazily*, rather than being eagerly
  sucked in by the renamer. Instead, we use unsafeInterleaveIO to capture
  a thunk for the unfolding of an imported function (say).  If that unfolding
  is every pulled on, TcIface will scramble over the unfolding, which may
  in turn pull in the interface files of things mentioned in the unfolding.

  The External Package State is held in a mutable variable so that it
  can be side-effected by this lazy-sucking-in process (which may happen
  way later, e.g. when the simplifier runs).   In effect, the EPS is a kind
  of lazy memo table, filled in as we suck things in.  Or you could think
  of it as a global symbol table, populated on demand.

* This lazy sucking is very cool, but it can lead to truly awful bugs. The
  intent is that updates to the symbol table happen atomically, but very bad
  things happen if you read the variable for the table, and then force a
  thunk which updates the table.  Updates can get lost that way. I regret
  this subtlety.

  One example of the way it showed up is that the top level of TidyPgm
  (which updates the global name cache) to be much more disciplined about
  those updates, since TidyPgm may itself force thunks which allocate new
  names.

* Version numbering in interface files has changed completely, fixing
  one major bug with ghc --make.  Previously, the version of A.f changed
  only if A.f's type and unfolding was textually different.  That missed
  changes to things that A.f's unfolding mentions; which was fixed by
  eagerly sucking in all of those things, and listing them in the module's
  usage list.  But that didn't work with --make, because they might have
  been already sucked in.

  Now, A.f's version changes if anything reachable from A.f (via interface
  files) changes.  A module with unchanged source code needs recompiling
  only if the versions of any of its free variables changes. [This isn't
  quite right for dictionary functions and rules, which aren't mentioned
  explicitly in the source.  There are extensive comments in module MkIface,
  where all version-handling stuff is done.]

* We don't need equality on HsDecls any more (because they aren't used in
  interface files).  Instead we have a specialised equality for IfaceSyn
  (eqIfDecl etc), which uses IfaceEq instead of Bool as its result type.
  See notes in IfaceSyn.

* The horrid bit of the renamer that tried to predict what instance decls
  would be needed has gone entirely.  Instead, the type checker simply
  sucks in whatever instance decls it needs, when it needs them.  Easy!

  Similarly, no need for 'implicitModuleFVs' and 'implicitTemplateHaskellFVs'
  etc.  Hooray!

Types and type checking
~~~~~~~~~~~~~~~~~~~~~~~
* Kind-checking of types is far far tidier (new module TcHsTypes replaces
  the badly-named TcMonoType).  Strangely, this was one of my
  original goals, because the kind check for types is the Right Place to
  do type splicing, but it just didn't fit there before.

* There's a new representation for newtypes in TypeRep.lhs.  Previously
  they were represented using "SourceTypes" which was a funny compromise.
  Now they have their own constructor in the Type datatype.  SourceType
  has turned back into PredType, which is what it used to be.

* Instance decl overlap checking done lazily.  Consider
instance C Int b
instance C a Int
  These were rejected before as overlapping, because when seeking
  (C Int Int) one couldn't tell which to use.  But there's no problem when
  seeking (C Bool Int); it can only be the second.

  So instead of checking for overlap when adding a new instance declaration,
  we check for overlap when looking up an Inst.  If we find more than one
  matching instance, we see if any of the candidates dominates the others
  (in the sense of being a substitution instance of all the others);
  and only if not do we report an error.

------------------------
     Medium things
------------------------

* The TcRn monad is generalised a bit further.  It's now based on utils/IOEnv.lhs,
  the IO monad with an environment.  The desugarer uses the monad too,
  so that anything it needs can get faulted in nicely.

* Reduce the number of wired-in things; in particular Word and Integer
  are no longer wired in.  The latter required HsLit.HsInteger to get a
  Type argument.  The 'derivable type classes' data types (:+:, :*: etc)
  are not wired in any more either (see stuff about derivable type classes
  below).

* The PersistentComilerState is now held in a mutable variable
  in the HscEnv.  Previously (a) it was passed to and then returned by
  many top-level functions, which was painful; (b) it was invariably
  accompanied by the HscEnv.  This change tidies up top-level plumbing
  without changing anything important.

* Derivable type classes are treated much more like 'deriving' clauses.
  Previously, the Ids for the to/from functions lived inside the TyCon,
  but now the TyCon simply records their existence (with a simple boolean).
  Anyone who wants to use them must look them up in the environment.

  This in turn makes it easy to generate the to/from functions (done
  in types/Generics) using HsSyn (like TcGenDeriv for ordinary derivings)
  instead of CoreSyn, which in turn means that (a) we don't have to figure
  out all the type arguments etc; and (b) it'll be type-checked for us.
  Generally, the task of generating the code has become easier, which is
  good for Manuel, who wants to make it more sophisticated.

* A Name now says what its "parent" is. For example, the parent of a data
  constructor is its type constructor; the parent of a class op is its
  class.  This relationship corresponds exactly to the Avail data type;
  there may be other places we can exploit it.  (I made the change so that
  version comparison in interface files would be a bit easier; but in
  fact it tided up other things here and there (see calls to
  Name.nameParent).  For example, the declaration pool, of declararations
  read from interface files, but not yet used, is now keyed only by the 'main'
  name of the declaration, not the subordinate names.

* New types OccEnv and OccSet, with the usual operations.
  OccNames can be efficiently compared, because they have uniques, thanks
  to the hashing implementation of FastStrings.

* The GlobalRdrEnv is now keyed by OccName rather than RdrName.  Not only
  does this halve the size of the env (because we don't need both qualified
  and unqualified versions in the env), but it's also more efficient because
  we can use a UniqFM instead of a FiniteMap.

  Consequential changes to Provenance, which has moved to RdrName.

* External Core remains a bit of a hack, as it was before, done with a mixture
  of HsDecls (so that recursiveness and argument variance is still inferred),
  and IfaceExprs (for value declarations).  It's not thoroughly tested.

------------------------
     Minor things
------------------------

* DataCon fields dcWorkId, dcWrapId combined into a single field
  dcIds, that is explicit about whether the data con is a newtype or not.
  MkId.mkDataConWorkId and mkDataConWrapId are similarly combined into
  MkId.mkDataConIds

* Choosing the boxing strategy is done for *source* type decls only, and
  hence is now in TcTyDecls, not DataCon.

* WiredIn names are distinguished by their n_sort field, not by their location,
  which was rather strange

* Define Maybes.mapCatMaybes :: (a -> Maybe b) -> [a] -> [b]
  and use it here and there

* Much better pretty-printing of interface files (--show-iface)

Many, many other small things.

------------------------
     File changes
------------------------
* New iface/ subdirectory
* Much of RnEnv has moved to iface/IfaceEnv
* MkIface and BinIface have moved from main/ to iface/
* types/Variance has been absorbed into typecheck/TcTyDecls
* RnHiFiles and RnIfaces have vanished entirely.  Their
  work is done by iface/LoadIface
* hsSyn/HsCore has gone, replaced by iface/IfaceSyn
* typecheck/TcIfaceSig has gone, replaced by iface/TcIface
* typecheck/TcMonoType has been renamed to typecheck/TcHsType
* basicTypes/Var.hi-boot and basicTypes/Generics.hi-boot have gone altogether
149 files changed:
ghc/compiler/Makefile
ghc/compiler/absCSyn/AbsCUtils.lhs
ghc/compiler/absCSyn/PprAbsC.lhs
ghc/compiler/basicTypes/BasicTypes.lhs
ghc/compiler/basicTypes/DataCon.lhs
ghc/compiler/basicTypes/Id.lhs
ghc/compiler/basicTypes/IdInfo.lhs
ghc/compiler/basicTypes/Literal.lhs
ghc/compiler/basicTypes/MkId.hi-boot
ghc/compiler/basicTypes/MkId.hi-boot-5
ghc/compiler/basicTypes/MkId.hi-boot-6
ghc/compiler/basicTypes/MkId.lhs
ghc/compiler/basicTypes/Module.hi-boot-5
ghc/compiler/basicTypes/Module.hi-boot-6
ghc/compiler/basicTypes/Module.lhs
ghc/compiler/basicTypes/Name.lhs
ghc/compiler/basicTypes/NameSet.lhs
ghc/compiler/basicTypes/OccName.lhs
ghc/compiler/basicTypes/RdrName.lhs
ghc/compiler/basicTypes/SrcLoc.lhs
ghc/compiler/codeGen/CgCon.lhs
ghc/compiler/codeGen/CgRetConv.lhs
ghc/compiler/codeGen/ClosureInfo.lhs
ghc/compiler/codeGen/CodeGen.lhs
ghc/compiler/compMan/CompManager.lhs
ghc/compiler/coreSyn/CorePrep.lhs
ghc/compiler/coreSyn/CoreSyn.hi-boot-6
ghc/compiler/coreSyn/CoreUnfold.lhs
ghc/compiler/coreSyn/CoreUtils.lhs
ghc/compiler/coreSyn/ExternalCore.lhs
ghc/compiler/coreSyn/MkExternalCore.lhs
ghc/compiler/coreSyn/PprCore.lhs
ghc/compiler/coreSyn/PprExternalCore.lhs
ghc/compiler/coreSyn/Subst.lhs
ghc/compiler/deSugar/Desugar.lhs
ghc/compiler/deSugar/DsArrows.lhs
ghc/compiler/deSugar/DsBinds.lhs
ghc/compiler/deSugar/DsCCall.lhs
ghc/compiler/deSugar/DsExpr.lhs
ghc/compiler/deSugar/DsForeign.lhs
ghc/compiler/deSugar/DsGRHSs.lhs
ghc/compiler/deSugar/DsListComp.lhs
ghc/compiler/deSugar/DsMeta.hs
ghc/compiler/deSugar/DsMonad.lhs
ghc/compiler/deSugar/DsUtils.lhs
ghc/compiler/deSugar/Match.lhs
ghc/compiler/deSugar/MatchCon.lhs
ghc/compiler/deSugar/MatchLit.lhs
ghc/compiler/ghci/ByteCodeAsm.lhs
ghc/compiler/ghci/InteractiveUI.hs
ghc/compiler/ghci/Linker.lhs
ghc/compiler/hsSyn/Convert.lhs
ghc/compiler/hsSyn/HsBinds.lhs
ghc/compiler/hsSyn/HsDecls.lhs
ghc/compiler/hsSyn/HsExpr.lhs
ghc/compiler/hsSyn/HsLit.lhs
ghc/compiler/hsSyn/HsSyn.lhs
ghc/compiler/hsSyn/HsTypes.lhs
ghc/compiler/ilxGen/IlxGen.lhs
ghc/compiler/main/BinIface.hs [deleted file]
ghc/compiler/main/CmdLineOpts.lhs
ghc/compiler/main/DriverFlags.hs
ghc/compiler/main/DriverPipeline.hs
ghc/compiler/main/HscMain.lhs
ghc/compiler/main/HscStats.lhs
ghc/compiler/main/HscTypes.lhs
ghc/compiler/main/Main.hs
ghc/compiler/main/MkIface.lhs [deleted file]
ghc/compiler/main/ParsePkgConf.y
ghc/compiler/main/TidyPgm.lhs
ghc/compiler/nativeGen/MachMisc.lhs
ghc/compiler/nativeGen/StixMacro.lhs
ghc/compiler/nativeGen/StixPrim.lhs
ghc/compiler/ndpFlatten/FlattenMonad.hs
ghc/compiler/ndpFlatten/Flattening.hs
ghc/compiler/ndpFlatten/NDPCoreUtils.hs
ghc/compiler/ndpFlatten/PArrAnal.hs
ghc/compiler/parser/Lexer.x
ghc/compiler/parser/Parser.y
ghc/compiler/parser/ParserCore.y
ghc/compiler/parser/RdrHsSyn.lhs
ghc/compiler/prelude/PrelInfo.lhs
ghc/compiler/prelude/PrelNames.lhs
ghc/compiler/prelude/PrimOp.lhs
ghc/compiler/prelude/TysPrim.lhs
ghc/compiler/prelude/TysWiredIn.lhs
ghc/compiler/rename/RnBinds.lhs
ghc/compiler/rename/RnEnv.lhs
ghc/compiler/rename/RnExpr.lhs
ghc/compiler/rename/RnHiFiles.lhs [deleted file]
ghc/compiler/rename/RnHsSyn.lhs
ghc/compiler/rename/RnIfaces.lhs [deleted file]
ghc/compiler/rename/RnNames.lhs
ghc/compiler/rename/RnSource.lhs
ghc/compiler/rename/RnTypes.lhs
ghc/compiler/simplCore/SimplCore.lhs
ghc/compiler/specialise/Rules.lhs
ghc/compiler/stgSyn/CoreToStg.lhs
ghc/compiler/stgSyn/StgLint.lhs
ghc/compiler/stgSyn/StgSyn.lhs
ghc/compiler/stranal/DmdAnal.lhs
ghc/compiler/stranal/WorkWrap.lhs
ghc/compiler/stranal/WwLib.lhs
ghc/compiler/typecheck/Inst.lhs
ghc/compiler/typecheck/TcArrows.lhs
ghc/compiler/typecheck/TcBinds.lhs
ghc/compiler/typecheck/TcClassDcl.lhs
ghc/compiler/typecheck/TcDefaults.lhs
ghc/compiler/typecheck/TcDeriv.lhs
ghc/compiler/typecheck/TcEnv.lhs
ghc/compiler/typecheck/TcExpr.lhs
ghc/compiler/typecheck/TcForeign.lhs
ghc/compiler/typecheck/TcGenDeriv.lhs
ghc/compiler/typecheck/TcHsSyn.lhs
ghc/compiler/typecheck/TcIfaceSig.lhs [deleted file]
ghc/compiler/typecheck/TcInstDcls.lhs
ghc/compiler/typecheck/TcMType.lhs
ghc/compiler/typecheck/TcMatches.lhs
ghc/compiler/typecheck/TcMonoType.lhs [deleted file]
ghc/compiler/typecheck/TcPat.lhs
ghc/compiler/typecheck/TcRnDriver.lhs
ghc/compiler/typecheck/TcRnMonad.lhs
ghc/compiler/typecheck/TcRnTypes.lhs
ghc/compiler/typecheck/TcRules.lhs
ghc/compiler/typecheck/TcSimplify.lhs
ghc/compiler/typecheck/TcSplice.lhs
ghc/compiler/typecheck/TcTyClsDecls.lhs
ghc/compiler/typecheck/TcTyDecls.lhs
ghc/compiler/typecheck/TcType.lhs
ghc/compiler/typecheck/TcUnify.lhs
ghc/compiler/types/Class.lhs
ghc/compiler/types/FunDeps.lhs
ghc/compiler/types/Generics.hi-boot-5 [deleted file]
ghc/compiler/types/Generics.hi-boot-6 [deleted file]
ghc/compiler/types/Generics.lhs
ghc/compiler/types/InstEnv.lhs
ghc/compiler/types/PprType.lhs
ghc/compiler/types/TyCon.lhs
ghc/compiler/types/Type.lhs
ghc/compiler/types/TypeRep.hi-boot-6
ghc/compiler/types/TypeRep.lhs
ghc/compiler/types/Variance.lhs [deleted file]
ghc/compiler/utils/Binary.hs
ghc/compiler/utils/Digraph.lhs
ghc/compiler/utils/FastString.lhs
ghc/compiler/utils/Maybes.lhs
ghc/compiler/utils/Outputable.lhs
ghc/compiler/utils/Pretty.lhs
ghc/compiler/utils/Util.lhs