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

时间:2015-08-10 20:23:28

标签: haskell module

请注意以下文件:

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的定义?

1 个答案:

答案 0 :(得分:1)

没有办法优先考虑,但可以轻松覆盖个别名称。例如,要覆盖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