Skip to content
  • Simon Peyton Jones's avatar
    [project @ 2000-09-22 15:56:12 by simonpj] · 1bba522f
    Simon Peyton Jones authored
    --------------------------------------------------
    	Tidying up HsLit, and making it possible to define
    		your own numeric library
    
    		Simon PJ 22 Sept 00
    	--------------------------------------------------
    
    ** NOTE: I did these changes on the aeroplane.  They should compile,
    	 and the Prelude still compiles OK, but it's entirely 
    	 possible that I've broken something
    
    The original reason for this many-file but rather shallow
    commit is that it's impossible in Haskell to write your own
    numeric library.  Why?  Because when you say '1' you get 
    (Prelude.fromInteger 1), regardless of what you hide from the
    Prelude, or import from other libraries you have written.  So the
    idea is to extend the -fno-implicit-prelude flag so that 
    in addition to no importing the Prelude, you can rebind 
    	fromInteger	-- Applied to literal constants
    	fromRational	-- Ditto
    	negate		-- Invoked by the syntax (-x)
    	the (-) used when desugaring n+k patterns
    
    After toying with other designs, I eventually settled on a simple,
    crude one: rather than adding a new flag, I just extended the
    semantics of -fno-implicit-prelude so that uses of fromInteger,
    fromRational and negate are all bound to "whatever is in scope" 
    rather than "the fixed Prelude functions".  So if you say
    
    	{-# OPTIONS -fno-implicit-prelude #-}
    	module M where
     	import MyPrelude( fromInteger )
    
    	x = 3
    
    the literal 3 will use whatever (unqualified) "fromInteger" is in scope,
    in this case the one gotten from MyPrelude.
    
    
    On the way, though, I studied how HsLit worked, and did a substantial tidy
    up, deleting quite a lot of code along the way.  In particular.
    
    * HsBasic.lhs is renamed HsLit.lhs.  It defines the HsLit type.
    
    * There are now two HsLit types, both defined in HsLit.
    	HsLit for non-overloaded literals (like 'x')
    	HsOverLit for overloaded literals (like 1 and 2.3)
    
    * HsOverLit completely replaces Inst.OverloadedLit, which disappears.
      An HsExpr can now be an HsOverLit as well as an HsLit.
    
    * HsOverLit carries the Name of the fromInteger/fromRational operation,
      so that the renamer can help with looking up the unqualified name 
      when -fno-implicit-prelude is on.  Ditto the HsExpr for negation.
      It's all very tidy now.
    
    * RdrHsSyn contains the stuff that handles -fno-implicit-prelude
      (see esp RdrHsSyn.prelQual).  RdrHsSyn also contains all the "smart constructors"
      used by the parser when building HsSyn.  See for example RdrHsSyn.mkNegApp
      (previously the renamer (!) did the business of turning (- 3#) into -3#).
    
    * I tidied up the handling of "special ids" in the parser.  There's much
      less duplication now.
    
    * Move Sven's Horner stuff to the desugarer, where it belongs.  
      There's now a nice function DsUtils.mkIntegerLit which brings together
      related code from no fewer than three separate places into one single
      place.  Nice!
    
    * A nice tidy-up in MatchLit.partitionEqnsByLit became possible.
    
    * Desugaring of HsLits is now much tidier (DsExpr.dsLit)
    
    * Some stuff to do with RdrNames is moved from ParseUtil.lhs to RdrHsSyn.lhs,
      which is where it really belongs.
    
    * I also removed 
    	many unnecessary imports from modules 
    	quite a bit of dead code
      in divers places
    1bba522f