ghc-source-gen-0.4.3.0: Constructs Haskell syntax trees for the GHC API.
Safe HaskellNone
LanguageHaskell2010

GHC.SourceGen.Binds

Description

This module provides combinators for constructing Haskell declarations.

Synopsis

Bindings

class HasValBind t Source #

Syntax types which can declare/define functions. For example: declarations, or the body of a class declaration or class instance.

To declare the type of a function or value, use typeSig or typeSigs.

To define a function, use funBind or funBinds.

To define a value, use valBind or valBindGuarded.

Minimal complete definition

sigB, bindB

Instances

Instances details
HasValBind HsDecl' Source # 
Instance details

Defined in GHC.SourceGen.Binds.Internal

Methods

sigB :: Sig' -> HsDecl'

bindB :: HsBind' -> HsDecl'

HasValBind RawValBind Source # 
Instance details

Defined in GHC.SourceGen.Binds.Internal

Methods

sigB :: Sig' -> RawValBind

bindB :: HsBind' -> RawValBind

HasValBind RawInstDecl Source # 
Instance details

Defined in GHC.SourceGen.Decl

Methods

sigB :: Sig' -> RawInstDecl

bindB :: HsBind' -> RawInstDecl

HasValBind ClassDecl Source # 
Instance details

Defined in GHC.SourceGen.Decl

Methods

sigB :: Sig' -> ClassDecl

bindB :: HsBind' -> ClassDecl

Type signatures

typeSig :: HasValBind t => OccNameStr -> HsType' -> t Source #

Declares the type of a single function or value.

f :: A
=====
typeSig "f" (var "A")

typeSigs :: HasValBind t => [OccNameStr] -> HsType' -> t Source #

Declares the type of multiple functions or values.

f, g :: A
=====
typeSigs ["f", "g"] (var "A")

Functions

funBind :: HasValBind t => OccNameStr -> RawMatch -> t Source #

Defines a function that has a single case.

f = x
=====
funBind "f" (match [] "x")
id x = x
=====
funBind "id" $ match [bvar "x"] (var "x")

funBinds :: HasValBind t => OccNameStr -> [RawMatch] -> t Source #

Defines a function or value.

f = x
=====
funBinds "f" [match [] "x"]
id x = x
=====
funBinds "id" [match [var "x"] (var "x")]
not True = False
not False = True
=====
funBinds "not"
  [ match [conP "True" []] (var "False")
  , match [conP "False" []] (var "True")
  ]

funBindsWithFixity :: HasValBind t => Maybe LexicalFixity -> OccNameStr -> [RawMatch] -> t Source #

Defines a function or value, with an explicit fixity. When given Nothing, use infix notation iff the given name is symbolic.

id x = x
=====
funBindsWithFixity (Just Prefix) "id" [match [var "x"] (var "x")]
True && True = True
True && False = False
=====
funBindsWithFixity Nothing "not"
  [ match [conP "True" []] (var "False")
  , match [conP "False" []] (var "True")
  ]

Values

valBind :: HasValBind t => OccNameStr -> HsExpr' -> t Source #

Defines a value without any guards.

The resulting syntax is the same as a function with no arguments.

x = y
=====
valBind "x" $ var "y"

valBindGRHSs :: HasValBind t => OccNameStr -> RawGRHSs -> t Source #

Defines a value consisting of multiple guards.

The resulting syntax is the same as a function with no arguments.

x
  | test = 1
  | otherwise = 2
=====
valBindGRHSs "x"
  $ guardedRhs
      [ var "test" `guard` int 1
      , var "otherwise" `guard` int 2
      ]

Patterns

class HasValBind t => HasPatBind t Source #

Syntax types which can declare/define pattern bindings. For example: declarations at the top-level or in let/where clauses.

Note: this class is more restrictive than HasValBind since pattern bindings cannot be used in class or instance declarations.

Instances

Instances details
HasPatBind HsDecl' Source # 
Instance details

Defined in GHC.SourceGen.Binds

HasPatBind RawValBind Source # 
Instance details

Defined in GHC.SourceGen.Binds

patBind :: HasPatBind t => Pat' -> HsExpr' -> t Source #

Defines a pattern binding without any guards.

(x, y) = e
=====
patBind (tuple [bvar "x", bvar "y"]) e

patBindGRHSs :: HasPatBind t => Pat' -> RawGRHSs -> t Source #

Defines a pattern binding consisting of multiple guards.

(x, y)
  | test = (1, 2)
  | otherwise = (2, 3)
=====
patBindGrhs (tuple [bvar "x", bvar "y"])
  $ guardedRhs
      [ var "test" `guard` tuple [int 1, int 2]
      , var "otherwise" `guard` [int 2, int 3]
      ]

Matches

A function definition is made up of one or more RawMatch terms. Each RawMatch corresponds to a single pattern match. For example, to define the "not" function:

not True = False
not False = True

We could using a list of two RawMatches:

funBinds "not"
  [ match [conP "True" []] (var "False")
  , match [conP "False" [] (var "True")
  ]

A match may consist of one or more guarded expressions. For example, to define the function as:

not x
  | x = False
  | otherwise = True

We would say:

funBind "not"
     $ matchGRHSs [bvar "x"] $ guardedRhs
         [ guard (var "x") (var "False")
         , guard (var "otherwise") (var "True")
         ]

data RawMatch Source #

A single function pattern match, including an optional "where" clause.

For example:

f x
   | cond = y
   | otherwise = z
 where
   y = ...
   z = ...

match :: [Pat'] -> HsExpr' -> RawMatch Source #

A function match with a single case.

matchGRHSs :: [Pat'] -> RawGRHSs -> RawMatch Source #

A function match consisting of multiple guards.

Right-hand sides

data RawGRHSs Source #

A set of match guards plus an optional "where" clause.

This type is used in matches and in multi-way if expressions.

For example:

   | cond = y
   | otherwise = z
 where
   y = ...
   z = ...

rhs :: HsExpr' -> RawGRHSs Source #

A right-hand side of a match, with no guards.

Guards

guardedRhs :: [GuardedExpr] -> RawGRHSs Source #

A guarded right-hand side of a match.

  | x = False
  | otherwise = True
=====
guardedRhs
  [ guard (var "x") (var "False")
  , guard (var "otherwise") (var "True")
  ]

type GuardedExpr = GRHS' LHsExpr' Source #

An expression with a single guard.

For example:

| otherwise = ()

guards :: [Stmt'] -> HsExpr' -> GuardedExpr Source #

An expression guarded by multiple statements, using the PatternGuards extension.

  | Just y <- x, y = ()
=====
guards [conP "Just" (bvar "x") <-- var "y", bvar "x"] unit

guard :: HsExpr' -> HsExpr' -> GuardedExpr Source #

An expression guarded by a single boolean statement.

  | otherwise = ()
=====
guard (var "otherwise") unit

Where clauses

where' :: RawGRHSs -> [RawValBind] -> RawGRHSs Source #

Adds a "where" clause to an existing RawGRHSs.

f x = y
  where y = x
=====
funBind "x"
  $ matchGRHSs [bvar "x"]
  $ rhs (var "y")
     `where` [valBind "y" $ var "x']

data RawValBind Source #

A binding definition inside of a let or where clause.

RawValBind definitions may be constructed using its instance of HasValBind. For more details, see the documentation of that function, and of GHC.SourceGen.Binds overall.

Instances

Instances details
HasValBind RawValBind Source # 
Instance details

Defined in GHC.SourceGen.Binds.Internal

Methods

sigB :: Sig' -> RawValBind

bindB :: HsBind' -> RawValBind

HasPatBind RawValBind Source # 
Instance details

Defined in GHC.SourceGen.Binds

Statements

stmt :: HsExpr' -> Stmt' Source #

An expression statement. May be used in a do expression (with do') or in a match (with guard).

TODO: also allow using statements in list comprehensions.

(<--) :: Pat' -> HsExpr' -> Stmt' infixl 1 Source #

A statement that binds a pattern.

x <- act
=====
bvar "x" <-- var "act"