| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Howl.Expr.TH
Description
Template Haskell helpers for generating cached symbol-expression pattern synonyms.
Documentation
Arguments
| :: Name | Expr type, e.g. ''Expr |
| -> Name | mkExpr function, e.g. 'mkExpr |
| -> String | pattern name, e.g. Plus |
| -> String | interned text, e.g. Plus |
| -> Q [Dec] |
Generate a cached symbol CAF + a bidirectional pattern synonym. These are convenient for pattern matching, and they also ensure that we don't need to call fromString in the middle of matching a pattern, which would obviate the advantage of having O(1) equality checks for Symbols.
It is recommended to use declareExprPattern instead of fromString whenever a symbol will be involved in evaluation, either in pattern matching, or returned from a function.
Produces (for patStr=Plus, txt=Plus):
{-# NOINLINE plusExpr #-} plusExpr :: Expr plusExpr = mkExpr Plus
isPlus :: Expr -> Bool isPlus s = s == plusExpr
{-# INLINE Plus #-} pattern Plus :: Expr pattern Plus (isPlus - True) where Plus = plusExpr
{-# COMPLETE Plus #-}