3

GHCIに、コンパイル時にコンパイルされていないバージョンよりも大幅に高速なモジュールのコンパイル済みオブジェクトコードをロードしてもらいたいです。これは、すべてのファイルが同じディレクトリ(モジュール階層なし)にある場合にうまく機能していました。ただし、ファイルがモジュール階層にある場合は機能しません。

作業バージョンMyFile.hs:

import Basic
import Histogram

ここで、Basic.oとHistogram.oはMyFile.hsと同じディレクトリにあります

動作しないバージョンMyFile.hs:

import Util.Basic
import Util.Histogram

ここで、Basic.oとHistogram.oはサブディレクトリUtilにあります。このバージョンでは、MyFile.hsをロードすると次のようになります。

[1 of 2] Compiling Util.Basic ( Util/Basic.hs, interpreted )
[2 of 2] Compiling Util.Histogram ( Util/Histogram.hs, interpreted )
Ok, modules loaded: Util.Basic, Util.Histogram.

コードをモジュールに整理できるようにしたいのですが、コンパイルされたoファイルを使用することでメリットが得られます。

また、oファイルがコンパイルされてからソースファイルが変更されていないことに注意してください。

編集:各ファイルの内容は次のとおりです。

MyFile.hs

import Util.Basic
import Util.Histogram

Util / Basic.hs

module Util.Basic () where

Util / Histogram.hs

module Util.Histogram () where

ファイル/コンパイル:

$:~/programming/haskell/example-error$ ls
MyFile.hs  MyFile.hs~  Util
$:~/programming/haskell/example-error$ cd Util
$:~/programming/haskell/example-error/Util$ ls
Basic.hs  Basic.hs~  Histogram.hs  Histogram.hs~
$:~/programming/haskell/example-error/Util$ ghc *.hs
[1 of 2] Compiling Util.Histogram   ( Histogram.hs, Histogram.o )
[2 of 2] Compiling Util.Basic       ( Basic.hs, Basic.o )
$:~/programming/haskell/example-error/Util$ ls
Basic.hi  Basic.hs~  Histogram.hi  Histogram.hs~
Basic.hs  Basic.o    Histogram.hs  Histogram.o
$:~/programming/haskell/example-error/Util$  cd ../
$:~/programming/haskell/example-error$ ghci -ignore-dot-ghci MyFile.hs
GHCi, version 7.4.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 3] Compiling Util.Histogram   ( Util/Histogram.hs, interpreted )
[2 of 3] Compiling Util.Basic       ( Util/Basic.hs, interpreted )
[3 of 3] Compiling Main             ( MyFile.hs, interpreted )
Ok, modules loaded: Util.Basic, Util.Histogram, Main.
*Main> 

ダニエルによって提案されたように機能したソリューション:

The fix is to compile the importing file, and the files in the 
subdirectory only as a consequence of that, not directly. 
4

1 に答える 1

5

この問題は以下で説明するものと同じで、フラグが変更されました。

~/.../Util> ghc Other.hs
[1 of 1] Compiling Util.Other       ( Other.hs, Other.o )
~/.../Util> cd ..
~/.../src> ghc MyFile.hs
[1 of 2] Compiling Util.Other       ( Util/Other.hs, Util/Other.o ) [flags changed]
[2 of 2] Compiling MyFile           ( MyFile.hs, MyFile.o )

特にどのフラグか、または個別のコンパイル中に渡されたフラグが、インポート元モジュールから追跡されたモジュールとしてコンパイルするときに渡されたものと異なる理由はわかりませんが、それらは変更されるため、再コンパイルが必要です (具体的には、ファイル内のフラグ ハッシュ値が.hi変更されます)。

したがって、修正は、モジュールを個別にコンパイルするのではなく、トップレベルのインポーターの依存関係としてコンパイルすることです。


元のほぼ正しい推測:

部分的にしか再現できません。コンパイルしてからtouchingした後MyFile.hs

$ ghci-7.4.2 MyFile.hs
-- snip
[1 of 2] Compiling Util.Other       ( Util/Other.hs, interpreted )
[2 of 2] Compiling MyFile           ( MyFile.hs, interpreted )
Ok, modules loaded: MyFile, Util.Other.

あなたと同じように見えますが、7.6.1 ではヒント (コンパイルとtouching)が得られます。

$ ghci MyFile.hs
-- snip
[1 of 2] Compiling Util.Other       ( Util/Other.hs, interpreted ) [flags changed]
[2 of 2] Compiling MyFile           ( MyFile.hs, interpreted )
Ok, modules loaded: MyFile, Util.Other.

フラグが変更されました。ファイルにあり:set -XNoMonomorphismRestriction.ghciフラグの変更により再コンパイルが発生します。

$ ghci -ignore-dot-ghci MyFile.hs
GHCi, version 7.6.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[2 of 2] Compiling MyFile           ( MyFile.hs, interpreted )
Ok, modules loaded: MyFile, Util.Other.

.ghciコンパイルに与えられなかったフラグで違反を無視し、変更Util.Otherされていないものは解釈されず、コンパイルされたコードが使用されます。(GHC < 7.4 では、.ghciファイルを無視する必要さえありません。)

.ghci言語オプション ( NoMonomorphismRestriction、 、 ...) と ghc >= 7.4 を設定したファイルがある場合、モジュールをロードするときにそのファイルTypeFamiliesを無視する必要があります。.ghci

そうでない場合、再コンパイルは予期された動作ではありません。次に、問題を診断して修正を見つけるために、より多くの情報が必要になります。

半回避策は-fobject-code、ghci のフラグになります。

于 2012-11-28T03:54:04.963 に答える