HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleghc-source-gen-0.4.6.0Haskell2010

GHC.SourceGen.Binds

This module provides combinators for constructing Haskell declarations.

  • 6 types
  • 2 classes
  • 18 values

Bindings

2 declarations
classclass HasValBind t where
#

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 GHC.SourceGen.Binds.valBindGuarded.

Instances4HasValBind

Type signatures

2 declarations

Functions

3 declarations
valuefunBind :: HasValBind t => OccNameStr -> RawMatch -> t
#

Defines a function that has a single case.

f = x
=====
funBind "f" (match [] "x")
id x = x
=====
funBind "id" $ match [bvar "x"] (var "x")
valuefunBinds :: HasValBind t => OccNameStr -> [RawMatch] -> t
#

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")
  ]

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

2 declarations
valuevalBind :: HasValBind t => OccNameStr -> HsExpr' -> t
#

Defines a value without any guards.

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

x = y
=====
valBind "x" $ var "y"
valuevalBindGRHSs :: HasValBind t => OccNameStr -> RawGRHSs -> t
#

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

classclass HasValBind t => HasPatBind t
#

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.

Instances2HasPatBind
valuepatBind :: HasPatBind t => Pat' -> HsExpr' -> t
#

Defines a pattern binding without any guards.

(x, y) = e
=====
patBind (tuple [bvar "x", bvar "y"]) e
valuepatBindGRHSs :: HasPatBind t => Pat' -> RawGRHSs -> t
#

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

3 declarations

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")
         ]
datadata RawMatch
#

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

For example:

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

Right-hand sides

2 declarations
datadata RawGRHSs
#

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 = ...

Guards

valueguardedRhs :: [GuardedExpr] -> RawGRHSs
#

A guarded right-hand side of a match.

  | x = False
  | otherwise = True
=====
guardedRhs
  [ guard (var "x") (var "False")
  , guard (var "otherwise") (var "True")
  ]
typetype GuardedExpr = GRHS' LHsExpr'
#

An expression with a single guard.

For example:

| otherwise = ()
valueguards :: [Stmt'] -> HsExpr' -> GuardedExpr
#

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

  | Just y <- x, y = ()
=====
guards [conP "Just" (bvar "x") <-- var "y", bvar "x"] unit
valueguard :: HsExpr' -> HsExpr' -> GuardedExpr
#

An expression guarded by a single boolean statement.

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

Where clauses

valuewhere' :: RawGRHSs -> [RawValBind] -> RawGRHSs
#

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']

Statements

2 declarations
valuestmt :: HsExpr' -> Stmt'
#

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.

value(<--) :: Pat' -> HsExpr' -> Stmt'
#

A statement that binds a pattern.

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