Use a newtype `Code` for the return type of typed quotations (Proposal #195)
There are three problems with the current API: 1. It is hard to properly write instances for ``Quote m => m (TExp a)`` as the type is the composition of two type constructors. Doing so in your program involves making your own newtype and doing a lot of wrapping/unwrapping. For example, if I want to create a language which I can either run immediately or generate code from I could write the following with the new API. :: class Lang r where _int :: Int -> r Int _if :: r Bool -> r a -> r a -> r a instance Lang Identity where _int = Identity _if (Identity b) (Identity t) (Identity f) = Identity (if b then t else f) instance Quote m => Lang (Code m) where _int = liftTyped _if cb ct cf = [|| if $$cb then $$ct else $$cf ||] 2. When doing code generation it is common to want to store code fragments in a map. When doing typed code generation, these code fragments contain a type index so it is desirable to store them in one of the parameterised map data types such as ``DMap`` from ``dependent-map`` or ``MapF`` from ``parameterized-utils``. :: compiler :: Env -> AST a -> Code Q a data AST a where ... data Ident a = ... type Env = MapF Ident (Code Q) newtype Code m a = Code (m (TExp a)) In this example, the ``MapF`` maps an ``Ident String`` directly to a ``Code Q String``. Using one of these map types currently requires creating your own newtype and constantly wrapping every quotation and unwrapping it when using a splice. Achievable, but it creates even more syntactic noise than normal metaprogramming. 3. ``m (TExp a)`` is ugly to read and write, understanding ``Code m a`` is easier. This is a weak reason but one everyone can surely agree with. Updates text submodule.
Showing
- compiler/GHC/Builtin/Names/TH.hs 24 additions, 34 deletionscompiler/GHC/Builtin/Names/TH.hs
- compiler/GHC/Tc/Deriv/Generate.hs 13 additions, 12 deletionscompiler/GHC/Tc/Deriv/Generate.hs
- compiler/GHC/Tc/Gen/Splice.hs 5 additions, 5 deletionscompiler/GHC/Tc/Gen/Splice.hs
- docs/users_guide/exts/deriving_extra.rst 1 addition, 1 deletiondocs/users_guide/exts/deriving_extra.rst
- docs/users_guide/exts/template_haskell.rst 5 additions, 5 deletionsdocs/users_guide/exts/template_haskell.rst
- libraries/template-haskell/Language/Haskell/TH.hs 2 additions, 0 deletionslibraries/template-haskell/Language/Haskell/TH.hs
- libraries/template-haskell/Language/Haskell/TH/CodeDo.hs 20 additions, 0 deletionslibraries/template-haskell/Language/Haskell/TH/CodeDo.hs
- libraries/template-haskell/Language/Haskell/TH/Lib.hs 1 addition, 1 deletionlibraries/template-haskell/Language/Haskell/TH/Lib.hs
- libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs 1 addition, 0 deletions...ries/template-haskell/Language/Haskell/TH/Lib/Internal.hs
- libraries/template-haskell/Language/Haskell/TH/Syntax.hs 108 additions, 51 deletionslibraries/template-haskell/Language/Haskell/TH/Syntax.hs
- libraries/template-haskell/changelog.md 3 additions, 0 deletionslibraries/template-haskell/changelog.md
- libraries/template-haskell/template-haskell.cabal.in 1 addition, 1 deletionlibraries/template-haskell/template-haskell.cabal.in
- libraries/text 1 addition, 1 deletionlibraries/text
- testsuite/tests/deriving/should_compile/drv-empty-data.stderr 3 additions, 1 deletion...suite/tests/deriving/should_compile/drv-empty-data.stderr
- testsuite/tests/parser/should_compile/Proposal229f_instances.hs 3 additions, 3 deletions...ite/tests/parser/should_compile/Proposal229f_instances.hs
- testsuite/tests/partial-sigs/should_compile/TypedSplice.hs 1 addition, 1 deletiontestsuite/tests/partial-sigs/should_compile/TypedSplice.hs
- testsuite/tests/partial-sigs/should_compile/TypedSplice.stderr 2 additions, 2 deletions...uite/tests/partial-sigs/should_compile/TypedSplice.stderr
- testsuite/tests/quotes/T17857.hs 1 addition, 1 deletiontestsuite/tests/quotes/T17857.hs
- testsuite/tests/th/T10945.stderr 3 additions, 3 deletionstestsuite/tests/th/T10945.stderr
- testsuite/tests/th/T11452.stderr 4 additions, 4 deletionstestsuite/tests/th/T11452.stderr
Loading
Please register or sign in to comment