HsDecls.lhs 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
%
\section[HsDecls]{Abstract syntax: global declarations}

Definitions for: @FixityDecl@, @TyDecl@ and @ConDecl@, @ClassDecl@,
@InstDecl@, @DefaultDecl@.

\begin{code}
#include "HsVersions.h"

module HsDecls where

14
IMP_Ubiq()
15 16

-- friends:
17
import HsBinds		( HsBinds, MonoBinds, Sig, nullMonoBinds )
18
import HsPragmas	( DataPragmas, ClassPragmas,
19 20
			  InstancePragmas, ClassOpPragmas
			)
21
import HsTypes
22 23 24
import IdInfo
import SpecEnv		( SpecEnv )
import HsCore		( UfExpr )
25
import HsBasic		( Fixity )
26 27

-- others:
28
import Name		( pprSym, pprNonSym, getOccName, OccName )
29 30 31
import Outputable	( interppSP, interpp'SP,
			  Outputable(..){-instance * []-}
			)
32 33
import Pretty
import SrcLoc		( SrcLoc )
34
import PprStyle		( PprStyle(..), ifaceStyle )
35 36
\end{code}

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

%************************************************************************
%*									*
\subsection[HsDecl]{Declarations}
%*									*
%************************************************************************

\begin{code}
data HsDecl tyvar uvar name pat
  = TyD		(TyDecl name)
  | ClD		(ClassDecl tyvar uvar name pat)
  | InstD	(InstDecl  tyvar uvar name pat)
  | DefD	(DefaultDecl name)
  | ValD	(HsBinds tyvar uvar name pat)
  | SigD	(IfaceSig name)
\end{code}

\begin{code}
hsDeclName (TyD (TyData _ name _ _ _ _ _))    = name
hsDeclName (TyD (TyNew  _ name _ _ _ _ _))    = name
hsDeclName (TyD (TySynonym name _ _ _))       = name
hsDeclName (ClD (ClassDecl _ name _ _ _ _ _)) = name
hsDeclName (SigD (IfaceSig name _ _ _))	      = name
-- Others don't make sense
\end{code}

\begin{code}
instance (NamedThing name, Outputable name, Outputable pat,
	  Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
	=> Outputable (HsDecl tyvar uvar name pat) where

    ppr sty (TyD td)     = ppr sty td
    ppr sty (ClD cd)     = ppr sty cd
    ppr sty (SigD sig)   = ppr sty sig
    ppr sty (ValD binds) = ppr sty binds
    ppr sty (DefD def)   = ppr sty def
    ppr sty (InstD inst) = ppr sty inst
74 75 76 77

-- In interfaces, top-level binders are printed without their "Module." prefix
ppr_top_binder sty bndr | ifaceStyle sty = ppr sty (getOccName bndr)
	  	        | otherwise	 = ppr sty bndr
78 79 80
\end{code}


81 82 83 84 85 86 87
%************************************************************************
%*									*
\subsection[FixityDecl]{A fixity declaration}
%*									*
%************************************************************************

\begin{code}
88 89 90 91
data FixityDecl name  = FixityDecl name Fixity SrcLoc

instance Outputable name => Outputable (FixityDecl name) where
  ppr sty (FixityDecl name fixity loc) = ppSep [ppr sty fixity, ppr sty name]
92 93
\end{code}

94

95 96 97 98 99 100 101 102 103 104
%************************************************************************
%*									*
\subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
%*									*
%************************************************************************

\begin{code}
data TyDecl name
  = TyData	(Context name)	-- context
		name		-- type constructor
105
		[HsTyVar name]	-- type variables
106 107 108 109 110 111 112 113 114 115
		[ConDecl name]	-- data constructors (empty if abstract)
		(Maybe [name])	-- derivings; Nothing => not specified
				-- (i.e., derive default); Just [] => derive
				-- *nothing*; Just <list> => as you would
				-- expect...
		(DataPragmas name)
		SrcLoc

  | TyNew	(Context name)	-- context
		name		-- type constructor
116 117
		[HsTyVar name]	-- type variables
		(ConDecl name)	-- data constructor
118 119 120 121 122
		(Maybe [name])	-- derivings; as above
		(DataPragmas name)
		SrcLoc

  | TySynonym	name		-- type constructor
123 124
		[HsTyVar name]	-- type variables
		(HsType name)	-- synonym expansion
125 126 127 128 129 130 131 132 133 134
		SrcLoc

\end{code}

\begin{code}
instance (NamedThing name, Outputable name)
	      => Outputable (TyDecl name) where

    ppr sty (TySynonym tycon tyvars mono_ty src_loc)
      = ppHang (pp_decl_head sty SLIT("type") ppNil tycon tyvars)
135
	     4 (ppr sty mono_ty)
136 137 138

    ppr sty (TyData context tycon tyvars condecls derivings pragmas src_loc)
      = pp_tydecl sty
139
		  (pp_decl_head sty SLIT("data") (pp_context_and_arrow sty context) tycon tyvars)
140 141 142 143 144
		  (pp_condecls sty condecls)
		  derivings

    ppr sty (TyNew context tycon tyvars condecl derivings pragmas src_loc)
      = pp_tydecl sty
145 146
		  (pp_decl_head sty SLIT("newtype") (pp_context_and_arrow sty context) tycon tyvars)
		  (ppr sty condecl)
147 148 149
		  derivings

pp_decl_head sty str pp_context tycon tyvars
150
  = ppCat [ppPStr str, pp_context, ppr_top_binder sty tycon,
151
	   interppSP sty tyvars, ppPStr SLIT("=")]
152

153
pp_condecls sty [] = ppNil		-- Curious!
154
pp_condecls sty (c:cs)
155
  = ppSep (ppr sty c : map (\ c -> ppBeside (ppPStr SLIT("| ")) (ppr sty c)) cs)
156 157 158 159

pp_tydecl sty pp_head pp_decl_rhs derivings
  = ppHang pp_head 4 (ppSep [
	pp_decl_rhs,
160 161 162 163 164 165 166 167 168
	case (derivings, sty) of
	  (Nothing,_) 	   -> ppNil
	  (_,PprInterface) -> ppNil	-- No derivings in interfaces
	  (Just ds,_)	   -> ppCat [ppPStr SLIT("deriving"), ppParens (interpp'SP sty ds)]
    ])

pp_context_and_arrow :: Outputable name => PprStyle -> Context name -> Pretty
pp_context_and_arrow sty [] = ppNil
pp_context_and_arrow sty theta = ppCat [pprContext sty theta, ppPStr SLIT("=>")]
169 170 171 172 173 174 175 176 177
\end{code}

A type for recording what types a datatype should be specialised to.
It's called a ``Sig'' because it's sort of like a ``type signature''
for an datatype declaration.

\begin{code}
data SpecDataSig name
  = SpecDataSig name		-- tycon to specialise
178
		(HsType name)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		SrcLoc

instance (NamedThing name, Outputable name)
	      => Outputable (SpecDataSig name) where

    ppr sty (SpecDataSig tycon ty _)
      = ppCat [ppStr "{-# SPECIALIZE data", ppr sty ty, ppStr "#-}"]
\end{code}

%************************************************************************
%*									*
\subsection[ConDecl]{A data-constructor declaration}
%*									*
%************************************************************************

\begin{code}
data ConDecl name
  = ConDecl	name		-- prefix-style con decl
		[BangType name]
		SrcLoc

  | ConOpDecl	(BangType name)	-- infix-style con decl
		name
		(BangType name)
		SrcLoc

  | RecConDecl	name
206
		[([name], BangType name)]	-- list of "fields"
207 208 209
		SrcLoc

  | NewConDecl  name		-- newtype con decl
210
		(HsType name)
211 212 213
		SrcLoc

data BangType name
214 215
  = Banged   (HsType name)	-- HsType: to allow Haskell extensions
  | Unbanged (HsType name)	-- (MonoType only needed for straight Haskell)
216 217 218 219 220 221
\end{code}

\begin{code}
instance (NamedThing name, Outputable name) => Outputable (ConDecl name) where

    ppr sty (ConDecl con tys _)
222
      = ppCat [ppr_top_binder sty con, ppInterleave ppNil (map (ppr_bang sty) tys)]
223 224

	-- We print ConOpDecls in prefix form in interface files
225
    ppr sty (ConOpDecl ty1 op ty2 _)
226 227 228 229
      | ifaceStyle sty
      = ppCat [ppr_top_binder sty op, ppr_bang sty ty1, ppr_bang sty ty2]
      | otherwise
      = ppCat [ppr_bang sty ty1, ppr_top_binder sty op, ppr_bang sty ty2]
230

231
    ppr sty (NewConDecl con ty _)
232
      = ppCat [ppr_top_binder sty con, pprParendHsType sty ty]
233
    ppr sty (RecConDecl con fields _)
234
      = ppCat [ppr_top_binder sty con,
235 236
	       ppCurlies (ppInterleave pp'SP (map pp_field fields))
	      ]
237
      where
238
	pp_field (ns, ty) = ppCat [ppCat (map (ppr_top_binder sty) ns), 
239
				   ppPStr SLIT("::"), ppr_bang sty ty]
240

241
ppr_bang sty (Banged   ty) = ppBeside (ppPStr SLIT("! ")) (pprParendHsType sty ty)
242 243 244
				-- The extra space helps the lexical analyser that lexes
				-- interface files; it doesn't make the rigid operator/identifier
				-- distinction, so "!a" is a valid identifier so far as it is concerned
245
ppr_bang sty (Unbanged ty) = pprParendHsType sty ty
246 247 248 249 250 251 252 253 254 255 256 257
\end{code}

%************************************************************************
%*									*
\subsection[ClassDecl]{A class declaration}
%*									*
%************************************************************************

\begin{code}
data ClassDecl tyvar uvar name pat
  = ClassDecl	(Context name)	    		-- context...
		name		    		-- name of the class
258
		(HsTyVar name)	    		-- the class type variable
259 260 261 262 263 264 265 266 267 268 269 270
		[Sig name]			-- methods' signatures
		(MonoBinds tyvar uvar name pat)	-- default methods
		(ClassPragmas name)
		SrcLoc
\end{code}

\begin{code}
instance (NamedThing name, Outputable name, Outputable pat,
	  Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
		=> Outputable (ClassDecl tyvar uvar name pat) where

    ppr sty (ClassDecl context clas tyvar sigs methods pragmas src_loc)
271 272 273 274
      | null sigs	-- No "where" part
      = top_matter

      | iface_style	-- All on one line (for now at least)
275
      = ppCat [top_matter, ppPStr SLIT("where"), 
276 277 278
	       ppCurlies (ppInterleave (ppPStr SLIT("; ")) pp_sigs)]

      | otherwise	-- Laid out
279
      = ppSep [ppCat [top_matter, ppPStr SLIT("where {")],
280
	       ppNest 4 ((ppIntersperse ppSemi pp_sigs `ppAbove` pp_methods)
281
			 `ppBeside` ppChar '}')]
282
      where
283 284
        top_matter = ppCat [ppPStr SLIT("class"), pp_context_and_arrow sty context,
                            ppr_top_binder sty clas, ppr sty tyvar]
285 286 287
	pp_sigs     = map (ppr sty) sigs 
	pp_methods  = ppr sty methods
	iface_style = case sty of {PprInterface -> True; other -> False}
288 289 290 291 292 293 294 295 296 297
\end{code}

%************************************************************************
%*									*
\subsection[InstDecl]{An instance declaration (also, @SpecInstSig@)}
%*									*
%************************************************************************

\begin{code}
data InstDecl tyvar uvar name pat
298
  = InstDecl	(HsType name)	-- Context => Class Instance-type
299 300 301 302 303
				-- Using a polytype means that the renamer conveniently
				-- figures out the quantified type variables for us.

		(MonoBinds tyvar uvar name pat)

304
		[Sig name]		-- User-supplied pragmatic info
305

306
		(Maybe name)		-- Name for the dictionary function
307 308 309 310 311 312 313 314 315

		SrcLoc
\end{code}

\begin{code}
instance (NamedThing name, Outputable name, Outputable pat,
	  Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
	      => Outputable (InstDecl tyvar uvar name pat) where

316 317 318
    ppr sty (InstDecl inst_ty binds uprags dfun_name src_loc)
      | case sty of { PprInterface -> True; other -> False} ||
	nullMonoBinds binds && null uprags
319
      = ppCat [ppPStr SLIT("instance"), ppr sty inst_ty]
320 321

      | otherwise
322
      =	ppAboves [ppCat [ppPStr SLIT("instance"), ppr sty inst_ty, ppPStr SLIT("where")],
323 324
	          ppNest 4 (ppr sty uprags),
	          ppNest 4 (ppr sty binds) ]
325 326 327 328 329 330 331 332
\end{code}

A type for recording what instances the user wants to specialise;
called a ``Sig'' because it's sort of like a ``type signature'' for an
instance.
\begin{code}
data SpecInstSig name
  = SpecInstSig  name		    -- class
333
		 (HsType name)    -- type to specialise to
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		 SrcLoc

instance (NamedThing name, Outputable name)
	      => Outputable (SpecInstSig name) where

    ppr sty (SpecInstSig clas ty _)
      = ppCat [ppStr "{-# SPECIALIZE instance", ppr sty clas, ppr sty ty, ppStr "#-}"]
\end{code}

%************************************************************************
%*									*
\subsection[DefaultDecl]{A @default@ declaration}
%*									*
%************************************************************************

There can only be one default declaration per module, but it is hard
for the parser to check that; we pass them all through in the abstract
syntax, and that restriction must be checked in the front end.

\begin{code}
data DefaultDecl name
355
  = DefaultDecl	[HsType name]
356 357 358 359 360 361 362 363
		SrcLoc

instance (NamedThing name, Outputable name)
	      => Outputable (DefaultDecl name) where

    ppr sty (DefaultDecl tys src_loc)
      = ppBeside (ppPStr SLIT("default ")) (ppParens (interpp'SP sty tys))
\end{code}
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

%************************************************************************
%*									*
\subsection{Signatures in interface files}
%*									*
%************************************************************************

\begin{code}
data IfaceSig name
  = IfaceSig	name
		(HsType name)
		[HsIdInfo name]
		SrcLoc

instance (NamedThing name, Outputable name) => Outputable (IfaceSig name) where
    ppr sty (IfaceSig var ty _ _)
380
      = ppHang (ppCat [ppr_top_binder sty var, ppPStr SLIT("::")])
381 382 383 384 385 386 387 388 389 390 391 392
	     4 (ppr sty ty)

data HsIdInfo name
  = HsArity		ArityInfo
  | HsStrictness	(StrictnessInfo name)
  | HsUnfold		(UfExpr name)
  | HsUpdate		UpdateInfo
  | HsDeforest		DeforestInfo
  | HsArgUsage		ArgUsageInfo
  | HsFBType		FBTypeInfo
	-- ToDo: specialisations
\end{code}