The typechecker now complains that the `ViewPatterns` language extension is not turned on

 

I think it’s the renamer:

 

rnPatAndThen mk p@(ViewPat _ expr pat)

  = do { liftCps $ do { vp_flag <- xoptM LangExt.ViewPatterns

                      ; checkErr vp_flag (badViewPat p) }

 

 

More generally, don’t you just want OverloadedStrings or OverloadedLists?

 

You might want to read Note [Handling overloaded and rebindable constructs] in GHC.Rename.Expr, and

Note [Rebindable syntax and HsExpansion] in GCH.Hs.Expr.  These Notes describe how GHC already does something similar to what you want.   Maybe you can use the same mechanism in your plugin.

 

Simon

 

 

 

From: ghc-devs <ghc-devs-bounces@haskell.org> On Behalf Of Erdi, Gergo via ghc-devs
Sent: 06 July 2021 09:08
To: ghc-devs@haskell.org
Subject: Marking ParsedModule fragments as non-user-originating

 

PUBLIC

 

Hi,

 

I’d like to hijack some syntax (like string literals or list patterns) for my own use, and I thought a low-tech way of doing that is to transform the ParsedModule before typechecking. For example, if I have a function `uncons :: Array a -> Maybe (a, Array a)`, I can rewrite the pattern `[x1, x2, x3]` into the view pattern `(uncons -> Just (x1, (uncons -> Just (x2, (uncons –> Just (x3, (uncons -> Nothing)))))))` and let the normal GHC type checker take over from here.

 

This is working for me so far, except for one problem: the typechecker now complains that the `ViewPatterns` language extension is not turned on. I would like to make the view patterns coming from my ParsedModule rewriter to be exempt from this check (but of course still require the `ViewPatterns` extension for user-originating code). Is there a way to do that? Or would I be better off checking for user-originating view patterns myself before the rewrite and then changing the `DynFlags` to always enable view patterns for typechecking?

 

Thanks,

            Gergo