Re: Loading a typechecked module and then using it immediately as a package

2021-07-01 Thread Erdi, Gergo via ghc-devs
PUBLIC

Unfortunately, that would take quite some extra effort. However, I think I have 
figured this out in the meantime: it seems it wasn't the FinderCache that 
needed invalidating, but the ModuleGraph. So now I have the following code for 
changing the home unit:

```
setHomeUnit :: (GhcMonad m) => UnitId -> m ()
setHomeUnit unitId = do
modifySession $ \env -> env
{ hsc_dflags = (hsc_dflags env)
{ homeUnitId = unitId
}
}
invalidateModSummaryCache

invalidateModSummaryCache :: (GhcMonad m) => m ()
invalidateModSummaryCache = modifySession $ \env -> env
{ hsc_mod_graph = invalidateMG (hsc_mod_graph env)
}
  where
invalidateMG = mapMG invalidateMS
invalidateMS ms = ms{ ms_hs_date = addUTCTime (-1) (ms_hs_date ms) }
```

Here, `invalidateModSummaryCache` is based on the one in the `GHC` module which 
doesn't export it.

With the addition of `invalidateModSummaryCache` to `setHomeUnit`, I can now 
import `MyLib` from `Test` without using a package-qualified import. Adding or 
removing a `flushFinderCaches` call doesn’t seem to change anything.

-Original Message-
From: Matthew Pickering  
Sent: Wednesday, June 30, 2021 6:11 PM
To: Erdi, Gergo 
Subject: [External] Re: Loading a typechecked module and then using it 
immediately as a package

ATTENTION: This email came from an external source. Do not open attachments or 
click on links from unknown senders or unexpected emails. Always report 
suspicious emails using the Report As Phishing button in Outlook to protect the 
Bank and our clients.


Could you provide the code which is failing? Then it will be easier to fix.

On Tue, Jun 29, 2021 at 12:00 PM Erdi, Gergo  wrote:
>
> PUBLIC
>
> Should I? OK, I just tried calling `flushFinderCaches` after I change the 
> home unit to `mainUnitId`, but I still get exactly the same behaviour: 
> `findInstalledHomeModule` returns `InstalledFound` and things go downhill 
> from there.
>
> -Original Message-
> From: Matthew Pickering 
> Sent: Tuesday, June 29, 2021 6:34 PM
> To: Erdi, Gergo 
> Cc: ghc-devs@haskell.org
> Subject: [External] Re: Loading a typechecked module and then using it 
> immediately as a package
>
> ATTENTION: This email came from an external source. Do not open attachments 
> or click on links from unknown senders or unexpected emails. Always report 
> suspicious emails using the Report As Phishing button in Outlook to protect 
> the Bank and our clients.
>
>
> Are you clearing the FinderCache?
>
> On Tue, Jun 29, 2021 at 11:14 AM Erdi, Gergo  wrote:
> >
> > PUBLIC
> >
> > I don't know yet what's going on, but one thing I did notice is that 
> > `findInstalledHomeModule` returns `InstalledFound` for `MyLib`, which 
> > doesn't sound right to me -- `MyLib` should come from the "fake-uid" unit, 
> > and `Test` is typechecked in the `mainUnitId`.
> >
> > -Original Message-
> > From: Erdi, Gergo
> > Sent: Tuesday, June 29, 2021 5:51 PM
> > To: Matthew Pickering 
> > Subject: Re: Loading a typechecked module and then using it immediately as 
> > a package
> >
> > PUBLIC
> >
> > I tried moving `MyLib.hs` into a directory different than `Test.sh`, but 
> > the error message still refers to its correct location! So this error is 
> > not guessing the file name of where `MyLib.hs` could be loaded from; 
> > instead, it seems to refer correctly to where the module (previously 
> > loaded) was. Hmm.
> >
> > -Original Message-
> > From: Matthew Pickering 
> > Sent: Tuesday, June 29, 2021 5:04 PM
> > To: Erdi, Gergo 
> > Subject: [External] Re: Loading a typechecked module and then using it 
> > immediately as a package
> >
> >
> > Do you have a `MyLib.hs` source file? If you move that somewhere else 
> > (another folder) then things might work?
> >
> > On Tue, Jun 29, 2021 at 9:41 AM Erdi, Gergo  wrote:
> > >
> > > PUBLIC
> > >
> > > What's weird about it is that if I print the `moduleNameProvidersMap`, I 
> > > can see `MyLib` inside, and it looks no different than any other module 
> > > from e.g. `ghc-prim` that I can import into `Test.hs` without any package 
> > > qualification. Also, why does the error message refer to the file name 
> > > `input/MyLib.hs`? Why does GHC even know that that is where it would have 
> > > to be loaded from, if it weren't to be used from an already loaded 
> > > package?
> > >
> >
> > 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 

Re: Loading a typechecked module and then using it immediately as a package

2021-06-29 Thread Sylvain Henry

Hi,

This part of the API is still awful and a bit in flux to make it less 
so. Modifying the UnitState directly isn't currently supported and seems 
difficult to do correctly (e.g. in your code snippet below you don't 
modify the moduleNameProvidersMap field), so it would probably be better 
to recreate the UnitState from scratch with mkUnitState/initUnitConfig.


You may also have a look to 
GHC.Driver.Backpack.{withBkpSession,buildUnit} in TcSession mode which 
registers virtual units for Backpack's .bkp files similarly to what you 
want to do. If you really don't want to use the filesystem at all, 
however, I think you will have to deal with moving MyLib from the HPT to 
the EPS and I don't know if it is easily feasible (Backpack resets these 
tables via withTempSession so that interface files are read from disk as 
usual instead iiuc).


Good luck :)
Sylvain


On 25/06/2021 11:17, Erdi, Gergo via ghc-devs wrote:


PUBLIC


Hi,

I have the following to .hs files:

 1. MyLib.hs:

module MyLib where
…

 2. Test.hs:

{-# LANGUAGE PackageImports  #-}
module Test where
import “my-pkg” MyLib
…

I would like to parse/typecheck/load MyLib.hs into some Unit 
“my-unit”, then add that to the package “my-pkg”, and then typecheck 
Test.hs, all in-proc using the GHC API, without putting any other 
files on disk. How do I do that?


What I tried is loading MyLib.hs after setting the homeUnitId in the 
DynFlags to “my-unit”, then editing the packageNameMap in the 
unitState of the DynFlags to may “my-pkg” to “my-unit”:


setHomeUnit :: (GhcMonad m) => UnitId -> m ()

setHomeUnit unitId = do

    dflags <- getSessionDynFlags

modifySession $ \h -> h{ hsc_dflags = dflags{ homeUnitId = unitId } }

registerUnit :: (GhcMonad m) => PackageName -> UnitId -> m ()

registerUnit pkg unitId = modifySession $ \h -> h{ hsc_dflags = 
addUnit $ hsc_dflags h }


  where

    addUnit dflags = dflags

    { unitState = let us = unitState dflags in us

    { packageNameMap = M.insert pkg (Indefinite unitId 
Nothing) $ packageNameMap us


    }

    }

pipeline = do

setHomeUnit myUnit

loadModule =<< typecheckModule =<< parseModule =<< modSumarryFor “MyLib”

registerUnit myPkg myUnit

setHomeUnit mainUnitId

typecheckModule =<< parseModule =<< modSumarryFor “Test”

Alas, this doesn’t work: the import of `MyLib` from `my-pkg` fails with:

input/linking/Test.hs:5:1: error:

    Could not find module ‘MyLib’

    It is not a module in the current program, or in any known package.

TBH I’m not very surprised that it didn’t work – that registerUnit 
function is doing some pretty deep surgery on the unitState that 
probably breaks several invariants. Still, I wasn’t able to find a 
better way – all the functions in GHC.Unit.State seem to be for 
querying only.


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.



Re: Loading a typechecked module and then using it immediately as a package

2021-06-29 Thread Erdi, Gergo via ghc-devs
PUBLIC

Should I? OK, I just tried calling `flushFinderCaches` after I change the home 
unit to `mainUnitId`, but I still get exactly the same behaviour: 
`findInstalledHomeModule` returns `InstalledFound` and things go downhill from 
there.

-Original Message-
From: Matthew Pickering  
Sent: Tuesday, June 29, 2021 6:34 PM
To: Erdi, Gergo 
Cc: ghc-devs@haskell.org
Subject: [External] Re: Loading a typechecked module and then using it 
immediately as a package

ATTENTION: This email came from an external source. Do not open attachments or 
click on links from unknown senders or unexpected emails. Always report 
suspicious emails using the Report As Phishing button in Outlook to protect the 
Bank and our clients.


Are you clearing the FinderCache?

On Tue, Jun 29, 2021 at 11:14 AM Erdi, Gergo  wrote:
>
> PUBLIC
>
> I don't know yet what's going on, but one thing I did notice is that 
> `findInstalledHomeModule` returns `InstalledFound` for `MyLib`, which doesn't 
> sound right to me -- `MyLib` should come from the "fake-uid" unit, and `Test` 
> is typechecked in the `mainUnitId`.
>
> -Original Message-
> From: Erdi, Gergo
> Sent: Tuesday, June 29, 2021 5:51 PM
> To: Matthew Pickering 
> Subject: Re: Loading a typechecked module and then using it immediately as a 
> package
>
> PUBLIC
>
> I tried moving `MyLib.hs` into a directory different than `Test.sh`, but the 
> error message still refers to its correct location! So this error is not 
> guessing the file name of where `MyLib.hs` could be loaded from; instead, it 
> seems to refer correctly to where the module (previously loaded) was. Hmm.
>
> -Original Message-
> From: Matthew Pickering 
> Sent: Tuesday, June 29, 2021 5:04 PM
> To: Erdi, Gergo 
> Subject: [External] Re: Loading a typechecked module and then using it 
> immediately as a package
>
>
> Do you have a `MyLib.hs` source file? If you move that somewhere else 
> (another folder) then things might work?
>
> On Tue, Jun 29, 2021 at 9:41 AM Erdi, Gergo  wrote:
> >
> > PUBLIC
> >
> > What's weird about it is that if I print the `moduleNameProvidersMap`, I 
> > can see `MyLib` inside, and it looks no different than any other module 
> > from e.g. `ghc-prim` that I can import into `Test.hs` without any package 
> > qualification. Also, why does the error message refer to the file name 
> > `input/MyLib.hs`? Why does GHC even know that that is where it would have 
> > to be loaded from, if it weren't to be used from an already loaded package?
> >
>
> 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 

Re: Loading a typechecked module and then using it immediately as a package

2021-06-29 Thread Matthew Pickering
Are you clearing the FinderCache?

On Tue, Jun 29, 2021 at 11:14 AM Erdi, Gergo  wrote:
>
> PUBLIC
>
> I don't know yet what's going on, but one thing I did notice is that 
> `findInstalledHomeModule` returns `InstalledFound` for `MyLib`, which doesn't 
> sound right to me -- `MyLib` should come from the "fake-uid" unit, and `Test` 
> is typechecked in the `mainUnitId`.
>
> -Original Message-
> From: Erdi, Gergo
> Sent: Tuesday, June 29, 2021 5:51 PM
> To: Matthew Pickering 
> Subject: Re: Loading a typechecked module and then using it immediately as a 
> package
>
> PUBLIC
>
> I tried moving `MyLib.hs` into a directory different than `Test.sh`, but the 
> error message still refers to its correct location! So this error is not 
> guessing the file name of where `MyLib.hs` could be loaded from; instead, it 
> seems to refer correctly to where the module (previously loaded) was. Hmm.
>
> -Original Message-
> From: Matthew Pickering 
> Sent: Tuesday, June 29, 2021 5:04 PM
> To: Erdi, Gergo 
> Subject: [External] Re: Loading a typechecked module and then using it 
> immediately as a package
>
>
> Do you have a `MyLib.hs` source file? If you move that somewhere else 
> (another folder) then things might work?
>
> On Tue, Jun 29, 2021 at 9:41 AM Erdi, Gergo  wrote:
> >
> > PUBLIC
> >
> > What's weird about it is that if I print the `moduleNameProvidersMap`, I 
> > can see `MyLib` inside, and it looks no different than any other module 
> > from e.g. `ghc-prim` that I can import into `Test.hs` without any package 
> > qualification. Also, why does the error message refer to the file name 
> > `input/MyLib.hs`? Why does GHC even know that that is where it would have 
> > to be loaded from, if it weren't to be used from an already loaded package?
> >
>
> 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.
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


RE: Loading a typechecked module and then using it immediately as a package

2021-06-29 Thread Erdi, Gergo via ghc-devs
PUBLIC

I don't know yet what's going on, but one thing I did notice is that 
`findInstalledHomeModule` returns `InstalledFound` for `MyLib`, which doesn't 
sound right to me -- `MyLib` should come from the "fake-uid" unit, and `Test` 
is typechecked in the `mainUnitId`.

-Original Message-
From: Erdi, Gergo 
Sent: Tuesday, June 29, 2021 5:51 PM
To: Matthew Pickering 
Subject: Re: Loading a typechecked module and then using it immediately as a 
package

PUBLIC

I tried moving `MyLib.hs` into a directory different than `Test.sh`, but the 
error message still refers to its correct location! So this error is not 
guessing the file name of where `MyLib.hs` could be loaded from; instead, it 
seems to refer correctly to where the module (previously loaded) was. Hmm.

-Original Message-
From: Matthew Pickering  
Sent: Tuesday, June 29, 2021 5:04 PM
To: Erdi, Gergo 
Subject: [External] Re: Loading a typechecked module and then using it 
immediately as a package


Do you have a `MyLib.hs` source file? If you move that somewhere else (another 
folder) then things might work?

On Tue, Jun 29, 2021 at 9:41 AM Erdi, Gergo  wrote:
>
> PUBLIC
>
> What's weird about it is that if I print the `moduleNameProvidersMap`, I can 
> see `MyLib` inside, and it looks no different than any other module from e.g. 
> `ghc-prim` that I can import into `Test.hs` without any package 
> qualification. Also, why does the error message refer to the file name 
> `input/MyLib.hs`? Why does GHC even know that that is where it would have to 
> be loaded from, if it weren't to be used from an already loaded package?
>

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.
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Loading a typechecked module and then using it immediately as a package

2021-06-29 Thread Erdi, Gergo via ghc-devs
PUBLIC

What's weird about it is that if I print the `moduleNameProvidersMap`, I can 
see `MyLib` inside, and it looks no different than any other module from e.g. 
`ghc-prim` that I can import into `Test.hs` without any package qualification. 
Also, why does the error message refer to the file name `input/MyLib.hs`? Why 
does GHC even know that that is where it would have to be loaded from, if it 
weren't to be used from an already loaded package?

-Original Message-
From: Erdi, Gergo 
Sent: Monday, June 28, 2021 3:04 PM
To: 'Matthew Pickering' 
Cc: 'ghc-devs@haskell.org' 
Subject: RE: [External] Re: Loading a typechecked module and then using it 
immediately as a package

INTERNAL

Oh, and one more question. This all works if in `Test.hs` I use a package 
import for `MyLib`. However, if I remove that, I get an error 

attempting to use module ‘main:MyLib’ (input/MyLib.hs) which is not 
loaded

So I tried adding an ExposePackage to the DynFlags's packageFlags between 
loading the library and typechecking `Test.hs`, but I couldn't get that to work:
1. If I just set it in the `hsc_dflags`, it doesn't take.
2. If I use `initUnits` after setting it in `hsc_dflags`, I get a new error:

cannot satisfy my-pkg


-Original Message-
From: Erdi, Gergo
Sent: Monday, June 28, 2021 2:41 PM
To: Matthew Pickering 
Cc: ghc-devs@haskell.org
Subject: RE: [External] Re: Loading a typechecked module and then using it 
immediately as a package

INTERNAL

Thanks, I got this working (after changing it slightly to use the GHC 9.0 API 
to the UnitState). However, I have some followup questions:

1. In the fakeUnitInfo, what is the relationship between unitLibraries and 
unitExposedModules? 
2. In the fakeUnitInfo, what is the unitImportDirs used for?
3. What is the difference between a PackageId and a PackageName?

Other than that, the code registers MyLib into the my-pkg package by letting 
`load` do the actual heavy lifting. For my purposes, I will need to control 
this loading myself: only `MyLib`'s typechecked interface should be used, with 
no actual compilation of definitions. So I wrote some code that makes a 
ModIface from a TcGblEnv. I seemingly got the whole thing working just by 
setting mi_exports from the TcGblEnv and leaving everything else with trivial 
values (fingerprint0 for fingerprints, empty lists for instances, etc.). If 
`MyLib` has no external dependencies and doesn't define any typeclasses, 
instances, or type families, is this enough? What is the role of `mi_globals`, 
should I set that from `tcg_rdr_env`, or should I leave it empty?

Thanks,
Gergo


-Original Message-
From: Matthew Pickering 
Sent: Friday, June 25, 2021 6:54 PM
To: Erdi, Gergo 
Cc: ghc-devs@haskell.org
Subject: [External] Re: Loading a typechecked module and then using it 
immediately as a package

ATTENTION: This email came from an external source. Do not open attachments or 
click on links from unknown senders or unexpected emails. Always report 
suspicious emails using the Report As Phishing button in Outlook to protect the 
Bank and our clients.


Hi Gergo,

Please see a minimal example in this gist.

https://clicktime.symantec.com/3Eb2qXqu8Yp6VtdK9d5pNmL7Vc?u=https%3A%2F%2Fgist.github.com%2Fmpickering%2F5029c7f244c484c91d665bcbc6bc6406

Cheers,

Matt

On Fri, Jun 25, 2021 at 10:20 AM Erdi, Gergo via ghc-devs 
 wrote:
>
> PUBLIC
>
>
> Hi,
>
>
>
> I have the following to .hs files:
>
>
>
> MyLib.hs:
>
> module MyLib where
> …
>
> Test.hs:
>
> {-# LANGUAGE PackageImports  #-}
> module Test where
> import “my-pkg” MyLib
> …
>
>
>
> I would like to parse/typecheck/load MyLib.hs into some Unit “my-unit”, then 
> add that to the package “my-pkg”, and then typecheck Test.hs, all in-proc 
> using the GHC API, without putting any other files on disk. How do I do that?
>
>
>
> What I tried is loading MyLib.hs after setting the homeUnitId in the DynFlags 
> to “my-unit”, then editing the packageNameMap in the unitState of the 
> DynFlags to may “my-pkg” to “my-unit”:
>
> setHomeUnit :: (GhcMonad m) => UnitId -> m ()
>
> setHomeUnit unitId = do
>
> dflags <- getSessionDynFlags
>
> modifySession $ \h -> h{ hsc_dflags = dflags{ homeUnitId = unitId 
> } }
>
>
>
> registerUnit :: (GhcMonad m) => PackageName -> UnitId -> m ()
>
> registerUnit pkg unitId = modifySession $ \h -> h{ hsc_dflags = 
> addUnit $ hsc_dflags h }
>
>   where
>
> addUnit dflags = dflags
>
> { unitState = let us = unitState dflags in us
>
> { packageNameMap = M.insert pkg (Indefinite unitId
> Nothing) $ packageNameMap us
>
> }
>
> }
>
>
>
> pipeline = do
>
> setHomeUnit myUnit
>
> loadModule =<< typecheckModule =<< parseModule =<< modSumarryFor 
> “MyLib”
>
> registerUnit myPkg myUnit
>
>
>
> setHomeUnit mainUnitId
>
> typecheckModule =<< parseModule =<< modSumarryFor “Test”
>
>
>
>
>
> Alas, this doesn’t work: the import of `MyLib` from 

RE: [External] Re: Loading a typechecked module and then using it immediately as a package

2021-06-28 Thread Erdi, Gergo via ghc-devs
INTERNAL

Oh, and one more question. This all works if in `Test.hs` I use a package 
import for `MyLib`. However, if I remove that, I get an error 

attempting to use module ‘main:MyLib’ (input/MyLib.hs) which is not 
loaded

So I tried adding an ExposePackage to the DynFlags's packageFlags between 
loading the library and typechecking `Test.hs`, but I couldn't get that to work:
1. If I just set it in the `hsc_dflags`, it doesn't take.
2. If I use `initUnits` after setting it in `hsc_dflags`, I get a new error:

cannot satisfy my-pkg


-Original Message-
From: Erdi, Gergo 
Sent: Monday, June 28, 2021 2:41 PM
To: Matthew Pickering 
Cc: ghc-devs@haskell.org
Subject: RE: [External] Re: Loading a typechecked module and then using it 
immediately as a package

INTERNAL

Thanks, I got this working (after changing it slightly to use the GHC 9.0 API 
to the UnitState). However, I have some followup questions:

1. In the fakeUnitInfo, what is the relationship between unitLibraries and 
unitExposedModules? 
2. In the fakeUnitInfo, what is the unitImportDirs used for?
3. What is the difference between a PackageId and a PackageName?

Other than that, the code registers MyLib into the my-pkg package by letting 
`load` do the actual heavy lifting. For my purposes, I will need to control 
this loading myself: only `MyLib`'s typechecked interface should be used, with 
no actual compilation of definitions. So I wrote some code that makes a 
ModIface from a TcGblEnv. I seemingly got the whole thing working just by 
setting mi_exports from the TcGblEnv and leaving everything else with trivial 
values (fingerprint0 for fingerprints, empty lists for instances, etc.). If 
`MyLib` has no external dependencies and doesn't define any typeclasses, 
instances, or type families, is this enough? What is the role of `mi_globals`, 
should I set that from `tcg_rdr_env`, or should I leave it empty?

Thanks,
Gergo


-Original Message-
From: Matthew Pickering 
Sent: Friday, June 25, 2021 6:54 PM
To: Erdi, Gergo 
Cc: ghc-devs@haskell.org
Subject: [External] Re: Loading a typechecked module and then using it 
immediately as a package

ATTENTION: This email came from an external source. Do not open attachments or 
click on links from unknown senders or unexpected emails. Always report 
suspicious emails using the Report As Phishing button in Outlook to protect the 
Bank and our clients.


Hi Gergo,

Please see a minimal example in this gist.

https://clicktime.symantec.com/3Eb2qXqu8Yp6VtdK9d5pNmL7Vc?u=https%3A%2F%2Fgist.github.com%2Fmpickering%2F5029c7f244c484c91d665bcbc6bc6406

Cheers,

Matt

On Fri, Jun 25, 2021 at 10:20 AM Erdi, Gergo via ghc-devs 
 wrote:
>
> PUBLIC
>
>
> Hi,
>
>
>
> I have the following to .hs files:
>
>
>
> MyLib.hs:
>
> module MyLib where
> …
>
> Test.hs:
>
> {-# LANGUAGE PackageImports  #-}
> module Test where
> import “my-pkg” MyLib
> …
>
>
>
> I would like to parse/typecheck/load MyLib.hs into some Unit “my-unit”, then 
> add that to the package “my-pkg”, and then typecheck Test.hs, all in-proc 
> using the GHC API, without putting any other files on disk. How do I do that?
>
>
>
> What I tried is loading MyLib.hs after setting the homeUnitId in the DynFlags 
> to “my-unit”, then editing the packageNameMap in the unitState of the 
> DynFlags to may “my-pkg” to “my-unit”:
>
> setHomeUnit :: (GhcMonad m) => UnitId -> m ()
>
> setHomeUnit unitId = do
>
> dflags <- getSessionDynFlags
>
> modifySession $ \h -> h{ hsc_dflags = dflags{ homeUnitId = unitId 
> } }
>
>
>
> registerUnit :: (GhcMonad m) => PackageName -> UnitId -> m ()
>
> registerUnit pkg unitId = modifySession $ \h -> h{ hsc_dflags = 
> addUnit $ hsc_dflags h }
>
>   where
>
> addUnit dflags = dflags
>
> { unitState = let us = unitState dflags in us
>
> { packageNameMap = M.insert pkg (Indefinite unitId
> Nothing) $ packageNameMap us
>
> }
>
> }
>
>
>
> pipeline = do
>
> setHomeUnit myUnit
>
> loadModule =<< typecheckModule =<< parseModule =<< modSumarryFor 
> “MyLib”
>
> registerUnit myPkg myUnit
>
>
>
> setHomeUnit mainUnitId
>
> typecheckModule =<< parseModule =<< modSumarryFor “Test”
>
>
>
>
>
> Alas, this doesn’t work: the import of `MyLib` from `my-pkg` fails with:
>
>
>
> input/linking/Test.hs:5:1: error:
>
> Could not find module ‘MyLib’
>
> It is not a module in the current program, or in any known package.
>
>
>
> TBH I’m not very surprised that it didn’t work – that registerUnit function 
> is doing some pretty deep surgery on the unitState that probably breaks 
> several invariants. Still, I wasn’t able to find a better way – all the 
> functions in GHC.Unit.State seem to be for querying only.
>
>
>
> 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 

RE: [External] Re: Loading a typechecked module and then using it immediately as a package

2021-06-28 Thread Erdi, Gergo via ghc-devs
INTERNAL

Thanks, I got this working (after changing it slightly to use the GHC 9.0 API 
to the UnitState). However, I have some followup questions:

1. In the fakeUnitInfo, what is the relationship between unitLibraries and 
unitExposedModules? 
2. In the fakeUnitInfo, what is the unitImportDirs used for?
3. What is the difference between a PackageId and a PackageName?

Other than that, the code registers MyLib into the my-pkg package by letting 
`load` do the actual heavy lifting. For my purposes, I will need to control 
this loading myself: only `MyLib`'s typechecked interface should be used, with 
no actual compilation of definitions. So I wrote some code that makes a 
ModIface from a TcGblEnv. I seemingly got the whole thing working just by 
setting mi_exports from the TcGblEnv and leaving everything else with trivial 
values (fingerprint0 for fingerprints, empty lists for instances, etc.). If 
`MyLib` has no external dependencies and doesn't define any typeclasses, 
instances, or type families, is this enough? What is the role of `mi_globals`, 
should I set that from `tcg_rdr_env`, or should I leave it empty?

Thanks,
Gergo


-Original Message-
From: Matthew Pickering  
Sent: Friday, June 25, 2021 6:54 PM
To: Erdi, Gergo 
Cc: ghc-devs@haskell.org
Subject: [External] Re: Loading a typechecked module and then using it 
immediately as a package

ATTENTION: This email came from an external source. Do not open attachments or 
click on links from unknown senders or unexpected emails. Always report 
suspicious emails using the Report As Phishing button in Outlook to protect the 
Bank and our clients.


Hi Gergo,

Please see a minimal example in this gist.

https://clicktime.symantec.com/3Eb2qXqu8Yp6VtdK9d5pNmL7Vc?u=https%3A%2F%2Fgist.github.com%2Fmpickering%2F5029c7f244c484c91d665bcbc6bc6406

Cheers,

Matt

On Fri, Jun 25, 2021 at 10:20 AM Erdi, Gergo via ghc-devs 
 wrote:
>
> PUBLIC
>
>
> Hi,
>
>
>
> I have the following to .hs files:
>
>
>
> MyLib.hs:
>
> module MyLib where
> …
>
> Test.hs:
>
> {-# LANGUAGE PackageImports  #-}
> module Test where
> import “my-pkg” MyLib
> …
>
>
>
> I would like to parse/typecheck/load MyLib.hs into some Unit “my-unit”, then 
> add that to the package “my-pkg”, and then typecheck Test.hs, all in-proc 
> using the GHC API, without putting any other files on disk. How do I do that?
>
>
>
> What I tried is loading MyLib.hs after setting the homeUnitId in the DynFlags 
> to “my-unit”, then editing the packageNameMap in the unitState of the 
> DynFlags to may “my-pkg” to “my-unit”:
>
> setHomeUnit :: (GhcMonad m) => UnitId -> m ()
>
> setHomeUnit unitId = do
>
> dflags <- getSessionDynFlags
>
> modifySession $ \h -> h{ hsc_dflags = dflags{ homeUnitId = unitId 
> } }
>
>
>
> registerUnit :: (GhcMonad m) => PackageName -> UnitId -> m ()
>
> registerUnit pkg unitId = modifySession $ \h -> h{ hsc_dflags = 
> addUnit $ hsc_dflags h }
>
>   where
>
> addUnit dflags = dflags
>
> { unitState = let us = unitState dflags in us
>
> { packageNameMap = M.insert pkg (Indefinite unitId 
> Nothing) $ packageNameMap us
>
> }
>
> }
>
>
>
> pipeline = do
>
> setHomeUnit myUnit
>
> loadModule =<< typecheckModule =<< parseModule =<< modSumarryFor 
> “MyLib”
>
> registerUnit myPkg myUnit
>
>
>
> setHomeUnit mainUnitId
>
> typecheckModule =<< parseModule =<< modSumarryFor “Test”
>
>
>
>
>
> Alas, this doesn’t work: the import of `MyLib` from `my-pkg` fails with:
>
>
>
> input/linking/Test.hs:5:1: error:
>
> Could not find module ‘MyLib’
>
> It is not a module in the current program, or in any known package.
>
>
>
> TBH I’m not very surprised that it didn’t work – that registerUnit function 
> is doing some pretty deep surgery on the unitState that probably breaks 
> several invariants. Still, I wasn’t able to find a better way – all the 
> functions in GHC.Unit.State seem to be for querying only.
>
>
>
> 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 

Re: Loading a typechecked module and then using it immediately as a package

2021-06-25 Thread ÉRDI Gergő

On Fri, 25 Jun 2021, Matthew Pickering wrote:


Hi Gergo,

Please see a minimal example in this gist.

https://gist.github.com/mpickering/5029c7f244c484c91d665bcbc6bc6406


Thanks for the quick reply! Unfortunately, I won't be able to try it out 
until Monday.

___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Loading a typechecked module and then using it immediately as a package

2021-06-25 Thread Matthew Pickering
Hi Gergo,

Please see a minimal example in this gist.

https://gist.github.com/mpickering/5029c7f244c484c91d665bcbc6bc6406

Cheers,

Matt

On Fri, Jun 25, 2021 at 10:20 AM Erdi, Gergo via ghc-devs
 wrote:
>
> PUBLIC
>
>
> Hi,
>
>
>
> I have the following to .hs files:
>
>
>
> MyLib.hs:
>
> module MyLib where
> …
>
> Test.hs:
>
> {-# LANGUAGE PackageImports  #-}
> module Test where
> import “my-pkg” MyLib
> …
>
>
>
> I would like to parse/typecheck/load MyLib.hs into some Unit “my-unit”, then 
> add that to the package “my-pkg”, and then typecheck Test.hs, all in-proc 
> using the GHC API, without putting any other files on disk. How do I do that?
>
>
>
> What I tried is loading MyLib.hs after setting the homeUnitId in the DynFlags 
> to “my-unit”, then editing the packageNameMap in the unitState of the 
> DynFlags to may “my-pkg” to “my-unit”:
>
> setHomeUnit :: (GhcMonad m) => UnitId -> m ()
>
> setHomeUnit unitId = do
>
> dflags <- getSessionDynFlags
>
> modifySession $ \h -> h{ hsc_dflags = dflags{ homeUnitId = unitId } }
>
>
>
> registerUnit :: (GhcMonad m) => PackageName -> UnitId -> m ()
>
> registerUnit pkg unitId = modifySession $ \h -> h{ hsc_dflags = addUnit $ 
> hsc_dflags h }
>
>   where
>
> addUnit dflags = dflags
>
> { unitState = let us = unitState dflags in us
>
> { packageNameMap = M.insert pkg (Indefinite unitId Nothing) $ 
> packageNameMap us
>
> }
>
> }
>
>
>
> pipeline = do
>
> setHomeUnit myUnit
>
> loadModule =<< typecheckModule =<< parseModule =<< modSumarryFor 
> “MyLib”
>
> registerUnit myPkg myUnit
>
>
>
> setHomeUnit mainUnitId
>
> typecheckModule =<< parseModule =<< modSumarryFor “Test”
>
>
>
>
>
> Alas, this doesn’t work: the import of `MyLib` from `my-pkg` fails with:
>
>
>
> input/linking/Test.hs:5:1: error:
>
> Could not find module ‘MyLib’
>
> It is not a module in the current program, or in any known package.
>
>
>
> TBH I’m not very surprised that it didn’t work – that registerUnit function 
> is doing some pretty deep surgery on the unitState that probably breaks 
> several invariants. Still, I wasn’t able to find a better way – all the 
> functions in GHC.Unit.State seem to be for querying only.
>
>
>
> 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.
> ___
> ghc-devs mailing list
> ghc-devs@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs