Moduleghc-source-gen-0.4.6.0Haskell2010
GHC.SourceGen.Binds
This module provides combinators for constructing Haskell declarations.
- 6 types
- 2 classes
- 18 values
- Packageghc-source-gen-0.4.6.0
- Exports26
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceBinds.hs
Bindings
2 declarationsSyntax 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
HasValBind RawValBindDefined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.Binds.InternalHasValBind ClassDeclDefined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.DeclHasValBind RawInstDeclDefined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.DeclHasValBind HsDecl'Defined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.Binds.Internal
Type signatures
2 declarationsDeclares the type of a single function or value.
f :: A
=====
typeSig "f" (var "A")Declares the type of multiple functions or values.
f, g :: A
=====
typeSigs ["f", "g"] (var "A")Functions
3 declarationsDefines a function that has a single case.
f = x
=====
funBind "f" (match [] "x")id x = x
=====
funBind "id" $ match [bvar "x"] (var "x")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 declarationsDefines a value without any guards.
The resulting syntax is the same as a function with no arguments.
x = y
=====
valBind "x" $ var "y"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
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
HasPatBind RawValBindDefined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.BindsHasPatBind HsDecl'Defined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.Binds
Defines a pattern binding without any guards.
(x, y) = e
=====
patBind (tuple [bvar "x", bvar "y"]) eDefines 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 declarationsA 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 = TrueWe 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 = TrueWe would say:
funBind "not"
$ matchGRHSs [bvar "x"] $ guardedRhs
[ guard (var "x") (var "False")
, guard (var "otherwise") (var "True")
]A single function pattern match, including an optional "where" clause.
For example:
f x
| cond = y
| otherwise = z
where
y = ...
z = ...A function match with a single case.
A function match consisting of multiple guards.
Right-hand sides
2 declarationsA 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 = ...A right-hand side of a match, with no guards.
Guards
A guarded right-hand side of a match.
| x = False
| otherwise = True
=====
guardedRhs
[ guard (var "x") (var "False")
, guard (var "otherwise") (var "True")
]An expression with a single guard.
For example:
| otherwise = ()An expression guarded by multiple statements, using the PatternGuards extension.
| Just y <- x, y = ()
=====
guards [conP "Just" (bvar "x") <-- var "y", bvar "x"] unitAn expression guarded by a single boolean statement.
| otherwise = ()
=====
guard (var "otherwise") unitWhere clauses
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']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.
Instances2HasPatBind, HasValBind
HasPatBind RawValBindDefined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.BindsHasValBind RawValBindDefined in ghc-source-gen-0.4.6.0 · GHC.SourceGen.Binds.Internal
Statements
2 declarationsAn 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.
A statement that binds a pattern.
x <- act
=====
bvar "x" <-- var "act"