Using overloaded syntax to avoid `base` dependency (RE: Marking ParsedModule fragments as non-user-originating)

Simon Peyton Jones simonpj at microsoft.com
Mon Jul 12 08:37:25 UTC 2021


I don't really understand how my question fits into the 'bug report' bucket. The quoted passage is not from the user manual, but rather, from a GHC Note

Only that GHC is doing something that you think is wrong - or at least not as documented.  If so, that's a bug. If not, the conversation is illuminating, and more easily rediscovered later in the bug tracker.

I am not interested in end-to-end behaviour, but what actually happens GHC phase by GHC phase. When is the reference to `fromString` introduced, when is it resolved (by default to `Data.String.fromString`), does `RebindableSyntax` allo me to replace not just `fromString`, but also `unpackCString#`?

I'm happy to help - but can I ask that when you think you understand, can you submit a patch that clarifies the relevant Note(s), or adds one, so that the Gergos of the future will find the answer laid out right where you tried to find it?

In GHC.Rename.Pat


rnOverLit origLit

  = do  { opt_NumDecimals <- xoptM LangExt.NumDecimals

        ; let { lit@(OverLit {ol_val=val})

            | opt_NumDecimals = origLit {ol_val = generalizeOverLitVal (ol_val origLit)}

            | otherwise       = origLit

          }

        ; let std_name = hsOverLitName val

        ; (from_thing_name, fvs1) <- lookupSyntaxName std_name




  *   hsOverLitName returns Data.String.fromString for string literals. That is where fromString first appears.


  *   Then lookupSyntaxName just returns Data.String.fromString when RebindableSyntax is off; or looks up "fromString" when RebindableSyntax is on.

When I say "Data.String.fromString" here, I mean the original name i.e. the fromString defined in Data.String - not some possibly different entity that happens to be in scope with the qualified name  "Data.String.fromString".

Does that help?



From: Erdi, Gergo <Gergo.Erdi at sc.com>
Sent: 12 July 2021 09:21
To: Simon Peyton Jones <simonpj at microsoft.com>
Cc: 'GHC' <ghc-devs at haskell.org>
Subject: RE: Using overloaded syntax to avoid `base` dependency (RE: Marking ParsedModule fragments as non-user-originating)


PUBLIC

I don't really understand how my question fits into the 'bug report' bucket. The quoted passage is not from the user manual, but rather, from a GHC Note. My reading of that note was that if I write a string literal in a Haskell program, and compile it with OverloadedStrings, it would parse into `HsLit _ (HsString _ fs)` with `HsOverLit _ (OverLit _ (HsIsString _ fs) "Data.String.fromString"`, and then the renamer and the type checker would work from that. If this understanding were correct, then I could generate parsed (and not yet renamed/typechecked) code that is, instead, `HsOverLit _ (OverLit _ (HsIsString _ fs) "myStringLitUnpackerFunction"`, and there would be no `fromString` dependency. Yet, that's not what seems to happen.

Can you (or anyone else) go into more detail about how rebindable syntax resolution and OverloadedStrings interacts in this particular case? I am not interested in end-to-end behaviour, but what actually happens GHC phase by GHC phase. When is the reference to `fromString` introduced, when is it resolved (by default to `Data.String.fromString`), does `RebindableSyntax` allo me to replace not just `fromString`, but also `unpackCString#`?


From: Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>>
Sent: Monday, July 12, 2021 3:32 PM
To: Erdi, Gergo <Gergo.Erdi at sc.com<mailto:Gergo.Erdi at sc.com>>
Cc: 'GHC' <ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>>
Subject: [External] RE: Using overloaded syntax to avoid `base` dependency (RE: Marking ParsedModule fragments as non-user-originating)

Gergo,

If you think you have uncovered a bug, could you submit a bug report on the issue tracker, with a way to reproduce it? It's a bit hard to decode exactly what is happening from what you say.

The user manual documentation doesn't say this in so many words (that's a bug), but with OverloadedStrings, the literal "foo" is replaced by Data.String.fromString "foo"

Guessing a bit, that is probably why GHC complains that it can't load Data.String.fromString.

If in addition you want to use your own fromString, not the built-in one, then you need to add RebindableSyntax.

Simon

From: Erdi, Gergo <Gergo.Erdi at sc.com<mailto:Gergo.Erdi at sc.com>>
Sent: 12 July 2021 08:13
To: Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>>
Cc: 'GHC' <ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>>
Subject: Using overloaded syntax to avoid `base` dependency (RE: Marking ParsedModule fragments as non-user-originating)


PUBLIC

OK so I tried out OverloadedStrings and it basically went as bad as I expected. The documentation on `HsOverLit` is very promising: it points to the Note [Overloaded literal witnesses], which states:

Note [Overloaded literal witnesses]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*Before* type checking, the HsExpr in an HsOverLit is the
name of the coercion function, 'fromInteger' or 'fromRational'.

So that sounds great, right? It sounds like just before renaming, I should be able to replace `HsLit _ (HsString _ fs)` with `HsOverLit _ (OverLit _ (HsIsString _ fs) unpack` with my own `unpack` function coming from my own package, and everything would work out. Unfortunately, this is not what happens: if I try getting this through the renamer, I get this error:

Failed to load interface for 'Data.String'
no unit id matching 'base' was found
Can't find interface-file declaration for variable fromString
  Probable cause: bug in .hi-boot file, or inconsistent .hi file
  Use -ddump-if-trace to get an idea of which file caused the error

So even though I am specifying my own coercion function, it is still looking for `Data.String.fromString` which is not going to work, since I don't have `base`. So either I am misunderstanding that Note, or it is simply out of date, but in either case, this isn't going to be a viable route to going base-less.

            Gergo

From: Erdi, Gergo
Sent: Tuesday, July 6, 2021 5:39 PM
To: Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>>
Cc: GHC <ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>>
Subject: RE: Marking ParsedModule fragments as non-user-originating


PUBLIC

Thanks Simon!

Of course, you're right, it's the renamer, not the typechecker - I didn't really check, just saw that "it happens during `typecheckModule`.

I'll look at the rebindable syntax stuff in detail, but at least for OverloadedStrings, I already know that the problem will be that ultimately they do go through the `String` type from `base`, and I need to use GHC baselessly. This is a problem for two reasons:


  *   I can't implement `IsString` for `MyString`, because `IsString` is in `base`
  *   Even if I made my own fake `base` with a fake `IsString` class, there is nothing to put in the codomain of `fromString`: I *only* have `MyString`, not `String`. And renaming `MyString to `String` in my fake `base` is not going to cut it, since `String` is wired into GHC to be a type synonym for `[Char]` (which `MyString` is not).

I foresee similar problems for OverloadedLists :/

Thanks,
            Gergo

From: Simon Peyton Jones <simonpj at microsoft.com<mailto:simonpj at microsoft.com>>
Sent: Tuesday, July 6, 2021 5:08 PM
To: Erdi, Gergo <Gergo.Erdi at sc.com<mailto:Gergo.Erdi at sc.com>>
Cc: GHC <ghc-devs at haskell.org<mailto:ghc-devs at haskell.org>>
Subject: [External] RE: Marking ParsedModule fragments as non-user-originating

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 at haskell.org<mailto:ghc-devs-bounces at haskell.org>> On Behalf Of Erdi, Gergo via ghc-devs
Sent: 06 July 2021 09:08
To: ghc-devs at haskell.org<mailto:ghc-devs at 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


This email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please delete all copies and notify the sender immediately. You may wish to refer to the incorporation details of Standard Chartered PLC, Standard Chartered Bank and their subsidiaries at https: //www.sc.com/en/our-locations

Where you have a Financial Markets relationship with Standard Chartered PLC, Standard Chartered Bank and their subsidiaries (the "Group"), information on the regulatory standards we adhere to and how it may affect you can be found in our Regulatory Compliance Statement at https: //www.sc.com/rcs/ and Regulatory Compliance Disclosures at http: //www.sc.com/rcs/fm

Insofar as this communication is not sent by the Global Research team and contains any market commentary, the market commentary has been prepared by the sales and/or trading desk of Standard Chartered Bank or its affiliate. It is not and does not constitute research material, independent research, recommendation or financial advice. Any market commentary is for information purpose only and shall not be relied on for any other purpose and is subject to the relevant disclaimers available at https: //www.sc.com/en/regulatory-disclosures/#market-disclaimer.

Insofar as this communication is sent by the Global Research team and contains any research materials prepared by members of the team, the research material is for information purpose only and shall not be relied on for any other purpose, and is subject to the relevant disclaimers available at https: //research.sc.com/research/api/application/static/terms-and-conditions.

Insofar as this e-mail contains the term sheet for a proposed transaction, by responding affirmatively to this e-mail, you agree that you have understood the terms and conditions in the attached term sheet and evaluated the merits and risks of the transaction. We may at times also request you to sign the term sheet to acknowledge the same.

Please visit https: //www.sc.com/en/regulatory-disclosures/dodd-frank/ for important information with respect to derivative products.

This email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please delete all copies and notify the sender immediately. You may wish to refer to the incorporation details of Standard Chartered PLC, Standard Chartered Bank and their subsidiaries at https: //www.sc.com/en/our-locations

Where you have a Financial Markets relationship with Standard Chartered PLC, Standard Chartered Bank and their subsidiaries (the "Group"), information on the regulatory standards we adhere to and how it may affect you can be found in our Regulatory Compliance Statement at https: //www.sc.com/rcs/ and Regulatory Compliance Disclosures at http: //www.sc.com/rcs/fm

Insofar as this communication is not sent by the Global Research team and contains any market commentary, the market commentary has been prepared by the sales and/or trading desk of Standard Chartered Bank or its affiliate. It is not and does not constitute research material, independent research, recommendation or financial advice. Any market commentary is for information purpose only and shall not be relied on for any other purpose and is subject to the relevant disclaimers available at https: //www.sc.com/en/regulatory-disclosures/#market-disclaimer.

Insofar as this communication is sent by the Global Research team and contains any research materials prepared by members of the team, the research material is for information purpose only and shall not be relied on for any other purpose, and is subject to the relevant disclaimers available at https: //research.sc.com/research/api/application/static/terms-and-conditions.

Insofar as this e-mail contains the term sheet for a proposed transaction, by responding affirmatively to this e-mail, you agree that you have understood the terms and conditions in the attached term sheet and evaluated the merits and risks of the transaction. We may at times also request you to sign the term sheet to acknowledge the same.

Please visit https: //www.sc.com/en/regulatory-disclosures/dodd-frank/ for important information with respect to derivative products.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20210712/aaa866fa/attachment.html>


More information about the ghc-devs mailing list