haskell-src-1.0.1.5: Support for manipulating Haskell source code

Portability | portable
Stability | experimental
Maintainer | libraries@haskell.org
Safe Haskell | None

Language.Haskell.Syntax

Contents

- Modules
- Declarations
- Class Assertions and Contexts
- Types
- Expressions
- Patterns
- Literals
- Variables, Constructors and Operators
- Builtin names
  - Modules
  - Main function of a program
  - Constructors
  - Type constructors
- Source coordinates

Description

A suite of datatypes describing the abstract syntax of Haskell 98 http://www.haskell.org/onlinereport/ plus a few extensions:

- multi-parameter type classes
- parameters of type class assertions are unrestricted

This module has been changed so that show is a real show. For GHC, we also derive Typeable and Data for all types.

Synopsis

- data HsModule = HsModule SrcLoc Module (Maybe [HsExportSpec]) [HsImportDecl] [HsDecl]
- data HsExportSpec
  - = HsEVar HsQName
  - | HsEAbs HsQName
  - | HsEThingAll HsQName
  - | HsEThingWith HsQName [HsCName]
  - | HsEModuleContents Module
- data HsImportDecl = HsImportDecl {
  - importLoc :: SrcLoc
  - importModule :: Module
  - importQualified :: Bool
  - importAs :: Maybe Module
  - importSpecs :: Maybe (Bool, [HsImportSpec])
  }
- data HsImportSpec
  - = HsIVar HsName
  - | HsIAbs HsName
  - | HsIThingAll HsName
  - | HsIThingWith HsName [HsCName]
- data HsAssoc
  - = HsAssocNone
  - | HsAssocLeft
  - | HsAssocRight
- data HsDecl
  - = HsTypeDecl SrcLoc HsName [HsName] HsType
  - | HsDataDecl SrcLoc HsContext HsName [HsName] [HsConDecl] [HsQName]
  - | HsInfixDecl SrcLoc HsAssoc Int [HsOp]
  - | HsNewTypeDecl SrcLoc HsContext HsName [HsName] HsConDecl [HsQName]
  - | HsClassDecl SrcLoc HsContext HsName [HsName] [HsDecl]
  - | HsInstDecl SrcLoc HsContext HsQName [HsType] [HsDecl]
  - | HsDefaultDecl SrcLoc [HsType]
  - | HsTypeSig SrcLoc [HsName] HsQualType
  - | HsFunBind [HsMatch]
  - | HsPatBind SrcLoc HsPat HsRhs [HsDecl]
  - | HsForeignImport SrcLoc String HsSafety String HsName HsType
  - | HsForeignExport SrcLoc String String HsName HsType
- data HsConDecl
  - = HsConDecl SrcLoc HsName [HsBangType]
  - | HsRecDecl SrcLoc HsName [([HsName], HsBangType)]
- data HsBangType
  - = HsBangedTy HsType
  - | HsUnBangedTy HsType
- data HsMatch = HsMatch SrcLoc HsName [HsPat] HsRhs [HsDecl]
- data HsRhs
  - = HsUnGuardedRhs HsExp
  - | HsGuardedRhss [HsGuardedRhs]
- data HsGuardedRhs = HsGuardedRhs SrcLoc HsExp HsExp
- data HsSafety
  - = HsSafe
  - | HsUnsafe
- data HsQualType = HsQualType HsContext HsType
- type HsContext = [HsAsst]
- type HsAsst = (HsQName, [HsType])
- data HsType
  - = HsTyFun HsType HsType
  - | HsTyTuple [HsType]
  - | HsTyApp HsType HsType
  - | HsTyVar HsName
  - | HsTyCon HsQName
- data HsExp
  - = HsVar HsQName
  - | HsCon HsQName
  - | HsLit HsLiteral
  - | HsInfixApp HsExp HsQOp HsExp
  - | HsApp HsExp HsExp
  - | HsNegApp HsExp
  - | HsLambda SrcLoc [HsPat] HsExp
  - | HsLet [HsDecl] HsExp
  - | HsIf HsExp HsExp HsExp
  - | HsCase HsExp [HsAlt]
  - | HsDo [HsStmt]
  - | HsTuple [HsExp]
  - | HsList [HsExp]
  - | HsParen HsExp
  - | HsLeftSection HsExp HsQOp
  - | HsRightSection HsQOp HsExp
  - | HsRecConstr HsQName [HsFieldUpdate]
  - | HsRecUpdate HsExp [HsFieldUpdate]
  - | HsEnumFrom HsExp
  - | HsEnumFromTo HsExp HsExp
  - | HsEnumFromThen HsExp HsExp
  - | HsEnumFromThenTo HsExp HsExp HsExp
  - | HsListComp HsExp [HsStmt]
  - | HsExpTypeSig SrcLoc HsExp HsQualType
  - | HsAsPat HsName HsExp
  - | HsWildCard
  - | HsIrrPat HsExp
- data HsStmt
  - = HsGenerator SrcLoc HsPat HsExp
  - | HsQualifier HsExp
  - | HsLetStmt [HsDecl]
- data HsFieldUpdate = HsFieldUpdate HsQName HsExp
- data HsAlt = HsAlt SrcLoc HsPat HsGuardedAlts [HsDecl]
- data HsGuardedAlts
  - = HsUnGuardedAlt HsExp
  - | HsGuardedAlts [HsGuardedAlt]
- data HsGuardedAlt = HsGuardedAlt SrcLoc HsExp HsExp
- data HsPat
  - = HsPVar HsName
  - | HsPLit HsLiteral
  - | HsPNeg HsPat
  - | HsPInfixApp HsPat HsQName HsPat
  - | HsPApp HsQName [HsPat]
  - | HsPTuple [HsPat]
  - | HsPList [HsPat]
  - | HsPParen HsPat
  - | HsPRec HsQName [HsPatField]
  - | HsPAsPat HsName HsPat
  - | HsPWildCard
  - | HsPIrrPat HsPat
- data HsPatField = HsPFieldPat HsQName HsPat
- data HsLiteral
  - = HsChar Char
  - | HsString String
  - | HsInt Integer
  - | HsFrac Rational
  - | HsCharPrim Char
  - | HsStringPrim String
  - | HsIntPrim Integer
  - | HsFloatPrim Rational
  - | HsDoublePrim Rational
- newtype Module = Module String
- data HsQName
  - = Qual Module HsName
  - | UnQual HsName
  - | Special HsSpecialCon
- data HsName
  - = HsIdent String
  - | HsSymbol String
- data HsQOp
  - = HsQVarOp HsQName
  - | HsQConOp HsQName
- data HsOp
  - = HsVarOp HsName
  - | HsConOp HsName
- data HsSpecialCon
  - = HsUnitCon
  - | HsListCon
  - | HsFunCon
  - | HsTupleCon Int
  - | HsCons
- data HsCName
  - = HsVarName HsName
  - | HsConName HsName
- prelude_mod :: Module
- main_mod :: Module
- main_name :: HsName
- unit_con_name :: HsQName
- tuple_con_name :: Int -> HsQName
- list_cons_name :: HsQName
- unit_con :: HsExp
- tuple_con :: Int -> HsExp
- unit_tycon_name :: HsQName
- fun_tycon_name :: HsQName
- list_tycon_name :: HsQName
- tuple_tycon_name :: Int -> HsQName
- unit_tycon :: HsType
- fun_tycon :: HsType
- list_tycon :: HsType
- tuple_tycon :: Int -> HsType
- data SrcLoc = SrcLoc {
  - srcFilename :: String
  - srcLine :: Int
  - srcColumn :: Int
  }

# Modules

data HsModule Source

A Haskell source module.

Constructors

HsModule SrcLoc Module (Maybe [HsExportSpec]) [HsImportDecl] [HsDecl] |

Instances

Data HsModule |
Show HsModule |
Typeable HsModule |
Pretty HsModule |

data HsExportSpec Source

Export specification.

Constructors

HsEVar HsQName | variable
HsEAbs HsQName | `T`: a class or datatype exported abstractly, or a type synonym.
HsEThingAll HsQName | `T(..)`: a class exported with all of its methods, or a datatype exported with all of its constructors.
HsEThingWith HsQName [HsCName] | `T(C_1,...,C_n)`: a class exported with some of its methods, or a datatype exported with some of its constructors.
HsEModuleContents Module | `module M`: re-export a module.

Instances

Eq HsExportSpec |
Data HsExportSpec |
Show HsExportSpec |
Typeable HsExportSpec |
Pretty HsExportSpec |

data HsImportDecl Source

Import declaration.

Constructors

HsImportDecl |
Fields importLoc :: SrcLoc position of the `import` keyword. importModule :: Module name of the module imported. importQualified :: Bool imported `qualified`? importAs :: Maybe Module optional alias name in an `as` clause. importSpecs :: Maybe (Bool, [HsImportSpec]) optional list of import specifications. The `Bool` is `True` if the names are excluded by `hiding`.

Instances

Eq HsImportDecl |
Data HsImportDecl |
Show HsImportDecl |
Typeable HsImportDecl |
Pretty HsImportDecl |

data HsImportSpec Source

Import specification.

Constructors

HsIVar HsName | variable
HsIAbs HsName | `T`: the name of a class, datatype or type synonym.
HsIThingAll HsName | `T(..)`: a class imported with all of its methods, or a datatype imported with all of its constructors.
HsIThingWith HsName [HsCName] | `T(C_1,...,C_n)`: a class imported with some of its methods, or a datatype imported with some of its constructors.

Instances

Eq HsImportSpec |
Data HsImportSpec |
Show HsImportSpec |
Typeable HsImportSpec |
Pretty HsImportSpec |

data HsAssoc Source

Associativity of an operator.

Constructors

HsAssocNone | non-associative operator (declared with `infix`)
HsAssocLeft | left-associative operator (declared with `infixl`).
HsAssocRight | right-associative operator (declared with `infixr`)

Instances

Eq HsAssoc |
Data HsAssoc |
Show HsAssoc |
Typeable HsAssoc |
Pretty HsAssoc |

# Declarations

data HsDecl Source

Constructors

HsTypeDecl SrcLoc HsName [HsName] HsType |
HsDataDecl SrcLoc HsContext HsName [HsName] [HsConDecl] [HsQName] |
HsInfixDecl SrcLoc HsAssoc Int [HsOp] |
HsNewTypeDecl SrcLoc HsContext HsName [HsName] HsConDecl [HsQName] |
HsClassDecl SrcLoc HsContext HsName [HsName] [HsDecl] |
HsInstDecl SrcLoc HsContext HsQName [HsType] [HsDecl] |
HsDefaultDecl SrcLoc [HsType] |
HsTypeSig SrcLoc [HsName] HsQualType |
HsFunBind [HsMatch] |
HsPatBind SrcLoc HsPat HsRhs [HsDecl] |
HsForeignImport SrcLoc String HsSafety String HsName HsType |
HsForeignExport SrcLoc String String HsName HsType |

Instances

Eq HsDecl |
Data HsDecl |
Show HsDecl |
Typeable HsDecl |
Pretty HsDecl |

data HsConDecl Source

Declaration of a data constructor.

Constructors

HsConDecl SrcLoc HsName [HsBangType] | ordinary data constructor
HsRecDecl SrcLoc HsName [([HsName], HsBangType)] | record constructor

Instances

Eq HsConDecl |
Data HsConDecl |
Show HsConDecl |
Typeable HsConDecl |
Pretty HsConDecl |

data HsBangType Source

The type of a constructor argument or field, optionally including a strictness annotation.

Constructors

HsBangedTy HsType | strict component, marked with "`!`"
HsUnBangedTy HsType | non-strict component

Instances

Eq HsBangType |
Data HsBangType |
Show HsBangType |
Typeable HsBangType |
Pretty HsBangType |

data HsMatch Source

Clauses of a function binding.

Constructors

HsMatch SrcLoc HsName [HsPat] HsRhs [HsDecl] |

Instances

Eq HsMatch |
Data HsMatch |
Show HsMatch |
Typeable HsMatch |
Pretty HsMatch |

data HsRhs Source

The right hand side of a function or pattern binding.

Constructors

HsUnGuardedRhs HsExp | unguarded right hand side (exp)
HsGuardedRhss [HsGuardedRhs] | guarded right hand side (gdrhs)

Instances

Eq HsRhs |
Data HsRhs |
Show HsRhs |
Typeable HsRhs |
Pretty HsRhs |

data HsGuardedRhs Source

A guarded right hand side `|` exp `=` exp. The first expression will be Boolean-valued.

Constructors

HsGuardedRhs SrcLoc HsExp HsExp |

Instances

Eq HsGuardedRhs |
Data HsGuardedRhs |
Show HsGuardedRhs |
Typeable HsGuardedRhs |
Pretty HsGuardedRhs |

data HsSafety Source

Safety level for invoking a foreign entity

Constructors

HsSafe | call may generate callbacks
HsUnsafe | call will not generate callbacks

Instances

Eq HsSafety |
Data HsSafety |
Ord HsSafety |
Show HsSafety |
Typeable HsSafety |
Pretty HsSafety |

# Class Assertions and Contexts

data HsQualType Source

A type qualified with a context. An unqualified type has an empty context.

Constructors

HsQualType HsContext HsType |

Instances

Eq HsQualType |
Data HsQualType |
Show HsQualType |
Typeable HsQualType |
Pretty HsQualType |

type HsContext = [HsAsst]Source

type HsAsst = (HsQName, [HsType])Source

Class assertions. In Haskell 98, the argument would be a tyvar, but this definition allows multiple parameters, and allows them to be types.

# Types

data HsType Source

Haskell types and type constructors.

Constructors

HsTyFun HsType HsType | function type
HsTyTuple [HsType] | tuple type
HsTyApp HsType HsType | application of a type constructor
HsTyVar HsName | type variable
HsTyCon HsQName | named type or type constructor

Instances

Eq HsType |
Data HsType |
Show HsType |
Typeable HsType |
Pretty HsType |

# Expressions

data HsExp Source

Haskell expressions.

Notes:

- Because it is difficult for parsers to distinguish patterns from expressions, they typically parse them in the same way and then check that they have the appropriate form. Hence the expression type includes some forms that are found only in patterns. After these checks, these constructors should not be used.
- The parser does not take precedence and associativity into account, so it will leave `HsInfixApp`s associated to the left.
- The `Pretty` instance for `HsExp` does not add parentheses in printing.

Constructors

HsVar HsQName | variable
HsCon HsQName | data constructor
HsLit HsLiteral | literal constant
HsInfixApp HsExp HsQOp HsExp | infix application
HsApp HsExp HsExp | ordinary application
HsNegApp HsExp | negation expression `-` exp
HsLambda SrcLoc [HsPat] HsExp | lambda expression
HsLet [HsDecl] HsExp | local declarations with `let`
HsIf HsExp HsExp HsExp | `if` exp `then` exp `else` exp
HsCase HsExp [HsAlt] | `case` exp `of` alts
HsDo [HsStmt] | `do`-expression: the last statement in the list should be an expression.
HsTuple [HsExp] | tuple expression
HsList [HsExp] | list expression
HsParen HsExp | parenthesized expression
HsLeftSection HsExp HsQOp | left section `(`exp qop`)`
HsRightSection HsQOp HsExp | right section `(`qop exp`)`
HsRecConstr HsQName [HsFieldUpdate] | record construction expression
HsRecUpdate HsExp [HsFieldUpdate] | record update expression
HsEnumFrom HsExp | unbounded arithmetic sequence, incrementing by 1
HsEnumFromTo HsExp HsExp | bounded arithmetic sequence, incrementing by 1
HsEnumFromThen HsExp HsExp | unbounded arithmetic sequence, with first two elements given
HsEnumFromThenTo HsExp HsExp HsExp | bounded arithmetic sequence, with first two elements given
HsListComp HsExp [HsStmt] | list comprehension
HsExpTypeSig SrcLoc HsExp HsQualType | expression type signature
HsAsPat HsName HsExp | patterns only
HsWildCard | patterns only
HsIrrPat HsExp | patterns only

Instances

Eq HsExp |
Data HsExp |
Show HsExp |
Typeable HsExp |
Pretty HsExp |

data HsStmt Source

This type represents both stmt in a `do`-expression, and qual in a list comprehension.

Constructors

HsGenerator SrcLoc HsPat HsExp | a generator pat `<-` exp
HsQualifier HsExp | an exp by itself: in a `do`-expression, an action whose result is discarded; in a list comprehension, a guard expression
HsLetStmt [HsDecl] | local bindings

Instances

Eq HsStmt |
Data HsStmt |
Show HsStmt |
Typeable HsStmt |
Pretty HsStmt |

data HsFieldUpdate Source

An fbind in a labeled record construction or update expression.

Constructors

HsFieldUpdate HsQName HsExp |

Instances

Eq HsFieldUpdate |
Data HsFieldUpdate |
Show HsFieldUpdate |
Typeable HsFieldUpdate |
Pretty HsFieldUpdate |

data HsAlt Source

An alt in a `case` expression.

Constructors

HsAlt SrcLoc HsPat HsGuardedAlts [HsDecl] |

Instances

Eq HsAlt |
Data HsAlt |
Show HsAlt |
Typeable HsAlt |
Pretty HsAlt |

data HsGuardedAlts Source

Constructors

HsUnGuardedAlt HsExp | `->` exp
HsGuardedAlts [HsGuardedAlt] | gdpat

Instances

Eq HsGuardedAlts |
Data HsGuardedAlts |
Show HsGuardedAlts |
Typeable HsGuardedAlts |
Pretty HsGuardedAlts |

data HsGuardedAlt Source

A guarded alternative `|` exp `->` exp. The first expression will be Boolean-valued.

Constructors

HsGuardedAlt SrcLoc HsExp HsExp |

Instances

Eq HsGuardedAlt |
Data HsGuardedAlt |
Show HsGuardedAlt |
Typeable HsGuardedAlt |
Pretty HsGuardedAlt |

# Patterns

data HsPat Source

A pattern, to be matched against a value.

Constructors

HsPVar HsName | variable
HsPLit HsLiteral | literal constant
HsPNeg HsPat | negated pattern
HsPInfixApp HsPat HsQName HsPat | pattern with infix data constructor
HsPApp HsQName [HsPat] | data constructor and argument patterns
HsPTuple [HsPat] | tuple pattern
HsPList [HsPat] | list pattern
HsPParen HsPat | parenthesized pattern
HsPRec HsQName [HsPatField] | labelled pattern
HsPAsPat HsName HsPat | `@`-pattern
HsPWildCard | wildcard pattern (`_`)
HsPIrrPat HsPat | irrefutable pattern (`~`)

Instances

Eq HsPat |
Data HsPat |
Show HsPat |
Typeable HsPat |
Pretty HsPat |

data HsPatField Source

An fpat in a labeled record pattern.

Constructors

HsPFieldPat HsQName HsPat |

Instances

Eq HsPatField |
Data HsPatField |
Show HsPatField |
Typeable HsPatField |
Pretty HsPatField |

# Literals

data HsLiteral Source

literal. Values of this type hold the abstract value of the literal, not the precise string representation used. For example, `10`, `0o12` and `0xa` have the same representation.

Constructors

HsChar Char | character literal
HsString String | string literal
HsInt Integer | integer literal
HsFrac Rational | floating point literal
HsCharPrim Char | GHC unboxed character literal
HsStringPrim String | GHC unboxed string literal
HsIntPrim Integer | GHC unboxed integer literal
HsFloatPrim Rational | GHC unboxed float literal
HsDoublePrim Rational | GHC unboxed double literal

Instances

Eq HsLiteral |
Data HsLiteral |
Show HsLiteral |
Typeable HsLiteral |
Pretty HsLiteral |

# Variables, Constructors and Operators

newtype Module Source

The name of a Haskell module.

Constructors

Module String |

Instances

Eq Module |
Data Module |
Ord Module |
Show Module |
Typeable Module |
Pretty Module |

data HsQName Source

This type is used to represent qualified variables, and also qualified constructors.

Constructors

Qual Module HsName | name qualified with a module name
UnQual HsName | unqualified name
Special HsSpecialCon | built-in constructor with special syntax

Instances

Eq HsQName |
Data HsQName |
Ord HsQName |
Show HsQName |
Typeable HsQName |
Pretty HsQName |

data HsName Source

This type is used to represent variables, and also constructors.

Constructors

HsIdent String | varid or conid
HsSymbol String | varsym or consym

Instances

Eq HsName |
Data HsName |
Ord HsName |
Show HsName |
Typeable HsName |
Pretty HsName |

data HsQOp Source

Possibly qualified infix operators (qop), appearing in expressions.

Constructors

HsQVarOp HsQName | variable operator (qvarop)
HsQConOp HsQName | constructor operator (qconop)

Instances

Eq HsQOp |
Data HsQOp |
Ord HsQOp |
Show HsQOp |
Typeable HsQOp |
Pretty HsQOp |

data HsOp Source

Operators, appearing in `infix` declarations.

Constructors

HsVarOp HsName | variable operator (varop)
HsConOp HsName | constructor operator (conop)

Instances

Eq HsOp |
Data HsOp |
Ord HsOp |
Show HsOp |
Typeable HsOp |
Pretty HsOp |

data HsSpecialCon Source

Constructors with special syntax. These names are never qualified, and always refer to builtin type or data constructors.

Constructors

HsUnitCon | unit type and data constructor `()`
HsListCon | list type constructor `[]`
HsFunCon | function type constructor `->`
HsTupleCon Int | n-ary tuple type and data constructors `(,)` etc
HsCons | list data constructor `(:)`

Instances

Eq HsSpecialCon |
Data HsSpecialCon |
Ord HsSpecialCon |
Show HsSpecialCon |
Typeable HsSpecialCon |

data HsCName Source

A name (cname) of a component of a class or data type in an `import` or export specification.

Constructors

HsVarName HsName | name of a method or field
HsConName HsName | name of a data constructor

Instances

Eq HsCName |
Data HsCName |
Ord HsCName |
Show HsCName |
Typeable HsCName |
Pretty HsCName |

# Builtin names

## Modules

prelude_mod :: Module Source

main_mod :: Module Source

## Main function of a program

main_name :: HsName Source

## Constructors

unit_con_name :: HsQName Source

tuple_con_name :: Int -> HsQName Source

list_cons_name :: HsQName Source

unit_con :: HsExp Source

tuple_con :: Int -> HsExp Source

## Type constructors

unit_tycon_name :: HsQName Source

fun_tycon_name :: HsQName Source

list_tycon_name :: HsQName Source

tuple_tycon_name :: Int -> HsQName Source

unit_tycon :: HsType Source

fun_tycon :: HsType Source

list_tycon :: HsType Source

tuple_tycon :: Int -> HsType Source

# Source coordinates

data SrcLoc Source

A position in the source.

Constructors

SrcLoc |
Fields srcFilename :: String srcLine :: Int srcColumn :: Int

Instances

Eq SrcLoc |
Data SrcLoc |
Ord SrcLoc |
Show SrcLoc |
Typeable SrcLoc |