1. 21 Feb, 2003 1 commit
  2. 04 Feb, 2003 1 commit
    • simonpj's avatar
      [project @ 2003-02-04 12:28:22 by simonpj] · 115f0fae
      simonpj authored
      ---------------------------------------------------
      	Important fix to the handling of class methods that
      	      mention their own class type variable
      	---------------------------------------------------
      
      [NB: I'm not 100% certain that this commit is independent of the
           Template-Haskell-related commit I'm doing at the same time.
           I've tried to separate them but may not have succeeded totally.]
      
      This bug gives utterly bogus (detected by Core Lint) programs.
      Isaac Jones discovered it.  Here's an example, now enshrined as tc165.
      
          class C a where
      	f :: (Eq a) => a
      
          instance C () where
      	f = f
      
      The instance decl was translated as
      
          dfC() = MkC (let f = \dEq -> f in f)
      
      which is utterly wrong.  Reason: the 'f' on the left was being treated
      as an available Inst, but it doesn't obey INVARIANT 2 for Insts, which
      is that they are applied to all their dictionaries.  (See the data type
      decl for Inst.)
      
      Solution: don't include such class methods in the available Insts.
      115f0fae
  3. 13 Jan, 2003 2 commits
  4. 28 Nov, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-11-28 17:17:41 by simonpj] · 3c58c25b
      simonpj authored
      -------------------------------
            A day's work to improve error messages
      	-------------------------------
      
      1.  Indicate when the cause of the error is likely to be the monomorpism
          restriction, and identify the offending variables.  This involves
          mainly tcSimplifyTop and its error generation.
      
      2.  Produce much better kind error messages.  No more
            ../alonzo/DiGraph.hs:40:
      	  Couldn't match `* -> *' against `Type bx'
      	      Expected kind: * -> *
      	      Inferred kind: Type bx
      	  When checking that `DiGraph n' is a type
      
      It took a surprisingly long time to get the details right.
      3c58c25b
  5. 19 Nov, 2002 1 commit
  6. 18 Nov, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-11-18 14:25:50 by simonpj] · 4e84be0c
      simonpj authored
      ----------------------------------------
      	Class ops that do not introduce for-alls
      	----------------------------------------
      
      	MERGE TO STABLE (if poss)
      
      The handling of class ops that do not add an extra for-all
      was utterly bogus.  For example:
      
      	class C a where
      	    fc :: (?p :: String) => a;
      
      	class D a where
      	    fd :: (Ord a) => [a] -> [a]
      
      De-bogus-ing means
      
      a) Being careful when taking apart the class op type in
      	MkIface.tcClassOpSig
      
      b) Ditto when making the method Id in an instance binding.
         Hence new function Inst.tcInstClassOp, and its calls
         in TcInstDcls, and TcClassDcls
      4e84be0c
  7. 18 Oct, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-10-18 13:41:50 by simonpj] · f53483a2
      simonpj authored
      --------------------------------
         Fix a serious error in the "newtype deriving" feature
      	--------------------------------
      
      The "newtype deriving" feature lets you derive arbitrary classes for
      a newtype, not just the built-in ones (Read, Show, Ix etc).  It's very
      cool, but Hal Duame discovered that it did utterly the Wrong Thing
      for superclasses.  E.g.
      
      	newtype Foo = MkFoo Int deriving( Show, Num, Eq )
      
      You'd get a Num instance for Foo that was *identical* to the
      Num instance for Int, *including* the Show superclass. So the
      superclass in the Num dictionary would show a Foo just like an
      Int, which is wrong... it should show as "Foo n".
      
      This commit fixes the problem, by building a new dictionary every time,
      but using the methods from the dictionary for the representation type.
      
      I also fixed a bug that prevented it working altogether when the
      representation type was not the application of a type constructor.
      For example, this now works
      
      	newtype Foo a = MkFoo a deriving( Num, Eq, Show )
      
      
      I also made it a bit more efficient in the case where the type is
      not parameterised.  Then the "dfun" doesn't need to be a function.
      f53483a2
  8. 09 Oct, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-10-09 15:03:48 by simonpj] · 8c1b6bd7
      simonpj authored
      -----------------------------------
      	Lots more Template Haskell stuff
      	-----------------------------------
      
      At last!  Top-level declaration splices work!
      Syntax is
      
      	$(f x)
      
      not "splice (f x)" as in the paper.
      
      Lots jiggling around, particularly with the top-level plumbining.
      Note the new data type HsDecls.HsGroup.
      8c1b6bd7
  9. 13 Sep, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-09-13 15:02:25 by simonpj] · 9af77fa4
      simonpj authored
      --------------------------------------
      	Make Template Haskell into the HEAD
      	--------------------------------------
      
      This massive commit transfers to the HEAD all the stuff that
      Simon and Tim have been doing on Template Haskell.  The
      meta-haskell-branch is no more!
      
      WARNING: make sure that you
      
        * Update your links if you are using link trees.
          Some modules have been added, some have gone away.
      
        * Do 'make clean' in all library trees.
          The interface file format has changed, and you can
          get strange panics (sadly) if GHC tries to read old interface files:
          e.g.  ghc-5.05: panic! (the `impossible' happened, GHC version 5.05):
      	  Binary.get(TyClDecl): ForeignType
      
        * You need to recompile the rts too; Linker.c has changed
      
      
      However the libraries are almost unaltered; just a tiny change in
      Base, and to the exports in Prelude.
      
      
      NOTE: so far as TH itself is concerned, expression splices work
      fine, but declaration splices are not complete.
      
      
      		---------------
      		The main change
      		---------------
      
      The main structural change: renaming and typechecking have to be
      interleaved, because we can't rename stuff after a declaration splice
      until after we've typechecked the stuff before (and the splice
      itself).
      
      * Combine the renamer and typecheker monads into one
      	(TcRnMonad, TcRnTypes)
        These two replace TcMonad and RnMonad
      
      * Give them a single 'driver' (TcRnDriver).  This driver
        replaces TcModule.lhs and Rename.lhs
      
      * The haskell-src library package has a module
      	Language/Haskell/THSyntax
        which defines the Haskell data type seen by the TH programmer.
      
      * New modules:
      	hsSyn/Convert.hs 	converts THSyntax -> HsSyn
      	deSugar/DsMeta.hs 	converts HsSyn -> THSyntax
      
      * New module typecheck/TcSplice type-checks Template Haskell splices.
      
      		-------------
      		Linking stuff
      		-------------
      
      * ByteCodeLink has been split into
      	ByteCodeLink	(which links)
      	ByteCodeAsm	(which assembles)
      
      * New module ghci/ObjLink is the object-code linker.
      
      * compMan/CmLink is removed entirely (was out of place)
        Ditto CmTypes (which was tiny)
      
      * Linker.c initialises the linker when it is first used (no need to call
        initLinker any more).  Template Haskell makes it harder to know when
        and whether to initialise the linker.
      
      
      	-------------------------------------
      	Gathering the LIE in the type checker
      	-------------------------------------
      
      * Instead of explicitly gathering constraints in the LIE
      	tcExpr :: RenamedExpr -> TcM (TypecheckedExpr, LIE)
        we now dump the constraints into a mutable varabiable carried
        by the monad, so we get
      	tcExpr :: RenamedExpr -> TcM TypecheckedExpr
      
        Much less clutter in the code, and more efficient too.
        (Originally suggested by Mark Shields.)
      
      
      		-----------------
      		Remove "SysNames"
      		-----------------
      
      Because the renamer and the type checker were entirely separate,
      we had to carry some rather tiresome implicit binders (or "SysNames")
      along inside some of the HsDecl data structures.  They were both
      tiresome and fragile.
      
      Now that the typechecker and renamer are more intimately coupled,
      we can eliminate SysNames (well, mostly... default methods still
      carry something similar).
      
      		-------------
      		Clean up HsPat
      		-------------
      
      One big clean up is this: instead of having two HsPat types (InPat and
      OutPat), they are now combined into one.  This is more consistent with
      the way that HsExpr etc is handled; there are some 'Out' constructors
      for the type checker output.
      
      So:
      	HsPat.InPat	--> HsPat.Pat
      	HsPat.OutPat	--> HsPat.Pat
      	No 'pat' type parameter in HsExpr, HsBinds, etc
      
      	Constructor patterns are nicer now: they use
      		HsPat.HsConDetails
      	for the three cases of constructor patterns:
      		prefix, infix, and record-bindings
      
      	The *same* data type HsConDetails is used in the type
      	declaration of the data type (HsDecls.TyData)
      
      Lots of associated clean-up operations here and there.  Less code.
      Everything is wonderful.
      9af77fa4
  10. 29 Jul, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-07-29 12:22:37 by simonpj] · 2ddea0a8
      simonpj authored
      *** MERGE TO STABLE BRANCH ***
      
      
      Surprisingly large delta to make rebindable names work properly.
      I was sloppily not checking the type of the user-supplied name,
      and Ashley Yakeley's first experiment showed up the problem!
      
      Solution: typechecker has to check both the 'standard' name and
      the 'user' name and check the latter has a type compatible with the
      former.
      
      The main comment is with Inst.tcSyntaxName (a new function).
      2ddea0a8
  11. 23 Jul, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-07-23 14:58:16 by simonpj] · e0a56725
      simonpj authored
      Use tcLookupId, not tcLookupGlobalId, in Inst.newMethodFromName
      
          This really only affects the behaviour with -fno-implicit-prelude
      
      	*** MERGE TO STABLE BRANCH ***
      e0a56725
  12. 11 Apr, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-04-11 12:03:29 by simonpj] · a7b95beb
      simonpj authored
      -------------------
      	Mainly derived Read
      	-------------------
      
      This commit is a tangle of several things that somehow got wound up
      together, I'm afraid.
      
      
      The main course
      ~~~~~~~~~~~~~~~
      Replace the derived-Read machinery with Koen's cunning new parser
      combinator library.   The result should be
      	* much smaller code sizes from derived Read
      	* faster execution of derived Read
      
      WARNING: I have not thoroughly tested this stuff; I'd be glad if you did!
      	 All the hard work is done, but there may be a few nits.
      
      The Read class gets two new methods, not exposed
      in the H98 inteface of course:
        class Read a where
          readsPrec    :: Int -> ReadS a
          readList     :: ReadS [a]
          readPrec     :: ReadPrec a		-- NEW
          readListPrec :: ReadPrec [a]	-- NEW
      
      There are the following new libraries:
      
        Text.ParserCombinators.ReadP		Koens combinator parser
        Text.ParserCombinators.ReadPrec	Ditto, but with precedences
      
        Text.Read.Lex				An emasculated lexical analyser
      					that provides the functionality
      					of H98 'lex'
      
      TcGenDeriv is changed to generate code that uses the new libraries.
      The built-in instances of Read (List, Maybe, tuples, etc) use the new
      libraries.
      
      
      Other stuff
      ~~~~~~~~~~~
      1. Some fixes the the plumbing of external-core generation. Sigbjorn
      did most of the work earlier, but this commit completes the renaming and
      typechecking plumbing.
      
      2. Runtime error-generation functions, such as GHC.Err.recSelErr,
      GHC.Err.recUpdErr, etc, now take an Addr#, pointing to a UTF8-encoded
      C string, instead of a Haskell string.  This makes the *calls* to these
      functions easier to generate, and smaller too, which is a good thing.
      
      In particular, it means that MkId.mkRecordSelectorId doesn't need to
      be passed "unpackCStringId", which was GRUESOME; and that in turn means
      that tcTypeAndClassDecls doesn't need to be passed unf_env, which is
      a very worthwhile cleanup.   Win/win situation.
      
      3.  GHC now faithfully translates do-notation using ">>" for statements
      with no binding, just as the report says.  While I was there I tidied
      up HsDo to take a list of Ids instead of 3 (but now 4) separate Ids.
      Saves a bit of code here and there.  Also introduced Inst.newMethodFromName
      to package a common idiom.
      a7b95beb
  13. 03 Apr, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-04-03 09:45:14 by simonpj] · f2f40c0f
      simonpj authored
      -----------------------------
      	Put existential tyvars second
      	[fixes ParsecPerm lint error]
      	-----------------------------
      
      In an existential data constr:
      
      	data Eq a => T a = forall b. Ord b => MkT a [b]
      
      the type of MkT is
      
      	MkT :: forall a b . Ord b => a -> [b] -> MkT a
      
      Note that the existential tyvars (b in this case) come *after*
      the "ordinary" tyvars.
      
      I had switched this around earlier in the week, but I'm putting
      it back (and fixing a bug) because I found it really works better second.
      
      Reason: in a case expression we may find:
      	case (e :: T t) of { MkT b (d:Ord b) (x:t) (xs:[b]) -> ... }
      It's convenient to apply the rep-type of MkT to 't', to get
      	forall b. Ord b => ...
      and use that to check the pattern.  Mind you, this is really only
      use in CoreLint.
      f2f40c0f
  14. 01 Apr, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-04-01 08:23:30 by simonpj] · 9003a18c
      simonpj authored
      ------------------------------------
      	Change the treatment of the stupid
      	   context on data constructors
      	-----------------------------------
      
      Data types can have a context:
      
      	data (Eq a, Ord b) => T a b = T1 a b | T2 a
      
      and that makes the constructors have a context too
      (notice that T2's context is "thinned"):
      
      	T1 :: (Eq a, Ord b) => a -> b -> T a b
      	T2 :: (Eq a) => a -> T a b
      
      Furthermore, this context pops up when pattern matching
      (though GHC hasn't implemented this, but it is in H98, and
      I've fixed GHC so that it now does):
      
      	f (T2 x) = x
      gets inferred type
      	f :: Eq a => T a b -> a
      
      I say the context is "stupid" because the dictionaries passed
      are immediately discarded -- they do nothing and have no benefit.
      It's a flaw in the language.
      
      Up to now I have put this stupid context into the type of
      the "wrapper" constructors functions, T1 and T2, but that turned
      out to be jolly inconvenient for generics, and record update, and
      other functions that build values of type T (because they don't
      have suitable dictionaries available).
      
      So now I've taken the stupid context out.  I simply deal with
      it separately in the type checker on occurrences of a constructor,
      either in an expression or in a pattern.
      
      To this end
      
      * Lots of changes in DataCon, MkId
      
      * New function Inst.tcInstDataCon to instantiate a data constructor
      
      
      
      I also took the opportunity to
      
      * Rename
      	dataConId --> dataConWorkId
        for consistency.
      
      * Tidied up MkId.rebuildConArgs quite a bit, and renamed it
      	mkReboxingAlt
      
      * Add function DataCon.dataConExistentialTyVars, with the obvious meaning
      9003a18c
  15. 28 Mar, 2002 1 commit
  16. 25 Mar, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-03-25 15:08:38 by simonpj] · aaed1181
      simonpj authored
      -------------------------------
      	Fix bugs in rank-N polymorphism
      	-------------------------------
      
      Discussion with Mark showed up some bugs in the rank-N
      polymorphism stuff, especally concerning the treatment of
      'hole' type variables.
      
      See especially TcMType:
      	newHoleTyVar
      	readHoleResult
      	zapToType
      
      Also the treatment of conditionals and case branches
      is done right now, using zapToType
      aaed1181
  17. 08 Mar, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-03-08 15:50:53 by simonpj] · a170160c
      simonpj authored
      --------------------------------------
      	Lift the class-method type restriction
      	--------------------------------------
      
      Haskell 98 prohibits class method types to mention constraints on the
      class type variable, thus:
      
        class Seq s a where
          fromList :: [a] -> s a
          elem     :: Eq a => a -> s a -> Bool
      
      The type of 'elem' is illegal in Haskell 98, because it contains the
      constraint 'Eq a', which constrains only the class type variable (in
      this case 'a').
      
      This commit lifts the restriction.  The way we do that is to do a full
      context reduction (tcSimplifyCheck) step for each method separately in
      TcClassDcl.tcMethodBind, rather than doing a single context reduction
      for the whole group of method bindings.
      
      As a result, I had to reorganise the code a bit, and tidy up.
      a170160c
  18. 04 Mar, 2002 1 commit
    • simonmar's avatar
      [project @ 2002-03-04 17:01:26 by simonmar] · 0171936c
      simonmar authored
      Binary Interface Files - stage 1
      --------------------------------
      
      This commit changes the default interface file format from text to
      binary, in order to improve compilation performace.
      
      To view an interface file, use 'ghc --show-iface Foo.hi'.
      
      utils/Binary.hs is the basic Binary I/O library, based on the nhc98
      binary I/O library but much stripped-down and working in terms of
      bytes rather than bits, and with some special features for GHC: it
      remembers which Module is being emitted to avoid dumping too many
      qualified names, and it keeps track of a "dictionary" of FastStrings
      so that we don't dump the same FastString more than once into the
      binary file.  I'll make a generic version of this for the libraries at
      some point.
      
      main/BinIface.hs contains most of the Binary instances.  Some
      instances are in the same module as the data type (RdrName, Name,
      OccName in particular).  Most instances were generated using a
      modified version of DrIFT, which I'll commit later.  However, editing
      them by hand isn't hard (certainly easier than modifying
      ParseIface.y).
      
      The first thing in a binary interface is the interface version, so
      nice error messages will be generated if the binary format changes and
      you still have old interfaces lying around.  The version also now
      includes the "way" as an extra sanity check.
      
      Other changes
      -------------
      
      I don't like the way FastStrings contain both hashed strings (with
      O(1) comparison) and literal C strings (with O(n) comparison).  So as
      a first step to separating these I made serveral "literal" type
      strings into hashed strings.  SLIT() still generates a literal, and
      now FSLIT() generates a hashed string.  With DEBUG on, you'll get a
      warning if you try to compare any SLIT()s with anything, and the
      compiler will fall over if you try to dump any literal C strings into
      an interface file (usually indicating a use of SLIT() which should be
      FSLIT()).
      
      mkSysLocal no longer re-encodes its FastString argument each time it
      is called.
      
      I also fixed the -pgm options so that the argument can now optionally
      be separted from the option.
      
      Bugfix: PrelNames declared Names for several comparison primops, eg.
      eqCharName, eqIntName etc. but these had different uniques from the
      real primop names.  I've moved these to PrimOps and defined them using
      mkPrimOpIdName instead, and deleted some for which we don't have real
      primops (Manuel: please check that things still work for you after
      this change).
      0171936c
  19. 14 Feb, 2002 1 commit
  20. 13 Feb, 2002 1 commit
    • simonpj's avatar
      [project @ 2002-02-13 15:14:06 by simonpj] · e7030995
      simonpj authored
      --------------------------------------------
      	Fix a bugs in type inference for rank-N types
      	--------------------------------------------
      
      We discovered this bug when looking at type rules!
      
      1. When type checking (e :: sigma-ty), we must specialise sigma-ty,
         else we lose the invariant that tcMonoType has.
      
      2. In tcExpr_id, we should pass in a Hole tyvar not an ordinary tyvar.
      
      As usual, I moved some functions around in consequence.
      e7030995
  21. 07 Feb, 2002 1 commit
  22. 28 Dec, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-12-28 17:25:31 by simonpj] · ae969b47
      simonpj authored
      ---------------------
      	Dealing with deriving
      	---------------------
      
      I spent a ridiculously long time peering at a bug report whereby
      a 'deriving' clause sent GHC 5.02.1 into a loop.  It was all to
      do with allowing instances like
      
      	instance Foo a b => Baz (T a)
      
      (Notice the 'b' on the left which does not appear on the right.)
      
      I realised that it's hard for the deriving machinery to find a
      fixpoint when these sort of instance decls are around.  So I
      now constrain *derived* instance decls not to have this form;
      all the tyvars on the left must appear on the right.
      
      On the way I commoned up the previously-separate tcSimplify
      machinery for 'deriving' and 'default' decls with that for
      everything else.   As a result, quite a few files are touched.
      
      I hope I havn't broken anything.
      ae969b47
  23. 20 Dec, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-12-20 11:19:05 by simonpj] · 91c750cb
      simonpj authored
      ---------------------------------------------
      	More type system extensions (for John Hughes)
      	---------------------------------------------
      
      1.  Added a brand-new extension that lets you derive ARBITRARY CLASSES
      for newtypes.  Thus
      
      	newtype Age = Age Int deriving( Eq, Ord, Shape, Ix )
      
      The idea is that the dictionary for the user-defined class Shape Age
      is *identical* to that for Shape Int, so there is really no deriving
      work to do.   This saves you writing the very tiresome instance decl:
      
      	instance Shape Age where
      	   shape_op1 (Age x) = shape_op1 x
      	   shape_op2 (Age x1) (Age x2) = shape_op2 x1 x2
      	   ...etc...
      
      It's more efficient, too, becuase the Shape Age dictionary really
      will be identical to the Shape Int dictionary.
      
      There's an exception for Read and Show, because the derived instance
      *isn't* the same.
      
      There is a complication where higher order stuff is involved.  Here is
      the example John gave:
      
         class StateMonad s m | m -> s where ...
      
         newtype Parser tok m a = Parser (State [tok] (Failure m) a)
      			  deriving( Monad, StateMonad )
      
      Then we want the derived instance decls to be
      
         instance Monad (State [tok] (Failure m)) => Monad (Parser tok m)
         instance StateMonad [tok] (State [tok] (Failure m))
      	 => StateMonad [tok] (Parser tok m)
      
      John is writing up manual entry for all of this, but this commit
      implements it.   I think.
      
      
      2.  Added -fallow-incoherent-instances, and documented it.  The idea
      is that sometimes GHC is over-protective about not committing to a
      particular instance, and the programmer may want to say "commit anyway".
      Here's the example:
      
          class Sat a where
            dict :: a
      
          data EqD a = EqD {eq :: a->a->Bool}
      
          instance Sat (EqD a) => Eq a where
            (==) = eq dict
      
          instance Sat (EqD Integer) where
            dict = EqD{eq=(==)}
      
          instance Eq a => Sat (EqD a) where
            dict = EqD{eq=(==)}
      
          class Collection c cxt | c -> cxt where
            empty :: Sat (cxt a) => c a
            single :: Sat (cxt a) => a -> c a
            union :: Sat (cxt a) => c a -> c a -> c a
            member :: Sat (cxt a) => a -> c a -> Bool
      
          instance Collection [] EqD where
            empty = []
            single x = [x]
            union = (++)
            member = elem
      
      It's an updated attempt to model "Restricted Data Types", if you
      remember my Haskell workshop paper. In the end, though, GHC rejects
      the program (even with fallow-overlapping-instances and
      fallow-undecideable-instances), because there's more than one way to
      construct the Eq instance needed by elem.
      
      Yet all the ways are equivalent! So GHC is being a bit over-protective
      of me, really: I know what I'm doing and I would LIKE it to pick an
      arbitrary one. Maybe a flag fallow-incoherent-instances would be a
      useful thing to add?
      91c750cb
  24. 29 Nov, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-11-29 13:47:09 by simonpj] · 32a89583
      simonpj authored
      ------------------------------
      	Add linear implicit parameters
      	------------------------------
      
      Linear implicit parameters are an idea developed by Koen Claessen,
      Mark Shields, and Simon PJ, last week.  They address the long-standing
      problem that monads seem over-kill for certain sorts of problem, notably:
      
      	* distributing a supply of unique names
      	* distributing a suppply of random numbers
      	* distributing an oracle (as in QuickCheck)
      
      
      Linear implicit parameters are just like ordinary implicit parameters,
      except that they are "linear" -- that is, they cannot be copied, and
      must be explicitly "split" instead.  Linear implicit parameters are
      written '%x' instead of '?x'.  (The '/' in the '%' suggests the
      split!)
      
      For example:
      
          data NameSupply = ...
      
          splitNS :: NameSupply -> (NameSupply, NameSupply)
          newName :: NameSupply -> Name
      
          instance PrelSplit.Splittable NameSupply where
      	split = splitNS
      
      
          f :: (%ns :: NameSupply) => Env -> Expr -> Expr
          f env (Lam x e) = Lam x' (f env e)
      		    where
      		      x'   = newName %ns
      		      env' = extend env x x'
          ...more equations for f...
      
      Notice that the implicit parameter %ns is consumed
      	once by the call to newName
      	once by the recursive call to f
      
      So the translation done by the type checker makes
      the parameter explicit:
      
          f :: NameSupply -> Env -> Expr -> Expr
          f ns env (Lam x e) = Lam x' (f ns1 env e)
      		       where
      	 		 (ns1,ns2) = splitNS ns
      			 x' = newName ns2
      			 env = extend env x x'
      
      Notice the call to 'split' introduced by the type checker.
      How did it know to use 'splitNS'?  Because what it really did
      was to introduce a call to the overloaded function 'split',
      ndefined by
      
      	class Splittable a where
      	  split :: a -> (a,a)
      
      The instance for Splittable NameSupply tells GHC how to implement
      split for name supplies.  But we can simply write
      
      	g x = (x, %ns, %ns)
      
      and GHC will infer
      
      	g :: (Splittable a, %ns :: a) => b -> (b,a,a)
      
      The Splittable class is built into GHC.  It's defined in PrelSplit,
      and exported by GlaExts.
      
      Other points:
      
      * '?x' and '%x' are entirely distinct implicit parameters: you
        can use them together and they won't intefere with each other.
      
      * You can bind linear implicit parameters in 'with' clauses.
      
      * You cannot have implicit parameters (whether linear or not)
        in the context of a class or instance declaration.
      
      
      Warnings
      ~~~~~~~~
      The monomorphism restriction is even more important than usual.
      Consider the example above:
      
          f :: (%ns :: NameSupply) => Env -> Expr -> Expr
          f env (Lam x e) = Lam x' (f env e)
      		    where
      		      x'   = newName %ns
      		      env' = extend env x x'
      
      If we replaced the two occurrences of x' by (newName %ns), which is
      usually a harmless thing to do, we get:
      
          f :: (%ns :: NameSupply) => Env -> Expr -> Expr
          f env (Lam x e) = Lam (newName %ns) (f env e)
      		    where
      		      env' = extend env x (newName %ns)
      
      But now the name supply is consumed in *three* places
      (the two calls to newName,and the recursive call to f), so
      the result is utterly different.  Urk!  We don't even have
      the beta rule.
      
      Well, this is an experimental change.  With implicit
      parameters we have already lost beta reduction anyway, and
      (as John Launchbury puts it) we can't sensibly reason about
      Haskell programs without knowing their typing.
      
      Of course, none of this is throughly tested, either.
      32a89583
  25. 26 Nov, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-11-26 09:20:25 by simonpj] · 5e3f005d
      simonpj authored
      ----------------------
      	Implement Rank-N types
      	----------------------
      
      This commit implements the full glory of Rank-N types, using
      the Odersky/Laufer approach described in their paper
      	"Putting type annotations to work"
      
      In fact, I've had to adapt their approach to deal with the
      full glory of Haskell (including pattern matching, and the
      scoped-type-variable extension).  However, the result is:
      
      * There is no restriction to rank-2 types.  You can nest forall's
        as deep as you like in a type.  For example, you can write a type
        like
      	p :: ((forall a. Eq a => a->a) -> Int) -> Int
        This is a rank-3 type, illegal in GHC 5.02
      
      * When matching types, GHC uses the cunning Odersky/Laufer coercion
        rules.  For example, suppose we have
      	q :: (forall c. Ord c => c->c) -> Int
        Then, is this well typed?
      	x :: Int
      	x = p q
        Yes, it is, but GHC has to generate the right coercion.  Here's
        what it looks like with all the big lambdas and dictionaries put in:
      
      	x = p (\ f :: (forall a. Eq a => a->a) ->
      		 q (/\c \d::Ord c -> f c (eqFromOrd d)))
      
        where eqFromOrd selects the Eq superclass dictionary from the Ord
        dicationary:		eqFromOrd :: Ord a -> Eq a
      
      
      * You can use polymorphic types in pattern type signatures.  For
        example:
      
      	f (g :: forall a. a->a) = (g 'c', g True)
      
        (Previously, pattern type signatures had to be monotypes.)
      
      * The basic rule for using rank-N types is that you must specify
        a type signature for every binder that you want to have a type
        scheme (as opposed to a plain monotype) as its type.
      
        However, you don't need to give the type signature on the
        binder (as I did above in the defn for f).  You can give it
        in a separate type signature, thus:
      
      	f :: (forall a. a->a) -> (Char,Bool)
      	f g = (g 'c', g True)
      
        GHC will push the external type signature inwards, and use
        that information to decorate the binders as it comes across them.
        I don't have a *precise* specification of this process, but I
        think it is obvious enough in practice.
      
      * In a type synonym you can use rank-N types too.  For example,
        you can write
      
      	type IdFun = forall a. a->a
      
      	f :: IdFun -> (Char,Bool)
      	f g = (g 'c', g True)
      
        As always, type synonyms must always occur saturated; GHC
        expands them before it does anything else.  (Still, GHC goes
        to some trouble to keep them unexpanded in error message.)
      
      
      The main plan is as before.  The main typechecker for expressions,
      tcExpr, takes an "expected type" as its argument.  This greatly
      improves error messages.  The new feature is that when this
      "expected type" (going down) meets an "actual type" (coming up)
      we use the new subsumption function
      	TcUnify.tcSub
      which checks that the actual type can be coerced into the
      expected type (and produces a coercion function to demonstrate).
      
      The main new chunk of code is TcUnify.tcSub.  The unifier itself
      is unchanged, but it has moved from TcMType into TcUnify.  Also
      checkSigTyVars has moved from TcMonoType into TcUnify.
      Result: the new module, TcUnify, contains all stuff relevant
      to subsumption and unification.
      
      Unfortunately, there is now an inevitable loop between TcUnify
      and TcSimplify, but that's just too bad (a simple TcUnify.hi-boot
      file).
      
      
      All of this doesn't come entirely for free.  Here's the typechecker
      line count (INCLUDING comments)
      	Before	16,551
      	After	17,116
      5e3f005d
  26. 19 Nov, 2001 1 commit
  27. 31 Oct, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-10-31 15:22:53 by simonpj] · 61bfd5dd
      simonpj authored
      ------------------------------------------
      	Improved handling of scoped type variables
      	------------------------------------------
      
      The main effect of this commit is to allow scoped type variables
      in pattern bindings, thus
      
      	(x::a, y::b) = e
      
      This was illegal, but now it's ok.  a and b have the same scope
      as x and y.
      
      
      On the way I beefed up the info inside a type variable
      (TcType.TyVarDetails; c.f. IdInfo.GlobalIdDetails) which
      helps to improve error messages. Hence the wide ranging changes.
      Pity about the extra loop from Var to TcType, but can't be helped.
      61bfd5dd
  28. 25 Oct, 2001 2 commits
    • simonpj's avatar
      [project @ 2001-10-25 14:30:43 by simonpj] · d5f94cc1
      simonpj authored
      -------------------------------------------------------
        Correct an error in the handling of implicit parameters
        -------------------------------------------------------
      
      	MERGE WITH STABLE BRANCH UNLESS HARD TO DO
      
      Mark Shields discovered a bug in the way that implicit parameters
      are dealt with by the type checker.  It's all a bit subtle, and
      is extensively documented in TcSimplify.lhs.
      
      This commit makes the code both simpler and more correct.  It subtly
      changes the way in which type signatures are treated, but not in a way
      anyone would notice: see notes with "Question 2: type signatures"
      in TcSimplify.lhs.
      d5f94cc1
    • sof's avatar
      [project @ 2001-10-25 02:13:10 by sof] · 9e933350
      sof authored
      - Pet peeve removal / code tidyup, replaced various sub-optimal
        uses of 'length' with something a bit better, i.e., replaced
        the following patterns
      
         *  length as `cmpOp` length bs
         *  length as `cmpOp` val   -- incl. uses where val == 1 and val == 0
         *  {take,drop,splitAt} (length as) bs
         *  length [ () | pat <- as ]
      
        with uses of misc Util functions.
      
        I'd be surprised if there's a noticeable reduction in running
        times as a result of these changes, but every little bit helps.
      
        [ The changes have been tested wrt testsuite/ - I'm seeing a couple
          of unexpected breakages coming from CorePrep, but I'm currently
          assuming that these are due to other recent changes. ]
      
      - compMan/CompManager.lhs: restored 4.08 compilability + some code
        cleanup.
      
      None of these changes are HEADworthy.
      9e933350
  29. 20 Aug, 2001 1 commit
  30. 20 Jul, 2001 1 commit
  31. 13 Jul, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-07-13 13:29:56 by simonpj] · d4e38936
      simonpj authored
      ------------------------------------
      	Tidy up the "syntax rebinding" story
      	------------------------------------
      
      I found a bug in the code that dealt with re-binding implicit
      numerical syntax:
      	literals 	(fromInteger/fromRational)
      	negation	(negate)
      	n+k patterns	(minus)
      
      This is triggered by the -fno-implicit-prelude flag, and it
      used to be handled via the PrelNames.SyntaxMap.
      
      But I found a nicer way to do it that involves much less code,
      and doesn't have the bug.  The explanation is with
      	RnEnv.lookupSyntaxName
      d4e38936
  32. 25 Jun, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-06-25 08:09:57 by simonpj] · d069cec2
      simonpj authored
      ----------------
      	Squash newtypes
      	----------------
      
      This commit squashes newtypes and their coerces, from the typechecker
      onwards.  The original idea was that the coerces would not get in the
      way of optimising transformations, but despite much effort they continue
      to do so.   There's no very good reason to retain newtype information
      beyond the typechecker, so now we don't.
      
      Main points:
      
      * The post-typechecker suite of Type-manipulating functions is in
      types/Type.lhs, as before.   But now there's a new suite in types/TcType.lhs.
      The difference is that in the former, newtype are transparent, while in
      the latter they are opaque.  The typechecker should only import TcType,
      not Type.
      
      * The operations in TcType are all non-monadic, and most of them start with
      "tc" (e.g. tcSplitTyConApp).  All the monadic operations (used exclusively
      by the typechecker) are in a new module, typecheck/TcMType.lhs
      
      * I've grouped newtypes with predicate types, thus:
      	data Type = TyVarTy Tyvar | ....
      		  | SourceTy SourceType
      
      	data SourceType = NType TyCon [Type]
      			| ClassP Class [Type]
      			| IParam Type
      
      [SourceType was called PredType.]  This is a little wierd in some ways,
      because NTypes can't occur in qualified types.   However, the idea is that
      a SourceType is a type that is opaque to the type checker, but transparent
      to the rest of the compiler, and newtypes fit that as do implicit parameters
      and dictionaries.
      
      * Recursive newtypes still retain their coreces, exactly as before. If
      they were transparent we'd get a recursive type, and that would make
      various bits of the compiler diverge (e.g. things which do type comparison).
      
      * I've removed types/Unify.lhs (non-monadic type unifier and matcher),
      merging it into TcType.
      
      Ditto typecheck/TcUnify.lhs (monadic unifier), merging it into TcMType.
      d069cec2
  33. 18 May, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-05-18 08:46:18 by simonpj] · b4775e5e
      simonpj authored
      -----------------------------
      	Get unbox-strict-fields right
      	-----------------------------
      
      The problem was that when a library was compiled *without* -funbox-strict-fields,
      and the main program was compiled *with* that flag, we were wrongly treating
      the fields of imported data types as unboxed.
      
      To fix this I added an extra constructor to StrictnessMark to express whether
      the "!" annotation came from an interface file (don't fiddle) or a source
      file (decide whether to unbox).
      
      On the way I tided things up:
      
      * StrictnessMark moves to Demand.lhs, and doesn't have the extra DataCon
        fields that kept it in DataCon before.
      
      * HsDecls.BangType has one constructor, not three, with a StrictnessMark field.
      
      * DataCon keeps track of its strictness signature (dcRepStrictness), but not
        its "user strict marks" (which were never used)
      
      * All the functions, like getUniquesDs, that used to take an Int saying how
        many uniques to allocate, now return an infinite list. This saves arguments
        and hassle.  But it involved touching quite a few files.
      
      * rebuildConArgs takes a list of Uniques to use as its unique supply.  This
        means I could combine DsUtils.rebuildConArgs with MkId.rebuildConArgs
        (hooray; the main point of the previous change)
      
      
      I also tidied up one or two error messages
      b4775e5e
  34. 03 May, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-05-03 08:13:25 by simonpj] · cd7dc9b1
      simonpj authored
      ****	MERGE WITH 5.00 BRANCH     ********
      
      	--------------------------------
      	Fix a bad implicit parameter bug
      	--------------------------------
      
      TcSimplify.tcSimplifyIPs was just completely wrong; it wasn't
      doing improvement properly nor binding values properly. Sigh.
      
      To make this work nicely I added
      	Inst.instName :: Inst -> Name
      cd7dc9b1
  35. 09 Apr, 2001 1 commit
  36. 13 Mar, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-03-13 14:58:25 by simonpj] · 788faebb
      simonpj authored
      ----------------
      	Nuke ClassContext
      	----------------
      
      This commit tidies up a long-standing inconsistency in GHC.
      The context of a class or instance decl used to be restricted
      to predicates of the form
      	C t1 .. tn
      with
      	type ClassContext = [(Class,[Type])]
      
      but everywhere else in the compiler we used
      
      	type ThetaType = [PredType]
      where PredType can be any sort of constraint (= predicate).
      
      The inconsistency actually led to a crash, when compiling
      	class (?x::Int) => C a where {}
      
      I've tidied all this up by nuking ClassContext altogether, and using
      PredType throughout.  Lots of modified files, but all in
      more-or-less trivial ways.
      
      I've also added a check that the context of a class or instance
      decl doesn't include a non-inheritable predicate like (?x::Int).
      
      Other things
      
       * rename constructor 'Class' from type TypeRep.Pred to 'ClassP'
         (makes it easier to grep for)
      
       * rename constructor HsPClass  => HsClassP
      		      HsPIParam => HsIParam
      788faebb
  37. 08 Mar, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-03-08 12:07:38 by simonpj] · 51a571c0
      simonpj authored
      --------------------
      	A major hygiene pass
      	--------------------
      
      1. The main change here is to
      
      	Move what was the "IdFlavour" out of IdInfo,
      	and into the varDetails field of a Var
      
         It was a mess before, because the flavour was a permanent attribute
         of an Id, whereas the rest of the IdInfo was ephemeral.  It's
         all much tidier now.
      
         Main places to look:
      
      	   Var.lhs	Defn of VarDetails
      	   IdInfo.lhs	Defn of GlobalIdDetails
      
         The main remaining infelicity is that SpecPragmaIds are right down
         in Var.lhs, which seems unduly built-in for such an ephemeral thing.
         But that is no worse than before.
      
      
      2. Tidy up the HscMain story a little.  Move mkModDetails from MkIface
         into CoreTidy (where it belongs more nicely)
      
         This was partly forced by (1) above, because I didn't want to make
         DictFun Ids into a separate kind of Id (which is how it was before).
         Not having them separate means we have to keep a list of them right
         through, rather than pull them out of the bindings at the end.
      
      3. Add NameEnv as a separate module (to join NameSet).
      
      4. Remove unnecessary {-# SOURCE #-} imports from FieldLabel.
      51a571c0
  38. 22 Feb, 2001 1 commit
    • simonpj's avatar
      [project @ 2001-02-22 13:17:57 by simonpj] · be2c67eb
      simonpj authored
      fromInt
      
      Remove fromInt from class Num, though it is retained
      as an overloaded operation (with unchanged type) in PrelNum.
      
      There are quite a few consequential changes in the Prelude.
      I hope I got them all correct!
      
      Also fix a bug that meant Integer (and its instances)
      wasn't getting slurped in by the renamer, even though it
      was needed for defaulting.
      be2c67eb