是否可以导入具有名称解析优先级的库?

Is it possible to import a library with name-solving priority?

注意以下文件:

import Common
main = print (length [1,2,3])

Common 是一个库,它重组了 Prelude 中的函数以导出我喜欢的版本(即,基于折叠的函数而不是基于列表的函数)。这会导致名称冲突:

test.hs:3:15:
    Ambiguous occurrence ‘length’
    It could refer to either ‘Prelude.length’,
                             imported from ‘Prelude’ at test.hs:1:1
                             (and originally defined in ‘GHC.List’)
                          or ‘Common.length’, imported from ‘Common’ at test.hs:1:1-11

由于想法是在创建新文件时避免官僚作风,因此仅使用 Import Prelude hiding ... 在这里无济于事。有没有什么办法可以让 GHC 支持 Common.hs 的定义而不是 Prelude 的定义?

无法确定优先级,但可以轻松覆盖个别名称。例如,覆盖 length:

module Common where

import Prelude hiding (length)
import qualified Data.List

length :: Num n => [a] -> n
length = Data.List.genericLength

您可以在 ghci 中检查一切是否正常:

% ghci -XNoImplicitPrelude test.hs
GHCi, version 7.10.1: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Common           ( test.hs, interpreted )
Ok, modules loaded: Common.
*Common> :t length
length :: Num n => [a] -> n
*Common> :t (+)
(+) :: Num a => a -> a -> a