Should TH TExp be able use the Q monad

Simon Peyton Jones simonpj at microsoft.com
Mon Apr 18 13:14:10 UTC 2016


|  > Hang on!  The design for typed splices, describe here,
|  > https://ghc.haskell.org/trac/ghc/wiki/TemplateHaskell/BlogPostChanges
|  > says "Unlike TH, the only way to construct a value of type TExp is
|  with a quote. You cannot drop into do-notation, nor use explicit
|  construction of the values in the Exp algebraic data type. That
|  restriction limits expressiveness, but it enables the strong typing
|  guarantees."
|  >
|  > So why does the above work?  $$(e) requires a TExp, and do-notation
|  doesn’t produce a TExp.
|  
|  Indeed this is true -- this is what that page says. But it's not what's
|  implemented: when I say $$( _ ) :: Bool, GHC tells me that the hole has
|  type Q (TExp Bool).
|  There still is no way to create a TExp other than to use a type TH quote

Humph.  I suppose that provided you can't forge a TExp, there's no way to splice in a type-incorrect program; and having Q available lets you (say) do input/output or consult the context to decide which of two quotes to return.

We could do this selectively.  For example we'd certainly need (TExp t) to be able to fail

> bool :: String -> TExp Bool
> bool "true"  = [|| True ||]
> bool "false" = [|| False ||]
> bool _ = failTexp "not a bool"

My instinct is to make it less expressive, though, and only allow (TExp t) as the argument of $$. 

Does anyone care either way?   I suppose we'd better open a ticket for this.

Simon

|  
|  quote.
|  
|  Addressing your other message: a typed quasiquoter would be somewhat
|  limited, but not utterly silly. For example, this works:
|  
|  > bool :: String -> Q (TExp Bool)
|  > bool "true"  = [|| True ||]
|  > bool "false" = [|| False ||]
|  > bool _ = fail "not a bool"
|  >
|  > -- and then, in another module because of the stage restriction:
|  > yes :: Bool
|  > yes = $$(bool "true")
|  
|  Now `bool` could be a typed quasiquoter.
|  
|  I don't know whether any of this is worth implementing, but it's not, a
|  priori, a terrible idea.
|  
|  Richard
|  
|  >
|  > |  * Should we consider it a bug (and file a ticket) that reification
|  > | in  typed splices is able to observe the order of type checking,
|  > | just like  reify used to do in untyped splices?
|  >
|  > Yes I think so!!!
|  >
|  > Simon
|  >



More information about the ghc-devs mailing list