问题
I am working with the newsynth
package and am running into a bug that I am starting to suspect has to do with the update random-1.2.0
over the summer (newsynth
was last updated in late 2019, and random-1.2.0
came out in June. I have run cabal update
since then, which is why both seem to be installed.) Here is the code that I ran in GHCi:
λ> import System.Random
λ> import Quantum.Synthesis.Ring
λ> import Quantum.Synthesis.Diophantine
λ> g <- getStdGen
λ> diophantine g (RootTwo 5 0)
<interactive>:5:1: error:
• No instance for (random-1.1:System.Random.RandomGen StdGen)
arising from a use of ‘diophantine’
There are instances for similar types:
instance random-1.1:System.Random.RandomGen
random-1.1:System.Random.StdGen
-- Defined in ‘random-1.1:System.Random’
• In the expression: diophantine g (RootTwo 5 0)
In an equation for ‘it’: it = diophantine g (RootTwo 5 0)
As was suggested to me in another SO post, I tried hiding the other copy of random
from GHCi. I ran
$ ghc-pkg --global --package-db ~/.cabal/store/ghc-8.8.3/package.db list random
~/.ghcup/ghc/8.8.3/lib/ghc-8.8.3/package.conf.d
(no packages)
~/.cabal/store/ghc-8.8.3/package.db
random-1.1
random-1.2.0
so I tried $ ghc-pkg --global --package-db ~/.cabal/store/ghc-8.8.3/package.db hide random-1.2.0
.
However, when the bug persisted, I also tried $ ghc-pkg --global --package-db ~/.cabal/store/ghc-8.8.3/package.db hide random-1.1
(just to make sure hide
did what I thought it would), yet inexplicably found that in GHCi (even after restarting Terminal) I was able to run λ> import System.Random
despite all known instances of random
being hidden. Am I hiding packages in the wrong way?
Thank you in advance.
(I asked a somewhat related question the other day but since I think this issue is kinda different I thought it made sense be separate. If this is not the case I can update the other post.)
回答1:
Use cabal or stack when you want to manage packages. But first, reset to clean state:
rm -rf ~/.ghc ~/.cabal
cabal update
cabal v2-repl -b newsynth
This will get you into a GHCi session with the desired package available.
回答2:
I you want to work directly in ghci without going through cabal repl
or stack ghci
, one way is to create a local GHC package environment in a folder, using cabal-install:
cabal install --lib --constraint="random == 1.1" --package-env . random
Besides "random" itself, you'll need to explicitly list other packages that you want to be present in the environment. Likewise, you might add multiple --constraint
arguments.
This will create a file named ".ghc.environment.xxx" in the folder, which should be picked up by standalone ghci or ghc invocations made there.
来源:https://stackoverflow.com/questions/64362142/hiding-versions-of-random-from-ghci