Why are nested brackets disallowed?

Matthew Pickering matthewtpickering at gmail.com
Wed Jan 30 15:42:22 UTC 2019


I put up a patch which removes this restriction (for untyped expressions).

https://gitlab.haskell.org/ghc/ghc/merge_requests/259

On Thu, Jan 24, 2019 at 11:46 AM Matthew Pickering
<matthewtpickering at gmail.com> wrote:
>
> There is a check in `RnSplice` which errors on the following program
> with nested brackets.
>
> ```
> prog = [| [| True |] |]
>
> T.hs:4:11: error:
>     • Template Haskell brackets cannot be nested (without intervening splices)
>     • In the Template Haskell quotation [| True |]
>       In the Template Haskell quotation [| [| True |] |]
>   |
> 4 | prog = [| [| True |] |]
>   |           ^^^^^^^^^^
>
> ```
>
> As far as I can see the check was added in 2013 in this commit,
> https://github.com/ghc/ghc/commit/d0d47ba76f8f0501cf3c4966bc83966ab38cac27#diff-58794f81a415f148d011de563988c2b5R170
>
> But there is no note, no tests and no comment about why it was added.
>
> I removed the check and added a `BracketE` constructor to the
> template-haskell AST and the
> code compiles fine.
>
> I can also construct a program which needs to be spliced twice and
> this also works fine.
>
> ```
> func Add = [| (+) |]
> func Mul = [| (*) |]
>
> f1 "+" = [| Add |]
> f1 "*" = [| Mul |]
>
> comb s = [| func $(f1 s) |]
> ```
> ```
> res = $($(comb "*"))
> ```
>
> So it seems the restriction is quite arbitrary but I was wondering if
> I was missing some limitation which meant this check was added. I
> would not be surprised if something more complicated goes wrong with
> splicing.
>
> Cheers,
>
> Matt


More information about the ghc-devs mailing list