Why are nested brackets disallowed?

Simon Peyton Jones simonpj at microsoft.com
Fri Jan 25 12:49:32 UTC 2019


Interesting. I don’t recall a specific reason why nested brackets were outlawed. I think it was just that we didn't think we needed them, so it seemed simplest not to have them.  TH does not do runtime codegen, so there are really only two stages: compile time and run time.

Do you have a compelling use-case?

Simon

|  -----Original Message-----
|  From: ghc-devs <ghc-devs-bounces at haskell.org> On Behalf Of Matthew
|  Pickering
|  Sent: 25 January 2019 11:57
|  To: Richard Eisenberg <rae at cs.brynmawr.edu>
|  Cc: GHC developers <ghc-devs at haskell.org>
|  Subject: Re: Why are nested brackets disallowed?
|  
|  I don't think that cross stage persistence will work as it is currently
|  implemented which is probably why the check exists.
|  
|  1. The normal case
|  
|  foo x = [| x |] ===>
|    foo x = [| $(lift x) |]
|  
|  2. x is defined at stage 0, and used at stage 2.
|  
|  One option is:
|  foo x = [| [| x |] |] ===>
|    foo x = [| [| $($(lift (lift x))) |] |] or foo x = [| [| x |] |] ===>
|    foo x = [| let x' = $(lift x) in [| $(lift [| x' |]) |]
|  
|  We need to think a bit how to `lift` something of type `Q Exp` because of
|  the `Q` monad. Lifting an `Exp` seems trivial as it's a normal ADT (I
|  tested and this works after deriving 40 instances).
|  
|  You can define `lift2` which lifts an expression twice as follows.
|  
|  ```
|  lift2 :: Lift a => a -> Q Exp
|  lift2 a = lift a >>= \e -> [| return $ $(lift e) |] ```
|  
|  3. x is defined at stage 1 and used in stage 2
|  
|  foo = [| \x -> [| x |] |] ===>
|    foo = [| \x -> [| $(lift x) |] |]
|  
|  Desugared with a single call to `lift` like normal.
|  
|  4. x is defined in stage 2 and used in stage 1
|  
|  foo = [| [| \x -> $(x) |] |]
|  
|  Rejected just like usual. `x` won't be bound when the splice is run.
|  
|  It seems that with some suitable care that things will work out when
|  lifting across multiple levels but that is the point where care needs to
|  be taken.
|  
|  Matt
|  
|  
|  
|  On Thu, Jan 24, 2019 at 5:46 PM Richard Eisenberg <rae at cs.brynmawr.edu>
|  wrote:
|  >
|  > I think Geoff was primarily concerned with typed Template Haskell, not
|  the untyped variety.
|  >
|  > I, too, have wondered if there was a technical reason behind this
|  restriction, or if merely it was assumed that nested brackets were not
|  worthwhile.
|  >
|  > One question: how would staging work between nesting levels of
|  brackets?
|  >
|  > Richard
|  >
|  > > On Jan 24, 2019, at 12:42 PM, Ben Gamari <ben at smart-cactus.org>
|  wrote:
|  > >
|  > > Matthew Pickering <matthewtpickering at gmail.com> writes:
|  > >
|  > >> There is a check in `RnSplice` which errors on the following
|  > >> program with nested brackets.
|  > >>
|  > > It might be good to explicitly include Geoff Mainland in this thread.
|  > > I'm not sure he'll see it otherwise.
|  > >
|  > > Cheers,
|  > >
|  > > - Ben
|  > >
|  > > _______________________________________________
|  > > ghc-devs mailing list
|  > > ghc-devs at haskell.org
|  > > https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmai
|  > > l.haskell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fghc-devs&data=02%
|  > > 7C01%7Csimonpj%40microsoft.com%7Cdf0aa539bb1041dca42308d682bc3d4b%7C
|  > > 72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636840142327169830&sd
|  > > ata=oIiA768uqGGGBJh7ogpymmPvuKBDLj%2BiqJCZpig6SPg%3D&reserved=0
|  >
|  _______________________________________________
|  ghc-devs mailing list
|  ghc-devs at haskell.org
|  https://nam06.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmail.has
|  kell.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fghc-
|  devs&data=02%7C01%7Csimonpj%40microsoft.com%7Cdf0aa539bb1041dca42308d
|  682bc3d4b%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636840142327169830
|  &sdata=oIiA768uqGGGBJh7ogpymmPvuKBDLj%2BiqJCZpig6SPg%3D&reserved=
|  0


More information about the ghc-devs mailing list