| Copyright | (C) 2026 QBayLogic B.V. |
|---|---|
| License | BSD2 (see the file LICENSE) |
| Maintainer | QBayLogic B.V. <devops@qbaylogic.com> |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Clash.Rewrite.StrategyDSL
Description
A pure description language for rewrite strategies. A strategy written in this DSL is compiled by Clash.Rewrite.StrategyDSL.TH into a fused traversal that matches each node's constructor once and runs only the transformations registered for that constructor.
The DSL has two layers:
Step: what runs at a single node inside a fused traversal. An orderedchainof transformations, sequencing and success/failure conditionals between chain segments, or a wholenestedstrategy embedded as one node action.Strat: a complete strategy. Fused traversals over aStep, whole-term passes, and the usual combinators (>->,!->,>-!,>-!->,repeatR) between them.
Synopsis
- data Constructor
- data TransformationWorker
- = FieldWorker !Constructor !Name
- | Worker !Constructor !Name
- | AnyWorker !Name
- data Transformation = Transformation {
- name :: !String
- workers :: ![TransformationWorker]
- toTransformation :: String -> [TransformationWorker] -> Transformation
- anyConstructor :: String -> Name -> Transformation
- named :: String -> Transformation -> Transformation
- onVar :: Name -> [TransformationWorker]
- onData :: Name -> [TransformationWorker]
- onLiteral :: Name -> [TransformationWorker]
- onPrim :: Name -> [TransformationWorker]
- onLam :: Name -> [TransformationWorker]
- onTyLam :: Name -> [TransformationWorker]
- onApp :: Name -> [TransformationWorker]
- onTyApp :: Name -> [TransformationWorker]
- onLet :: Name -> [TransformationWorker]
- onCase :: Name -> [TransformationWorker]
- onCast :: Name -> [TransformationWorker]
- onTick :: Name -> [TransformationWorker]
- onVarNode :: Name -> [TransformationWorker]
- onPrimNode :: Name -> [TransformationWorker]
- onAppNode :: Name -> [TransformationWorker]
- onTyAppNode :: Name -> [TransformationWorker]
- onLetNode :: Name -> [TransformationWorker]
- onTickNode :: Name -> [TransformationWorker]
- data Step
- chain :: [Transformation] -> Step
- one :: Transformation -> Step
- nested :: Strat -> Step
- data Strat
- topdown :: ToStep s => s -> Strat
- bottomup :: ToStep s => s -> Strat
- topdownFix :: ToStep s => s -> Strat
- topdownSuc :: ToStep s => s -> Strat
- innerMost :: ToStep s => s -> Strat
- pass :: String -> Name -> Strat
- callStrategy :: Name -> Strat
- repeatR :: Strat -> Strat
- class Strategic a where
- (>-!->) :: Strat -> Strat -> Strat
- class ToStep s where
Transformation metadata
data Constructor Source #
One Term constructor.
Instances
data TransformationWorker Source #
A single function separated by the constructor it fires on. Note that this
isn't a single transformation per se, as one transformation might fire on
multiple constructors. Transformation ties together multiple workers into
a single transformation.
The Name must refer to an exported top-level worker so the generated
dispatch code can reference it from the splice site.
A worker that returns a term other than the one it was given must say so
with changed: a chain hands the fields it matched to
the next member as long as no change was signaled.
Constructors
| FieldWorker !Constructor !Name |
|
| Worker !Constructor !Name |
|
| AnyWorker !Name |
|
Instances
| Show TransformationWorker Source # | |
Defined in Clash.Rewrite.StrategyDSL Methods showsPrec :: Int -> TransformationWorker -> ShowS # show :: TransformationWorker -> String # showList :: [TransformationWorker] -> ShowS # | |
| Eq TransformationWorker Source # | |
Defined in Clash.Rewrite.StrategyDSL Methods (==) :: TransformationWorker -> TransformationWorker -> Bool # (/=) :: TransformationWorker -> TransformationWorker -> Bool # | |
data Transformation Source #
Single, named transformation. Can consist of multiple workers (see
TransformationWorker).
Constructors
| Transformation | |
Fields
| |
Instances
| Show Transformation Source # | |
Defined in Clash.Rewrite.StrategyDSL Methods showsPrec :: Int -> Transformation -> ShowS # show :: Transformation -> String # showList :: [Transformation] -> ShowS # | |
| ToStep Transformation Source # | |
Defined in Clash.Rewrite.StrategyDSL Methods toStep :: Transformation -> Step Source # | |
| Eq Transformation Source # | |
Defined in Clash.Rewrite.StrategyDSL Methods (==) :: Transformation -> Transformation -> Bool # (/=) :: Transformation -> Transformation -> Bool # | |
toTransformation :: String -> [TransformationWorker] -> Transformation Source #
Declare a transformation's workers: at most one per constructor, and
either an AnyWorker or per-constructor workers, never both. Checked when
the transformation is compiled.
anyConstructor :: String -> Name -> Transformation Source #
Declare a transformation that fires at any constructor. See AnyWorker.
named :: String -> Transformation -> Transformation Source #
Give a transformation a different name; used for deliberate debug aliases of a transformation that runs at several strategy positions (the transformation counters and rewrite history key on the name).
Worker builders: workers receiving the constructor's fields
onVar :: Name -> [TransformationWorker] Source #
onData :: Name -> [TransformationWorker] Source #
onLiteral :: Name -> [TransformationWorker] Source #
onPrim :: Name -> [TransformationWorker] Source #
onLam :: Name -> [TransformationWorker] Source #
onTyLam :: Name -> [TransformationWorker] Source #
onApp :: Name -> [TransformationWorker] Source #
onTyApp :: Name -> [TransformationWorker] Source #
onLet :: Name -> [TransformationWorker] Source #
onCase :: Name -> [TransformationWorker] Source #
onCast :: Name -> [TransformationWorker] Source #
onTick :: Name -> [TransformationWorker] Source #
Worker builders: workers receiving the node itself
onVarNode :: Name -> [TransformationWorker] Source #
onPrimNode :: Name -> [TransformationWorker] Source #
onAppNode :: Name -> [TransformationWorker] Source #
onTyAppNode :: Name -> [TransformationWorker] Source #
onLetNode :: Name -> [TransformationWorker] Source #
onTickNode :: Name -> [TransformationWorker] Source #
Node layer
What runs at a single node inside a fused traversal.
Constructors
| Chain [Transformation] | One constructor match dispatches the members registered for that
constructor, in list order. Equivalent to the sequential
|
| SeqS Step Step |
|
| OnChangeS Step Step |
|
| OnNoChangeS Step Step |
|
| EmbedS Strat | A whole strategy as one node action. |
chain :: [Transformation] -> Step Source #
An ordered chain of transformations, dispatched per constructor. See
Chain.
one :: Transformation -> Step Source #
A singleton chain, for use as an operand of the node-level operators.
Strategy layer
A complete rewriting strategy.
Constructors
| TopDown Step | Fused |
| BottomUp Step | Fused |
| TopDownFix Step | Fused |
| TopDownSuc Step | Fused |
| InnerMost Step | Fused |
| Pass String Name | A whole-term pass: |
| CallStrategy Name | Reference to another compiled strategy binding. |
| Seq Strat Strat | |
| DeepSeqS Strat Strat | |
| OnChange Strat Strat | |
| OnNoChange Strat Strat | |
| Repeat Strat |
topdownFix :: ToStep s => s -> Strat Source #
topdownSuc :: ToStep s => s -> Strat Source #
callStrategy :: Name -> Strat Source #
Reference another compiled strategy by the name of its binding.
Combinators
class Strategic a where Source #
The combinators shared by both DSL layers. Fixities match Clash.Rewrite.Combinators; a strategy spec module should import only this module, so the names are unambiguous there.
Methods
(>->) :: a -> a -> a infixr 6 Source #
Apply two strategies in succession.
(!->) :: a -> a -> a infixr 5 Source #
Only apply the second strategy if the first one succeeds.
(>-!) :: a -> a -> a infixr 5 Source #
Only apply the second strategy if the first one fails.
(>-!->) :: Strat -> Strat -> Strat infixr 6 Source #
Apply two strategies in succession, with a deepseq of the term in
between. Strategy layer only.
Things that can be used where a Step is expected; a bare
Transformation lifts to a singleton chain.
Instances
| ToStep Step Source # | |
| ToStep Transformation Source # | |
Defined in Clash.Rewrite.StrategyDSL Methods toStep :: Transformation -> Step Source # | |