Skip to content

Template Haskell slice shouldn't be linear

Summary / Steps to reproduce

When I say

{-# LANGUAGE TemplateHaskell, LinearTypes #-}

module Bug where

import Language.Haskell.TH

f :: Q Exp ->. Q Exp
f x = [| Just $x |]

I get

*** Core Lint errors : in result of Desugar (before optimization) ***
Bug.hs:8:3: warning:
    [in body of lambda with binder x_a8Ki :: Q Exp]
    Linearity failure in lambda: x_a8Ki
    'Omega ⊈ 'One
*** Offending Program ***
Rec {
$trModule :: Module
[LclIdX]
$trModule = Module (TrNameS "main"#) (TrNameS "Bug"#)

f :: Q Exp ->. Q Exp
[LclIdX]
f = \ (x_a8Ki :: Q Exp) ->
      break<0>()
      appE
        (conE
           (mkNameG_d
              (unpackCString# "base"#)
              (unpackCString# "GHC.Maybe"#)
              (unpackCString# "Just"#)))
        x_a8Ki
end Rec }

*** End of Offense ***

Expected behavior

The problem is that Template Haskell slices desugars to calls to appE, conE, etc… which are not linear functions (and probably oughtn't be anyway). Consequently, slices should not be considered linear in their antiquoted positions.

It is probably sufficient to call tcScalingUsage Many in

  • tcPendingSplice
  • tcTypedBracket
  • tcUntypedBracket around the calls to tcMonoExpr and tcInferRhoNC.

Environment

  • GHC version used: current master
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information