-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate
Protocol
instances via template-haskell (#71)
This ensures that instances are available up to `maxTupleSize`.
- Loading branch information
Showing
4 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{-# OPTIONS_HADDOCK hide #-} | ||
|
||
module Protocols.Internal.TH (protocolTupleInstances) where | ||
|
||
import Language.Haskell.TH | ||
|
||
appTs :: Q Type -> [Q Type] -> Q Type | ||
appTs = foldl appT | ||
|
||
protocolTupleInstances :: Int -> Q [Dec] | ||
protocolTupleInstances n = mapM protocolTupleInstance [3..n] | ||
|
||
protocolTupleInstance :: Int -> Q Dec | ||
protocolTupleInstance n = | ||
instanceD | ||
(pure []) -- context | ||
(protocolConT `appT` tup) -- head | ||
[mkTyInst fwdConName, mkTyInst bwdConName] -- body | ||
|
||
where | ||
fwdConName = mkName "Fwd" | ||
bwdConName = mkName "Bwd" | ||
protocolConT = conT (mkName "Protocol") | ||
|
||
tyVars :: [TypeQ] | ||
tyVars = map (varT . mkName . ('a':) . show) [1..n] | ||
|
||
tup = tupleT n `appTs` tyVars | ||
|
||
mkTyInst :: Name -> DecQ | ||
mkTyInst con = | ||
tySynInstD $ tySynEqn Nothing lhs rhs | ||
where | ||
lhs, rhs :: TypeQ | ||
lhs = conT con `appT` tup | ||
rhs = tupleT n `appTs` map (conT con `appT`) tyVars | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters