[GHC] #16178: Brackets and splices should be overloaded like the static keyword
GHC
ghc-devs at haskell.org
Mon Jan 14 13:43:46 UTC 2019
#16178: Brackets and splices should be overloaded like the static keyword
-------------------------------------+-------------------------------------
Reporter: mpickering | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone:
Component: Template Haskell | Version: 8.6.3
Resolution: | Keywords:
| TypedTemplateHaskell
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: None/Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by mpickering):
If you define a partially static monoid type such as the following which
tries to evaluate as many concatenations statically as possible.
{{{
data PS a = Static a | Dynamic (Code a)
instance Monoid a => Monoid (PS a) where
mempty = Static mempty
(Static a) <> (Static b) = Static (a <> b)
-- Using the overloading here
a <> c = [|| $$a <> $$c ||]
lower :: Lift a => PS a -> Code a
lower (Static a) = lift a
lower (Dynamic c) = c
instance IsSplice PS where
toBracket = lower
instance IsBracket PS where
fromBracket = Dynamic
sta :: a -> PS a
sta = Static
}}}
Writing programs with this data type becomes much more pleasant as the
additional lifting and lowering become transparent. You can combine
together these layers multiple times to implicitly perform different
rewritings but the client's splice will still end up looking the same.
{{{
prog = (sta () <> sta ()) <> [|| () ||]
}}}
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/16178#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list