Syntax.hs 44 KB
Newer Older
1
{-# LANGUAGE UnboxedTuples #-}
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16
-----------------------------------------------------------------------------
-- |
-- Module      :  Language.Haskell.Syntax
-- Copyright   :  (c) The University of Glasgow 2003
-- License     :  BSD-style (see the file libraries/base/LICENSE)
-- 
-- Maintainer  :  libraries@haskell.org
-- Stability   :  experimental
-- Portability :  portable
--
-- Abstract syntax definitions for Template Haskell.
--
-----------------------------------------------------------------------------

17
module Language.Haskell.TH.Syntax where
18 19 20

import GHC.Base		( Int(..), Int#, (<#), (==#) )

21
import Data.Data (Data(..), Typeable, mkConstr, mkDataType, constrIndex)
Ross Paterson's avatar
Ross Paterson committed
22
import qualified Data.Data as Data
23
import Control.Applicative( Applicative(..) )
24
import Data.IORef
25
import System.IO.Unsafe	( unsafePerformIO )
26
import Control.Monad (liftM)
27
import System.IO	( hPutStrLn, stderr )
28
import Data.Char        ( isAlpha )
reinerp's avatar
reinerp committed
29
import Data.Word        ( Word8 )
30 31 32 33 34 35 36

-----------------------------------------------------
--
--		The Quasi class
--
-----------------------------------------------------

37
class (Monad m, Applicative m) => Quasi m where
38
  qNewName :: String -> m Name
aavogt's avatar
aavogt committed
39
	-- ^ Fresh names
40 41

	-- Error reporting and recovery
aavogt's avatar
aavogt committed
42
  qReport  :: Bool -> String -> m ()	-- ^ Report an error (True) or warning (False)
43
					-- ...but carry on; use 'fail' to stop
aavogt's avatar
aavogt committed
44 45 46
  qRecover :: m a -- ^ the error handler
           -> m a -- ^ action which may fail
           -> m a		-- ^ Recover from the monadic 'fail'
47 48
 
	-- Inspect the type-checker's environment
49 50 51 52 53 54 55 56
  qLookupName :: Bool -> String -> m (Maybe Name)
       -- True <=> type namespace, False <=> value namespace
  qReify          :: Name -> m Info
  qReifyInstances :: Name -> [Type] -> m [Dec]
       -- Is (n tys) an instance?
       -- Returns list of matching instance Decs 
       --    (with empty sub-Decs)
       -- Works for classes and type functions
57

58
  qLocation :: m Loc
59 60

  qRunIO :: IO a -> m a
aavogt's avatar
aavogt committed
61
  -- ^ Input/output (dangerous)
62

GregWeber's avatar
GregWeber committed
63
  qAddDependentFile :: FilePath -> m ()
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

-----------------------------------------------------
--	The IO instance of Quasi
-- 
--  This instance is used only when running a Q
--  computation in the IO monad, usually just to
--  print the result.  There is no interesting
--  type environment, so reification isn't going to
--  work.
--
-----------------------------------------------------

instance Quasi IO where
  qNewName s = do { n <- readIORef counter
                 ; writeIORef counter (n+1)
                 ; return (mkNameU s n) }

  qReport True  msg = hPutStrLn stderr ("Template Haskell error: " ++ msg)
  qReport False msg = hPutStrLn stderr ("Template Haskell error: " ++ msg)

84
  qLookupName _ _     = badIO "lookupName"
85
  qReify _            = badIO "reify"
86
  qReifyInstances _ _ = badIO "classInstances"
87 88
  qLocation    	      = badIO "currentLocation"
  qRecover _ _ 	      = badIO "recover" -- Maybe we could fix this?
GregWeber's avatar
GregWeber committed
89
  qAddDependentFile _ = badIO "addDependentFile"
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

  qRunIO m = m
  
badIO :: String -> IO a
badIO op = do	{ qReport True ("Can't do `" ++ op ++ "' in the IO monad")
		; fail "Template Haskell failure" }

-- Global variable to generate unique symbols
counter :: IORef Int
{-# NOINLINE counter #-}
counter = unsafePerformIO (newIORef 0)


-----------------------------------------------------
--
--		The Q monad
--
-----------------------------------------------------

newtype Q a = Q { unQ :: forall m. Quasi m => m a }

111 112 113 114 115 116 117 118 119 120 121
-- \"Runs\" the 'Q' monad. Normal users of Template Haskell
-- should not need this function, as the splice brackets @$( ... )@
-- are the usual way of running a 'Q' computation.
--
-- This function is primarily used in GHC internals, and for debugging
-- splices by running them in 'IO'. 
--
-- Note that many functions in 'Q', such as 'reify' and other compiler
-- queries, are not supported when running 'Q' in 'IO'; these operations
-- simply fail at runtime. Indeed, the only operations guaranteed to succeed
-- are 'newName', 'runIO', 'reportError' and 'reportWarning'.
122 123 124 125 126 127 128
runQ :: Quasi m => Q a -> m a
runQ (Q m) = m

instance Monad Q where
  return x   = Q (return x)
  Q m >>= k  = Q (m >>= \x -> unQ (k x))
  Q m >> Q n = Q (m >> n)
129
  fail s     = report True s >> Q (fail "Q monad failure")
130

131 132 133
instance Functor Q where
  fmap f (Q x) = Q (fmap f x)

134 135 136 137
instance Applicative Q where 
  pure x = Q (pure x) 
  Q f <*> Q x = Q (f <*> x) 

138 139
----------------------------------------------------
-- Packaged versions for the programmer, hiding the Quasi-ness
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

{- | 
Generate a fresh name, which cannot be captured. 

For example, this:

@f = $(do
  nm1 <- newName \"x\"
  let nm2 = 'mkName' \"x\"
  return ('LamE' ['VarP' nm1] (LamE [VarP nm2] ('VarE' nm1)))
 )@

will produce the splice

>f = \x0 -> \x -> x0

In particular, the occurrence @VarE nm1@ refers to the binding @VarP nm1@,
and is not captured by the binding @VarP nm2@.

Although names generated by @newName@ cannot /be captured/, they can
/capture/ other names. For example, this:

>g = $(do
>  nm1 <- newName "x"
>  let nm2 = mkName "x"
>  return (LamE [VarP nm2] (LamE [VarP nm1] (VarE nm2)))
> )

will produce the splice

>g = \x -> \x0 -> x0

since the occurrence @VarE nm2@ is captured by the innermost binding
of @x@, namely @VarP nm1@.
-}
175 176 177
newName :: String -> Q Name
newName s = Q (qNewName s)

178 179
-- | Report an error (True) or warning (False), 
-- but carry on; use 'fail' to stop.
180 181
report  :: Bool -> String -> Q ()
report b s = Q (qReport b s)
182
{-# DEPRECATED report "Use reportError or reportWarning instead" #-} -- deprecated in 7.6
183 184 185 186 187 188 189 190

-- | Report an error to the user, but allow the current splice's computation to carry on. To abort the computation, use 'fail'.
reportError :: String -> Q ()
reportError = report True

-- | Report a warning to the user, and carry on.
reportWarning :: String -> Q ()
reportWarning = report False
191

192 193 194
-- | Recover from errors raised by 'reportError' or 'fail'.
recover :: Q a -- ^ handler to invoke on failure
        -> Q a -- ^ computation to run
aavogt's avatar
aavogt committed
195
        -> Q a
196 197
recover (Q r) (Q m) = Q (qRecover r m)

198 199 200 201 202
-- We don't export lookupName; the Bool isn't a great API
-- Instead we export lookupTypeName, lookupValueName
lookupName :: Bool -> String -> Q (Maybe Name)
lookupName ns s = Q (qLookupName ns s)

203 204
-- | Look up the given name in the (type namespace of the) current splice's scope. See "Language.Haskell.TH.Syntax#namelookup" for more details.
lookupTypeName :: String -> Q (Maybe Name)
205
lookupTypeName  s = Q (qLookupName True s)
206 207 208

-- | Look up the given name in the (value namespace of the) current splice's scope. See "Language.Haskell.TH.Syntax#namelookup" for more details.
lookupValueName :: String -> Q (Maybe Name)
209 210
lookupValueName s = Q (qLookupName False s)

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
{-
Note [Name lookup]
~~~~~~~~~~~~~~~~~~
-}
{- $namelookup #namelookup#
The functions 'lookupTypeName' and 'lookupValueName' provide
a way to query the current splice's context for what names
are in scope. The function 'lookupTypeName' queries the type
namespace, whereas 'lookupValueName' queries the value namespace,
but the functions are otherwise identical.

A call @lookupValueName s@ will check if there is a value
with name @s@ in scope at the current splice's location. If
there is, the @Name@ of this value is returned;
if not, then @Nothing@ is returned.

The returned name cannot be \"captured\". 
For example:

> f = "global"
> g = $( do
>          Just nm <- lookupValueName "f"
>          [| let f = "local" in $( varE nm ) |]

In this case, @g = \"global\"@; the call to @lookupValueName@
returned the global @f@, and this name was /not/ captured by
the local definition of @f@.

The lookup is performed in the context of the /top-level/ splice
being run. For example:

> f = "global"
> g = $( [| let f = "local" in 
>            $(do
>                Just nm <- lookupValueName "f"
>                varE nm
>             ) |] )

Again in this example, @g = \"global\"@, because the call to
@lookupValueName@ queries the context of the outer-most @$(...)@.

Operators should be queried without any surrounding parentheses, like so:

> lookupValueName "+"

Qualified names are also supported, like so:

> lookupValueName "Prelude.+"
> lookupValueName "Prelude.map"

-}


{- | 'reify' looks up information about the 'Name'.

It is sometimes useful to construct the argument name using 'lookupTypeName' or 'lookupValueName'
to ensure that we are reifying from the right namespace. For instance, in this context:

> data D = D

which @D@ does @reify (mkName \"D\")@ return information about? (Answer: @D@-the-type, but don't rely on it.)
To ensure we get information about @D@-the-value, use 'lookupValueName':

> do
>   Just nm <- lookupValueName "D"
>   reify nm

and to get information about @D@-the-type, use 'lookupTypeName'.
-}
280 281 282
reify :: Name -> Q Info
reify v = Q (qReify v)

283 284 285 286 287 288
{- | @reifyInstances nm tys@ returns a list of visible instances of @nm tys@. That is, 
if @nm@ is the name of a type class, then all instances of this class at the types @tys@
are returned. Alternatively, if @nm@ is the name of a data family or type family,
all instances of this family at the types @tys@ are returned.
-}
reifyInstances :: Name -> [Type] -> Q [InstanceDec]
289
reifyInstances cls tys = Q (qReifyInstances cls tys)
290

291
-- | Is the list of instances returned by 'reifyInstances' nonempty?
292 293 294
isInstance :: Name -> [Type] -> Q Bool
isInstance nm tys = do { decs <- reifyInstances nm tys
                       ; return (not (null decs)) }
295

296
-- | The location at which this computation is spliced.
297 298
location :: Q Loc
location = Q qLocation
299

dons's avatar
dons committed
300
-- |The 'runIO' function lets you run an I\/O computation in the 'Q' monad.
301 302 303 304 305 306
-- Take care: you are guaranteed the ordering of calls to 'runIO' within 
-- a single 'Q' computation, but not about the order in which splices are run.  
--
-- Note: for various murky reasons, stdout and stderr handles are not 
-- necesarily flushed when the  compiler finishes running, so you should
-- flush them yourself.
307 308 309
runIO :: IO a -> Q a
runIO m = Q (qRunIO m)

GregWeber's avatar
GregWeber committed
310 311 312 313 314 315 316
-- | Record external files that runIO is using (dependent upon).
-- The compiler can then recognize that it should re-compile the file using this TH when the external file changes.
-- Note that ghc -M will still not know about these dependencies - it does not execute TH.
-- Expects an absolute file path.
addDependentFile :: FilePath -> Q ()
addDependentFile fp = Q (qAddDependentFile fp)

317
instance Quasi Q where
GregWeber's avatar
GregWeber committed
318 319 320 321 322 323 324 325 326
  qNewName  	    = newName
  qReport   	    = report
  qRecover  	    = recover 
  qReify    	    = reify
  qReifyInstances   = reifyInstances
  qLookupName       = lookupName
  qLocation 	    = location
  qRunIO    	    = runIO
  qAddDependentFile = addDependentFile
327 328 329 330


----------------------------------------------------
-- The following operations are used solely in DsMeta when desugaring brackets
331
-- They are not necessary for the user, who can use ordinary return and (>>=) etc
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

returnQ :: a -> Q a
returnQ = return

bindQ :: Q a -> (a -> Q b) -> Q b
bindQ = (>>=)

sequenceQ :: [Q a] -> Q [a]
sequenceQ = sequence


-----------------------------------------------------
--
--		The Lift class
--
-----------------------------------------------------

class Lift t where
  lift :: t -> Q Exp
  
instance Lift Integer where
  lift x = return (LitE (IntegerL x))

instance Lift Int where
  lift x= return (LitE (IntegerL (fromIntegral x)))

instance Lift Char where
  lift x = return (LitE (CharL x))

instance Lift Bool where
  lift True  = return (ConE trueName)
  lift False = return (ConE falseName)

365 366 367 368 369 370 371 372
instance Lift a => Lift (Maybe a) where
  lift Nothing  = return (ConE nothingName)
  lift (Just x) = liftM (ConE justName `AppE`) (lift x)

instance (Lift a, Lift b) => Lift (Either a b) where
  lift (Left x)  = liftM (ConE leftName  `AppE`) (lift x)
  lift (Right y) = liftM (ConE rightName `AppE`) (lift y)

373 374 375
instance Lift a => Lift [a] where
  lift xs = do { xs' <- mapM lift xs; return (ListE xs') }

376 377 378 379
liftString :: String -> Q Exp
-- Used in TcExpr to short-circuit the lifting for strings
liftString s = return (LitE (StringL s))

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
instance (Lift a, Lift b) => Lift (a, b) where
  lift (a, b)
    = liftM TupE $ sequence [lift a, lift b]

instance (Lift a, Lift b, Lift c) => Lift (a, b, c) where
  lift (a, b, c)
    = liftM TupE $ sequence [lift a, lift b, lift c]

instance (Lift a, Lift b, Lift c, Lift d) => Lift (a, b, c, d) where
  lift (a, b, c, d)
    = liftM TupE $ sequence [lift a, lift b, lift c, lift d]

instance (Lift a, Lift b, Lift c, Lift d, Lift e)
      => Lift (a, b, c, d, e) where
  lift (a, b, c, d, e)
    = liftM TupE $ sequence [lift a, lift b, lift c, lift d, lift e]

instance (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f)
      => Lift (a, b, c, d, e, f) where
  lift (a, b, c, d, e, f)
    = liftM TupE $ sequence [lift a, lift b, lift c, lift d, lift e, lift f]

instance (Lift a, Lift b, Lift c, Lift d, Lift e, Lift f, Lift g)
      => Lift (a, b, c, d, e, f, g) where
  lift (a, b, c, d, e, f, g)
    = liftM TupE $ sequence [lift a, lift b, lift c, lift d, lift e, lift f, lift g]

407 408 409 410 411 412 413 414 415 416
-- TH has a special form for literal strings,
-- which we should take advantage of.
-- NB: the lhs of the rule has no args, so that
--     the rule will apply to a 'lift' all on its own
--     which happens to be the way the type checker 
--     creates it.
{-# RULES "TH:liftString" lift = \s -> return (LitE (StringL s)) #-}


trueName, falseName :: Name
Ian Lynagh's avatar
Ian Lynagh committed
417 418
trueName  = mkNameG DataName "ghc-prim" "GHC.Types" "True"
falseName = mkNameG DataName "ghc-prim" "GHC.Types" "False"
419

420 421 422 423 424 425 426 427
nothingName, justName :: Name
nothingName = mkNameG DataName "base" "Data.Maybe" "Nothing"
justName    = mkNameG DataName "base" "Data.Maybe" "Just"

leftName, rightName :: Name
leftName  = mkNameG DataName "base" "Data.Either" "Left"
rightName = mkNameG DataName "base" "Data.Either" "Right"

428 429 430 431 432

-----------------------------------------------------
--		Names and uniques 
-----------------------------------------------------

433 434 435 436 437 438 439 440 441
newtype ModName = ModName String	-- Module name
 deriving (Eq,Ord,Typeable,Data)

newtype PkgName = PkgName String	-- package name
 deriving (Eq,Ord,Typeable,Data)

newtype OccName = OccName String
 deriving (Eq,Ord,Typeable,Data)

442
mkModName :: String -> ModName
443
mkModName s = ModName s
444 445

modString :: ModName -> String
446
modString (ModName m) = m
447

448 449

mkPkgName :: String -> PkgName
450
mkPkgName s = PkgName s
451 452

pkgString :: PkgName -> String
453
pkgString (PkgName m) = m
454 455


456 457 458 459 460
-----------------------------------------------------
--		OccName
-----------------------------------------------------

mkOccName :: String -> OccName
461
mkOccName s = OccName s
462 463

occString :: OccName -> String
464
occString (OccName occ) = occ
465 466 467 468 469


-----------------------------------------------------
--		 Names
-----------------------------------------------------
470
-- 
aavogt's avatar
aavogt committed
471
-- For "global" names ('NameG') we need a totally unique name,
472 473
-- so we must include the name-space of the thing
--
aavogt's avatar
aavogt committed
474
-- For unique-numbered things ('NameU'), we've got a unique reference
475 476
-- anyway, so no need for name space
--
aavogt's avatar
aavogt committed
477
-- For dynamically bound thing ('NameS') we probably want them to
478 479
-- in a context-dependent way, so again we don't want the name
-- space.  For example:
aavogt's avatar
aavogt committed
480 481 482
--
-- > let v = mkName "T" in [| data $v = $v |]
--
483
-- Here we use the same Name for both type constructor and data constructor
aavogt's avatar
aavogt committed
484 485 486 487 488 489 490 491 492 493
--
--
-- NameL and NameG are bound *outside* the TH syntax tree
-- either globally (NameG) or locally (NameL). Ex:
--
-- > f x = $(h [| (map, x) |])
--
-- The 'map' will be a NameG, and 'x' wil be a NameL
--
-- These Names should never appear in a binding position in a TH syntax tree
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

{- $namecapture #namecapture#
Much of 'Name' API is concerned with the problem of /name capture/, which
can be seen in the following example.

> f expr = [| let x = 0 in $expr |]
> ...
> g x = $( f [| x |] )
> h y = $( f [| y |] )

A naive desugaring of this would yield:

> g x = let x = 0 in x
> h y = let x = 0 in y

All of a sudden, @g@ and @h@ have different meanings! In this case,
we say that the @x@ in the RHS of @g@ has been /captured/
by the binding of @x@ in @f@.

What we actually want is for the @x@ in @f@ to be distinct from the
@x@ in @g@, so we get the following desugaring:

> g x = let x' = 0 in x
> h y = let x' = 0 in y

which avoids name capture as desired. 

In the general case, we say that a @Name@ can be captured if
the thing it refers to can be changed by adding new declarations.
-}

{- |
An abstract type representing names in the syntax tree.

'Name's can be constructed in several ways, which come with different
name-capture guarantees (see "Language.Haskell.TH.Syntax#namecapture" for
an explanation of name capture):

  * the built-in syntax @'f@ and @''T@ can be used to construct names, 
    The expression @'f@ gives a @Name@ which refers to the value @f@ 
    currently in scope, and @''T@ gives a @Name@ which refers to the
    type @T@ currently in scope. These names can never be captured.
    
  * 'lookupValueName' and 'lookupTypeName' are similar to @'f@ and 
     @''T@ respectively, but the @Name@s are looked up at the point
     where the current splice is being run. These names can never be
     captured.

  * 'newName' monadically generates a new name, which can never
     be captured.
     
  * 'mkName' generates a capturable name.

Names constructed using @newName@ and @mkName@ may be used in bindings
(such as @let x = ...@ or @\x -> ...@), but names constructed using
@lookupValueName@, @lookupTypeName@, @'f@, @''T@ may not.
-}
551
data Name = Name OccName NameFlavour deriving (Typeable, Data)
552 553

data NameFlavour
aavogt's avatar
aavogt committed
554 555 556 557 558 559
  = NameS           -- ^ An unqualified name; dynamically bound
  | NameQ ModName   -- ^ A qualified name; dynamically bound
  | NameU Int#      -- ^ A unique local name
  | NameL Int#      -- ^ Local name bound outside of the TH AST
  | NameG NameSpace PkgName ModName -- ^ Global name bound outside of the TH AST:
                -- An original name (occurrences only, not binders)
560 561
		-- Need the namespace too to be sure which 
		-- thing we are naming
562 563
  deriving ( Typeable )

aavogt's avatar
aavogt committed
564
-- |
565 566 567 568 569 570 571
-- Although the NameFlavour type is abstract, the Data instance is not. The reason for this
-- is that currently we use Data to serialize values in annotations, and in order for that to
-- work for Template Haskell names introduced via the 'x syntax we need gunfold on NameFlavour
-- to work. Bleh!
--
-- The long term solution to this is to use the binary package for annotation serialization and
-- then remove this instance. However, to do _that_ we need to wait on binary to become stable, since
Gabor Greif's avatar
Gabor Greif committed
572
-- boot libraries cannot be upgraded separately from GHC itself.
573 574
--
-- This instance cannot be derived automatically due to bug #2701
575
instance Data NameFlavour where
576 577 578 579 580 581 582 583 584 585 586 587
     gfoldl _ z NameS          = z NameS
     gfoldl k z (NameQ mn)     = z NameQ `k` mn
     gfoldl k z (NameU i)      = z (\(I# i') -> NameU i') `k` (I# i)
     gfoldl k z (NameL i)      = z (\(I# i') -> NameL i') `k` (I# i)
     gfoldl k z (NameG ns p m) = z NameG `k` ns `k` p `k` m
     gunfold k z c = case constrIndex c of
         1 -> z NameS
         2 -> k $ z NameQ
         3 -> k $ z (\(I# i) -> NameU i)
         4 -> k $ z (\(I# i) -> NameL i)
         5 -> k $ k $ k $ z NameG
         _ -> error "gunfold: NameFlavour"
588 589 590 591 592 593 594
     toConstr NameS = con_NameS
     toConstr (NameQ _) = con_NameQ
     toConstr (NameU _) = con_NameU
     toConstr (NameL _) = con_NameL
     toConstr (NameG _ _ _) = con_NameG
     dataTypeOf _ = ty_NameFlavour

Ross Paterson's avatar
Ross Paterson committed
595 596 597 598 599 600
con_NameS, con_NameQ, con_NameU, con_NameL, con_NameG :: Data.Constr
con_NameS = mkConstr ty_NameFlavour "NameS" [] Data.Prefix
con_NameQ = mkConstr ty_NameFlavour "NameQ" [] Data.Prefix
con_NameU = mkConstr ty_NameFlavour "NameU" [] Data.Prefix
con_NameL = mkConstr ty_NameFlavour "NameL" [] Data.Prefix
con_NameG = mkConstr ty_NameFlavour "NameG" [] Data.Prefix
Ian Lynagh's avatar
Ian Lynagh committed
601

Ross Paterson's avatar
Ross Paterson committed
602
ty_NameFlavour :: Data.DataType
603 604 605
ty_NameFlavour = mkDataType "Language.Haskell.TH.Syntax.NameFlavour"
                            [con_NameS, con_NameQ, con_NameU,
                             con_NameL, con_NameG]
606

aavogt's avatar
aavogt committed
607 608 609
data NameSpace = VarName	-- ^ Variables
	       | DataName	-- ^ Data constructors 
	       | TcClsName	-- ^ Type constructors and classes; Haskell has them
610
				-- in the same name space for now.
611
	       deriving( Eq, Ord, Data, Typeable )
612 613 614

type Uniq = Int

615
-- | The name without its module prefix
616 617 618
nameBase :: Name -> String
nameBase (Name occ _) = occString occ

619
-- | Module prefix of a name, if it exists
620
nameModule :: Name -> Maybe String
Ian Lynagh's avatar
Ian Lynagh committed
621
nameModule (Name _ (NameQ m))     = Just (modString m)
622
nameModule (Name _ (NameG _ _ m)) = Just (modString m)
Ian Lynagh's avatar
Ian Lynagh committed
623
nameModule _                      = Nothing
624

625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
{- | 
Generate a capturable name. Occurrences of such names will be
resolved according to the Haskell scoping rules at the occurrence
site.

For example:

> f = [| pi + $(varE (mkName "pi")) |]
> ...
> g = let pi = 3 in $f

In this case, @g@ is desugared to

> g = Prelude.pi + 3

Note that @mkName@ may be used with qualified names:

> mkName "Prelude.pi"

See also 'Language.Haskell.TH.Lib.dyn' for a useful combinator. The above example could
be rewritten using 'dyn' as

> f = [| pi + $(dyn "pi") |]
-}
649
mkName :: String -> Name
650
-- The string can have a '.', thus "Foo.baz",
651 652 653 654 655 656
-- giving a dynamically-bound qualified name,
-- in which case we want to generate a NameQ
--
-- Parse the string to see if it has a "." in it
-- so we know whether to generate a qualified or unqualified name
-- It's a bit tricky because we need to parse 
aavogt's avatar
aavogt committed
657 658 659
--
-- > Foo.Baz.x   as    Qual Foo.Baz x
--
660 661 662 663 664
-- So we parse it from back to front
mkName str
  = split [] (reverse str)
  where
    split occ []        = Name (mkOccName occ) NameS
665 666 667 668 669 670 671 672 673
    split occ ('.':rev)	| not (null occ), 
			  not (null rev), head rev /= '.'
			= Name (mkOccName occ) (NameQ (mkModName (reverse rev)))
	-- The 'not (null occ)' guard ensures that
	-- 	mkName "&." = Name "&." NameS
	-- The 'rev' guards ensure that
	--	mkName ".&" = Name ".&" NameS
	--	mkName "Data.Bits..&" = Name ".&" (NameQ "Data.Bits")
	-- This rather bizarre case actually happened; (.&.) is in Data.Bits
674
    split occ (c:rev)   = split (c:occ) rev
675

aavogt's avatar
aavogt committed
676 677
-- | Only used internally
mkNameU :: String -> Uniq -> Name
678 679
mkNameU s (I# u) = Name (mkOccName s) (NameU u)

aavogt's avatar
aavogt committed
680 681
-- | Only used internally
mkNameL :: String -> Uniq -> Name
682 683
mkNameL s (I# u) = Name (mkOccName s) (NameL u)

aavogt's avatar
aavogt committed
684 685 686
-- | Used for 'x etc, but not available to the programmer
mkNameG :: NameSpace -> String -> String -> String -> Name
mkNameG ns pkg modu occ
Ian Lynagh's avatar
Ian Lynagh committed
687
  = Name (mkOccName occ) (NameG ns (mkPkgName pkg) (mkModName modu))
688

689
mkNameG_v, mkNameG_tc, mkNameG_d :: String -> String -> String -> Name
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
mkNameG_v  = mkNameG VarName
mkNameG_tc = mkNameG TcClsName
mkNameG_d  = mkNameG DataName

instance Eq Name where
  v1 == v2 = cmpEq (v1 `compare` v2)

instance Ord Name where
  (Name o1 f1) `compare` (Name o2 f2) = (f1 `compare` f2)   `thenCmp`
				        (o1 `compare` o2)

instance Eq NameFlavour where
  f1 == f2 = cmpEq (f1 `compare` f2)

instance Ord NameFlavour where
705
	-- NameS < NameQ < NameU < NameL < NameG
706
  NameS `compare` NameS = EQ
Ian Lynagh's avatar
Ian Lynagh committed
707
  NameS `compare` _     = LT
708

709 710
  (NameQ _)  `compare` NameS      = GT
  (NameQ m1) `compare` (NameQ m2) = m1 `compare` m2
Ian Lynagh's avatar
Ian Lynagh committed
711
  (NameQ _)  `compare` _          = LT
712 713 714

  (NameU _)  `compare` NameS      = GT
  (NameU _)  `compare` (NameQ _)  = GT
715 716 717
  (NameU u1) `compare` (NameU u2) | u1  <# u2 = LT
				  | u1 ==# u2 = EQ
				  | otherwise = GT
Ian Lynagh's avatar
Ian Lynagh committed
718
  (NameU _)  `compare` _     = LT
719

720 721 722 723 724 725
  (NameL _)  `compare` NameS      = GT
  (NameL _)  `compare` (NameQ _)  = GT
  (NameL _)  `compare` (NameU _)  = GT
  (NameL u1) `compare` (NameL u2) | u1  <# u2 = LT
				  | u1 ==# u2 = EQ
				  | otherwise = GT
Ian Lynagh's avatar
Ian Lynagh committed
726
  (NameL _)  `compare` _          = LT
727

728 729 730
  (NameG ns1 p1 m1) `compare` (NameG ns2 p2 m2) = (ns1 `compare` ns2) `thenCmp`
                                            (p1 `compare` p2) `thenCmp`
					    (m1 `compare` m2) 
Ian Lynagh's avatar
Ian Lynagh committed
731
  (NameG _ _ _)    `compare` _ = GT
732

Ian Lynagh's avatar
Ian Lynagh committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
data NameIs = Alone | Applied | Infix

showName :: Name -> String
showName = showName' Alone

showName' :: NameIs -> Name -> String
showName' ni nm
 = case ni of
       Alone        -> nms
       Applied
        | pnam      -> nms
        | otherwise -> "(" ++ nms ++ ")"
       Infix
        | pnam      -> "`" ++ nms ++ "`"
        | otherwise -> nms
748
    where
749 750 751 752 753
	-- For now, we make the NameQ and NameG print the same, even though
	-- NameQ is a qualified name (so what it means depends on what the
	-- current scope is), and NameG is an original name (so its meaning
	-- should be independent of what's in scope.
	-- We may well want to distinguish them in the end.
754 755
	-- Ditto NameU and NameL
        nms = case nm of
Ian Lynagh's avatar
Ian Lynagh committed
756 757 758 759 760
                    Name occ NameS         -> occString occ
                    Name occ (NameQ m)     -> modString m ++ "." ++ occString occ
                    Name occ (NameG _ _ m) -> modString m ++ "." ++ occString occ
                    Name occ (NameU u)     -> occString occ ++ "_" ++ show (I# u)
                    Name occ (NameL u)     -> occString occ ++ "_" ++ show (I# u)
761 762 763

        pnam = classify nms

Ian Lynagh's avatar
Ian Lynagh committed
764 765
        -- True if we are function style, e.g. f, [], (,)
        -- False if we are operator style, e.g. +, :+
766
        classify "" = False -- shouldn't happen; . operator is handled below
Ian Lynagh's avatar
Ian Lynagh committed
767
        classify (x:xs) | isAlpha x || (x `elem` "_[]()") =
768 769 770 771
                            case dropWhile (/='.') xs of
                                  (_:xs') -> classify xs'
                                  []      -> True
                        | otherwise = False
772

773
instance Show Name where
Ian Lynagh's avatar
Ian Lynagh committed
774
  show = showName
775

776
-- Tuple data and type constructors
777 778 779 780
-- | Tuple data constructor
tupleDataName :: Int -> Name
-- | Tuple type constructor
tupleTypeName :: Int -> Name
781

782
tupleDataName 0 = mk_tup_name 0 DataName
783
tupleDataName 1 = error "tupleDataName 1"
784
tupleDataName n = mk_tup_name (n-1) DataName
785

786
tupleTypeName 0 = mk_tup_name 0 TcClsName
787
tupleTypeName 1 = error "tupleTypeName 1"
788
tupleTypeName n = mk_tup_name (n-1) TcClsName
789

Ian Lynagh's avatar
Ian Lynagh committed
790
mk_tup_name :: Int -> NameSpace -> Name
791
mk_tup_name n_commas space
Ian Lynagh's avatar
Ian Lynagh committed
792
  = Name occ (NameG space (mkPkgName "ghc-prim") tup_mod)
793 794
  where
    occ = mkOccName ('(' : replicate n_commas ',' ++ ")")
Ian Lynagh's avatar
Ian Lynagh committed
795
    tup_mod = mkModName "GHC.Tuple"
796

797
-- Unboxed tuple data and type constructors
798 799 800 801
-- | Unboxed tuple data constructor
unboxedTupleDataName :: Int -> Name
-- | Unboxed tuple type constructor
unboxedTupleTypeName :: Int -> Name
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

unboxedTupleDataName 0 = error "unboxedTupleDataName 0"
unboxedTupleDataName 1 = error "unboxedTupleDataName 1"
unboxedTupleDataName n = mk_unboxed_tup_name (n-1) DataName

unboxedTupleTypeName 0 = error "unboxedTupleTypeName 0"
unboxedTupleTypeName 1 = error "unboxedTupleTypeName 1"
unboxedTupleTypeName n = mk_unboxed_tup_name (n-1) TcClsName

mk_unboxed_tup_name :: Int -> NameSpace -> Name
mk_unboxed_tup_name n_commas space
  = Name occ (NameG space (mkPkgName "ghc-prim") tup_mod)
  where
    occ = mkOccName ("(#" ++ replicate n_commas ',' ++ "#)")
    tup_mod = mkModName "GHC.Tuple"

818

819

820 821 822 823 824 825 826 827 828 829 830
-----------------------------------------------------
--		Locations
-----------------------------------------------------

data Loc
  = Loc { loc_filename :: String
	, loc_package  :: String
	, loc_module   :: String
	, loc_start    :: CharPos
	, loc_end      :: CharPos }

831
type CharPos = (Int, Int)	-- ^ Line and character position
832

833

834 835 836 837 838 839
-----------------------------------------------------
--
--	The Info returned by reification
--
-----------------------------------------------------

aavogt's avatar
aavogt committed
840 841
-- | Obtained from 'reify' in the 'Q' Monad.
data Info
842 843 844 845 846 847 848
  = 
  -- | A class, with a list of its visible instances
  ClassI 
      Dec
      [InstanceDec]
  
  -- | A class method
849
  | ClassOpI
850 851 852 853 854 855
       Name
       Type
       ParentName
       Fixity
  
  -- | A \"plain\" type constructor. \"Fancier\" type constructors are returned using 'PrimTyConI' or 'FamilyI' as appropriate
856 857 858
  | TyConI 
        Dec

859 860
  -- | A type or data family, with a list of its visible instances. A closed
  -- type family is returned with 0 instances.
861
  | FamilyI 
862 863
        Dec
        [InstanceDec]
864 865 866 867 868 869 870 871
  
  -- | A \"primitive\" type constructor, which can't be expressed with a 'Dec'. Examples: @(->)@, @Int#@.
  | PrimTyConI 
       Name
       Arity
       Unlifted
  
  -- | A data constructor
872
  | DataConI 
873 874 875 876
       Name
       Type
       ParentName
       Fixity
877

878 879 880 881 882 883 884 885 886 887
  {- | 
  A \"value\" variable (as opposed to a type variable, see 'TyVarI').
  
  The @Maybe Dec@ field contains @Just@ the declaration which 
  defined the variable -- including the RHS of the declaration -- 
  or else @Nothing@, in the case where the RHS is unavailable to
  the compiler. At present, this value is _always_ @Nothing@:
  returning the RHS has not yet been implemented because of
  lack of interest.
  -}
888
  | VarI 
889 890 891 892
       Name
       Type
       (Maybe Dec)
       Fixity
893

894 895 896 897 898 899 900
  {- | 
  A type variable.
  
  The @Type@ field contains the type which underlies the variable.
  At present, this is always @'VarT' theName@, but future changes
  may permit refinement of this.
  -}
901 902 903
  | TyVarI 	-- Scoped type variable
	Name
	Type	-- What it is bound to
904
  deriving( Show, Data, Typeable )
905

906 907 908 909 910 911 912 913 914 915 916 917
{- | 
In 'ClassOpI' and 'DataConI', name of the parent class or type
-}
type ParentName = Name

-- | In 'PrimTyConI', arity of the type constructor
type Arity = Int

-- | In 'PrimTyConI', is the type constructor unlifted?
type Unlifted = Bool

-- | 'InstanceDec' desribes a single instance of a class or type function.
918
-- It is just a 'Dec', but guaranteed to be one of the following:
919 920 921 922 923 924
--
--   * 'InstanceD' (with empty @['Dec']@)
--
--   * 'DataInstD' or 'NewtypeInstD' (with empty derived @['Name']@)
--
--   * 'TySynInstD'
925
type InstanceDec = Dec
926

927 928 929 930
data Fixity          = Fixity Int FixityDirection
    deriving( Eq, Show, Data, Typeable )
data FixityDirection = InfixL | InfixR | InfixN
    deriving( Eq, Show, Data, Typeable )
931

932
-- | Highest allowed operator precedence for 'Fixity' constructor (answer: 9)
933
maxPrecedence :: Int
934
maxPrecedence = (9::Int)
935

936
-- | Default fixity: @infixl 9@
937
defaultFixity :: Fixity
938 939 940
defaultFixity = Fixity maxPrecedence InfixL


941
{-
942 943
Note [Unresolved infix]
~~~~~~~~~~~~~~~~~~~~~~~
944 945
-}
{- $infix #infix#
946 947 948
When implementing antiquotation for quasiquoters, one often wants
to parse strings into expressions:

949
> parse :: String -> Maybe Exp
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003

But how should we parse @a + b * c@? If we don't know the fixities of
@+@ and @*@, we don't know whether to parse it as @a + (b * c)@ or @(a
+ b) * c@.

In cases like this, use 'UInfixE' or 'UInfixP', which stand for
\"unresolved infix expression\" and \"unresolved infix pattern\". When
the compiler is given a splice containing a tree of @UInfixE@
applications such as

> UInfixE
>   (UInfixE e1 op1 e2)
>   op2
>   (UInfixE e3 op3 e4)

it will look up and the fixities of the relevant operators and
reassociate the tree as necessary.

  * trees will not be reassociated across 'ParensE' or 'ParensP',
    which are of use for parsing expressions like

    > (a + b * c) + d * e

  * 'InfixE' and 'InfixP' expressions are never reassociated.

  * The 'UInfixE' constructor doesn't support sections. Sections
    such as @(a *)@ have no ambiguity, so 'InfixE' suffices. For longer
    sections such as @(a + b * c -)@, use an 'InfixE' constructor for the
    outer-most section, and use 'UInfixE' constructors for all
    other operators:

    > InfixE
    >   Just (UInfixE ...a + b * c...)
    >   op
    >   Nothing

    Sections such as @(a + b +)@ and @((a + b) +)@ should be rendered
    into 'Exp's differently:

    > (+ a + b)   ---> InfixE Nothing + (Just $ UInfixE a + b)
    >                    -- will result in a fixity error if (+) is left-infix
    > (+ (a + b)) ---> InfixE Nothing + (Just $ ParensE $ UInfixE a + b)
    >                    -- no fixity errors

  * Quoted expressions such as

    > [| a * b + c |] :: Q Exp
    > [p| a : b : c |] :: Q Pat

    will never contain 'UInfixE', 'UInfixP', 'ParensE', or 'ParensP'
    constructors.

-}

1004 1005 1006 1007 1008 1009
-----------------------------------------------------
--
--	The main syntax data types
--
-----------------------------------------------------

1010 1011
data Lit = CharL Char 
         | StringL String 
aavogt's avatar
aavogt committed
1012
         | IntegerL Integer     -- ^ Used for overloaded and non-overloaded
1013 1014 1015 1016 1017
                                -- literals. We don't have a good way to
                                -- represent non-overloaded literals at
                                -- the moment. Maybe that doesn't matter?
         | RationalL Rational   -- Ditto
         | IntPrimL Integer
1018
         | WordPrimL Integer
1019 1020
         | FloatPrimL Rational
         | DoublePrimL Rational
reinerp's avatar
reinerp committed
1021
         | StringPrimL [Word8]	-- ^ A primitive C-style string, type Addr#
1022
    deriving( Show, Eq, Data, Typeable )
1023 1024 1025 1026 1027

    -- We could add Int, Float, Double etc, as we do in HsLit, 
    -- but that could complicate the
    -- suppposedly-simple TH.Syntax literal type

aavogt's avatar
aavogt committed
1028
-- | Pattern in Haskell given in @{}@
1029
data Pat 
aavogt's avatar
aavogt committed
1030 1031 1032
  = LitP Lit                      -- ^ @{ 5 or 'c' }@
  | VarP Name                     -- ^ @{ x }@
  | TupP [Pat]                    -- ^ @{ (p1,p2) }@
1033
  | UnboxedTupP [Pat]             -- ^ @{ (# p1,p2 #) }@
aavogt's avatar
aavogt committed
1034 1035
  | ConP Name [Pat]               -- ^ @data T1 = C1 t1 t2; {C1 p1 p1} = e@
  | InfixP Pat Name Pat           -- ^ @foo ({x :+ y}) = e@
1036 1037
  | UInfixP Pat Name Pat          -- ^ @foo ({x :+ y}) = e@
                                  --
1038
                                  -- See "Language.Haskell.TH.Syntax#infix"
1039 1040
  | ParensP Pat                   -- ^ @{(p)}@
                                  --
1041
                                  -- See "Language.Haskell.TH.Syntax#infix"
aavogt's avatar
aavogt committed
1042 1043 1044 1045 1046 1047 1048
  | TildeP Pat                    -- ^ @{ ~p }@
  | BangP Pat                     -- ^ @{ !p }@
  | AsP Name Pat                  -- ^ @{ x \@ p }@
  | WildP                         -- ^ @{ _ }@
  | RecP Name [FieldPat]          -- ^ @f (Pt { pointx = x }) = g x@
  | ListP [ Pat ]                 -- ^ @{ [1,2,3] }@
  | SigP Pat Type                 -- ^ @{ p :: t }@
reinerp's avatar
reinerp committed
1049
  | ViewP Exp Pat                 -- ^ @{ e -> p }@
1050
  deriving( Show, Eq, Data, Typeable )
1051 1052 1053

type FieldPat = (Name,Pat)

aavogt's avatar
aavogt committed
1054
data Match = Match Pat Body [Dec] -- ^ @case e of { pat -> body where decs }@
1055
    deriving( Show, Eq, Data, Typeable )
1056
data Clause = Clause [Pat] Body [Dec]
aavogt's avatar
aavogt committed
1057
                                  -- ^ @f { p1 p2 = body where decs }@
1058
    deriving( Show, Eq, Data, Typeable )
1059 1060
 
data Exp 
aavogt's avatar
aavogt committed
1061 1062 1063 1064
  = VarE Name                          -- ^ @{ x }@
  | ConE Name                          -- ^ @data T1 = C1 t1 t2; p = {C1} e1 e2  @
  | LitE Lit                           -- ^ @{ 5 or 'c'}@
  | AppE Exp Exp                       -- ^ @{ f x }@
1065

aavogt's avatar
aavogt committed
1066
  | InfixE (Maybe Exp) Exp (Maybe Exp) -- ^ @{x + y} or {(x+)} or {(+ x)} or {(+)}@
1067

1068 1069 1070 1071 1072 1073
    -- It's a bit gruesome to use an Exp as the
    -- operator, but how else can we distinguish
    -- constructors from non-constructors?
    -- Maybe there should be a var-or-con type?
    -- Or maybe we should leave it to the String itself?

1074 1075
  | UInfixE Exp Exp Exp                -- ^ @{x + y}@
                                       --
1076
                                       -- See "Language.Haskell.TH.Syntax#infix"
1077 1078
  | ParensE Exp                        -- ^ @{ (e) }@
                                       --
1079
                                       -- See "Language.Haskell.TH.Syntax#infix"
aavogt's avatar
aavogt committed
1080
  | LamE [Pat] Exp                     -- ^ @{ \ p1 p2 -> e }@
1081
  | LamCaseE [Match]                   -- ^ @{ \case m1; m2 }@
aavogt's avatar
aavogt committed
1082
  | TupE [Exp]                         -- ^ @{ (e1,e2) }  @
1083
  | UnboxedTupE [Exp]                  -- ^ @{ (# e1,e2 #) }  @
aavogt's avatar
aavogt committed
1084
  | CondE Exp Exp Exp                  -- ^ @{ if e1 then e2 else e3 }@
1085
  | MultiIfE [(Guard, Exp)]            -- ^ @{ if | g1 -> e1 | g2 -> e2 }@
aavogt's avatar
aavogt committed
1086 1087 1088
  | LetE [Dec] Exp                     -- ^ @{ let x=e1;   y=e2 in e3 }@
  | CaseE Exp [Match]                  -- ^ @{ case e of m1; m2 }@
  | DoE [Stmt]                         -- ^ @{ do { p <- e1; e2 }  }@
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
  | CompE [Stmt]                       -- ^ @{ [ (x,y) | x <- xs, y <- ys ] }@ 
      --
      -- The result expression of the comprehension is
      -- the /last/ of the @'Stmt'@s, and should be a 'NoBindS'.
      --
      -- E.g. translation:
      --
      -- > [ f x | x <- xs ]
      --
      -- > CompE [BindS (VarP x) (VarE xs), NoBindS (AppE (VarE f) (VarE x))]

aavogt's avatar
aavogt committed
1100 1101 1102 1103 1104
  | ArithSeqE Range                    -- ^ @{ [ 1 ,2 .. 10 ] }@
  | ListE [ Exp ]                      -- ^ @{ [1,2,3] }@
  | SigE Exp Type                      -- ^ @{ e :: t }@
  | RecConE Name [FieldExp]            -- ^ @{ T { x = y, z = w } }@
  | RecUpdE Exp [FieldExp]             -- ^ @{ (f x) { z = w } }@
1105
  deriving( Show, Eq, Data, Typeable )
1106 1107 1108 1109 1110 1111

type FieldExp = (Name,Exp)

-- Omitted: implicit parameters

data Body
1112 1113 1114
  = GuardedB [(Guard,Exp)]   -- ^ @f p { | e1 = e2 
                                 --      | e3 = e4 } 
                                 -- where ds@
aavogt's avatar
aavogt committed
1115
  | NormalB Exp              -- ^ @f p { = e } where ds@
1116
  deriving( Show, Eq, Data, Typeable )
1117

1118
data Guard
1119 1120
  = NormalG Exp -- ^ @f x { | odd x } = x@
  | PatG [Stmt] -- ^ @f x { | Just y <- x, Just z <- y } = z@
1121
  deriving( Show, Eq, Data, Typeable )
1122

1123 1124 1125 1126 1127
data Stmt
  = BindS Pat Exp
  | LetS [ Dec ]
  | NoBindS Exp
  | ParS [[Stmt]]
1128
  deriving( Show, Eq, Data, Typeable )
1129 1130 1131

data Range = FromR Exp | FromThenR Exp Exp
           | FromToR Exp Exp | FromThenToR Exp Exp Exp
1132
          deriving( Show, Eq, Data, Typeable )
1133 1134
  
data Dec 
aavogt's avatar
aavogt committed
1135 1136
  = FunD Name [Clause]            -- ^ @{ f p1 p2 = b where decs }@
  | ValD Pat Body [Dec]           -- ^ @{ p = b where decs }@
1137
  | DataD Cxt Name [TyVarBndr] 
aavogt's avatar
aavogt committed
1138 1139
         [Con] [Name]             -- ^ @{ data Cxt x => T x = A x | B (T x)
                                  --       deriving (Z,W)}@
1140
  | NewtypeD Cxt Name [TyVarBndr] 
aavogt's avatar
aavogt committed
1141 1142 1143
         Con [Name]               -- ^ @{ newtype Cxt x => T x = A (B x)
                                  --       deriving (Z,W)}@
  | TySynD Name [TyVarBndr] Type  -- ^ @{ type T x = (x,x) }@
1144
  | ClassD Cxt Name [TyVarBndr] 
aavogt's avatar
aavogt committed
1145 1146 1147 1148
         [FunDep] [Dec]           -- ^ @{ class Eq a => Ord a where ds }@
  | InstanceD Cxt Type [Dec]      -- ^ @{ instance Show w => Show [w]
                                  --       where ds }@
  | SigD Name Type                -- ^ @{ length :: [a] -> Int }@
1149 1150
  | ForeignD Foreign              -- ^ @{ foreign import ... }
                                  --{ foreign export ... }@
aavogt's avatar
aavogt committed
1151

1152 1153